²é¿´: 362  |  »Ø¸´: 0
µ±Ç°Ö÷ÌâÒѾ­´æµµ¡£

sdlj8051

½ð³æ (ÖøÃûдÊÖ)

[½»Á÷] [תÌù]Ó¦ÓÃÄÚ´æ¶ÏµãµÄLoader

program Loader;
(***********************************)
(*                                 *)
(* Memory Access Breakpoint Loader *)
(*            by tt.t              *)
(*                                 *)
(*                                 *)
(***********************************)

{
Ä¿±ê£ºÀ¹½Ø¶ÔÖ¸¶¨µØÖ·µÄд²Ù×÷£¬nopµôдµØÖ·µÄÖ¸Áî¡£
Äѵ㣺ÔÚÒª²¹¶¡µØÖ·ÉèMemory Write Breakpoint£¬²éÁ˰ëÌìûÕÒµ½ÏֳɵĶ«Î÷£¬Ö»ºÃ×Ô¼ºÐ´¡£
˼·£º½«Òª²¹¶¡µÄµØÖ·ÉèΪ²»¿Éд£¬µ±Ð´Ê±»á·¢ÉúAV´íÎó£¬È»ºó½øÐÐPatch¡£µ«VirtualProtectEx»á½«Õû

¸öPageÉèΪ²»¿ÉдÊôÐÔ£¬ËùÓÐдPageµÄ²Ù×÷¶¼»á²úÉúAccess violation£¬ÎÞ·¨Ö±½ÓÕÒµ½ÒªPatchµÄ´úÂë¡£
½â¾ö·½·¨£ºÒª±£´æµØÖ·ËùÔÚPageµÄµØÖ··¶Î§£¬·¢ÉúAV´íÎóʱÅжÏÊÇ·ñÒòΪVirtualProtect²úÉúµÄExceptio

n£¬Èç²»Êǽ«PageµÄÊôÐÔÉèΪ¿Éд£¬Í¬Ê±ÉèÖõ¥²½±êÖ¾£¬Ð´²Ù×÷Íê³Éºó»Ö¸´PageΪ²»¿ÉдÊôÐÔ£¬¼ÌÐøÖ´

ÐУ¬Ö±ÖÁÕÒµ½ÐèPatch´úÂë¡£
²»ÖªµÀÓÐûÓиüºÃµÄ·½·¨¡£
}

uses
  JwaWinType,
  JwaWinNt,
  JwaWinBase,
  JwaWinUser;

{$R *.res}

var
  si: STARTUPINFO;
  pi: PROCESS_INFORMATION;

function MyExtractFilePath(f: String): String;
var
  i, l: integer;
begin
  l := Length(f);
  for i := l downto 1 do
    if f = '' then Break;
  result := copy(f, 0, i);
end;

procedure CreateVictimProcess(Path: String);
const
  Nop: PChar = Chr($90) + Chr($90) + Chr($90);
var
  DbgEvent: TDebugEvent;
  DbgParam: DWORD;
  OldPrt, NewPrt: DWORD;
  pPatch: PByte;
  PgMin, PgMax: DWORD;
  MemInfo: TMemoryBasicInformation;
  WExpAddr: DWORD;
  DbgContext: TContext;
  rm: Boolean;
  hThread: DWORD;
