You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修正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 i directly as the offset, which gave you sequential addresses like 0x00000000, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:43:33