如何修正C代码,实现BIN文件字节转指定格式的unsigned int输出?
Fixing the Offset Calculation in Your BIN-to-JavaScript Converter
Looks like you're just one small tweak away from getting the exact output you want! The core issue here is how you're calculating the offset address in your fprintf call—your loop variable i increments by 1 each iteration, but you need offsets that step by 4 bytes to match the 4-byte chunks you're reading from the BIN file.
Here's the corrected code:
#include <stdio.h> int main(void) { FILE *f = fopen("loader", "rb"); fseek(f, 0, SEEK_END); int s = ftell(f); rewind(f); FILE *o = fopen("loader.txt", "wb"); int i; for(i = 0; i < s / 4; i++) { unsigned int u; fread(&u, sizeof(u), 1, f); // Updated: multiply i by 4 to get 4-byte stepped offsets fprintf(o, "p.write4(shellcode.add32(0x%08x), 0x%08x);\n", i * 4, u); } fclose(o); fclose(f); return 0; }
Why this works:
- Your original code used
idirectly as the offset, which gave you sequential addresses like0x00000000,0x00000001,0x00000002... - Since each loop iteration reads a 4-byte block from the file, each block's starting offset is the loop index multiplied by 4. This gives you the required 4-byte stepped offsets:
0x00000000,0x00000004,0x00000008...
Example corrected output:
p.write4(shellcode.add32(0x00000000), 0x00067be9); p.write4(shellcode.add32(0x00000004), 0x0f2e6600); ...
A quick side note: if you run into endianness mismatches between your BIN file and the target system, you might need to swap the byte order of u before writing it out. But based on your question, this offset fix is the key change you need to match your desired output format.
内容的提问来源于stack exchange,提问作者J. Doe
相关产品推荐
相关产品推荐