begin   
  ZeroMemory(@si, sizeof(STARTUPINFO));
  si.cb := sizeof(STARTUPINFO);
  if not CreateProcess(PChar(Path), nil, nil, nil, False, CREATE_SUSPENDED or

CREATE_DEFAULT_ERROR_MODE, nil,
                       PChar(MyExtractFilePath(Path)), si, pi) then
  begin
    MessageBox(0, 'CreateProcess failed! ', 'Error!', 0);
    exit;
  end;
  ResumeThread(pi.hThread);
  if WaitForInputIdle(pi.hProcess, INFINITE) <> 0 then
  begin
    MessageBox(0, 'WaitForInputIdle failed! ', 'Error!', 0);
    exit;
  end;
  if not DebugActiveProcess(pi.dwProcessId) then
  begin
    MessageBox(0, 'DebugActiveProcess failed! ', 'Error!', 0);
    exit;
  end;
  if VirtualQueryEx(pi.hProcess, Pointer($9c66BC), MemInfo, SizeOf(MemInfo)) = 0 then
  begin
    MessageBox(0, 'VirtualQueryEx failed! ', 'Error!', 0);
    exit;
  end;
  PgMin := DWORD(MemInfo.BaseAddress);
  pgMax := PgMin + MemInfo.RegionSize;
  {VirtualProtect»á½«Õû¸öPageÉèΪ²»¿ÉдÊôÐÔ£¬ËùÓÐдPageµÄ²Ù×÷¶¼»á²úÉúAccess violation£¬Òª±£´æ

Õû¸öPageµÄµØÖ··¶Î§£¬
   ºóÃæ¿ÉÒÔÅжÏÊÇ·ñÒòΪVirtualProtect²úÉúµÄException}      
  if not VirtualProtectEx(pi.hProcess, Pointer($9c66BC), 1, PAGE_EXECUTE_READ, @OldPrt) then
  begin
    MessageBox(0, 'VirtualProtectEx failed! ', 'Error!', 0);
    exit;
  end;
  {¸ÄдPageÊôÐÔΪ²»¿Éд}
  rm := false;
  while WaitForDebugEvent(DbgEvent, INFINITE) do
  begin
    DbgParam := DBG_CONTINUE;
    case DbgEvent.dwDebugEventCode of
      EXCEPTION_DEBUG_EVENT:
      begin
        if DbgEvent.Exception.ExceptionRecord.ExceptionCode <> EXCEPTION_BREAKPOINT then
          case DbgEvent.Exception.ExceptionRecord.ExceptionCode of
            EXCEPTION_SINGLE_STEP: {µ¥²½ÖжÏ}
            begin
              if rm then {ÓÉÓÚEXCEPTION_ACCESS_VIOLATION²úÉúµÄµ¥²½Öжϣ¬»Ö¸´PageΪ²»¿ÉдÊôÐÔ}
              begin
                rm := false;
                VirtualProtectEx(pi.hProcess, Pointer($9c66BC), 1, PAGE_EXECUTE_READ, @NewPrt);
              end;
            end;
            EXCEPTION_ACCESS_VIOLATION: {AVÖжÏ}
            begin
              DbgParam := DBG_EXCEPTION_NOT_HANDLED;
              if DbgEvent.Exception.ExceptionRecord.ExceptionInformation[0] = 1 then {д²Ù×÷}
              begin
                WExpAddr := DbgEvent.Exception.ExceptionRecord.ExceptionInformation[1]; {д²Ù×÷µÄÄ¿±êµØ

Ö·}
                if (WExpAddr >= PgMin) and (WExpAddr <= PgMax) then {Ä¿±êµØÖ·ÔÚPage·¶Î§}
                begin
                  DbgParam := DBG_CONTINUE;
                  if(WExpAddr <> $9c66BC) then {²»ÊÇдָ¶¨µØÖ·}
                  begin
                    VirtualProtectEx(pi.hProcess, Pointer($9c66BC), 1, OldPrt, @NewPrt);
                    DbgContext.ContextFlags := CONTEXT_CONTROL;
                    hThread := OpenThread(THREAD_ALL_ACCESS, false, DbgEvent.dwThreadId);
                    GetThreadContext(hThread, DbgContext);
                    DbgContext.EFlags := DbgContext.EFlags or $100;

{Éèµ¥²½±êÖ¾£¬»á´¥·¢EXCEPTION_SINGLE_STEP}
                    SetThreadContext(hThread, DbgContext);
                    rm := true; {±êÖ¾£¬±íÃ÷ÊÇEXCEPTION_ACCESS_VIOLATION²úÉúµÄµ¥²½ÖжÏ}
                  end
                  else
                  begin {Patch}
                    pPatch := DbgEvent.Exception.ExceptionRecord.ExceptionAddress;
                    VirtualProtectEx(pi.hProcess, pPatch, 3, PAGE_READWRITE, @NewPrt);
                    WriteProcessMemory(pi.hProcess, pPatch, Nop, 3, nil);
                    VirtualProtectEx(pi.hProcess, pPatch, 3, NewPrt, @NewPrt);
                  end;
                end;
              end;
            end;
            else
              DbgParam := DBG_EXCEPTION_NOT_HANDLED;
          end;
      end;
      EXIT_PROCESS_DEBUG_EVENT:
      begin
        ContinueDebugEvent(DbgEvent.dwProcessId, DbgEvent.dwThreadId, DbgParam);
        Break;
      end;
    end;
    ContinueDebugEvent(DbgEvent.dwProcessId, DbgEvent.dwThreadId, DbgParam);
  end;
