#include <stdio.h> #include <Windows.h> #define FileLEN 20 #define SIGNLEN 8
typedef struct SING { char FileName[FileLEN]; LONG FileOffset; BYTE VirusSign[SIGNLEN + 1]; }_SIGN,*PSIGN;
SING sign[2] = { { "setup.exe", 0x1a20, "\x05\x00\x00\x00\x90\x00\x00\x80" }, { "lyshark.exe", 0x3a10, "\x02\x00\x00\x00\x30\x00\x00\x90" } };
BOOL CheckSig(char *FilePath) { DWORD dwNum = 0; BYTE buffer[SIGNLEN + 1]; HANDLE hFile = NULL;
hFile = CreateFile(FilePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); for (int x = 0; x <= 2; x++) { SetFilePointer(hFile, sign[x].FileOffset, NULL, FILE_BEGIN); ReadFile(hFile, buffer, sizeof(buffer), &dwNum, NULL); if (memcmp(sign[x].VirusSign, buffer, SIGNLEN) == 0) { printf("发现病毒: %s\n", FilePath); CloseHandle(hFile); return TRUE; } } return FALSE; }
int main() { WIN32_FIND_DATA stFindFile; HANDLE hFindFile; char *szFilter = "*.exe"; char szFindFile[MAX_PATH]; char szSearch[MAX_PATH]; int ret = 0;
lstrcpy(szFindFile, "D:\\"); lstrcpy(szSearch, "D:\\"); lstrcat(szSearch, szFilter);
hFindFile = FindFirstFile(szSearch, &stFindFile); if (hFindFile != INVALID_HANDLE_VALUE) { do { lstrcat(szFindFile, stFindFile.cFileName); if (!CheckSig(szFindFile)) { printf("%s 不是病毒! \n", szFindFile); } szFindFile[3] = '\0'; ret = FindNextFile(hFindFile, &stFindFile); } while (ret != 0); } FindClose(hFindFile); getchar(); return 0; }
|