qqsniff.c
资源名称:QQ嗅探器与C代码.rar [点击查看]
上传用户:leadtec
上传日期:2013-04-05
资源大小:10k
文件大小:6k
源码类别:
网络截获/分析
开发平台:
C/C++
- #include <windows.h>
- #include <commctrl.h>
- #include <winsock2.h>
- #include "qqsniffres.h"
- #pragma comment(lib,"ws2_32.lib")
- #pragma resource "C:\borland\bcc55\bin\qqsniff.RES"
- #define MAX_HOSTNAME_LAN 255
- #define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
- #define MAX_ADDR_LEN 16
- char NumList[65535];
- struct ipheader {
- BYTE h_lenver; /* this means that each member is 4 bits */
- BYTE ip_tos;
- USHORT ip_len;
- USHORT ip_id;
- USHORT ip_off;
- BYTE ip_ttl;
- BYTE ip_p;
- USHORT ip_sum;
- DWORD ip_src;
- DWORD ip_dst;
- }; /* total ip header length: 20 bytes (=160 bits) */
- typedef struct udphdr {
- USHORT sport;
- USHORT dport;
- USHORT len;
- USHORT cksum;
- }UDP_HDR;
- static BOOL CALLBACK DialogFunc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
- BOOL InsertListViewItems(HWND hwndListView, char *srcip, char *dstip, char *Num)
- {
- //set the number of items in the list
- LVITEM lvi;
- int idx=0;
- lvi.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE;
- lvi.state = 0;
- lvi.stateMask = 0;
- lvi.iItem = 0;
- lvi.iSubItem = 0;
- lvi.lParam = 0;
- lvi.pszText = NULL;
- //lvi.pszText = LPSTR_TEXTCALLBACK;
- ListView_SetItemCount(hwndListView, 1);
- ListView_InsertItem(hwndListView, &lvi);
- ListView_SetItemText(hwndListView, idx, 0, srcip);
- ListView_SetItemText(hwndListView, idx, 1, dstip);
- ListView_SetItemText(hwndListView, idx, 2, Num);
- return TRUE;
- }
- BOOL InitListView(HWND hwndListView)
- {
- LV_COLUMN lvColumn;
- int i;
- char szString[3][20] = {"From", "To", "Number"};
- //empty the list
- ListView_DeleteAllItems(hwndListView);
- //initialize the columns
- lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
- lvColumn.fmt = LVCFMT_LEFT;
- lvColumn.cx = 75;
- for(i = 0; i < 3; i++)
- {
- lvColumn.pszText = szString[i];
- ListView_InsertColumn(hwndListView, i, &lvColumn);
- }
- return TRUE;
- }
- DWORD WINAPI Sniff(LPVOID lparam)
- {
- HWND hListView=GetDlgItem((HWND)lparam,IDLIST);
- SOCKET sock;
- WSADATA wsd;
- DWORD dwBytesRet;
- DWORD optval = 1;
- BYTE *dataudp,*pUdpData;
- int QQ,iUdpDataLen,i;
- SOCKADDR_IN sa,saSource, saDest;
- struct hostent FAR * pHostent;
- char FAR name[MAX_HOSTNAME_LAN];
- char szSourceIP[MAX_ADDR_LEN],szDestIP[MAX_ADDR_LEN],Num[15],msg[50],RecvBuf[65535] = {0};
- struct udphdr *pUdpheader;
- struct ipheader *pIpheader;
- WSAStartup(MAKEWORD(2,1),&wsd);
- memset(NumList,0,65535);
- if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP))==SOCKET_ERROR)exit(1);
- gethostname(name, MAX_HOSTNAME_LAN);
- pHostent = gethostbyname(name);
- sa.sin_family = AF_INET;
- sa.sin_port = htons(6000);
- memcpy(&sa.sin_addr.S_un.S_addr, pHostent->h_addr_list[0], pHostent->h_length);
- bind(sock, (SOCKADDR *)&sa, sizeof(sa));
- if ((WSAGetLastError())==10013)exit(1);
- WSAIoctl(sock, SIO_RCVALL, &optval, sizeof(optval), NULL, 0, &dwBytesRet, NULL, NULL);
- pIpheader = (struct ipheader *)RecvBuf;
- pUdpheader = (struct udphdr *) (RecvBuf+ sizeof(struct ipheader ));
- while (1)
- {
- memset(RecvBuf, 0, sizeof(RecvBuf));
- recv(sock, RecvBuf, sizeof(RecvBuf), 0);
- saSource.sin_addr.s_addr = pIpheader->ip_src;
- saDest.sin_addr.s_addr = pIpheader->ip_dst;
- strncpy(szSourceIP, inet_ntoa(saSource.sin_addr), MAX_ADDR_LEN);
- strncpy(szDestIP, inet_ntoa(saDest.sin_addr), MAX_ADDR_LEN);
- iUdpDataLen =(ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct udphdr)));
- if((pIpheader->ip_p)==IPPROTO_UDP&&iUdpDataLen!=0)
- {
- pUdpData=(BYTE *) RecvBuf+sizeof(struct ipheader)+sizeof(struct udphdr);
- iUdpDataLen=ntohs(pIpheader->ip_len)-(sizeof(struct ipheader)+sizeof(struct udphdr));
- if(ntohs(pUdpheader->dport)==8000)
- if(iUdpDataLen%4==0 && iUdpDataLen>0)
- if(pUdpData[0]==0x02 && pUdpData[3]==0x00 && pUdpData[iUdpDataLen-1]==0x03)
- {
- QQ = (pUdpData[7]&0xff);
- QQ = (QQ<<8) + (pUdpData[8]&0xff);
- QQ = (QQ<<8) + (pUdpData[9]&0xff);
- QQ = (QQ<<8) + (pUdpData[10]&0xff);
- wsprintf(Num,"%d",QQ);
- if(strstr(NumList,Num)==NULL){
- wsprintf(msg,"%s --> %s QQ: %sn",szSourceIP,szDestIP,Num);
- strcat(NumList,msg);
- InsertListViewItems(hListView,szSourceIP,szDestIP,Num);
- }
- }
- }
- }
- return TRUE;
- }
- BOOL CALLBACK MainDlgProc(HWND, UINT, WPARAM, LPARAM);
- HWND g_hWnd;
- HINSTANCE g_hInstance;
- int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
- {
- INITCOMMONCONTROLSEX icex;// Ensure that the common control DLL is loaded.
- icex.dwSize=sizeof(INITCOMMONCONTROLSEX);
- icex.dwICC=ICC_LISTVIEW_CLASSES;
- InitCommonControlsEx(&icex);
- g_hInstance=hInstance;
- return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), NULL, (DLGPROC)MainDlgProc);
- }
- BOOL CALLBACK MainDlgProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
- {
- HANDLE g_hThread;
- HWND hListView,hStart,hStop;
- hStart=GetDlgItem(hDlg,IDSTART);
- hStop=GetDlgItem(hDlg,IDSTOP);
- switch (message)
- {
- case WM_INITDIALOG:
- g_hWnd = hDlg;
- hListView = GetDlgItem(hDlg, IDLIST);
- EnableWindow(hStop,FALSE);
- InitListView(hListView);
- case WM_COMMAND:
- switch (LOWORD (wParam)){
- case IDSTART:
- EnableWindow(hStart,FALSE);
- EnableWindow(hStop,TRUE);
- g_hThread=CreateThread(NULL,0,Sniff,(LPVOID)hDlg,0,NULL);
- break;
- case IDSTOP:
- EnableWindow(hStart,TRUE);
- EnableWindow(hStop,FALSE);
- TerminateThread(g_hThread,1);
- break;
- case IDSAVE:
- {
- HGLOBAL hMem;
- LPTSTR pStr;
- hMem = GlobalAlloc(GHND | GMEM_SHARE, sizeof(NumList));
- pStr = GlobalLock(hMem);
- lstrcpy(pStr, NumList);
- GlobalUnlock(hMem);
- OpenClipboard(NULL);
- EmptyClipboard();
- SetClipboardData(CF_TEXT, hMem);
- CloseClipboard();
- GlobalFree(hMem);
- MessageBox(NULL,"Copy data to clipborad succesful,you can paste it!","Data copy",MB_OK);
- }
- break;
- }
- break;
- /* By default, WM_CLOSE is equivalent to CANCEL */
- case WM_CLOSE:
- EndDialog(hDlg,0);
- WSACleanup();
- return TRUE;
- }
- return FALSE;
- }