网上文摘 小说 Flash游戏 最近更新 下载排行 资源分类 下载指南
经典编程资源 精彩不容错过
设为首页
加入收藏
联系我们
当前位置:Delphi园地技巧文章编程心得 → DLL注入代码
DLL注入代码
日期:2007年11月20日 作者:小宇飞刀 人气: 查看:[大字体 中字体 小字体]
下面是注入的过程的代码,博主在Windows XP/2000测试通过,由于我没有Windows 2003/Vista,故没有测试。

----此篇文章来自《深入WINDOWS编程》

  1. unit toDllUnt;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  
  7.   StdCtrls, tlhelp32;  
  8. {type 
  9.   TProcessEntry32 = record 
  10.     dwSize: DWORD; 
  11.     cntUsage: DWORD; 
  12.     th32ProcessID: DWORD; 
  13.     th32DefaultHeapID: DWORD; 
  14.     th32ModuleID: DWORD; 
  15.     cntThreads: DWORD; 
  16.     th32ParentProcessID: DWORD; 
  17.     pcPriClassBase: integer; 
  18.     dwFlags: DWORD; 
  19.     szExeFile: array[0..MAX_PATH - 1] of char; 
  20.   end; }  
  21. type  
  22.   TtoDllFrm = class(TForm)  
  23.     Button1: TButton;  
  24.     procedure Button1Click(Sender: TObject);  
  25.   private  
  26.     { Private declarations }  
  27.   public  
  28.     { Public declarations }  
  29.   end;  
  30.   
  31. var  
  32.   toDllFrm: TtoDllFrm;  
  33.   
  34. implementation  
  35.   
  36. {$R *.DFM}  
  37.   
  38. procedure FindAProcess(const AFilename: string; const PathMatch: Boolean; var ProcessID: DWORD);  
  39. var  
  40.   lppe: TProcessEntry32;  
  41.   SsHandle: Thandle;  
  42.   FoundAProc, FoundOK: boolean;  
  43. begin  
  44.   ProcessID :=0;  
  45.   SsHandle := CreateToolHelp32SnapShot(TH32CS_SnapProcess, 0);  
  46.   FoundAProc := Process32First(Sshandle, lppe);  
  47.   while FoundAProc do  
  48.   begin  
  49.     if PathMatch then  
  50.       FoundOK := AnsiStricomp(lppe.szExefile, PChar(AFilename)) = 0  
  51.     else  
  52.       FoundOK := AnsiStricomp(PChar(ExtractFilename(lppe.szExefile)), PChar(ExtractFilename(AFilename))) = 0;  
  53.     if FoundOK then  
  54.     begin  
  55.       ProcessID := lppe.th32ProcessID;  
  56.       break;  
  57.     end;  
  58.     FoundAProc := Process32Next(SsHandle, lppe);  
  59.   end;  
  60.   CloseHandle(SsHandle);  
  61. end;  
  62.   
  63. function EnabledDebugPrivilege(const bEnabled: Boolean): Boolean;  
  64. var  
  65.   hToken: THandle;  
  66.   tp: TOKEN_PRIVILEGES;  
  67.   a: DWORD;  
  68. const  
  69.   SE_DEBUG_NAME = 'SeDebugPrivilege';  
  70. begin  
  71.   Result := False;  
  72.   if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, hToken)) then  
  73.   begin  
  74.     tp.PrivilegeCount := 1;  
  75.     LookupPrivilegeValue(nil, SE_DEBUG_NAME, tp.Privileges[0].Luid);  
  76.     if bEnabled then  
  77.       tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED  
  78.     else  
  79.       tp.Privileges[0].Attributes := 0;  
  80.     a := 0;  
  81.     AdjustTokenPrivileges(hToken, False, tp, SizeOf(tp), nil, a);  
  82.     Result := GetLastError = ERROR_SUCCESS;  
  83.     CloseHandle(hToken);  
  84.   end;  
  85. end;  
  86.   
  87. function AttachToProcess(const HostFile, GuestFile: string; const PID: DWORD = 0): DWORD;  
  88. var  
  89.   hRemoteProcess: THandle;  
  90.   dwRemoteProcessId: DWORD;  
  91.   cb: DWORD;  
  92.   pszLibFileRemote: Pointer;  
  93.   iReturnCode: Boolean;  
  94.   TempVar: DWORD;  
  95.   pfnStartAddr: TFNThreadStartRoutine;  
  96.   pszLibAFilename: PwideChar;  
  97. begin  
  98.   Result := 0;  
  99.   EnabledDebugPrivilege(True);  
  100.   Getmem(pszLibAFilename, Length(GuestFile) * 2 + 1);  
  101.   StringToWideChar(GuestFile, pszLibAFilename, Length(GuestFile) * 2 + 1);  
  102.   if PID > 0 then  
  103.      dwRemoteProcessID := PID  
  104.   else  
  105.      FindAProcess(HostFile, False, dwRemoteProcessID);  
  106.   hRemoteProcess := OpenProcess(PROCESS_CREATE_THREAD + {允许远程创建线程}  
  107.       PROCESS_VM_OPERATION + {允许远程VM操作}  
  108.       PROCESS_VM_WRITE, {允许远程VM写}  
  109.       FALSE, dwRemoteProcessId);  
  110.   cb := (1 + lstrlenW(pszLibAFilename)) * sizeof(WCHAR);  
  111.   pszLibFileRemote := PWIDESTRING(VirtualAllocEx(hRemoteProcess, nil, cb, MEM_COMMIT, PAGE_READWRITE));  
  112.   TempVar := 0;  
  113.   iReturnCode := WriteProcessMemory(hRemoteProcess, pszLibFileRemote, pszLibAFilename, cb, TempVar);  
  114.   if iReturnCode then  
  115.   begin  
  116.     pfnStartAddr := GetProcAddress(GetModuleHandle('Kernel32'), 'LoadLibraryW');  
  117.     TempVar := 0;  
  118.     Result := CreateRemoteThread(hRemoteProcess, nil, 0, pfnStartAddr, pszLibFileRemote, 0, TempVar);  
  119.   end;  
  120.   Freemem(pszLibAFilename);  
  121. end;  
  122.   
  123. procedure TtoDllFrm.Button1Click(Sender: TObject);  
  124. begin  
  125.   AttachToProcess('Explorer.exe', extractfilepath(paramstr(0))+'Project2.dll');  
  126. //其中Project2.dll是想要注入到Explorer.EXE的进程,Explorer.exe也可以是别的进程.  
  127. end;  
  128.   
  129. end.  

(出处:http://xieyunc.blog.163.com/blog/)

相关文章:
·用hook实现dll注入详解
·我的数据库及DLLTIPS
·Delphi中高级DLL的编写和调用(1)
·Delphi中高级DLL的编写和调用(2)
·Delphi中高级DLL的编写和调用(3)
·Dll(动态链接库)学习笔记
·从 .exe 或 .dll中提取icon
·取得DLL所在的运行目录
·DELPHI 中动态链接库(DLL)的使用
·写调用动态链接库DLL的应用程序
 → 特别推荐
 → 热点TOP10
关于我们 | 广告服务 | 发布资源 | 联系站长 Copyright © 2002-2006 Delphi园地 All Rights Reserved