Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens in this code? (Executing a char buffer)

Could somebody give me a complete explanation of what is happening in this second line of code?

I know that the address of the buffer containing the shellcode is casted to a function pointer which is executed. But I´m a little confused with all the braces and steps involved, so I need a little bit more detailed explanation.

unsigned char buf[] = "\x90\x90\x90\x90\x90\x90\x90\x90";

((void(*)())buf)();

I tried to explain it to myself this way:

buf                     //address of the buffer containing code
void(*)()               //"type" function pointer returning void, no parameters
(void(*)()) buf         //cast buf to said type
( (void(*)()) buf )()   //take the outcome of the cast and execute it by appending ()

Is this correct?

Edit: I am aware that DEP would block the execution and that even if it would execute, the program would crash because it would execute "random garbage" after the NOPs. My question is just about the syntax of the function call.

like image 608
asquared Avatar asked Jul 10 '14 09:07

asquared


People also ask

What does char buffer do in C?

Reads the character at the given index relative to the current position. Returns a stream of int zero-extending the char values from this sequence. Clears this buffer.

How do I check if a buffer is empty?

If you want to check if the buffer holds no characters, you can use the strlen() function from string. h (make sure the buffer is \0-terminated after fread(). If you want to check if malloc failed, compare the pointer with NULL.


1 Answers

  1. Cast buf (array name converted to a pointer) to a void(*)() function pointer

    (void(*)())buf
    
  2. Call that function through the pointer

    (function_pointer)();
    

Notice that this is just wrong because of operator precedence rules

(void(*)()) buf() // Function call has a higher precedence over type cast

so another pair of parenthesis is necessary.

Eventually execute it (if DEP permits it, this is system-dependent) and (if x86) Nop-Nop-Nop,etc...

You're thus correct.

As a sidenote: the NOP code will crash your app as well: there's no return statement and IP won't be restored when that payload finishes.

like image 149
Marco A. Avatar answered Sep 24 '22 09:09

Marco A.