end;

var
  Victim: string;
begin
  Victim := MyExtractFilePath(ParamStr(0)) + 'Wise***.exe'; //Êܺ¦³ÌÐò
  CreateVictimProcess(Victim);
  halt;
end.


--------------------------------------------------------------------------------

±ê Ìâ: ´ð¸´
·¢ÌûÈË:ttui
ʱ ¼ä: 2005-10-28 10:09
ÏêϸÐÅÏ¢:


ft£¬¼ÈÈ»¿´²»¶®ÄǾÍÄǾÍÏêϸעÊÍÏ¡£
¼ÇµÃÒª´óÌåÁ˽âÏÂdeubg apiµÄÓ÷¨ºÍʹÓÃÁ÷³ÌÏÈ.

procedure CreateVictimProcess(Path: String);
const
  Nop: PChar = Chr($90) + Chr($90) + Chr($90);
var
  DbgEvent: TDebugEvent;
  DbgParam: DWORD;  //ContinueDebugEventÓÃ,±êÖ¾ÈçºÎ´¦Àíµ÷ÊÔÏûÏ¢
  OldPrt, NewPrt: DWORD;
  pPatch: PByte;
  PgMin, PgMax: DWORD;
  MemInfo: TMemoryBasicInformation;
  WExpAddr: DWORD;
  DbgContext: TContext;
  rm: Boolean;
  hThread: DWORD;
