我之前从未做过这样的事情;)这将编译一些c源代码,创建一个b.bash包含二进制文件的脚本(以及用于简单开发的原始脚本)
(惑)
#!/bin/bash
if [ "$0" == "b.bash" ];then
tail -n +$[ `grep -n '^BINARY' $0|cut -d ':' -f 1` + 1 ] $0 | base64 -d > a2.out
chmod +x a2.out
./a2.out
echo $?
exit
fi
cat "$0" > b.bash
echo "BINARY" >> b.bash
cat > a.c << EOF
int main(){
return 12;
}
EOF
gcc a.c
base64 a.out >> b.bash
Run Code Online (Sandbox Code Playgroud)
调用(a.bash生成b.bash):
bash a.bash ;bash b.bash
Run Code Online (Sandbox Code Playgroud)
我不知道如何逃避在执行前将二进制文件写入临时文件...
我尝试了一下,效果很好。十六进制是用 xxd 生成的。
#!/bin/bash
xxd -r >foo <<'EndHere'
0000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............
0000010: 0200 3e00 0100 0000 e003 4000 0000 0000 ..>.......@.....
0000020: 4000 0000 0000 0000 000a 0000 0000 0000 @...............
0000030: 0000 0000 4000 3800 0800 4000 1e00 1b00 ....@.8...@.....
0000040: 0600 0000 0500 0000 4000 0000 0000 0000 ........@.......
0000050: 4000 4000 0000 0000 4000 4000 0000 0000 @.@.....@.@.....
...
0001960: 6400 5f65 6461 7461 006d 6169 6e00 5f69 d._edata.main._i
0001970: 6e69 7400 nit.
EndHere
chmod +x foo
./foo
Run Code Online (Sandbox Code Playgroud)
不要像其他几个答案所建议的那样重新发明轮子,只需使用古老的shar命令,它正是通过设计来做到这一点的。
假设您要嵌入脚本的文件是binaryfile,只需运行
$ shar binaryfile > binaryfile.shar
Run Code Online (Sandbox Code Playgroud)
你已经准备好了。您有一个名为的 shell 脚本binaryfile.shar,执行时将提取binaryfile.
| 归档时间: |
|
| 查看次数: |
12829 次 |
| 最近记录: |