Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inputting Non ASCII characters to scanf("%s")

Is there a way one can issue non ascii hex characters to a scanf that uses %s ? I'm trying to insert hexadecimal chars like \x08\xDE\xAD and so on (to demonstrate buffer overflow).

The input is not to a command line parameter, but to a scanf inside the program.

like image 960
asudhak Avatar asked Feb 27 '13 22:02

asudhak


2 Answers

I assume you want to feed arbitrary data on stdin (since you read with scanf).

You can use the shell to create the data and pipe it into your program, e.g.

printf '\x08\xDE\xAD' | yourprogram

Note that this will only work as long as there are no white-space characters to be fed (because scanf with a %s format stops at white-space).

like image 181
Jens Avatar answered Nov 02 '22 09:11

Jens


When you say 'to a scanf()', presumably there is other data than just this to be supplied. Would it work to have a program, perhaps a Perl or Python script, generate the data and write the non-ASCII characters to the standard input of your program? If you need standard input to appear like a terminal, then you should investigate expect which handles that for you. This is a common way of dealing with the problem.

like image 25
Jonathan Leffler Avatar answered Nov 02 '22 08:11

Jonathan Leffler