首页
社区
课程
招聘
[原创]DllHijacker For Delphi
2008-8-20 20:35 17444

[原创]DllHijacker For Delphi

2008-8-20 20:35
17444
生成用于Dll劫持的Delphi模板,部分DLL需要修改代码后才能支持劫持。
界面如下


生成ws2help.dll的模板如下
library ws2help; 

//============================================================================== 
// DllHijacker By pathletboy 
//============================================================================== 

uses 
Windows; 

{$R *.res} 

var 
ModHandle: Cardinal; 
POldWahCloseApcHelper: Pointer; 
POldWahCloseHandleHelper: Pointer; 
POldWahCloseNotificationHandleHelper: Pointer; 
POldWahCloseSocketHandle: Pointer; 
POldWahCloseThread: Pointer; 
POldWahCompleteRequest: Pointer; 
POldWahCreateHandleContextTable: Pointer; 
POldWahCreateNotificationHandle: Pointer; 
POldWahCreateSocketHandle: Pointer; 
POldWahDestroyHandleContextTable: Pointer; 
POldWahDisableNonIFSHandleSupport: Pointer; 
POldWahEnableNonIFSHandleSupport: Pointer; 
POldWahEnumerateHandleContexts: Pointer; 
POldWahInsertHandleContext: Pointer; 
POldWahNotifyAllProcesses: Pointer; 
POldWahOpenApcHelper: Pointer; 
POldWahOpenCurrentThread: Pointer; 
POldWahOpenHandleHelper: Pointer; 
POldWahOpenNotificationHandleHelper: Pointer; 
POldWahQueueUserApc: Pointer; 
POldWahReferenceContextByHandle: Pointer; 
POldWahRemoveHandleContext: Pointer; 
POldWahWaitForNotification: Pointer; 

procedure WahCloseApcHelper; asm jmp POldWahCloseApcHelper end; 
procedure WahCloseHandleHelper; asm jmp POldWahCloseHandleHelper end; 
procedure WahCloseNotificationHandleHelper; asm jmp POldWahCloseNotificationHandleHelper end; 
procedure WahCloseSocketHandle; asm jmp POldWahCloseSocketHandle end; 
procedure WahCloseThread; asm jmp POldWahCloseThread end; 
procedure WahCompleteRequest; asm jmp POldWahCompleteRequest end; 
procedure WahCreateHandleContextTable; asm jmp POldWahCreateHandleContextTable end; 
procedure WahCreateNotificationHandle; asm jmp POldWahCreateNotificationHandle end; 
procedure WahCreateSocketHandle; asm jmp POldWahCreateSocketHandle end; 
procedure WahDestroyHandleContextTable; asm jmp POldWahDestroyHandleContextTable end; 
procedure WahDisableNonIFSHandleSupport; asm jmp POldWahDisableNonIFSHandleSupport end; 
procedure WahEnableNonIFSHandleSupport; asm jmp POldWahEnableNonIFSHandleSupport end; 
procedure WahEnumerateHandleContexts; asm jmp POldWahEnumerateHandleContexts end; 
procedure WahInsertHandleContext; asm jmp POldWahInsertHandleContext end; 
procedure WahNotifyAllProcesses; asm jmp POldWahNotifyAllProcesses end; 
procedure WahOpenApcHelper; asm jmp POldWahOpenApcHelper end; 
procedure WahOpenCurrentThread; asm jmp POldWahOpenCurrentThread end; 
procedure WahOpenHandleHelper; asm jmp POldWahOpenHandleHelper end; 
procedure WahOpenNotificationHandleHelper; asm jmp POldWahOpenNotificationHandleHelper end; 
procedure WahQueueUserApc; asm jmp POldWahQueueUserApc end; 
procedure WahReferenceContextByHandle; asm jmp POldWahReferenceContextByHandle end; 
procedure WahRemoveHandleContext; asm jmp POldWahRemoveHandleContext end; 
procedure WahWaitForNotification; asm jmp POldWahWaitForNotification end; 

exports 
WahCloseApcHelper, 
WahCloseHandleHelper, 
WahCloseNotificationHandleHelper, 
WahCloseSocketHandle, 
WahCloseThread, 
WahCompleteRequest, 
WahCreateHandleContextTable, 
WahCreateNotificationHandle, 
WahCreateSocketHandle, 
WahDestroyHandleContextTable, 
WahDisableNonIFSHandleSupport, 
WahEnableNonIFSHandleSupport, 
WahEnumerateHandleContexts, 
WahInsertHandleContext, 
WahNotifyAllProcesses, 
WahOpenApcHelper, 
WahOpenCurrentThread, 
WahOpenHandleHelper, 
WahOpenNotificationHandleHelper, 
WahQueueUserApc, 
WahReferenceContextByHandle, 
WahRemoveHandleContext, 
WahWaitForNotification; 

