I'm trying to write a script which gets the hex characters of an object file and then redirects the output (in ascii) to a specific line in a file.
Basically, i do
export FILE=myobj_file
export j=''
for b in $(objdump -d -M intel $FILE | grep "^ " | cut -f2); do j+='\x'$b; done; echo $j
This prints me out the hex code (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
and i want to write that to a file containing:
#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;
}
I tried piping that output to sed as:
sed '6s/%d/$j/' file.c
but i get an error.
My result should be:
#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;
}
And either i get an error with sed or i get sed to insert HEX code instead of ASCII, by converting \x41 to 'A', instead of inserting \x41.
Thanks