【发布时间】:2023-04-01 09:03:01
【问题描述】:
我正在尝试编写一个脚本,该脚本获取目标文件的十六进制字符,然后将输出(在 ascii 中)重定向到文件中的特定行。
基本上是这样的
export FILE=myobj_file
export j=''
for b in $(objdump -d -M intel $FILE | grep "^ " | cut -f2); do j+='\x'$b; done; echo $j
这会打印出十六进制代码(shellcode):
\xeb\x0d\x5e\x31\xc9\x67\x2a\x42\x4b\x55\x55\x55\x85\xc8\xc3\xc4\x85\xd9\xc2\xeb\xe8\xe8\xe8\xe8\xe9\xe9\xe9\xe9
我想将其写入包含以下内容的文件:
#include <stdio.h>
#include <string.h>
int main() {
unsigned char code[] = {{PLACEHOLDER}} /*Here need to be inserted*/
printf("Length: %d\n", strlen(code));
int (*ret)() = (int(*)())code;
ret();
return 0;
}
我尝试将输出传递给 sed:
sed '6s/%d/$j/' file.c
但我得到一个错误。
我的结果应该是:
#include <stdio.h>
#include <string.h>
int main() {
unsigned char code[] = \
"\xeb\x0d\x5e\x31\xc9\x67\x2a\x42\x4b\x55\x55\x55\x85\xc8\xc3\xc4\x85\xd9\xc2\xeb\xe8\xe8\xe8\xe8\xe9\xe9\xe9\xe9";
printf("Length: %d\n", strlen(code));
int (*ret)() = (int(*)())code;
ret();
return 0;
}
要么我得到一个 sed 错误,要么我得到 sed 插入 HEX 代码而不是 ASCII,通过将 \x41 转换为 'A',而不是插入 \x41。
谢谢
【问题讨论】: