Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Visual Sniffer 1.0 - VSniffer.cpp

VSniffer.cpp

Caricato da: Piero Tofy
Scarica il programma completo

  1. #define STRICT
  2. #define WIN32_LEAN_AND_MEAN
  3.  
  4. #include <windows.h>
  5. #include <winsock2.h>
  6. #include <fstream.h>
  7. #include <shellapi.h>
  8.  
  9. #include "VSniffer.h"
  10. #include "Resource.h"
  11.  
  12. BOOL ThreadRunning;
  13. HWND hwndThread;
  14.  
  15. int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow){
  16.  
  17.         DialogBoxParam(0,MAKEINTRESOURCE(IDD_VSNIFFERDLG),0,DlgProc,0);
  18.  
  19.     return 0;
  20. }
  21.  
  22.  
  23. unsigned long WINAPI SniffThread(LPVOID pParam){
  24.         Sniff((HWND)pParam);
  25.         return 0;
  26. }
  27.  
  28. BOOL CALLBACK DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam){
  29.        
  30.         char *LogPath = new char[255];
  31.         char *WordPadPath = new char[255];
  32.  
  33.    /* Non utilizzo il sistema "semi-intelligente" con le macro
  34.         perchè devo gestire pochi eventi e non avrebbe senso complicarsi la vita... */
  35.         if(uMsg == WM_CLOSE) {
  36.                 /* Riceve il messaggio di chiusura... */
  37.                 EndDialog(hwndDlg, 0);
  38.                 return TRUE;
  39.         }else if(uMsg == WM_INITDIALOG){
  40.                 /* Inizializzazione... */
  41.  
  42.                 /*HICON hIcon = (HICON)LoadImage(NULL,"Server.ico",IMAGE_ICON,32,32,NULL);
  43.                 SendMessage(hwndDlg,WM_SETICON,ICON_BIG,(LPARAM)hIcon); */
  44.                 SetDlgItemInt(hwndDlg,IDC_EPORTVALUE,0,TRUE);
  45.                 SetWindowPos(hwndDlg, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW );
  46.                 EnableWindow(GetDlgItem(hwndDlg,IDC_BSTOP),FALSE);
  47.                 EnableWindow(GetDlgItem(hwndDlg,IDC_BEXECUTE),FALSE);
  48.         }else if(uMsg == WM_COMMAND){
  49.                 /* Messaggi sugli eventi dei controlli... */
  50.                 WORD wNotify = HIWORD(wParam);
  51.                 WORD wId = LOWORD(wParam);
  52.  
  53.                 if (wNotify == STN_CLICKED && wId == IDC_BSTART){
  54.                         /* Inizia thread */
  55.  
  56.                         DWORD ThreadId;
  57.                         ThreadRunning=TRUE;
  58.                         hwndThread = (HWND)CreateThread(NULL, 0, &SniffThread, hwndDlg, 0, &ThreadId);
  59.                         EnableWindow(GetDlgItem(hwndDlg,IDC_BSTART),FALSE);
  60.                         EnableWindow(GetDlgItem(hwndDlg,IDC_BSTOP),TRUE);
  61.                         EnableWindow(GetDlgItem(hwndDlg,IDC_BEXECUTE),FALSE);
  62.                         SetDlgItemText(hwndDlg,IDC_BEXECUTE,"Sniffing...");
  63.                 }else if (wNotify == STN_CLICKED && wId == IDC_BSTOP){
  64.                         /* Ferma thread */
  65.                         ThreadRunning=FALSE;
  66.                         EnableWindow(GetDlgItem(hwndDlg,IDC_BSTOP),FALSE);
  67.                         SetDlgItemText(hwndDlg,IDC_BEXECUTE,"Waiting for last packet...");
  68.                 }else if (wNotify == STN_CLICKED && wId == IDC_BEXECUTE){
  69.                         /* Apre il log con wordpad (prima ne cerca la locazione..) */
  70.                         wsprintf(LogPath,"\"%s%s\"",GetCurrentDir(),LOGFILE);
  71.                         GetSystemDirectory(WordPadPath,255);
  72.                         wsprintf(WordPadPath,"%s\\write.exe",WordPadPath);
  73.                         ShellExecute(hwndDlg,"open",WordPadPath,LogPath,"",SW_SHOWNORMAL);
  74.                 }
  75.                 return TRUE;
  76.         }
  77.     return FALSE;
  78. }
  79.  
  80.  
  81. int Sniff(HWND hwndParent){
  82.         /* Dichiarazione delle variabili... */
  83.         WSADATA wsd;
  84.         int RawSock;
  85.         SOCKADDR_IN sa;
  86.         struct hostent *pHostInfo;
  87.         char LocalHostName[HOSTNAME_LEN];
  88.         char Buffer[BUFFER_LEN];
  89.         char *pBuffer;
  90.         char LogPath[255];
  91.         unsigned long dwBytesRet ;
  92.         unsigned int  OptVal = 1 ;
  93.         IP_HDR  *pIpheader;
  94.         TCP_HDR *pTcpheader;
  95.         int FilterPort;
  96.         char *FilterText = new char[255];
  97.         unsigned long TotalBytes=0;
  98.         int PacketsCount=0;
  99.  
  100.        
  101.         /* Inizializza il WinSocket e il socket... */
  102.         WSAStartup(MAKEWORD(2,1),&wsd);
  103.         RawSock = socket(AF_INET,SOCK_RAW,IPPROTO_IP);
  104.        
  105.         /* Imposta il DNS a localhost  e inizializza la struttura da bindare... */
  106.         gethostname(LocalHostName,HOSTNAME_LEN);
  107.         pHostInfo=gethostbyname(LocalHostName);
  108.         sa.sin_family=AF_INET;
  109.         sa.sin_addr=*((struct in_addr *)pHostInfo->h_addr);
  110.         sa.sin_port=htons(6000);
  111.        
  112.         /* La chiamata a bind collega il socket con la struttura */
  113.         bind(RawSock,(SOCKADDR *)&sa,sizeof(sa));
  114.  
  115.         /* Non mi è del tutto chiara la sua funzione, l'MSDN e i tutorials
  116.         non sono stati in grado di fornirmi una risposta, ma senza questa
  117.         funzione, il programma non funzione correttamente quindi la
  118.         inserisco... */
  119.         WSAIoctl(RawSock,_WSAIOW(IOC_VENDOR,1),&OptVal,sizeof(OptVal),NULL,0,&dwBytesRet,NULL,NULL);
  120.        
  121.         /* Inizializza il sistema di scrittura su File... */
  122.         wsprintf(LogPath,"%s%s",GetCurrentDir(),LOGFILE);
  123.         ofstream out(LogPath,ios::trunc | ios::binary);
  124.  
  125.         /* Riceve il filtro porta... */
  126.         FilterPort = GetDlgItemInt(hwndParent,IDC_EPORTVALUE,NULL,0);
  127.  
  128.         /* Riceve il filtro testo... */
  129.         GetDlgItemText(hwndParent,IDC_ETEXTVALUE,FilterText,255);
  130.        
  131.         /* Ciclo infinito in attesa di pacchetti... */
  132.         while (ThreadRunning) {
  133.                 /* Incremente il counter e visualizza a monitor */
  134.                 SetDlgItemInt(hwndParent,IDC_SPACKSVALUE,PacketsCount,0);
  135.                 SetDlgItemInt(hwndParent,IDC_SBYTESVALUE,TotalBytes,0);
  136.                
  137.                 recv(RawSock,Buffer,BUFFER_LEN,0);
  138.  
  139.                 /* Frammenta il buffer nelle strutture definite nell'header */
  140.                 pIpheader = (IP_HDR *) &Buffer;
  141.                 pTcpheader = (TCP_HDR *) (Buffer + sizeof(IP_HDR)); /* Questo perchè l'header Tcp viene dopo l'header IP */
  142.                
  143.  
  144.                 if (!FilterPort || FilterPort==pTcpheader->dport || FilterPort==pTcpheader->sport){
  145.                         /* Puntatore al 40° bytes del pacchetto, dove incominciano i dati... */
  146.                         pBuffer=CutBuffer(&Buffer[40]);
  147.                        
  148.                         /* Se il filtro c'è, e non corrisponde al buffer... */
  149.                         if (FilterText[0]!='\0'){
  150.                                 /* Salta pacchetto */
  151.                                 if (!StrIsInBuf(FilterText,pBuffer)) continue;
  152.                         }
  153.  
  154.                         /* Incrementa i contatori di bytes e pacchetti e visualizza gli ip... */
  155.  
  156.                         TotalBytes+=pTcpheader->Checksum + pIpheader->ip_checksum;
  157.                         PacketsCount++;
  158.                         SetDlgItemText(hwndParent,IDC_SFROMIPVALUE,inet_ntoa(pIpheader->ip_srcaddr));
  159.                         SetDlgItemText(hwndParent,IDC_STOIPVALUE,inet_ntoa(pIpheader->ip_destaddr));
  160.  
  161.                         /* Scrive su File */
  162.                         out << "From " << inet_ntoa(pIpheader->ip_srcaddr);
  163.                         out << " To " << inet_ntoa(pIpheader->ip_destaddr) << "\n";
  164.                         out << "Source Port: " << pTcpheader->sport << "\n";
  165.                         out << "Destination Port: " << pTcpheader->dport << "\n";
  166.                         out << pBuffer << "\n\n";
  167.                 }
  168.         }
  169.  
  170.         /* Abilita il pulsante per aprire il file... */
  171.         EnableWindow(GetDlgItem(hwndParent,IDC_BEXECUTE),TRUE);
  172.         EnableWindow(GetDlgItem(hwndParent,IDC_BSTART),TRUE);
  173.  
  174.         /* Riutilizzo la variabile LogPath (per non doverne ridefinire una nuova...) */
  175.         wsprintf(LogPath,"Open %s",LOGFILE);
  176.         SetDlgItemText(hwndParent,IDC_BEXECUTE,LogPath);
  177.  
  178.        
  179.         out.close();
  180.         WSACleanup();
  181.  
  182.         return 0;
  183. }
  184.  
  185. BOOL StrIsInBuf(char *pString, char *pBuf){
  186.         char *TmpBuf = new char[255];
  187.         int StringLen = lstrlen(pString);
  188.         int BufferLen = lstrlen(pBuf);
  189.         int bufc=0, strc=0;
  190.  
  191.         while(bufc<BufferLen){
  192.                 /* Estra N. caratteri dal buffer per il confronto... */
  193.                 while(strc<StringLen){
  194.                         TmpBuf[strc]=pBuf[strc+bufc];
  195.                         strc++;
  196.                 }
  197.                 TmpBuf[strc]='\0';
  198.                 strc=0;
  199.                
  200.                 /* Se sono uguali... */
  201.                 if (lstrcmpi(TmpBuf,pString) == 0) return TRUE;
  202.                 bufc++;
  203.         }
  204.         return FALSE;
  205. }
  206.  
  207.  
  208. char *CutBuffer(char *pBuf){
  209.         int c;
  210.         for (c=0; c<BUFFER_LEN; c++){
  211.                 if (pBuf[c]==-52){ /* Carattere nullo -52 */
  212.                         pBuf[c]='\0';
  213.                         return pBuf;
  214.                 }
  215.         }
  216.         return pBuf;
  217. }
  218.  
  219. char *GetCurrentDir(void){
  220.         char *FullPath, *PathOnly;
  221.         FullPath = new char[255];
  222.         PathOnly = new char[255];
  223.         bool Found = false;
  224.         int Length;
  225.         int c;
  226.  
  227.         GetModuleFileName(NULL,FullPath,MAX_PATH);
  228.         Length = strlen(FullPath);
  229.  
  230.         /* Toglie l'estensione .exe */
  231.         for (c=Length; c>=0; c--){
  232.                 if (FullPath[c]=='\\' && (!Found)){
  233.                         Found=true;                                            
  234.                         PathOnly[c+1]='\0';
  235.                 }
  236.                 if (Found) {
  237.                         PathOnly[c]=FullPath[c];
  238.                 }
  239.         }
  240.  
  241.         return PathOnly;
  242. }