I have a small program where I wish to pass shellcode as argument. In the shellcode, there is a necessity to pass \x00. I tried the following command:
./program `python -c 'print "\x01\x00\x00\x00\x9c\xd8\xff\xbf"'`
But the \x00 doesn't get registered at all! The arguments passed to the program are "\x01\x9c\xff\xbf".
I don't think it's a problem with python, but rather with the shell which passes the argument. I am using the bash shell.
Now, how do I force the shell to pass the argument '\x00'?
Thanks and Regards,
Hrishikesh Murali
You can try this,
and
It proves \x00 is passed.
By the way, I find c program surely stip \00 in argv in my test. You can examine the argv such as,
So I guess shell or c program can stip \x00 chararcter automatically.Maybe someone can explain why it happens.
But we have other technique to avoid \x00 in shellcode.
You can refer to the article. https://www.exploit-db.com/docs/english/13019-shell-code-for-beginners.pdf
Not at all. Unix uses C-style strings for the arguments a command is invoked with, and they are NUL-terminated character sequences.
What you can do is to rewrite your program (or find an invocation variant) to accept the parameter in its standard input. NUL bytes work just fine there and are, in fact, widely used, typically as separators for file names, since they are pretty much the only thing a file name can never contain. See
find
's-print0
switch andxarg
's switch-0
for the arguably most popular examples.You can try putting the shellcode in a file and then read it back and pass it to the executable.
something like:
use the above perl cmdline as argument to the program
the xargs command with --null option can help:
I tried that, and it worked.
If you check with
wc
, you'll find that the NUL character is indeed passed:To get rid of the newline at the end:
This data is passed to the script, but the problem is that NUL can not be part of a variable value.
To see how, try to pass this to a script:
Gone. But there's a way to save the day - Read from standard input, using either redirection or a pipe:
I believe this is because Bash discards null characters.
To test this I used the
od
command to dump out the parameters in octal format, using the following script:and ran it using:
The null characters are not printed.
To get around this issue pass in a hexdump and then reverse it in your program. Example:
Now you see that the null characters are printed.