I\'m writing a program that loads and executes code from file. But i got a problem: \"write\" syscall does not work. Code successfully loads and executes, but does not displ
One thing: you should open the file as binary.
FILE* fp = fopen(argv[1],"rb");
Why are you not using .so files to dynamically load your code? Are you testing a security scenario or really trying to dynamically load and run code?
Read here on how to compile code as a .so, load it dynamically within a program, and execute exported functions out of it.
http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
Your approach can't work: shellcode must be position-independant, but your code refers to the absolute address str
. The unconditional jump can also be either relative or absolute: make sure you get the relative verison (opcodes EB and E9 on x86).
See The Technique of Writing Portable Shell Code for more information.
You don't specify the details of your CPU, but you might be running afoul of the NX bit. I would expect your code to SEGFAULT though rather than run to completion.
This is precisely what happens on my box (Linux 2.6.32-28-generic #55-Ubuntu SMP Mon Jan 10 23:42:43 UTC 2011 x86_64 GNU/Linux) running on Intel Xeon E5410.