begin 
ModHandle:= LoadLibrary('C:\WINDOWS\system32\ws2help.dll'); 
if ModHandle > 0 then 
begin 
   POldWahCloseApcHelper:= GetProcAddress(ModHandle, 'WahCloseApcHelper'); 
   POldWahCloseHandleHelper:= GetProcAddress(ModHandle, 'WahCloseHandleHelper'); 
   POldWahCloseNotificationHandleHelper:= GetProcAddress(ModHandle, 'WahCloseNotificationHandleHelper'); 
   POldWahCloseSocketHandle:= GetProcAddress(ModHandle, 'WahCloseSocketHandle'); 
   POldWahCloseThread:= GetProcAddress(ModHandle, 'WahCloseThread'); 
   POldWahCompleteRequest:= GetProcAddress(ModHandle, 'WahCompleteRequest'); 
   POldWahCreateHandleContextTable:= GetProcAddress(ModHandle, 'WahCreateHandleContextTable'); 
   POldWahCreateNotificationHandle:= GetProcAddress(ModHandle, 'WahCreateNotificationHandle'); 
   POldWahCreateSocketHandle:= GetProcAddress(ModHandle, 'WahCreateSocketHandle'); 
   POldWahDestroyHandleContextTable:= GetProcAddress(ModHandle, 'WahDestroyHandleContextTable'); 
   POldWahDisableNonIFSHandleSupport:= GetProcAddress(ModHandle, 'WahDisableNonIFSHandleSupport'); 
   POldWahEnableNonIFSHandleSupport:= GetProcAddress(ModHandle, 'WahEnableNonIFSHandleSupport'); 
   POldWahEnumerateHandleContexts:= GetProcAddress(ModHandle, 'WahEnumerateHandleContexts'); 
   POldWahInsertHandleContext:= GetProcAddress(ModHandle, 'WahInsertHandleContext'); 
   POldWahNotifyAllProcesses:= GetProcAddress(ModHandle, 'WahNotifyAllProcesses'); 
   POldWahOpenApcHelper:= GetProcAddress(ModHandle, 'WahOpenApcHelper'); 
   POldWahOpenCurrentThread:= GetProcAddress(ModHandle, 'WahOpenCurrentThread'); 
   POldWahOpenHandleHelper:= GetProcAddress(ModHandle, 'WahOpenHandleHelper'); 
   POldWahOpenNotificationHandleHelper:= GetProcAddress(ModHandle, 'WahOpenNotificationHandleHelper'); 
   POldWahQueueUserApc:= GetProcAddress(ModHandle, 'WahQueueUserApc'); 
   POldWahReferenceContextByHandle:= GetProcAddress(ModHandle, 'WahReferenceContextByHandle'); 
   POldWahRemoveHandleContext:= GetProcAddress(ModHandle, 'WahRemoveHandleContext'); 
   POldWahWaitForNotification:= GetProcAddress(ModHandle, 'WahWaitForNotification'); 
end; 
end. 


关键单元代码如下
{*******************************************************} 
{                                                       } 
{       DllHijacker                                     } 
{                                                       } 
{       版权所有 (C) 2008 pathletboy                    } 
{                                                       } 
{*******************************************************} 

unit Common; 

interface 

uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs; 

type 
  TBuildDprStatus = (SUCCESS, FILE_NOT_FOUND, FILE_READ_ERROR, FILE_NOT_PE, 
    FILE_NOT_DLL, FILE_NOT_HAVE_EXPORTTABLE, SAVEFILE_ERROR, UNKNOW_ERROR); 

function BuildDpr(const FileName: string): TBuildDprStatus; 
implementation 

function BuildDpr(const FileName: string): TBuildDprStatus; 

  function RvaToRaw(const ImageSectionHeader: PImageSectionHeader; Rva: 
    Cardinal): Cardinal; 
  var 
    Temp: PImageSectionHeader; 
  begin 
    Temp := ImageSectionHeader; 
    while Temp.VirtualAddress <> 0 do 
    begin 
      if (Rva >= Temp.VirtualAddress) and (Rva < (Temp.VirtualAddress 
        + Temp.Misc.VirtualSize)) then 
      begin 
        Result := Temp.PointerToRawData + Rva - Temp.VirtualAddress; 
        Break; 
      end; 
      inc(Temp); 
    end; 
  end; 

