سؤال حول كتابة Plugin لتجاوز دالة IsDebuggerPresent
#12
سأستمر في المثال الخاص بي ، إليك شفرة مصدر كاملة Under Windows7Pro Svpack1 32bit سوف أرى مثالك أيضًا
PHP كود :
program IsDebugged;

{
$APPTYPE CONSOLE}

uses Windows;

Type
  PVOID 
= Pointer;
  
NTSTATUS = UINT;

Type
  TNtQueryInformationProcess 
= function(
    
ProcessHandle: THandle;
    
ProcessInformationClass: ULONG;
    
ProcessInformation: PVOID;
    
ProcessInformationLength: ULONG;
    
ReturnLength: PULONG): NTSTATUS; stdcall;

  
TNtRemoveProcessDebug = function(
    
ProcessHandle: THandle;
    
DebugObjectHandle: THandle): NTSTATUS; stdcall;

  
TNtSetInformationDebugObject = function(
    
DebugObjectHandle: THandle;
    
DebugObjectInformationClass: ULONG;
    
DebugInformation: PVOID;
    
DebugInformationLength: ULONG;
    
ReturnLength: PULONG
    
): NTSTATUS; stdcall;

  
TNtClose = function(Handle: THandle): NTSTATUS; stdcall;

var
  
NtQueryInformationProcess: TNtQueryInformationProcess;
  
NtRemoveProcessDebug: TNtRemoveProcessDebug;
  
NtSetInformationDebugObject: TNtSetInformationDebugObject;
  
NtClose: TNtClose;

function 
GetProcessIsBeingDebugged(ProcessHandle: THandle; var IsBeingDebugged: Boolean): Boolean;
var
  
DebugPort: PVOID;
  
TargetProcessHandle: THandle;
  
SourceProcessHandle: THandle;
  
DuplicateResult: BOOL;
  
DesiredAccess: ACCESS_MASK;
begin
  Result 
:= False;
  try
    if (@
NtQueryInformationProcess <> nil) then
    begin
      DuplicateResult 
:= False;
      
TargetProcessHandle := 0;
      
DuplicateResult := DuplicateHandle(GetCurrentProcess(), ProcessHandle, GetCurrentProcess(), @TargetProcessHandle, 0, False, DUPLICATE_SAME_ACCESS);
      if 
DuplicateResult then
      begin
        
try
          if 
NtQueryInformationProcess(
            
TargetProcessHandle,
            
7,
            @
DebugPort,
            
SizeOf(PVOID),
            
nil
            
) = 0 then
            Result 
:= True;
          if 
DebugPort <> nil then
            IsBeingDebugged 
:= True;
        finally
          if 
TargetProcessHandle <> 0 then
            CloseHandle
(TargetProcessHandle);
        
end;
      
end;
    
end;
  
except
  end
;
end;

function 
DetachProcessFromDebugger(ProcessHandle: THandle): Boolean;
var
  
DebugObjectHandle: THandle;
  
DebugFlags: ULONG;
  
TargetProcessHandle: THandle;
  
SourceProcessHandle: THandle;
  
DuplicateResult: BOOL;
  
DesiredAccess: ACCESS_MASK;
begin
  Result 
:= False;
  try
    if (@
NtQueryInformationProcess <> nil) then
    begin
      DebugObjectHandle 
:= 0;
      if 
NtQueryInformationProcess(
        
ProcessHandle,
        
30,
        @
DebugObjectHandle,
        
SizeOf(THandle),
        
nil) = 0 then
      begin
        
try
          if (@
NtSetInformationDebugObject <> nil) and (@NtRemoveProcessDebug <> nil) then
          begin
            DebugFlags 
:= 0;
            
NtSetInformationDebugObject(
              
DebugObjectHandle,
              
2,
              @
DebugFlags,
              
SizeOf(ULONG),
              
nil);

            if 
NtRemoveProcessDebug(ProcessHandle, DebugObjectHandle) = 0 then
              Result 
:= True;
          
end;
        finally
          if 
DebugObjectHandle <> 0 then
            NtClose
(DebugObjectHandle);
        
end;
      
end;
    
end;
  
except
  end
;
end;

function 
_AddCurrentProcessPrivileges(PrivilegeName: WideString): Boolean;
var
  
TokenHandle: THandle;
  
TokenPrivileges: TTokenPrivileges;
  
ReturnLength: DWORD;
begin
  Result 
:= False;
  try
    if 
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, TokenHandle) then
    begin
      
try
        
LookupPrivilegeValueW(nil, PWideChar(PrivilegeName), TokenPrivileges.Privileges[0].Luid);
        
TokenPrivileges.PrivilegeCount := 1;
        
TokenPrivileges.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
        if 
AdjustTokenPrivileges(TokenHandle, False, TokenPrivileges, 0, nil, ReturnLength) then
          Result 
:= True;
      finally
        
CloseHandle(TokenHandle);
      
end;
    
end;
  
except
  end
;
end;

var
  
LibraryHandle: HMODULE;
  
HProcess:Thandle;
  
_IsDebugged:Boolean;
begin
  _AddCurrentProcessPrivileges
('SeDebugPrivilege');
  
LibraryHandle := LoadLibrary('ntdll.dll');
  if 
LibraryHandle <> 0 then
  begin
    
try
      @
NtQueryInformationProcess := GetProcAddress(LibraryHandle, 'NtQueryInformationProcess');
      @
NtRemoveProcessDebug := GetProcAddress(LibraryHandle, 'NtRemoveProcessDebug');
      @
NtSetInformationDebugObject := GetProcAddress(LibraryHandle, 'NtSetInformationDebugObject');
      @
NtClose := GetProcAddress(LibraryHandle, 'NtClose');
    finally
      
LibraryHandle := 0;
      
FreeLibrary(LibraryHandle);
    
end;
  
end;
  
hProcess := OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId);
  if 
hProcess <> INVALID_HANDLE_vALUE then
  begin
    GetProcessIsBeingDebugged
(hProcess, _IsDebugged);
    if 
_isDebugged then
    begin
      WriteLn
('Process Is Debugged');
      if 
DetachProcessFromDebugger(hProcess) then WriteLn('Process Detached')
      else 
WriteLn('Process Can''t be Detached');
    
end
   
else WriteLn('Process not debugged');
  
end;
  
ReadLn; 

@Rever7eR
في مثالك ، أعتقد أنه يجب عليك تخصيص عنوان للكتابة ، VirtualAllocEx
في رأيي ، لا يمكننا الكتابة في PROCESS دون تحديد العنوان ADDRESS
ملحوظة: في CurrentProcess تعمل بشكل جيد عند Debugged مع دلفي
[-] كل من 1 user says قال شكرا ل Hs32-Idir على المشاركة المفيدة
  • Rever7eR
الرد


الردود في هذا الموضوع
RE: سؤال حول كتابة Plugin لتجاوز دالة IsDebuggerPresent - بواسطة Hs32-Idir - 04-04-2020, 03:41 PM

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم