www.pudn.com > Hook-api-mir.rar > Util.cpp
// Copyright: www.pudn.com,程序员联合开发网,www.programsalon.com // 如果要使用或修改本程序,请保留次信息 // #include "stdafx.h" #include#include #include //#include #include "resource.h" #include "util.h" void GetFileName(char *fname) { char temp[200]; GetModuleFileName(NULL, temp, sizeof(temp)); int i =strlen(temp); while(i >0 && temp[i-1] !='\\' && temp[i-1] !=':') i--; strcpy(fname, &temp[i]); } // 如果是win9x,不能使用fopen函数 void WriteLog(char *fmt,...) { va_list args; char modname[200]; char temp[5000]; HANDLE hFile; GetModuleFileName(NULL, modname, sizeof(modname)); if((hFile =CreateFile("c:\\hookapi.log", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) <0) { return; } SetFilePointer(hFile, 0, NULL, FILE_END); wsprintf(temp, "mydll.dll:%s:", modname); DWORD dw; WriteFile(hFile, temp, strlen(temp), &dw, NULL); va_start(args,fmt); vsprintf(temp, fmt, args); va_end(args); WriteFile(hFile, temp, strlen(temp), &dw, NULL); wsprintf(temp, "\r\n"); WriteFile(hFile, temp, strlen(temp), &dw, NULL); CloseHandle(hFile); } int WriteBinData(char *function, char *buf, int len) { char mod_name[100]; char fname[128]; if(len <=0) return 0; GetFileName(mod_name); wsprintf(fname, "c:\\%s.log", mod_name); HANDLE hFile; if((hFile =CreateFile(fname, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) <0) { WriteLog("open file %s failed", fname); return -1; } SetFilePointer(hFile, 0, NULL, FILE_END); char temp[2048]; wsprintf(temp, "\r\n(%s,len=%d) ", function, len); DWORD dw; WriteFile(hFile, temp, strlen(temp), &dw, NULL); for(int i =0; i sin_port); } int GetLocalIPBySocket(SOCKET s, char *ip) { struct sockaddr name; int namelen =sizeof(name); getsockname(s, &name, &namelen); strcpy(ip, inet_ntoa(((struct sockaddr_in *)&name)->sin_addr)); return 0; } int GetRemotePortBySocket(SOCKET s) { struct sockaddr name; int namelen =sizeof(name); getpeername(s, &name, &namelen); return ntohs(((struct sockaddr_in *)&name)->sin_port); } int GetIPAndPortByAddr(struct sockaddr *paddr, char *ip, int *port) { *ip =0; *port =0; if(paddr ==NULL) return -1; strcpy(ip, inet_ntoa(((struct sockaddr_in *)paddr)->sin_addr)); *port =ntohs(((struct sockaddr_in *)paddr)->sin_port); return 0; } int GetRemoteIPBySocket(SOCKET s, char *ip) { struct sockaddr name; int namelen =sizeof(name); getpeername(s, &name, &namelen); strcpy(ip, inet_ntoa(((struct sockaddr_in *)&name)->sin_addr)); return 0; } int ipcmp(char *szip1, char *szip2) { ULONG ip1 =GetIntIP(szip1); ULONG ip2 =GetIntIP(szip2); if(ip1 > ip2) return 1; else if(ip1 127) { wsprintf(&new_url[k], "%%%2X", url[i]); k +=3; } else new_url[k++] =url[i]; } new_url[k] =0; return k; } HWND FindChild(HWND hWndParent, char *class_name) { HWND hwndFirst =GetWindow(hWndParent, GW_CHILD); HWND hwnd =hwndFirst; char temp[100]; while(hwnd !=NULL) { GetClassName(hwnd, temp, sizeof(temp)); if(strcmp(temp, class_name) ==0) return hwnd; hwnd =GetWindow(hwnd, GW_HWNDNEXT); if(hwnd ==hwndFirst) return NULL; } return NULL; } /* FARPROC *GetIATPtr(char *dll_name, char *api_name) { HANDLE hModCaller =GetModuleHandle(dll_name); HANDLE hModCallee =GetModuleHandle(dll_name); if(hModCallee ==NULL) return NULL; FARPROC papi =(FARPROC)GetProcAddress((HINSTANCE)hModCallee, api_name); DWORD dwSize; PIMAGE_IMPORT_DESCRIPTOR pImportDesc = (PIMAGE_IMPORT_DESCRIPTOR) ImageDirectoryEntryToData(hModCaller,TRUE,IMAGE_DIRECTORY_ENTRY_IMPORT,&dwSize); if (pImportDesc == NULL) { WriteLog("can not get image directory entry:process_mod:%s, api:%s", dll_name, api_name); return NULL; } //WriteLog("HookOneModule: debug: hook %s 2...", api_name); for (;pImportDesc->Name;pImportDesc++) { char * mod_name = (char *)((PBYTE)hModCaller+pImportDesc->Name); //WriteLog("mod_name=%s", mod_name); if (!strcmpi(dll_name, mod_name)) break; } if (pImportDesc->Name == NULL) { //WriteLog("can not found mod_name:%s", dll_name); return NULL; } PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA)((PBYTE)hModCaller+pImportDesc->FirstThunk); for (;pThunk->u1.Function;pThunk++) { FARPROC * ppfn= (FARPROC *)&pThunk->u1.Function; if (*ppfn == papi) return ppfn; } WriteLog("can not found proc"); return NULL; } */