var 
  TargetFile: TMemoryStream; 
  DosHead: TImageDosHeader; 
  NtHead: TImageNtHeaders; 
  ExportTable: TImageExportDirectory; 
  ExportTableAddress: Cardinal; 
  //ExportTableSize: Cardinal; 
  ExportFunctionNameAddress: Cardinal; 
  ArrayFunctionNames: array of string; 
  i: Integer; 
  OneChar: Char; 
  DprFileName: string; 
  DprSourceFile: TStringList; 
  PSectionHeader: PImageSectionHeader; 
begin 
  Result := UNKNOW_ERROR; 

  if not FileExists(FileName) then 
  begin 
    Result := FILE_NOT_FOUND; 
    Exit; 
  end; 

  TargetFile := TMemoryStream.Create; 
  try 

    try 
      TargetFile.LoadFromFile(FileName); 
      TargetFile.Read(DosHead, SizeOf(DosHead)); 
      if DosHead.e_magic <> IMAGE_DOS_SIGNATURE then 
      begin 
        Result := FILE_NOT_PE; 
        Exit; 
      end; 

      TargetFile.Seek(DosHead._lfanew, soFromBeginning); 
      TargetFile.Read(NtHead, SizeOf(NtHead)); 
      if NtHead.Signature <> IMAGE_NT_SIGNATURE then 
      begin 
        Result := FILE_NOT_PE; 
        Exit; 
      end; 

      if NtHead.FileHeader.Characteristics and IMAGE_FILE_DLL = 0 then 
      begin 
        Result := FILE_NOT_DLL; 
        Exit; 
      end; 

      if NtHead.OptionalHeader.DataDirectory[0].Size = 0 then //ExportTable 
      begin 
        Result := FILE_NOT_HAVE_EXPORTTABLE; 
        Exit; 
      end; 
      PSectionHeader := Pointer(DWORD(TargetFile.Memory) + DosHead._lfanew + 
        SizeOf(NtHead)); 

      ExportTableAddress := RvaToRaw(PSectionHeader, 
        NtHead.OptionalHeader.DataDirectory[0].VirtualAddress); 

      //ExportTableSize := NtHead.OptionalHeader.DataDirectory[0].Size; 
      TargetFile.Seek(ExportTableAddress, soFromBeginning); 
      TargetFile.Read(ExportTable, SizeOf(ExportTable)); 
      TargetFile.Seek(RvaToRaw(PSectionHeader, 
        DWORD(ExportTable.AddressOfNames)), 
        soFromBeginning); 
      TargetFile.Read(ExportFunctionNameAddress, 
        SizeOf(ExportFunctionNameAddress)); 
      TargetFile.Seek(RvaToRaw(PSectionHeader, ExportFunctionNameAddress), 
        soFromBeginning); 
      SetLength(ArrayFunctionNames, ExportTable.NumberOfNames); 
      for i := 0 to ExportTable.NumberOfNames - 1 do 
      begin 
        TargetFile.Read(OneChar, SizeOf(OneChar)); 
        while OneChar <> #0 do 
        begin 
          ArrayFunctionNames[i] := ArrayFunctionNames[i] + OneChar; 
          TargetFile.Read(OneChar, SizeOf(OneChar)); 
        end; 
        OutputDebugString(PChar(Format('函数名序号:%d 函数名:%s', [i, 
          ArrayFunctionNames[i]]))); 
      end; 

      DprFileName := StringReplace(ExtractFileName(FileName), 
        ExtractFileExt(FileName), '', [rfReplaceAll]); 
      DprSourceFile := TStringList.Create; 
      try 
        try 
          DprSourceFile.Add(Format('library %s;', [DprFileName])); 
          DprSourceFile.Add(''); 
          DprSourceFile.Add('//=============================================================================='); 
          DprSourceFile.Add('// DllHijacker By pathletboy'); 
          DprSourceFile.Add('//=============================================================================='); 
          DprSourceFile.Add(''); 
          DprSourceFile.Add('uses'); 
          DprSourceFile.Add(' Windows;'); 

          DprSourceFile.Add(''); 
          DprSourceFile.Add('{$R *.res}'); 
          DprSourceFile.Add(''); 
          DprSourceFile.Add('var'); 
          DprSourceFile.Add(' ModHandle: Cardinal;'); 
          for i := 0 to ExportTable.NumberOfNames - 1 do 
          begin 
            DprSourceFile.Add(' POld' + ArrayFunctionNames[i] + ': Pointer;'); 
          end; 
          DprSourceFile.Add(''); 

          for i := 0 to ExportTable.NumberOfNames - 1 do 
          begin 
            DprSourceFile.Add('procedure ' + ArrayFunctionNames[i] + 
              '; asm jmp POld' + ArrayFunctionNames[i] + ' end;'); 
          end; 
          DprSourceFile.Add(''); 

          DprSourceFile.Add('exports'); 
          for i := 0 to ExportTable.NumberOfNames - 1 do 
          begin 
            if i <> ExportTable.NumberOfNames - 1 then 
              DprSourceFile.Add(' ' + ArrayFunctionNames[i] + ',') 
            else 
              DprSourceFile.Add(' ' + ArrayFunctionNames[i] + ';'); 
          end; 
          DprSourceFile.Add(''); 

          DprSourceFile.Add('begin'); 
          DprSourceFile.Add(' ModHandle:= LoadLibrary(''' + FileName + ''');'); 
          DprSourceFile.Add(' if ModHandle > 0 then'); 
          DprSourceFile.Add(' begin'); 
          for i := 0 to ExportTable.NumberOfNames - 1 do 
          begin 
            DprSourceFile.Add('   POld' + ArrayFunctionNames[i] + 
              ':= GetProcAddress(ModHandle, ''' + ArrayFunctionNames[i] + 
              ''');'); 
          end; 
          DprSourceFile.Add(' end;'); 
          DprSourceFile.Add('end.'); 

          DprSourceFile.SaveToFile(ExtractFilePath(Application.ExeName) + 
            DprFileName + '.dpr'); 
          OutputDebugString(PChar(DprFileName + '.dpr Saved!')); 
        except 
          Result := SAVEFILE_ERROR; 
          Exit; 
        end; 
      finally 
        DprSourceFile.Free; 
      end; 

      OutputDebugString(PChar(DprFileName)); 
      Result := SUCCESS; 
    except 
      Result := FILE_READ_ERROR; 
      Exit; 
    end; 
  finally 
    TargetFile.Free; 
  end; 

end; 

end. 


完整的代码下载

[培训]内核驱动高级班,冲击BAT一流互联网大厂工 作,每周日13:00-18:00直播授课

上传的附件:
收藏
点赞7
打赏
分享
最新回复 (19)
雪    币: 110
活跃值: (1245)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
cd37ycs 2008-8-20 22:18
2
0
能举个应用范例吗?
雪    币: 437
活跃值: (253)
能力值: ( LV12,RANK:240 )
在线值:
发帖
回帖
粉丝
bzhkl 5 2008-8-20 22:22
3
0
支持 ~~~
雪    币: 225
活跃值: (10)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
dayed 1 2008-8-20 22:53
4
0
感谢共享~~~~
雪    币: 22
活跃值: (423)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
靴子 2008-8-21 00:13
5
0
很好哦:)  不过还是喜欢用C的 体积小呀
雪    币: 372
活跃值: (31)
能力值: ( LV12,RANK:410 )
在线值:
发帖
回帖
粉丝
vxin 10 2008-8-21 11:51
6
0
学习一下~~
雪    币: 200
活跃值: (145)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
lsuper 2008-8-22 00:00
7
0
不错!非常感谢
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
xiaoyuwed 2009-5-30 22:58
8
0
太感谢了,有了这个方便多了
雪    币: 220
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
绿豆青蛙 2009-5-30 23:04
9
0
delphi的代码总体看起来有点肥。。。
雪    币: 602
活跃值: (45)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
kangcin 2010-3-9 17:26
10
0
先感谢楼主 再测试
雪    币: 602
活跃值: (45)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
kangcin 2010-3-9 17:29
11
0
某些用序号的dll函数不能导出
雪    币: 203
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
deitytoday 2010-4-9 20:04
12
0
这个厉害了.. 弄来学习学习.  

其实主要是我突然想看看自己有多少现金..
雪    币: 218
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
sztxgg 2010-5-3 13:49
13
0
马上下载试用,不错哈
雪    币: 255
活跃值: (49)
能力值: ( LV9,RANK:180 )
在线值:
发帖
回帖
粉丝
ppanger 4 2010-5-12 13:29
14
0
感谢分享 ^_^
雪    币: 208
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
ddzhouqian 2010-5-12 18:17
15
0
楼主说了不支持所有的dll,某些dll需要修改代码。
今天再看有些熟悉了
雪    币: 253
活跃值: (89)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
YFLK 2010-5-13 07:54
16
0
这是好东西,非常有用,谢谢!
雪    币: 2065
活跃值: (1943)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
teletell 2010-5-18 08:31
17
0
不错的东东!!谢谢共享。
雪    币: 201
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
zhaoxiali 2010-6-10 22:27
18
0
支持 ~~~
雪    币: 419
活跃值: (48)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
moomlxm 2011-5-6 12:57
19
0
好东西 收藏下
雪    币: 204
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
cherokee 2011-5-19 19:13
20
0
学习了。。。。。
游客
登录 | 注册 方可回帖
返回