C program hangs / suspends while executing shellcode

#include <stdio.h>
char shellcode[] = "some shellcode here";
int main (int argc, char **argv) {
    void (*sptr)();
    sptr = (void(*)()) (&shellcode);
    printf("must display this");
    return 0;

While running the program, it executes the sptr() and hangs there, probably because of the shellcode is running in memory. printf("..") is never executed. My problem is I want the program to execute printf().

Please help :)

Reply to Eric Finn and Alvin Wong

I changed as what both of you instructed and the error I got is:

Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved.

X:>"my program.exe" '»".¼' is not recognized as an internal or external command, operable program or batch file. must display this

char shellcode[] is valid. I have compiled it successfully before.

below is the original code with malicious shellcode so your antivirus should detect it, just to verify you guys that the shellcode is not the problem:

#include <stdio.h>
#include <stdlib.h>

char shellcode[] = "\xda\xd3\xd9\x74\x24\xf4\xbd\xe9\x6d\xf8\x29\x58\x33\xc9\xb1"

int main (int argc, char **argv) {

void (*sptr)();
    sptr = (void(*)()) (&shellcode);
    printf("must display this"); // instead of more lines i put this one
    return 0;

the above code compiles successfully and runs perfectly

i changed some lines to system(shellcode). it compiles but doesnt run properly


As my understanding, you want to run some "machine code" (not shellcode), and no matter how the code runs it should continue the program.

This is possible, by using threading.

First add these includes:

#include <windows.h>
#include <process.h>

And in your code:

void (*sptr)(void*);                  // Type for `_beginthread`
sptr = (void(*)(void*)) (&shellcode); // PLEASE rename to `machinecode`
_beginthread(sptr,0,NULL);            // This starts your code in a new thread
Sleep(5000);                          // Wait for 5000 ms
printf("must display this");

Of course this is not a proper way to multi-thread a program, but since your code is "machine code" there's not much to be done.

P.S. When I try your code it finally reaches an "Access violation" (segmentation fault) (and it shows the "x.exe encountered a problem" dialog), and my antivirus didn't detect anything (do I need to switch to another one??), so you may need to review the code or add an exception handler...

Okay, since shellcode is actually machine code rather than shell code (according to your latest edit), the answer is different.

When you declare char shellcode[], shellcode is a pointer to a memory location. This means that instead of

sptr = (void(*)()) (&shellcode);

you should have

sptr = (void(*)()) (shellcode);

Additionally, you want the code to be in the executable part of your binary, rather than in the data part of the binary. That means you want char *shellcode = ... rather than char shellcode[] = ....

Also, you should be sure that shellcode is a valid compiled C function with the same calling convention as the code that calls it.

Need Your Help

Function pointer pointing to a function that takes a function pointer

c++ function-pointers

How do I declare a function pointer that points to a function taking the same function pointer as the argument?