thieumao

Code phần quét mã độc - anhkhang

Sep 15th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. // •  Code phần quét mã độc
  2.  
  3. private: System::Boolean compareStr(char *str1, char *str2)
  4.          {
  5.              //Hàm dùng để so sánh 2 chuỗi ký tự có giống nhau không
  6.              //Hàm trả về TRUE khi giống nhau và FALSE khi khác nhau
  7.              int i=0;
  8.              while (*(str1+i) == *(str2+i))
  9.              {
  10.                  i++;
  11.                  if (i == 32)
  12.                      return true;
  13.              }
  14.              return false;
  15.          }
  16. private: System::Boolean compareStr1(char *str1, char *str2, int m, int n)
  17.          {
  18.              //Hàm dùng để so sánh 2 chuỗi ký tự có giống nhau không
  19.              //Hàm trả về TRUE khi giống nhau và FALSE khi khác nhau
  20.              int i=0;
  21.              int j;
  22.              for (j=0;j<=m;j++)
  23.              {
  24.                  while (*(str1+i) == *(str2+i))
  25.                  {
  26.                      i++;
  27.                      if (i == n )
  28.                          return true;
  29.                  }
  30.                  return false;
  31.              }
  32.          }
  33. private: System::Void scan(System::String ^strPath)
  34.          {
  35.              if (isScan)
  36.              {
  37.                  System::String ^strFile;
  38.                  System::String ^tmp;
  39.                  char strDatFile[32];
  40.                  //Liệt kê file trong thư mục hiện tại
  41.                  for each (strFile in System::IO::Directory::GetFiles(strPath))
  42.                  {
  43.                      //Chuyen kieu String sang kieu char
  44.                      int lenght = strFile->Length;
  45.                      char *chTmpData = new char[lenght+1];
  46.                      for(int i = 0; i<lenght;i++)
  47.                      {
  48.                          *(chTmpData + i) = strFile[i];
  49.                      };
  50.                      *(chTmpData + lenght) = '\0';
  51.                      //Mở file cần được scan ra làm việc
  52.                      FILE *fs;
  53.                      fopen_s(&fs,chTmpData,"rb");
  54.                      if (fs != NULL)
  55.                      {
  56.                          FILE *f;
  57.                          char strPathSign[]="mn.dat";
  58.                          fopen_s(&f,strPathSign,"rb");
  59.                          m_Sign sign;
  60.                          int i=0;
  61.                          int posEnd;
  62.                          //Xác định kích thước file
  63.                          fseek(f,0,SEEK_END);
  64.                          posEnd=ftell(f);
  65.                          //Trả về vị trí ban đầu
  66.                          fseek(f,0,SEEK_SET);
  67.                          fseek(fs,0,SEEK_END);
  68.                          int posend=ftell(fs);
  69.                          //Trả về vị trí ban đầu
  70.                          fseek(fs,0,SEEK_SET);
  71.                          //Tiến hành vòng lặp liệt kê mã nhận dạng
  72.                          int j=0;
  73.                          while (ftell(f) < posEnd)
  74.                          {
  75.                              //Đọc mẫu tin lưu mã nhận dạng virus
  76.                              fread(&sign,sizeof(m_Sign),1,f);
  77.                              //Duyet file can quet
  78.                              while(ftell(fs)<posend)
  79.                              {                                  fseek(fs,j++,SEEK_SET);
  80.                              int cur1;
  81.                              cur1=ftell(fs)                             fread(strDatFile,32,1,fs);
  82.                              if (compareStr(strDatFile,sign.Sign) == true)
  83.                              {
  84.                                  //Nhận dạng đúng là virus
  85.                                  tmp = gcnew System::String(sign.Name);
  86.                                  lstRe->Items->Add(L"Phát hiện : " + tmp + L" đường dẫn : " + strFile);
  87.                                  detected++;
  88.                                  lblDetected->Text = Convert::ToString(detected);                   System::Windows::Forms::Application::DoEvents();
  89.                              }
  90.                              }
  91.                              //
  92.                              int cur2;
  93.                              cur2=ftell(f)                                          fread(strDatFile,32,1,fs);
  94.                              if (compareStr(strDatFile,sign.Sign) == true)
  95.                              {                          System::Windows::Forms::Application::DoEvents();
  96.                              }
  97.                          }
  98.                          fclose(f);
  99.                          fclose(fs);
  100.                      }
  101.                      //Ghi ra listBox thông báo đã quét file này
  102.                      tmp = gcnew System::String(strFile);
  103.                      lstFile->Items->Add(tmp);
  104.                      scaned++;
  105.                      lblScaned->Text =  Convert::ToString(scaned);              System::Windows::Forms::Application::DoEvents();
  106.                  }
  107.                  //Tiến hành liệt kê các tập tin trong thư mục
  108.                  for each (strFile in System::IO::Directory::GetDirectories(strPath))
  109.                  {
  110.                      try
  111.                      {
  112.                          //Lặp đệ qui
  113.                          scan(strFile);
  114.                      }
  115.                      catch(Exception ^e)
  116.                      {
  117.                          //Bắt lỗi                                      }
  118.                      }
  119.                  }
  120.              }
Advertisement
Add Comment
Please, Sign In to add comment