begin   
  ZeroMemory(@si, sizeof(STARTUPINFO));
  si.cb := sizeof(STARTUPINFO);
  if not CreateProcess(PChar(Path), nil, nil, nil, False, CREATE_SUSPENDED or CREATE_DEFAULT_ERROR_MODE, nil,
                       PChar(MyExtractFilePath(Path)), si, pi) then
  begin
    MessageBox(0, 'CreateProcess failed! ', 'Error!', 0);
    exit;
  end;
  //½¨Á¢Ä¿±ê½ø³Ì
  ResumeThread(pi.hThread);
  //»Ö¸´½ø³ÌÖ´ÐÐ.ÆäʵCreateProcessʱ²»¼ÓCREATE_SUSPENDED±êÖ¾¾Í¿ÉÒÔÊ¡µôÕâ¾ä
  if WaitForInputIdle(pi.hProcess, INFINITE) <> 0 then
  begin
    MessageBox(0, 'WaitForInputIdle failed! ', 'Error!', 0);
    exit;
  end;
  //µÈ´ýÄ¿±ê½ø³ÌÍêÈ«ÔËÐÐÖÁÆä¿ªÊ¼µÈ´ýÓû§ÊäÈë.
  //ÒòΪĿ±ê³ÌÐòÊǼӹý¿ÇµÄ,¿ÇµÄ²¿·Ö»á¼ì²éµ÷ÊÔÆ÷,ËùÒԵȵ½¿ÇÔËÐнáÊøÔÚÈ¥debugËü.
  if not DebugActiveProcess(pi.dwProcessId) then
  begin
    MessageBox(0, 'DebugActiveProcess failed! ', 'Error!', 0);
    exit;
  end;
  //¹ÒÉÏÄ¿±ê³ÌÐò
  if VirtualQueryEx(pi.hProcess, Pointer($9c66BC), MemInfo, SizeOf(MemInfo)) = 0 then
  begin
    MessageBox(0, 'VirtualQueryEx failed! ', 'Error!', 0);
    exit;
  end;
  //²éѯÐèÒªÉèÄÚ´æ¶ÏµãµÄµØÖ·($9c66BC,Ò²¾ÍÊÇ0x9c66BC,ºóÃæ¼ò³Æaddr)ËùÔÚpageµÄÇé¿ö
  PgMin := DWORD(MemInfo.BaseAddress);
  pgMax := PgMin + MemInfo.RegionSize;
  //µÃµ½addrËùÔÚµÄpageµÄʼĩµØÖ·.
  {ÒòΪVirtualProtect»á½«addrËùÔÚÕû¸öPageÉèΪ²»¿ÉдÊôÐÔ(¼ûVirtualProtectµÄAPI˵Ã÷)£¬ËùÓÐдPageµÄ²Ù×÷,¼´Ê¹²»ÊÇдÎÒÃǸÐÐËȤ(µ«Î»ÓÚÄǸöpageÉÏ)µÄµØÖ·µÄ²Ù×÷,¶¼»áÒòΪ½«addrÉèΪ²»¿ÉдÊôÐÔ¶ø²úÉúAccess violation.ËùÒÔÕâÀïÒª±£´æÕû¸öPageµÄµØÖ··¶Î§£¬ÒÔ±ãºóÃæÅжÏAVÊDz»ÊÇÒòдaddr¶ø²úÉúµÄ}  
  if not VirtualProtectEx(pi.hProcess, Pointer($9c66BC), 1, PAGE_EXECUTE_READ, @OldPrt) then
  begin
    MessageBox(0, 'VirtualProtectEx failed! ', 'Error!', 0);
    exit;
  end;
  {¸ÄдaddrÊôÐÔΪ²»¿Éд.ÕâÀï»á½«addrËùÔÚµÄÕû¸öPageÉèΪ²»¿ÉдÊôÐÔ}
  rm := false;  {±êÖ¾ÊDz»ÊÇÒò´¦ÀíAVÉèÖõĵ¥²½ÖжϲúÉúµÄÖжÏ}
  while WaitForDebugEvent(DbgEvent, INFINITE) do  //µÈ´ýµ÷ÊÔÏûÏ¢
  begin
    DbgParam := DBG_CONTINUE;  //ĬÈÏ´¦Àíµ÷ÊÔÏûÏ¢
    case DbgEvent.dwDebugEventCode of
      EXCEPTION_DEBUG_EVENT:
      begin
        if DbgEvent.Exception.ExceptionRecord.ExceptionCode <> EXCEPTION_BREAKPOINT then
        //ÅжÏÊDz»ÊÇint 3²úÉúµÄÖжÏ.ÒòΪDebugActiveProcess¹ÒÉϳÌÐòºó»áÏòdebuger·¢³öÒ»¸öEXCEPTION_BREAKPOINTµ÷ÊÔÏûÏ¢,ÕâÀïºöÂÔËü
          case DbgEvent.Exception.ExceptionRecord.ExceptionCode of  //ÅжÏÒì³£ÀàÐÍ
            EXCEPTION_ACCESS_VIOLATION: {AVÒì³£}
            begin
              DbgParam := DBG_EXCEPTION_NOT_HANDLED; //±íʾÓÉÄ¿±ê³ÌÐòµÄSEH´¦ÀíÒì³£
              if DbgEvent.Exception.ExceptionRecord.ExceptionInformation[0] = 1 then
              begin  {ÊÇд²Ù×÷²úÉúµÄAV}
                WExpAddr := DbgEvent.Exception.ExceptionRecord.ExceptionInformation[1]; {WExpAddr =д²Ù×÷µÄÄ¿±êµØÖ·}
                if (WExpAddr >= PgMin) and (WExpAddr <= PgMax) then {WExpAddrÔÚaddrËùÔÚPageÉÏ}
                begin
                  DbgParam := DBG_CONTINUE;//±íʾÎÒÃÇÀ´´¦ÀíÒì³£,ÈÃÄ¿±ê³ÌÐò´Ó·¢ÉúÒì³£´¦¼ÌÐøÖ´ÐÐ.
                  if(WExpAddr <> $9c66BC) then {WExpAddr²»ÊÇдaddr,ÒªÔÊÐíÄ¿±ê³ÌÐòд²Ù×÷}
                  begin
                    VirtualProtectEx(pi.hProcess, Pointer($9c66BC), 1, OldPrt, @NewPrt);//»Ö¸´addrËùÔÚpageΪԭÀ´µÄÊôÐÔ(¿Éд)

                    DbgContext.ContextFlags := CONTEXT_CONTROL;
                    hThread := OpenThread(THREAD_ALL_ACCESS, false, DbgEvent.dwThreadId);
                    //ÓÉThread IdµÃµ½Thread handle
                    GetThreadContext(hThread, DbgContext);
                    //µÃµ½threadµÄContext,ÉèÖõ¥²½Ö´ÐбêÖ¾,ÈÃд²Ù×÷Íê³Éºó·¢Éúµ¥²½ÖжÏ,ÒԱ㽫addrÖØÐÂÉèΪ²»¿Éд,À¹½ØÏÂÒ»´Îд²Ù×÷.
                    DbgContext.EFlags := DbgContext.EFlags or $100; {Éèµ¥²½±êÖ¾}
                    SetThreadContext(hThread, DbgContext);
                    //ÉèÖÃthreadµÄContext

                    rm := true; {±êÖ¾£¬±íÃ÷ÊÇÎÒÃÇ´¦ÀíAV¶ø²úÉúµÄµ¥²½ÖжÏ}
                  end
                  else  //{WExpAddrÊÇдaddr,½øÐÐPatch}
                  begin
                    pPatch := DbgEvent.Exception.ExceptionRecord.ExceptionAddress;
                    //дµØÖ·²Ù×÷Óï¾äµØÖ·
                    VirtualProtectEx(pi.hProcess, pPatch, 3, PAGE_READWRITE, @NewPrt);
                    //Éè²Ù×÷Óï¾äµØÖ·Îª¿Éд
                    WriteProcessMemory(pi.hProcess, pPatch, Nop, 3, nil);
                    //nopµôдµØÖ·Óï¾ä
                    VirtualProtectEx(pi.hProcess, pPatch, 3, NewPrt, @NewPrt);
                    //»Ö¸´Óï¾äµØÖ·ÊôÐÔ
                  end;
                end;
              end;
            end;{AVÒì³£´¦ÀíÍê±Ï}
            EXCEPTION_SINGLE_STEP: {·¢Éúµ¥²½ÖжÏ}
            begin
              if rm then {ÓÉÓÚ´¦ÀíAV²úÉúµÄµ¥²½ÖжÏ.}
              begin
                rm := false;
               //ÖØÖñêÖ¾
                VirtualProtectEx(pi.hProcess, Pointer($9c66BC), 1, PAGE_EXECUTE_READ, @NewPrt);
               //»Ö¸´PageΪ²»¿ÉдÊôÐÔ,ÒÔ±ãÀ¹½ØÏ´Îд²Ù×÷
              end;
            end;
            else//²»ÊÇAVÖжϻòµ¥²½ÖжÏ,ÓÉÄ¿±ê³ÌÐòµÄSEH´¦ÀíÒì³£
              DbgParam := DBG_EXCEPTION_NOT_HANDLED;
          end;
      end;
      EXIT_PROCESS_DEBUG_EVENT:
      begin
        ContinueDebugEvent(DbgEvent.dwProcessId, DbgEvent.dwThreadId, DbgParam);
        //Ä¿±ê³ÌÐòÍÆ³ö,loaderʹÃü½áÊø,Í˳ö
        Break;
      end;
    end;
    ContinueDebugEvent(DbgEvent.dwProcessId, DbgEvent.dwThreadId, DbgParam);
  end;
