예제 #1
0
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//sha256deep format
void ConsoleDirectory_sha256deep(char *tmp_path)
{
  char path[MAX_PATH];
  if (tmp_path[0] == '"')
  {
    //extract separators : "
    snprintf(path,strlen(tmp_path)-2,"%s",tmp_path+1);
    path[strlen(path)] = 0;
  }else strncpy(path,tmp_path,MAX_PATH);

  printf("[%s]\n",path);

  char search[MAX_PATH]="",path_ex[MAX_PATH],s_sha[MAX_PATH];
  snprintf(search,MAX_PATH,"%s*.*",path);

  WIN32_FIND_DATA data;
  HANDLE hfic = FindFirstFile(search, &data);
  if (hfic == INVALID_HANDLE_VALUE)return;
  do
  {
    if(data.cFileName[0] == '.' && (data.cFileName[1] == 0 || data.cFileName[1] == '.')){}
    else
    {
      if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
      {
        snprintf(path_ex,MAX_PATH,"%s%s\\",path,data.cFileName);
        ConsoleDirectory_sha256deep(path_ex);
      }else
      {
        s_sha[0] = 0;
        snprintf(path_ex,MAX_PATH,"%s%s",path,data.cFileName);
        if ((data.nFileSizeLow + data.nFileSizeHigh) < MAX_FILE_SIZE_HASH)FileToSHA256(path_ex, s_sha);
        if (s_sha[0] != 0)printf("%s  %s\n",s_sha,path_ex);
      }
    }
  }while(FindNextFile (hfic,&data));
}
예제 #2
0
//----------------------------------------------------------------
void CheckListFile(DWORD iitem, char *file_path, char*filename, long long int filesize, WIN32_FIND_DATA *data, DWORD cb_id)
{
  DWORD j, _nb_i = SendDlgItemMessage(h_main,cb_id,LB_GETCOUNT,(WPARAM)NULL,(LPARAM)NULL);
  char file[LINE_SIZE], tmp[LINE_SIZE], date[MAX_PATH];

  HANDLE hfile;
  char s_sha[SHA256_SIZE]="",s_md5[MAX_PATH];
  FILETIME LocalFileTime;
  SYSTEMTIME SysTimeModification;
  BOOL exist = FALSE;

  char ss_sha[SHA256_SIZE]="",ss_md5[MAX_PATH]="", s_size[MAX_PATH]="";
  long long int d_size = -1;

  BOOL hash_already_done = FALSE;

  for (j=0;j<_nb_i && scan_start;j++)
  {
    if (SendDlgItemMessage(h_main,cb_id,LB_GETTEXTLEN,(WPARAM)j,(LPARAM)NULL) > LINE_SIZE)continue;
    if (SendDlgItemMessage(h_main,cb_id,LB_GETTEXT,(WPARAM)j,(LPARAM)file))
    {
      exist = FALSE;
      //check if the file exist
      if (file[0] == ':') // hash type
      {
        ss_sha[0] = 0;
        ss_md5[0] = 0;
        s_size[0] = 0;
        d_size    = -1;

        //format= :size on octets:MD5 hash: SHA256 hash:
        char *c = file+1; //pass ':'******':')continue;
        while (*c && *c != ':')
        {
          *d++ = *c++;
        }
        *d = 0;
        d_size = atol(s_size);
        if (d_size < 1 ) d_size = -1;

        //MD5
        d = ss_md5;
        c++;//pass ':'******':')
        {
          while (*c && *c != ':')
          {
            *d++ = *c++;
          }
          *d = 0;
        }

        //SHA
        d = ss_sha;
        c++;//pass ':'******':')
        {
          *d++ = *c++;
        }
        *d = 0;

        //check
        if (d_size == filesize || d_size == -1)
        {
          if (hash_already_done)
          {
            if (ss_md5[0] != 0)
              if (compare_nocas(s_md5,ss_md5))
                exist = TRUE;

            if ((!exist && ss_md5[0] == 0) && (ss_sha[0] != 0))
              if (compare_nocas(s_sha,ss_sha))
                exist = TRUE;
          }else
          {
            hash_already_done = TRUE;

            //check all other datas
            s_md5[0] = 0;
            s_sha[0] = 0;

            hfile = CreateFile(file_path,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);
            if (hfile != INVALID_HANDLE_VALUE)
            {
              FileToMd5(hfile, s_md5);
              CloseHandle(hfile);

              hfile = CreateFile(file_path,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);
              if (hfile != INVALID_HANDLE_VALUE)
              {
                FileToSHA256(hfile, s_sha);
                CloseHandle(hfile);
              }
            }

            if (ss_md5[0] != 0)
              if (compare_nocas(s_md5,ss_md5))
                exist = TRUE;

            if (!exist && ss_sha[0] != 0)
              if (compare_nocas(s_sha,ss_sha))
                exist = TRUE;
          }

          if (!exist && ss_md5[0] == 0 && ss_sha[0] == 0)exist = TRUE;
        }
      }else if (file[0] == '*')//contient
      {
        if (Contient_nocas(filename,&file[1]))
        {
          exist = TRUE;

          s_md5[0] = 0;
          s_sha[0] = 0;

          hfile = CreateFile(file_path,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);
          if (hfile != INVALID_HANDLE_VALUE)
          {
            FileToMd5(hfile, s_md5);
            CloseHandle(hfile);

            hfile = CreateFile(file_path,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);
            if (hfile != INVALID_HANDLE_VALUE)
            {
              FileToSHA256(hfile, s_sha);
              CloseHandle(hfile);
            }
          }
        }
      }else //file type
      {
        if (compare_nocas(filename,file))
        {
          exist = TRUE;

          s_md5[0] = 0;
          s_sha[0] = 0;

          hfile = CreateFile(file_path,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);
          if (hfile != INVALID_HANDLE_VALUE)
          {
            FileToMd5(hfile, s_md5);
            CloseHandle(hfile);

            hfile = CreateFile(file_path,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);
            if (hfile != INVALID_HANDLE_VALUE)
            {
              FileToSHA256(hfile, s_sha);
              CloseHandle(hfile);
            }
          }
        }
      }

      if (exist && (filesize != 0 || (data->ftLastWriteTime.dwHighDateTime != 0 && data->ftLastWriteTime.dwLowDateTime != 0)))
      {
        date[0] = 0;
        FileTimeToLocalFileTime(&(data->ftLastWriteTime), &LocalFileTime);
        FileTimeToSystemTime(&LocalFileTime, &SysTimeModification);
        snprintf(date,MAX_PATH,"[Last_modification:%02d/%02d/%02d-%02d:%02d:%02d,Size:%lo]"
                     ,SysTimeModification.wYear,SysTimeModification.wMonth,SysTimeModification.wDay
                     ,SysTimeModification.wHour,SysTimeModification.wMinute,SysTimeModification.wSecond,filesize);


        if (s_sha[0] != 0)
        {
          snprintf(tmp,LINE_SIZE,"%s %s;MD5;%s;%s;%s",file_path,date,s_md5[0]==0?"":s_md5,SHA1_enable?"SHA1":"SHA256",s_sha[0]==0?"":s_sha);
        }else if (s_md5[0] != 0)
        {
          snprintf(tmp,LINE_SIZE,"%s %s;MD5;%s;;",file_path,date,s_md5[0]==0?"":s_md5);
        }else snprintf(tmp,LINE_SIZE,"%s %s;;;;",file_path,date);

        AddMsg(h_main,(char*)"FOUND (File3)",tmp,(char*)"");
        AddLSTVUpdateItem(tmp, COL_FILES, iitem);
      }
    }
  }
}
예제 #3
0
//----------------------------------------------------------------
void CheckRecursivFilesFromSizeAndEM(DWORD iitem, char *remote_name, long long int size, char *MD5, char *SHA256, BOOL recursif, char*source)
{
  #ifdef DEBUG_MODE_FILES
  AddMsg(h_main,"DEBUG","files:CheckRecursivFilesFromSizeAndEM START",remote_name);
  #endif
  WIN32_FIND_DATA data;
  char tmp_path[LINE_SIZE]="", tmp_remote_name[LINE_SIZE]="", date[MAX_PATH]="\0\0\0";

  //search
  BOOL exist;
  HANDLE hfile, hfind;
  LARGE_INTEGER filesize;
  char s_sha[SHA256_SIZE]="",s_md5[MAX_PATH];
  FILETIME LocalFileTime;
  SYSTEMTIME SysTimeModification;

  snprintf(tmp_path,LINE_SIZE,"%s\\*.*",remote_name);
  hfind = FindFirstFile(tmp_path, &data);
  if (hfind != INVALID_HANDLE_VALUE && scan_start)
  {
    do
    {
      if (data.cFileName[0] == '.' && (data.cFileName[1] == 0 || (data.cFileName[2] == 0 && data.cFileName[1] == '.')))continue;

      #ifdef DEBUG_MODE_FILES
      AddMsg(h_main,(char*)"DEBUG S(CheckRecursivFilesFromSizeAndEM)",remote_name,(char*)data.cFileName);
      #endif

      if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && recursif)
      {
        snprintf(tmp_remote_name,LINE_SIZE,"%s\\%s",remote_name,data.cFileName);

        #ifdef DEBUG_MODE_FILES
        AddMsg(h_main,(char*)"DEBUG D(CheckRecursivFilesFromSizeAndEM)",tmp_remote_name,(char*)"");
        #endif

        CheckRecursivFilesFromSizeAndEM(iitem, tmp_remote_name, size, MD5, SHA256, recursif, source);
        continue;
      }

      exist = FALSE;
      filesize.HighPart = data.nFileSizeHigh;
      filesize.LowPart  = data.nFileSizeLow;

      if (filesize.QuadPart == size || size == -1)
      {
        snprintf(tmp_remote_name,LINE_SIZE,"%s\\%s",remote_name,data.cFileName);

        s_md5[0] = 0;
        s_sha[0] = 0;

        if (MD5[0] != 0 || SHA256[0] != 0 || (SHA256[0] == 0 && MD5[0] == 0))
        {
          //make MD5 and SHA256 hashes
          //MD5
          hfile = CreateFile(tmp_remote_name,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);
          if (hfile != INVALID_HANDLE_VALUE)
          {
            FileToMd5(hfile, s_md5);
            CloseHandle(hfile);

            //SHA256
            hfile = CreateFile(tmp_remote_name,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);
            if (hfile != INVALID_HANDLE_VALUE)
            {
              FileToSHA256(hfile, s_sha);
              CloseHandle(hfile);
            }
          }

          if (MD5[0] == 0 && SHA256[0] == 0)exist = TRUE;
          else
          {
            if(MD5[0] != 0 && compare_nocas(MD5,s_md5))exist = TRUE;
            else if(SHA256[0] != 0 && compare_nocas(SHA256,s_sha))exist = TRUE;
          }

          if (exist && (filesize.QuadPart!=0 || (data.ftLastWriteTime.dwHighDateTime != 0 && data.ftLastWriteTime.dwLowDateTime != 0)))
          {
            date[0] = 0;
            FileTimeToLocalFileTime(&(data.ftLastWriteTime), &LocalFileTime);
            FileTimeToSystemTime(&LocalFileTime, &SysTimeModification);
            snprintf(date,MAX_PATH,"[Last_modification:%02d/%02d/%02d-%02d:%02d:%02d,Size:%lo]"
                         ,SysTimeModification.wYear,SysTimeModification.wMonth,SysTimeModification.wDay
                         ,SysTimeModification.wHour,SysTimeModification.wMinute,SysTimeModification.wSecond,filesize.QuadPart);

            if (s_sha[0] != 0)
            {
              snprintf(tmp_remote_name,LINE_SIZE,"%s %s;MD5;%s;%s;%s",tmp_remote_name,date,s_md5[0]==0?"":s_md5,SHA1_enable?"SHA1":"SHA256",s_sha[0]==0?"":s_sha);
            }else if (s_md5[0] != 0)
            {
              snprintf(tmp_remote_name,LINE_SIZE,"%s %s;MD5;%s;;",tmp_remote_name,date,s_md5);
            }else snprintf(tmp_remote_name,LINE_SIZE,"%s %s;;;;",tmp_remote_name,date);

            AddMsg(h_main,(char*)"FOUND (File2)",tmp_remote_name,(char*)"");
            AddLSTVUpdateItem(tmp_remote_name, COL_FILES, iitem);
          }
        }
      }
    }while(FindNextFile(hfind, &data) != 0 && scan_start);
    FindClose(hfind);
  }
  #ifdef DEBUG_MODE_FILES
  AddMsg(h_main,"DEBUG","files:CheckRecursivFilesFromSizeAndEM END",remote_name);
  #endif
}
예제 #4
0
//----------------------------------------------------------------
void CheckFile(DWORD iitem, char *file, WIN32_FIND_DATA *data, char *source)
{
  #ifdef DEBUG_MODE_FILES
  AddMsg(h_main,"DEBUG","files:CheckFile START",file);
  #endif
  char s_sha[SHA256_SIZE]="",s_md5[MAX_PATH], date[MAX_PATH]="\0\0\0";
  HANDLE hfile;
  LARGE_INTEGER filesize;
  FILETIME LocalFileTime;
  SYSTEMTIME SysTimeModification;

  BOOL error = FALSE;

  //last modify
  if (data == NULL)
  {
    WIN32_FIND_DATA d0;
    HANDLE hfind = FindFirstFile(file, &d0);
    if (hfind != INVALID_HANDLE_VALUE)
    {
      filesize.HighPart = d0.nFileSizeHigh;
      filesize.LowPart  = d0.nFileSizeLow;

      if (filesize.QuadPart == 0 && d0.ftLastWriteTime.dwHighDateTime == 0 && d0.ftLastWriteTime.dwLowDateTime == 0) error = TRUE;
      else
      {
        FileTimeToLocalFileTime(&(d0.ftLastWriteTime), &LocalFileTime);
        FileTimeToSystemTime(&LocalFileTime, &SysTimeModification);
        snprintf(date,MAX_PATH,"[Last_modification:%02d/%02d/%02d-%02d:%02d:%02d,Size:%do]"
                     ,SysTimeModification.wYear,SysTimeModification.wMonth,SysTimeModification.wDay
                     ,SysTimeModification.wHour,SysTimeModification.wMinute,SysTimeModification.wSecond,filesize.QuadPart);
        FindClose(hfind);
      }
    }
  }else if (data->ftLastWriteTime.dwHighDateTime != 0 || data->ftLastWriteTime.dwLowDateTime != 0)
  {
    filesize.HighPart = data->nFileSizeHigh;
    filesize.LowPart  = data->nFileSizeLow;

    if (filesize.QuadPart == 0) error = TRUE;
    else
    {
      FileTimeToLocalFileTime(&(data->ftLastWriteTime), &LocalFileTime);
      FileTimeToSystemTime(&LocalFileTime, &SysTimeModification);
      snprintf(date,MAX_PATH,"[Last_modification:%02d/%02d/%02d-%02d:%02d:%02d,Size:%do]"
                   ,SysTimeModification.wYear,SysTimeModification.wMonth,SysTimeModification.wDay
                   ,SysTimeModification.wHour,SysTimeModification.wMinute,SysTimeModification.wSecond,filesize.QuadPart);
    }
  }else error = TRUE;

  if (!error)
  {
    //MD5
    hfile = CreateFile(file,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);
    if (hfile != INVALID_HANDLE_VALUE)
    {
      FileToMd5(hfile, s_md5);
      CloseHandle(hfile);

      //SHA256
      hfile = CreateFile(file,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0);
      if (hfile != INVALID_HANDLE_VALUE)
      {
        FileToSHA256(hfile, s_sha);
        CloseHandle(hfile);
      }
    }

    if (s_sha[0] != 0)
    {
      snprintf(file,LINE_SIZE,"%s %s;MD5;%s;%s;%s",file,date,s_md5[0]==0?"":s_md5,SHA1_enable?"SHA1":"SHA256",s_sha[0]==0?"":s_sha);
    }else if (s_md5[0] != 0)
    {
      snprintf(file,LINE_SIZE,"%s %s;MD5;%s;;",file,date,s_md5);
    }else snprintf(file,LINE_SIZE,"%s %s;;;;",file,date);

    AddMsg(h_main,(char*)"FOUND (File1)",source,(char*)file);
    AddLSTVUpdateItem(file, COL_FILES, iitem);
  }

  #ifdef DEBUG_MODE_FILES
  AddMsg(h_main,"DEBUG","files:CheckFile END",file);
  #endif
}