飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 2927|回复: 2

在Win2000/XP上安静地替换正在使用的系统文件

[复制链接]
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    发表于 2010-5-26 17:07:47 | 显示全部楼层 |阅读模式
    1. #include <stdlib.h>
    2. #include "Windows.h"
    3. #include "Tlhelp32.h"
    4. #pragma comment( lib, "Advapi32.lib" )

    5. typedef void (_stdcall * CLOSEEVENTS)(void);
    6. typedef unsigned long DWORD;
    7. typedef DWORD ANTISFC_ACCESS;

    8. /*
    9. * ANTISFC structures
    10. */

    11. typedef struct _ANTISFC_PROCESS {
    12.     DWORD     Pid;                 // process pid
    13.     HANDLE ProcessHandle;       // process handle
    14.     char   ImageName[MAX_PATH]; // image name (not full path)
    15. } ANTISFC_PROCESS, *PANTISFC_PROCESS;

    16. __inline void ErrorMessageBox(char *szAdditionInfo)
    17. {
    18.     printf("error on %s, error code %d. \n", szAdditionInfo, GetLastError());
    19. }

    20. void usage(char *n) {
    21.     printf("usage: %s [/d]\n", n);
    22.     printf("\t/d: disable sfc file protecte fuction.\n");
    23.     exit(0);
    24. }

    25. DWORD Init() {
    26.     DWORD  Ret = 0;
    27.     HANDLE hToken;
    28.     LUID sedebugnameValue;
    29.     TOKEN_PRIVILEGES tkp;

    30.     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
    31.         ErrorMessageBox("OpenProcessToken");
    32.     } else {

    33.         if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue)) {
    34.             ErrorMessageBox("LookupPrivilegeValue");
    35.         } else {

    36.             tkp.PrivilegeCount = 1;
    37.             tkp.Privileges[0].Luid = sedebugnameValue;
    38.             tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    39.             if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof tkp, NULL, NULL)) {
    40.                 ErrorMessageBox("AdjustTokenPrivileges");
    41.             } else {
    42.                 Ret = 1;
    43.             }
    44.         }
    45.         CloseHandle(hToken);
    46.     }

    47.     return(Ret);
    48. }

    49. DWORD GetPidEx(char *proc_name, char *full_path) {
    50.     DWORD            dwPid=0;
    51.     HANDLE            hSnapshot;
    52.     PROCESSENTRY32    pe;
    53.     BOOL               Ret;
    54.    
    55.    if (isdigit(proc_name[0]))
    56.        dwPid = strtoul(proc_name, NULL, 0);
    57.    else
    58.       dwPid = -1;
    59.       
    60.     hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    61.     if (hSnapshot == (HANDLE) -1){
    62.         ErrorMessageBox("CreateToolhelp32Snapshot");
    63.         return(0);
    64.     }

    65.     pe.dwSize = sizeof(PROCESSENTRY32);
    66.     Ret = Process32First(hSnapshot, &pe);

    67.     while (Ret) {
    68.           if((strncmp(strlwr(pe.szExeFile), strlwr(proc_name), strlen(proc_name)) == 0)
    69.              || (pe.th32ProcessID == dwPid)) {
    70.                 dwPid = pe.th32ProcessID;
    71.                 strcpy(full_path, pe.szExeFile);
    72.                 break;
    73.         }
    74.         pe.dwSize = sizeof(PROCESSENTRY32);
    75.         Ret = Process32Next(hSnapshot, &pe);
    76.     }

    77.     CloseHandle(hSnapshot);
    78.     if (dwPid == -1)
    79.        dwPid = 0;
    80.     return(dwPid);
    81. }

    82. DWORD InitProcess(PANTISFC_PROCESS Process, char *proc_name, ANTISFC_ACCESS access) {
    83.     DWORD Ret=0;

    84.     Process->Pid = GetPidEx(proc_name, Process->ImageName);
    85.     if (Process->Pid != 0 && Process->ImageName[0] != 0) {
    86.         Process->ProcessHandle = OpenProcess(access, FALSE, Process->Pid);
    87.         if (Process->ProcessHandle == NULL)
    88.             ErrorMessageBox("OpenProcess");
    89.         else
    90.             Ret = 1;
    91.     }

    92.     return(Ret);
    93. }

    94. DWORD InjectThread(PANTISFC_PROCESS Process,
    95.                 PVOID function) {
    96.     HANDLE    hThread;
    97.     DWORD    dwThreadPid = 0, dwState;

    98.     hThread = CreateRemoteThread(Process->ProcessHandle,
    99.                                 NULL,
    100.                                 0,
    101.                                 (DWORD (__stdcall *) (void *)) function,
    102.                                 NULL,
    103.                                 0,
    104.                                 &dwThreadPid);
    105.     if (hThread == NULL) {
    106.         ErrorMessageBox("CreateRemoteThread");
    107.         goto cleanup;
    108.     }

    109.     dwState = WaitForSingleObject(hThread, 4000); // attends 4 secondes

    110.     switch (dwState) {
    111.     case WAIT_TIMEOUT:
    112.     case WAIT_FAILED:
    113.         ErrorMessageBox("WaitForSingleObject");
    114.         goto cleanup;

    115.     case WAIT_OBJECT_0:
    116.         break;

    117.     default:
    118.         ErrorMessageBox("WaitForSingleObject");
    119.         goto cleanup;
    120.     }

    121.     CloseHandle(hThread);
    122.     return dwThreadPid;
    123.    
    124. cleanup:
    125.     CloseHandle(hThread);

    126.     return 0;
    127. }

    128. int main(int argc, char* argv[])
    129. {
    130.     ANTISFC_PROCESS     Process;
    131.     HMODULE hSfc;
    132.     DWORD    dwThread;
    133.     CLOSEEVENTS pfnCloseEvents;
    134.     DWORD dwVersion;

    135.     printf("AntiSfc programed by bgate. :) *\n\n");

    136.     if (argc != 2)
    137.         usage(argv[0]);

    138.     if (strcmp(argv[1], "/d") != 0) {
    139.         usage(argv[0]);
    140.     }

    141.     if (Init()) {
    142.         printf("debug privilege set\n");
    143.     } else {
    144.         printf("error on get debug privilege\n");
    145.         return(0);
    146.     }

    147.     if(InitProcess(&Process, "winlogon.exe", PROCESS_ALL_ACCESS) == 0) {
    148.          printf("error on get process info. \n");
    149.         return(0);
    150.     }

    151.     dwVersion = GetVersion();
    152.     if ((DWORD)(LOBYTE(LOWORD(dwVersion))) == 5){                // Windows 2000/XP
    153.         if((DWORD)(HIBYTE(LOWORD(dwVersion))) == 0){             //Windows 2000
    154.             hSfc = LoadLibrary("sfc.dll");
    155.             printf("Win2000\n");
    156.         }
    157.         else {//if((DWORD)(HIBYTE(LOWORD(dwVersion))) = 1)             //Windows XP
    158.             hSfc = LoadLibrary("sfc_os.dll");
    159.             printf("Windows XP\n");
    160.         }
    161.     }   
    162.     //else if ()  //2003?
    163.     else {
    164.         printf("unsupported version\n");
    165.     }

    166.     pfnCloseEvents = (CLOSEEVENTS)GetProcAddress(hSfc,
    167.                                                  MAKEINTRESOURCE(2));
    168.     if(pfnCloseEvents == NULL){
    169.         printf("Load the sfc fuction failed\n");
    170.         FreeLibrary(hSfc);
    171.         return(0);
    172.     }

    173.     FreeLibrary(hSfc);

    174.     dwThread = InjectThread(&Process,
    175.                             pfnCloseEvents);
    176.    
    177.     if(dwThread == 0){
    178.         printf("failed\n");
    179.     }
    180.     else{
    181.         printf("OK\n");
    182.     }

    183.     CloseHandle(Process.ProcessHandle);
    184.     return(0);

    185. }
    复制代码
    PYG19周年生日快乐!
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

     楼主| 发表于 2010-5-26 17:09:35 | 显示全部楼层
    signed int __cdecl wmain(signed int a1, int a2)
    {
      const WCHAR *v2; // ebx@1
      const WCHAR *v3; // ebp@1
      bool v4; // edi@2
      const wchar_t **v6; // ebx@15
      int v7; // edi@15
      size_t v8; // esi@16
      int v9; // [sp-4h] [bp-510h]@5
      int v10; // [sp-4h] [bp-510h]@17
      char v11; // [sp+13h] [bp-4F9h]@25
      CHAR MultiByteStr; // [sp+14h] [bp-4F8h]@16
      int v13; // [sp+1Ch] [bp-4F0h]@1
      struct _OSVERSIONINFOA VersionInformation; // [sp+20h] [bp-4ECh]@1
      CHAR NewFileName; // [sp+B4h] [bp-458h]@29
      const CHAR puLen; // [sp+118h] [bp-3F4h]@6
      CHAR v17; // [sp+218h] [bp-2F4h]@2
      CHAR FileName; // [sp+268h] [bp-2A4h]@6
      CHAR PathName; // [sp+368h] [bp-1A4h]@29
      struct _WIN32_FIND_DATAA FindFileData; // [sp+3CCh] [bp-140h]@1

      v3 = *(const WCHAR **)(a2 + 4);
      v2 = *(const WCHAR **)(a2 + 8);
      FindFileData.dwFileAttributes = 0;
      v13 = 0;
      memset(&FindFileData.ftCreationTime, 0, 0x13Cu);
      memset(&VersionInformation, 0, sizeof(VersionInformation));
      VersionInformation.dwOSVersionInfoSize = 148;
      GetVersionExA(&VersionInformation);
      if ( VersionInformation.dwMajorVersion <= 4 )
      {
        v9 = (int)"This program requires Windows 2000";
        goto LABEL_31;
      }
      wsprintfA(&v17, "%d", LOWORD(VersionInformation.dwBuildNumber));
      v4 = unknown_libname_15(&v17, "2072") >= 0;
      ostream__operator__("InUse - version 1.4");
      ostream__operator__(10);
      sub_401820(sub_401840);
      ostream__operator__("---------------------------------------------------------------------------");
      ostream__operator__(10);
      sub_401820(sub_401840);
      ostream__operator__("Copyright (c) 1994-1999 Microsoft Corporation. All rights reserved");
      ostream__operator__(10);
      sub_401820(sub_401840);
      if ( v4 != 1 )
      {
        v9 = (int)"You are running on an un-supported beta build of Windows 2000\a";
        goto LABEL_31;
      }
      ostream__operator__("Windows 2000 detected - WFP is enforced");
      ostream__operator__(10);
      sub_401820(sub_401840);
      if ( a1 == 1 )
      {
    LABEL_4:
        sub_401730();
        return 0;
      }
      WideCharToMultiByte(0, 0, v3, -1, &FileName, 256, 0, 0);
      WideCharToMultiByte(0, 0, v2, -1, (LPSTR)&puLen, 256, 0, 0);
      if ( (int)FindFirstFileA(&FileName, &FindFileData) == -1 )
      {
        sub_401730();
        v9 = (int)"\a Error:\tReplacement file doesn't exist";
        goto LABEL_31;
      }
      if ( FindFirstFileA(&puLen, &FindFileData) == (HANDLE)-1 )
      {
        sub_401730();
        v9 = (int)"\a Error:\tDestination file doesn't exist";
        goto LABEL_31;
      }
      if ( SfcIsFileProtected(0, v2) )
      {
        ostream__operator__(10);
        sub_401820(sub_401840);
        ostream__operator__(&puLen);
        ostream__operator__(" is protected by WFP\a");
        ostream__operator__(10);
        sub_401820(sub_401840);
        return 1;
      }
      switch ( a1 )
      {
        case 1:
        case 2:
          sub_401730();
          return 1;
        case 3:
        case 4:
          if ( a1 <= 3 )
            goto LABEL_24;
          v7 = a1 - 3;
          v6 = (const wchar_t **)(a2 + 12);
          break;
        default:
          goto LABEL_4;
      }
      do
      {
        v8 = wcslen(*v6);
        WideCharToMultiByte(0, 0, *(LPCWSTR *)(a2 + 12), -1, &MultiByteStr, 256, 0, 0);
        if ( v8 == 2 )
        {
          if ( unknown_libname_15(&MultiByteStr, "/y") && unknown_libname_15(&MultiByteStr, "/Y") )
          {
            ostream__operator__("\n ");
            ostream__operator__(&MultiByteStr);
            ostream__operator__(" <-- invalid parameter");
            ostream__operator__(10);
            sub_401820(sub_401840);
            sub_401730();
            goto LABEL_23;
          }
          v13 = 1;
          v10 = (int)"Confirmation suppressed";
        }
        else
        {
          v10 = (int)" <-- invalid parameter";
          ostream__operator__(L" ");
          ostream__operator__(&MultiByteStr);
        }
        ostream__operator__(v10);
        ostream__operator__(10);
        sub_401820(sub_401840);
    LABEL_23:
        ++v6;
        --v7;
      }
      while ( v7 );
    LABEL_24:
      ostream__operator__("\nINUSE is about to replace the following file");
      ostream__operator__(10);
      sub_401820(sub_401840);
      ostream__operator__("\n\tExisting:   ");
      ostream__operator__(L" ");
      ostream__operator__(&puLen);
      ostream__operator__(10);
      sub_401820(sub_401840);
      ostream__operator__(L"\t");
      sub_4015C0(&puLen);
      ostream__operator__(10);
      sub_401820(sub_401840);
      ostream__operator__("\tReplacement:");
      ostream__operator__(L" ");
      ostream__operator__(&FileName);
      ostream__operator__(10);
      sub_401820(sub_401840);
      ostream__operator__(L"\t");
      sub_4015C0(&FileName);
      if ( v13 )
        goto LABEL_34;
      ostream__operator__("\n\aDo you want to continue?(y/n) ");
      istream__operator__(&v11);
      if ( v11 == 110 )
      {
        v9 = (int)"\a\nOperation cancelled";
        goto LABEL_31;
      }
      if ( v11 != 78 )
      {
    LABEL_34:
        GetTempPathA(0x200u, &PathName);
        GetTempFileNameA(&PathName, "INU", 0, &NewFileName);
        ostream__operator__("\n\t");
        ostream__operator__(&FileName);
        ostream__operator__(" is replacing --> ");
        ostream__operator__(&puLen);
        ostream__operator__(10);
        sub_401820(sub_401840);
        CopyFileA(&FileName, &NewFileName, 0);
        MoveFileExA(&NewFileName, &puLen, 5u);
        v9 = (int)"\nChanges will not take affect until you reboot.";
      }
      else
      {
        v9 = (int)"\a\nOperation cancelled";
      }
    LABEL_31:
      ostream__operator__(v9);
      ostream__operator__(10);
      sub_401820(sub_401840);
      return 0;
    }
    PYG19周年生日快乐!
  • TA的每日心情
    慵懒
    2019-3-12 17:25
  • 签到天数: 3 天

    [LV.2]偶尔看看I

     楼主| 发表于 2010-5-26 17:14:14 | 显示全部楼层
    只不过是移动拷贝而已,再就是用SfcIsFileProtected检查了一下,目标是否被保护。
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表