end;

[ Last edited by sdlj8051 on 2006-10-6 at 11:31 ]
»Ø¸´´ËÂ¥
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
Ïà¹Ø°æ¿éÌø×ª ÎÒÒª¶©ÔÄÂ¥Ö÷ sdlj8051 µÄÖ÷Ìâ¸üÐÂ
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[¿¼ÑÐ] Àíѧ£¬¹¤Ñ§£¬Å©Ñ§µ÷¼Á£¬ÉÙ×ßÍä·£¬ÕâÀï»¶Ó­Äú£¡ +3 likeihood 2026-03-02 5/250 2026-03-02 16:37 by ZRH_878
[¿¼ÑÐ] 290Çóµ÷¼Á +5 ErMiao1020 2026-03-02 5/250 2026-03-02 16:36 by Î޼ʵIJÝÔ­
[¿¼ÑÐ] ½­ËÕʡũ¿ÆÔºÕе÷¼Á1Ãû +4 Qwertyuop 2026-03-01 4/200 2026-03-02 14:27 by Éý¸ñ°¢´ï
[¿¼ÑÐ] ²ÄÁÏÓ뻯¹¤328Çóµ÷¼Á +3 ¡££¬¡££¬¡££¬¡£i 2026-03-02 3/150 2026-03-02 13:09 by houyaoxu
[¿¼ÑÐ] 291 Çóµ÷¼Á +3 »¯¹¤2026½ì±ÏÒµÉ 2026-03-02 3/150 2026-03-02 12:55 by houyaoxu
[¿¼ÑÐ] Çóµ÷¼Á +3 °¾Ò¹µÄèͷӥ 2026-03-02 3/150 2026-03-02 11:45 by Áõ±ø
[¿¼ÑÐ] »¯¹¤×¨Ë¶342£¬Ò»Ö¾Ô¸´óÁ¬Àí¹¤´óѧ£¬Çóµ÷¼Á +6 kyf»¯¹¤ 2026-02-28 7/350 2026-03-02 10:56 by Î޼ʵIJÝÔ­
[¿¼ÑÐ] 275Çóµ÷¼Á +3 L-xin? 2026-03-01 6/300 2026-03-02 10:22 by ÈÈÇéɳĮ
[¿¼ÑÐ] 0856²ÄÁϵ÷¼Á +4 ÑØ°¶Óб´¿ÇOUC 2026-03-02 4/200 2026-03-02 10:19 by ¹«èªåÐÒ£
[¿¼ÑÐ] ¸ß·Ö×Ó»¯Ñ§ÓëÎïÀíµ÷¼Á +6 ºÃºÃºÃ1233 2026-02-28 13/650 2026-03-02 07:27 by ºÃºÃºÃ1233
[¿¼ÑÐ] 279Çóµ÷¼Á +3 dua1 2026-03-01 4/200 2026-03-02 00:23 by ´óÁ³µ°×Ó
[»ù½ðÉêÇë] ±¾×ÓдÍêÁË£¬¸øDSÐֵܿ´ÁË£¬µÃÁË92·Ö +3 Doma 2026-03-01 7/350 2026-03-02 00:00 by jnzsy
[˶²©¼ÒÔ°] ²©Ê¿×Ô¼ö +7 ¿ÆÑй·111 2026-02-26 11/550 2026-03-01 22:24 by ÕÜÆ½L
[¿¼ÑÐ] 291·Ö¹¤¿ÆÇóµ÷¼Á +9 science¶ö¶ö 2026-03-01 10/500 2026-03-01 18:55 by 18137688336
[¿¼ÑÐ] 328Çóµ÷¼Á +3 aaadim 2026-03-01 5/250 2026-03-01 17:29 by njzyff
[¿¼ÑÐ] 304Çóµ÷¼Á +6 ÂüÊâ2266 2026-02-28 7/350 2026-03-01 15:14 by wjLi2017
[¿¼ÑÐ] Çóµ÷¼Á +6 repeatt?t 2026-02-28 6/300 2026-03-01 14:37 by Sakura»æ
[¿¼ÑÐ] 295¸´ÊÔµ÷¼Á +3 ¼òľChuFront 2026-03-01 3/150 2026-03-01 14:27 by zzxw520th
[˶²©¼ÒÔ°] 2025½ìË«·Ç»¯¹¤Ë¶Ê¿±ÏÒµ£¬É격 +3 ¸ü¶àµÄÊÇ 2026-02-27 4/200 2026-03-01 10:04 by ztg729
[¿¼ÑÐ] 085600²ÄÁϹ¤³ÌÒ»Ö¾Ô¸Öпƴó×Ü·Ö312Çóµ÷¼Á +8 ³ÔÏüÒ¹1 2026-02-28 10/500 2026-02-28 20:27 by L135790
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û