On the method of traversing disk Win32

Source: Internet
Author: User

Recently to write an online kill the east, although it is dedicated to kill (originally just to clear a few specific files and kill a few specific processes, and then restore the user's registry to normal, a lot of virus Trojan favorite thing to do is to write image hijacking and then the machine restarts, security-related software all finished, but there is no technical content , using the operating system "vulnerability" only), but because it is a disk drive, the virus (Trojan) is disgusting, is an infection type, your disk EXE file can all give you infected into a "small disk machine", very scary, hehe, so no way, to clear it, must kill the disk drive after the process, Scan the whole, for every infected EXE file (like COM file can not infect) to repair, how to do disk traversal? Take a look at the following code: (In fact, the anti-virus engine is the process of working on a file to traverse the disk, and then processing each file)

  1. // -------------------------------------------------------------------------
  2. Function: Scandirectory
  3. Function: Traverse a directory and do something (what do you want to do?)
  4. return value: DWORD
  5. Parameter: const WCHAR *pwszpath
  6. Note: The disk root directory can be
  7. // -------------------------------------------------------------------------
  8. DWORD scandirectory (const WCHAR *pwszpath)
  9. {
  10. Uses_conversion;
  11. static int ncountfile = 0;
  12. DWORD dwret = 1;
  13. WCHAR *s = NULL;
  14. HANDLE hfind = NULL;
  15. Win32_find_dataw fd = {0};
  16. WCHAR Wszfilename[max_path] = L"";
  17. Lstrcpyw (Wszfilename, Pwszpath);
  18. s = wszfilename + wcslen (wszfilename);
  19. if (* (s-1)! = L'//')
  20. *s++ = L'//';
  21. //wcscpy_s (S, 4, L "* *");
  22. :: Lstrcpyw (S, L"* *");
  23. Hfind = Findfirstfilew (Wszfilename, &FD);
  24. if (hfind==invalid_handle_value)
  25. goto Exit0;
  26. Do
  27. {
  28. //Filter
  29. if (_wcsicmp (L".", fd.cfilename) = = 0 | | _wcsicmp (L":", fd.cfilename) = = 0)
  30. continue;
  31. :: Lstrcpyw (S, fd.cfilename);
  32. * (S + lstrlenw (fd.cfilename)) = L'/0 ';
  33. //If the folder is recursive
  34. if (fd.dwfileattributes & file_attribute_directory)
  35. {
  36. //delete. SVN directory, I do a small job, delete the file, O (∩_∩) o ...
  37. WCHAR Wszsvncmd[max_path] = {0};
  38. :: Lstrcpyw (Wszsvncmd, L"rmdir/s/q");
  39. :: Lstrcatw (Wszsvncmd, wszfilename);
  40. :: Lstrcatw (Wszsvncmd, L"//.svn");
  41. System (W2A (Wszsvncmd));
  42. Scandirectory (Wszfilename);
  43. }
  44. Else
  45. {
  46. //Scan the file
  47. //Here you can put in your processing code for the file
  48. }
  49. }while (:: Findnextfilew (Hfind, &FD));
  50. Dwret = 0;
  51. Exit0:
  52. if (hfind! = INVALID_HANDLE_VALUE)
  53. {
  54. :: FindClose (Hfind);
  55. Hfind = NULL;
  56. }
  57. return dwret;
  58. }

Of course, the above code can only traverse a disk, unless you know that your machine has several disks, and then call a few times, but you do not know how many disks on the user's machine, so there must be the following code and the above match:

  1. // -------------------------------------------------------------------------
  2. Function: Parsediskname
  3. function: Resolves the name of the disk that can be scanned on the machine
  4. Return value: DWORD returns the number of disks that can be scanned
  5. Parameter: TCHAR *pszdiskname buffer put the English letter name of the disk that can be scanned
  6. Note:
  7. // -------------------------------------------------------------------------
  8. DWORD parsediskname (TCHAR *pszdiskname)
  9. {
  10. static TCHAR *pszwordtable = {"abcdefghijklmnopqrstuvwxyz"};
  11. DWORD Dwdisk;
  12. DWORD dwbase = 0x1;
  13. DWORD dwcount = 0; //record number of disks
  14. DWORD dwscancount = 0; //number of valid disks to scan for return
  15. DWORD dwstyle;
  16. TCHAR Szdiskpath[4] = {0}; //cache a disk root directory name
  17. TCHAR szdiskarray[26] = {0}; //Record all disk names to be scanned
  18. Dwdisk = GetLogicalDrives ();
  19. While (Dwdisk && dwcount <=:: Lstrlen (pszwordtable))
  20. {
  21. memset (Szdiskpath, 0, sizeof (Szdiskpath));
  22. if (Dwdisk & dwbase)
  23. {
  24. :: Lstrcpyn (Szdiskpath, pszwordtable + dwcount, 2);
  25. :: Lstrcat (Szdiskpath, TEXT ("://"));
  26. Dwstyle = GetDriveType (Szdiskpath);
  27. //Whether it is possible to scan the
  28. if (drive_removable = = Dwstyle | | drive_fixed = = dwstyle)
  29. {
  30. Szdiskarray[dwscancount] = Pszwordtable[dwcount];
  31. dwscancount++;
  32. }
  33. }
  34. Dwdisk = Dwdisk & ~dwbase;
  35. Dwbase = Dwbase * 2;
  36. dwcount++;
  37. }
  38. :: lstrcpy (Pszdiskname, Szdiskarray);
  39. return dwscancount;
  40. }

Hehe, almost, but the above traversal file with recursive implementation, there may be a stack overflow situation, with iterative implementation of traversing the disk is also possible, but I have not to write one, or feel too troublesome, recursive more convenient AH. Again show point code, to restart the computer, but this restart is similar to the power-down restart, we do not casually try Ah, a tune your machine can not get any notice on the restart, why so use it? It is because some viruses do something bad when they receive a system restart notification, and this is the way to completely erase it.

  1. // -------------------------------------------------------------------------
  2. Function: Forceshutdown
  3. Function: Forced restart
  4. return value: HRESULT
  5. Note:
  6. // -------------------------------------------------------------------------
  7. HRESULT Forceshutdown ()
  8. {
  9. //force restart Parameter function pointer declaration
  10. typedef ENUM _shutdown_action
  11. {
  12. Shutdownnoreboot,
  13. Shutdownreboot,
  14. Shutdownpoweroff
  15. } shutdown_action;
  16. typedef DWORD (winapi* lpntshutdownsystem) (shutdown_action ACTION);
  17. LONG nret = FALSE;
  18. HANDLE Htoken;
  19. Token_privileges TKP;
  20. HANDLE hprocess = NULL;
  21. hmodule hntdll = NULL;
  22. hprocess =:: GetCurrentProcess ();
  23. if (hprocess = = NULL)
  24. goto Exit0;
  25. if (!::openprocesstoken (hprocess, Token_adjust_privileges | Token_query, &htoken))
  26. goto Exit0;
  27. if (!::lookupprivilegevalue (NULL, Se_shutdown_name, &TKP. Privileges[0]. LUID))
  28. goto Exit0;
  29. Tkp. Privilegecount = 1;
  30. Tkp. Privileges[0]. Attributes = se_privilege_enabled;
  31. :: AdjustTokenPrivileges (Htoken, FALSE, &TKP, 0, (ptoken_privileges) NULL, 0);
  32. Hntdll = LoadLibrary (_t ("NTDLL.DLL"));
  33. if (hntdll)
  34. {
  35. Lpntshutdownsystem Ntshutdownsystem = (lpntshutdownsystem) GetProcAddress (Hntdll, "Ntshutdownsystem");
  36. if (ntshutdownsystem)
  37. {
  38. Ntshutdownsystem (Shutdownreboot);
  39. }
  40. :: FreeLibrary (Hntdll);
  41. Nret = TRUE;
  42. }
  43. Exit0:
  44. if (htoken)
  45. {
  46. :: CloseHandle (Htoken);
  47. Htoken = NULL;
  48. }
  49. if (hprocess)
  50. {
  51. :: CloseHandle (hprocess);
  52. hprocess = NULL;
  53. }
  54. return nret;
  55. }

Let's talk about that stack overflow later.

http://blog.csdn.net/magictong/article/details/2784420

On the method of traversing disk Win32

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.