delphi4arab منتديات دلفي للعرب
Check if ASLR is enabled - نسخة قابلة للطباعة

+- delphi4arab منتديات دلفي للعرب (http://delphi4arab.net/forum)
+-- قسم : منتديات دلفي للعرب التعليمية (http://delphi4arab.net/forum/forumdisplay.php?fid=3)
+--- قسم : مصادر دلفي مفتوحة تعليمية (http://delphi4arab.net/forum/forumdisplay.php?fid=14)
+--- الموضوع : Check if ASLR is enabled (/showthread.php?tid=9075)

الصفحات: 1 2


Check if ASLR is enabled - Agmcz - 28-12-2017

السلام عليكم
إجراء للتحقق من Address space layout randomization مفعلة أم لا

PHP كود :
unit uCheckASLR;

// Original C++ Source: https://stackoverflow.com/questions/47105480/how-to-check-if-aslr-is-enabled-for-a-process
// Converted to Delphi by Agmcz 28-12-2017 2:25:32

interface

uses
  Windows
;

function 
CheckASLR(dwProcessIdULONGout bASLRBoolean): ULONG;

implementation

const
 
 PROCESS_QUERY_LIMITED_INFORMATION = $1000;

type
 TSectionImageInformation  
record
    TransferAddress
Pointer;
 
   ZeroBitsULONG;
 
   MaximumStackSizeULONG;
 
   CommittedStackSizeULONG;
 
   SubSystemTypeULONG;
 
   MinorSubsystemVersionWord;
 
   MajorSubsystemVersionWord;
 
   GpValueULONG;
 
   ImageCharacteristicsWord;
 
   DllCharacteristicsWord;
 
   MachineWord;
 
   ImageContainsCodeBoolean;
 
   ImageFlagsByte;
 
   LoaderFlagsULONG;
 
   ImageFileSizeULONG;
 
   CheckSumULONG;
 
 end;

 
 PROCESSINFOCLASS = (
 
   ProcessBasicInformation,
 
   ProcessQuotaLimits,
 
   ProcessIoCounters,
 
   ProcessVmCounters,
 
   ProcessTimes,
 
   ProcessBasePriority,
 
   ProcessRaisePriority,
 
   ProcessDebugPort,
 
   ProcessExceptionPort,
 
   ProcessAccessToken,
 
   ProcessLdtInformation,
 
   ProcessLdtSize,
 
   ProcessDefaultHardErrorMode,
 
   ProcessIoPortHandlers,
 
   ProcessPooledUsageAndLimits,
 
   ProcessWorkingSetWatch,
 
   ProcessUserModeIOPL,
 
   ProcessEnableAlignmentFaultFixup,
 
   ProcessPriorityClass,
 
   ProcessWx86Information,
 
   ProcessHandleCount,
 
   ProcessAffinityMask,
 
   ProcessPriorityBoost,
 
   ProcessDeviceMap,
 
   ProcessSessionInformation,
 
   ProcessForegroundInformation,
 
   ProcessWow64Information,
 
   ProcessImageFileName,
 
   ProcessLUIDDeviceMapsEnabled,
 
   ProcessBreakOnTermination,
 
   ProcessDebugObjectHandle,
 
   ProcessDebugFlags,
 
   ProcessHandleTracing,
 
   ProcessIoPriority,
 
   ProcessExecuteFlags,
 
   ProcessResourceManagement,
 
   ProcessCookie,
 
   ProcessImageInformation,
 
   MaxProcessInfoClass);

type
  NTSTATUS 
LongWord;

function 
NtQueryInformationProcess(ProcessHandleTHandleProcessInformationClassPROCESSINFOCLASSProcessInformationPointerProcessInformationLengthULONGReturnLengthPULONG ): LongIntstdcallexternal 'ntdll.dll';
function 
RtlNtStatusToDosError(StatusNTSTATUS): Integerstdcallexternal 'ntdll.dll';

function 
CheckASLR(dwProcessIdULONGout bASLRBoolean): ULONG;
var
 
hProcessTHandle;
 
siiTSectionImageInformation;
 
statusNTSTATUS;
begin
  hProcess 
:= OpenProcess(PROCESS_QUERY_LIMITED_INFORMATIONFalsedwProcessId);
 
 if (hProcess <> 0) and (hProcess <> INVALID_HANDLE_VALUEthen
  begin
    status 
:= NtQueryInformationProcess(hProcessProcessImageInformation, @siiSizeOf(sii), 0);
 
   CloseHandle(hProcess);
 
   if 0 <= status then
    begin
      bASLR 
:= Boolean(sii.ImageFlags);
 
     Result := NOERROR;
 
     Exit;
 
   end;
 
   Result := RtlNtStatusToDosError(status);
 
   Exit;
 
 end;
 
 Result := GetLastError;
end;


end



RE: Check if ASLR is enabled - مبرمج حر - 07-01-2018

مشكوور اخي
لكن ما فائدتها ! ممكن مثال عملي


RE: Check if ASLR is enabled - Agmcz - 07-01-2018

فحص هيدر PE وتحقق من ASLR

PHP كود :
{************************************
Coded by Agmcz                    *
Hints by naquadria                *
Date2018-01-07                  *
************************************}

unit uCheckASLR;

interface

uses
  Windows
;

function 
CheckASLR(const FileNamestring): Boolean;

implementation

const
 
 IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE = $0040;
 
 IMAGE_DIRECTORY_ENTRY_BASERELOC 5;

function 
CheckASLR(const FileNamestring): Boolean;
var
 
 hFileTHandle;
 
 hMappingDWORD;
 
 pMapPointer;
 
 dwSizeDWORD;
 
 IDHPImageDosHeader;
 
 INHPImageNtHeaders;
 
 ISHPImageSectionHeader;
 
 nWord;
 
 dwRelocAddrdwRelocSizeDWORD;
begin
  Result 
:= False;
 
 hFile := CreateFile(PChar(FileName), GENERIC_READFILE_SHARE_READ or FILE_SHARE_WRITEnilOPEN_EXISTING00);
 
 if hFile <> INVALID_HANDLE_VALUE then
  begin
    dwSize 
:= GetFileSize(hFilenil);
 
   hMapping := CreateFileMapping(hFilenilPAGE_READONLY0dwSizenil);
 
   if hMapping <> 0 then
    begin
      pMap 
:= MapViewOfFile(hMappingFILE_MAP_READ000);
 
     if pMap <> nil then
      begin
        IDH 
:= PImageDosHeader(pMap);
 
       if IDH.e_magic IMAGE_DOS_SIGNATURE then
        begin
          INH 
:= PImageNtHeaders(DWORD(pMap) + LongWord(IDH._lfanew));
 
         if INH.Signature IMAGE_NT_SIGNATURE then
          begin
            if 
(INH.OptionalHeader.DllCharacteristics and IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) = IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE then
            begin
              ISH 
:= PImageSectionHeader(DWORD(pMap) + LongWord(IDH._lfanew) + SizeOf(DWORD) + SizeOf(INH.FileHeader) + INH.FileHeader.SizeOfOptionalHeader);
 
             dwRelocAddr := INH.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress;
 
             dwRelocSize := INH.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;
 
             if (dwRelocAddr <> 0) and (dwRelocSize <> 0then
              begin
                for n 
:= 0 to INH.FileHeader.NumberOfSections do
 
               begin
                  if ISH
.VirtualAddress dwRelocAddr then
                  begin
                    if 
(ISH.Misc.VirtualSize <> 0) and  (ISH.PointerToRawData <> 0) and (ISH.SizeOfRawData <> 0then
                      Result 
:= True;
 
                   Break;
 
                 end;
 
                 Inc(ISH);
 
               end;
 
             end;
 
           end;
 
         end;
 
       end;
 
       UnmapViewOfFile(pMap);
 
     end;
 
     CloseHandle(hMapping);
 
   end;
 
   CloseHandle(hFile);
 
 end;
end;

end

(07-01-2018, 02:17 PM)مبرمج حر كتب : مشكوور اخي
لكن ما فائدتها ! ممكن مثال عملي
(مقتبس من حل تمرين chimera#01 من منتدى AT4RE)
هي نوع من أنواع الحماية قامت شركة مايكروسوفت بإضافتها في أنظمة التشغيل خاصتها منذ صدور نظام التشغيل فيستا كمحاولة لمنع التنبؤ بعناوين الذاكرة التي يتم فيها تحميل البرامج التنفيذية, هذه الطريقة كان هدفها جعل لودر نظام التشغيل يحمل البرنامج التنفيدي في عنوان عشوائي في كل يتم فيها تشغيله وعند إعادة التشغيل الجهاز.

يعني عند عدم تفعيل ASLR  يكون عنوان ImageBase ثابت موقعه
على غرار تفعيل والذي سيكون متغير.
ألق نظرة هنا
How can I enable DEP/NX and ASLR on a Delphi 2006 or earlier executable?
يتم إستعمال التوجيه SetPEOptFlags
كود :
//{$SetPEOptFlags $100} //to set the ASLR flag
//{$SetPEOptFlags $40} //to set the DEP flag
{$SetPEOptFlags $140} //to set both DEP + ASLR in one line



RE: Check if ASLR is enabled - Agmcz - 08-04-2018

السلام عليكم
بعض التصحيحات على الكود لأنه توجد أخطاء
PHP كود :
unit uCheckASLR;

// Original C++ Source: https://stackoverflow.com/questions/47105480/how-to-check-if-aslr-is-enabled-for-a-process
// Ported to Delphi by Agmcz 28-12-2017 2:25:32
// Fix 04-08-2018 10:56:25

interface

uses
  Windows
;

function 
CheckASLR(dwProcessIdLongWordout bASLRBoolean): LongWord;

implementation

const
 
 PROCESS_QUERY_LIMITED_INFORMATION = $1000;

type
 TSectionImageInformation  
record
    TransferAddress
Pointer;
 
   ZeroBitsLongWord;
 
   MaximumStackSizeLongWord;
 
   CommittedStackSizeLongWord;
 
   SubSystemTypeLongWord;
 
   MinorSubsystemVersionWord;
 
   MajorSubsystemVersionWord;
 
   GpValueLongWord;
 
   ImageCharacteristicsWord;
 
   DllCharacteristicsWord;
 
   MachineWord;
 
   ImageContainsCodeBoolean;
 
   ImageFlagsByte;
 
   LoaderFlagsLongWord;
 
   ImageFileSizeLongWord;
 
   CheckSumLongWord;
 
 end;

 
 PROCESSINFOCLASS = (
 
   ProcessBasicInformation,
 
   ProcessQuotaLimits,
 
   ProcessIoCounters,
 
   ProcessVmCounters,
 
   ProcessTimes,
 
   ProcessBasePriority,
 
   ProcessRaisePriority,
 
   ProcessDebugPort,
 
   ProcessExceptionPort,
 
   ProcessAccessToken,
 
   ProcessLdtInformation,
 
   ProcessLdtSize,
 
   ProcessDefaultHardErrorMode,
 
   ProcessIoPortHandlers,
 
   ProcessPooledUsageAndLimits,
 
   ProcessWorkingSetWatch,
 
   ProcessUserModeIOPL,
 
   ProcessEnableAlignmentFaultFixup,
 
   ProcessPriorityClass,
 
   ProcessWx86Information,
 
   ProcessHandleCount,
 
   ProcessAffinityMask,
 
   ProcessPriorityBoost,
 
   ProcessDeviceMap,
 
   ProcessSessionInformation,
 
   ProcessForegroundInformation,
 
   ProcessWow64Information,
 
   ProcessImageFileName,
 
   ProcessLUIDDeviceMapsEnabled,
 
   ProcessBreakOnTermination,
 
   ProcessDebugObjectHandle,
 
   ProcessDebugFlags,
 
   ProcessHandleTracing,
 
   ProcessIoPriority,
 
   ProcessExecuteFlags,
 
   ProcessResourceManagement,
 
   ProcessCookie,
 
   ProcessImageInformation,
 
   MaxProcessInfoClass);

type
  NTSTATUS 
LongWord;

function 
NtQueryInformationProcess(ProcessHandleTHandleProcessInformationClassPROCESSINFOCLASSProcessInformationPointerProcessInformationLengthULONGReturnLengthPULONG ): LongIntstdcallexternal 'ntdll.dll';
function 
RtlNtStatusToDosError(StatusNTSTATUS): Integerstdcallexternal 'ntdll.dll';

function 
ImageDynamicallyRelocated(siiTSectionImageInformation): Boolean;
begin
  Result 
:= (sii.ImageFlags and (1 shl 2)) = 1;
end;

function 
CheckASLR(dwProcessIdLongWordout bASLRBoolean): LongWord;
var
 
hProcessTHandle;
 
siiTSectionImageInformation;
 
statusNTSTATUS;
begin
  hProcess 
:= OpenProcess(PROCESS_QUERY_LIMITED_INFORMATIONFalsedwProcessId);
 
 if (hProcess <> 0) and (hProcess <> INVALID_HANDLE_VALUEthen
  begin
    status 
:= NtQueryInformationProcess(hProcessProcessImageInformation, @siiSizeOf(sii), 0);
 
   CloseHandle(hProcess);
 
   if 0 <= status then
    begin
      bASLR 
:= ImageDynamicallyRelocated(sii);
 
     Result := NOERROR;
 
     Exit;
 
   end;
 
   Result := RtlNtStatusToDosError(status);
 
   Exit;
 
 end;
 
 Result := GetLastError;
end;

end



RE: Check if ASLR is enabled - Agmcz - 01-05-2018

مشكل في دالة ImageDynamicallyRelocated تم إصلاحه

PHP كود :
unit uCheckASLR;

// Original C++ Source: https://stackoverflow.com/questions/47105480/how-to-check-if-aslr-is-enabled-for-a-process
// Ported to Delphi by Agmcz 28-12-2017 2:25:32
// Fix 08-04-2018 10:56:25
// Fix2 01-05-2018 2:28:15

interface

uses
  Windows
;

function 
CheckASLR(dwProcessIdLongWordout bASLRBoolean): LongWord;

implementation

const
 
 PROCESS_QUERY_LIMITED_INFORMATION = $1000;

type
 TSectionImageInformation  
record
    TransferAddress
Pointer;
 
   ZeroBitsLongWord;
 
   MaximumStackSizeLongWord;
 
   CommittedStackSizeLongWord;
 
   SubSystemTypeLongWord;
 
   MinorSubsystemVersionWord;
 
   MajorSubsystemVersionWord;
 
   GpValueLongWord;
 
   ImageCharacteristicsWord;
 
   DllCharacteristicsWord;
 
   MachineWord;
 
   ImageContainsCodeBoolean;
 
   ImageFlagsByte;
 
   LoaderFlagsLongWord;
 
   ImageFileSizeLongWord;
 
   CheckSumLongWord;
 
 end;

 
 PROCESSINFOCLASS = (
 
   ProcessBasicInformation,
 
   ProcessQuotaLimits,
 
   ProcessIoCounters,
 
   ProcessVmCounters,
 
   ProcessTimes,
 
   ProcessBasePriority,
 
   ProcessRaisePriority,
 
   ProcessDebugPort,
 
   ProcessExceptionPort,
 
   ProcessAccessToken,
 
   ProcessLdtInformation,
 
   ProcessLdtSize,
 
   ProcessDefaultHardErrorMode,
 
   ProcessIoPortHandlers,
 
   ProcessPooledUsageAndLimits,
 
   ProcessWorkingSetWatch,
 
   ProcessUserModeIOPL,
 
   ProcessEnableAlignmentFaultFixup,
 
   ProcessPriorityClass,
 
   ProcessWx86Information,
 
   ProcessHandleCount,
 
   ProcessAffinityMask,
 
   ProcessPriorityBoost,
 
   ProcessDeviceMap,
 
   ProcessSessionInformation,
 
   ProcessForegroundInformation,
 
   ProcessWow64Information,
 
   ProcessImageFileName,
 
   ProcessLUIDDeviceMapsEnabled,
 
   ProcessBreakOnTermination,
 
   ProcessDebugObjectHandle,
 
   ProcessDebugFlags,
 
   ProcessHandleTracing,
 
   ProcessIoPriority,
 
   ProcessExecuteFlags,
 
   ProcessTlsInformation,
 
   ProcessCookie,
 
   ProcessImageInformation,
 
   ProcessCycleTime,
 
   ProcessPagePriority,
 
   ProcessInstrumentationCallback,
 
   ProcessThreadStackAllocation,
 
   ProcessWorkingSetWatchEx,
 
   ProcessImageFileNameWin32,
 
   ProcessImageFileMapping,
 
   ProcessAffinityUpdateMode,
 
   ProcessMemoryAllocationMode,
 
   ProcessGroupInformation,
 
   ProcessTokenVirtualizationEnabled,
 
   ProcessConsoleHostProcess,
 
   ProcessWindowInformation,
 
   MaxProcessInfoClass);

type
  NTSTATUS 
LongWord;

function 
NtQueryInformationProcess(ProcessHandleTHandleProcessInformationClassPROCESSINFOCLASSProcessInformationPointerProcessInformationLengthULONGReturnLengthPULONG ): LongIntstdcallexternal 'ntdll.dll';
function 
RtlNtStatusToDosError(StatusNTSTATUS): Integerstdcallexternal 'ntdll.dll';

function 
ImageDynamicallyRelocated(siiTSectionImageInformation): Boolean;
asm
  MOVZX EAX
BYTE PTR SS:[sii.ImageFlags]
 
 SHR AL2
  AND EAX
1
end
;

function 
CheckASLR(dwProcessIdLongWordout bASLRBoolean): LongWord;
var
 
hProcessTHandle;
 
siiTSectionImageInformation;
 
statusNTSTATUS;
begin
  hProcess 
:= OpenProcess(PROCESS_QUERY_LIMITED_INFORMATIONFalsedwProcessId);
 
 if (hProcess <> 0) and (hProcess <> INVALID_HANDLE_VALUEthen
  begin
    status 
:= NtQueryInformationProcess(hProcessProcessImageInformation, @siiSizeOf(sii), 0);
 
   CloseHandle(hProcess);
 
   if 0 <= status then
    begin
      bASLR 
:= ImageDynamicallyRelocated(sii);
 
     Result := NOERROR;
 
     Exit;
 
   end;
 
   Result := RtlNtStatusToDosError(status);
 
   Exit;
 
 end;
 
 Result := GetLastError;
end;

end



RE: Check if ASLR is enabled - Agmcz - 01-05-2018

Check ASLR from Remote PEB
PHP كود :
unit uCheckASLR;

{************************************
Coded by Agmcz                    *
Date01-05-2018                  *
************************************}

interface

uses
  Windows
;

function 
CheckASLRPEB32(hProcessTHandle): Boolean;

implementation

type
  PProcessBasicInformation 
= ^TProcessBasicInformation;
 
 TProcessBasicInformation record
    ExitStatus
LongInt;
 
   PebBaseAddressPointer;
 
   AffinityMaskCardinal;
 
   BasePriorityLongInt;
 
   UniqueProcessIdCardinal;
 
   InheritedFromUniqueProcessIdCardinal;
 
 end;

function 
NtQueryInformationProcess(ProcessHandleTHandleProcessInformationClassDWORD {PROCESSINFOCLASS}; ProcessInformationPointerProcessInformationLengthULONGReturnLengthPULONG): LongIntstdcallexternal 'ntdll.dll';
function 
NtReadVirtualMemory(ProcessHandleTHandleBaseAddressPointerBufferPointerBufferLengthULONGReturnLengthPULONG): Longintstdcallexternal 'ntdll.dll';

function 
ImageDynamicallyRelocated(BitFieldByte): Boolean;
asm
  SHR AL
2
  AND AL
1
end
;

function 
CheckASLRPEB32(hProcessTHandle): Boolean;
var
 
 PBITProcessBasicInformation;
 
 BitFieldByte;
begin
  Result 
:= False;
 
 if (hProcess <> 0) and (hProcess <> INVALID_HANDLE_VALUEthen
  begin
    if NtQueryInformationProcess
(hProcess0{ProcessBasicInformation 0}, @PBISizeOf(TProcessBasicInformation), 0) = 0 then
    begin
      if NtReadVirtualMemory
(hProcessPointer(DWORD(PBI.PebBaseAddress) + 3), @BitField{Peb.BitField}, SizeOf(Byte), 0) = 0 then
        Result 
:= ImageDynamicallyRelocated(BitField);
 
   end;
 
 end;
end;

end



RE: Check if ASLR is enabled - Agmcz - 16-05-2018

دعم كل من النظامين 32 و 64 بت
PHP كود :
unit uCheckASLR;

{************************************
Coded by Agmcz                    *
Date08-05-2018                  *
************************************}

interface

uses
  Windows
;

function 
CheckASLRPEB(hProcessTHandle): Boolean;

implementation

type
  PProcessBasicInformation 
= ^TProcessBasicInformation;
 
 TProcessBasicInformation record
    ExitStatus
LongInt;
 
   PebBaseAddressPointer;
 
   AffinityMaskCardinal;
 
   BasePriorityLongInt;
 
   UniqueProcessIdCardinal;
 
   InheritedFromUniqueProcessIdCardinal;
 
 end;

type
  PProcessBasicInformation64 
= ^TProcessBasicInformation64;
 
 TProcessBasicInformation64 record
    ExitStatus
Cardinal;
 
   Pad1Cardinal;
 
   PebBaseAddressUInt64;
 
   AffinityMaskUInt64;
 
   BasePriorityCardinal;
 
   Pad2Cardinal;
 
   UniqueProcessIdUInt64;
 
   InheritedFromUniqueProcessIdUInt64;
 
 end;

type
  TNtQueryInformationProcess 
= function(ProcessHandleTHandleProcessInformationClassDWORD {PROCESSINFOCLASS}; ProcessInformationPointerProcessInformationLengthULONGReturnLengthPointer): LongIntstdcall;
 
 TNtReadVirtualMemory = function(ProcessHandleTHandleBaseAddressPointerBufferPointerBufferLengthULONGReturnLengthPULONG): Longintstdcall;
 
 TNtWow64ReadVirtualMemory64 = function(ProcessHandleTHandleBaseAddressUInt64BufferPointerBufferLengthUInt64ReturnLengthPointer): LongIntstdcall;

function 
Is64OSLongBool;
asm
  XOR EAX
EAX
  MOV EAX
FS:[$C0]
end;

function 
ImageDynamicallyRelocated(BitFieldByte): Boolean;
asm
  CMP AL
4
  JNE 
@Else
 
 SHR AL2
  JMP 
@EndIF
 
 @Else:
 
 SHR AL3
  
@EndIF:
 
 AND AL1
end
;

function 
CheckASLRPEB(hProcessTHandle): Boolean;
var
 
 PBITProcessBasicInformation;
 
 PBI64TProcessBasicInformation64;
 
 BitFieldByte;
 
 hntdllHMODULE;
 
 NtQueryInformationProcessTNtQueryInformationProcess;
 
 NtReadVirtualMemoryTNtReadVirtualMemory;
 
 NtWow64QueryInformationProcess64TNtQueryInformationProcess;
 
 NtWow64ReadVirtualMemory64TNtWow64ReadVirtualMemory64;
begin
  Result 
:= False;
 
 if (hProcess <> 0) and (hProcess <> INVALID_HANDLE_VALUEthen
  begin
    hntdll 
:= LoadLibrary('ntdll.dll');
 
   if hntdll <> 0 then
    begin
      if Is64OS then
      begin
        
@NtWow64QueryInformationProcess64 := GetProcAddress(hntdll'NtWow64QueryInformationProcess64');
 
       @NtWow64ReadVirtualMemory64 := GetProcAddress(hntdll'NtWow64ReadVirtualMemory64');
 
       if NtWow64QueryInformationProcess64(hProcess0{ProcessBasicInformation 0}, @PBI64SizeOf(TProcessBasicInformation64), 0) = 0 then
        begin
          if NtWow64ReadVirtualMemory64
(hProcessPBI64.PebBaseAddress 3, @BitField{Peb.BitField}, SizeOf(Byte), 0) = 0 then
            Result 
:= ImageDynamicallyRelocated(BitField);
 
       end;
 
     end
      else
      begin
        
@NtQueryInformationProcess := GetProcAddress(hntdll'NtQueryInformationProcess');
 
       @NtReadVirtualMemory := GetProcAddress(hntdll'NtReadVirtualMemory');
 
       if NtQueryInformationProcess(hProcess0{ProcessBasicInformation 0}, @PBISizeOf(TProcessBasicInformation), 0) = 0 then
        begin
          if NtReadVirtualMemory
(hProcessPointer(DWORD(PBI.PebBaseAddress) + 3), @BitField{Peb.BitField}, SizeOf(Byte), nil) = 0 then
            Result 
:= ImageDynamicallyRelocated(BitField);
 
       end;
 
     end;
 
     FreeLibrary(hntdll);
 
   end;
 
 end;
end;

end



RE: Check if ASLR is enabled - Agmcz - 30-05-2018

Original C++ Source:

Check ASLR from file 2
PHP كود :
unit uCheckASLR
PHP كود :
interface

uses
  Windows
;

type
  NTSTATUS 
ULONG;
 
 SIZE_T Cardinal;
 
 PVOID Pointer;
 
 PLARGE_INTEGER = ^LARGE_INTEGER;
 
 HANDLE THANDLE;

function 
CheckASLR(const FileNameWideStringout bASLRBoolean): NTSTATUS;

implementation

const
 
 FILE_READ_DATA            = $0001// file & pipe
 
 FILE_READ_EA              = $0008// file & directory
 
 FILE_READ_ATTRIBUTES      = $0080// all
 
 FILE_GENERIC_READ    STANDARD_RIGHTS_READ or FILE_READ_DATA or
 
   FILE_READ_ATTRIBUTES or FILE_READ_EA or SYNCHRONIZE;

 
 FILE_SHARE_VALID_FLAGS = $00000007;
 
 FILE_SYNCHRONOUS_IO_NONALERT     = $00000020;

 
 OBJ_CASE_INSENSITIVE = $00000040;

type
  _SECTION_INFORMATION_CLASS 
= (
 
   SectionBasicInformation,
 
   SectionImageInformation);
 
 SECTION_INFORMATION_CLASS _SECTION_INFORMATION_CLASS;
 
 TSectionInformationClass SECTION_INFORMATION_CLASS;

 
TSectionImageInformation  record
    TransferAddress
Pointer;
 
   ZeroBitsLongWord;
 
   MaximumStackSizeLongWord;
 
   CommittedStackSizeLongWord;
 
   SubSystemTypeLongWord;
 
   MinorSubsystemVersionWord;
 
   MajorSubsystemVersionWord;
 
   GpValueLongWord;
 
   ImageCharacteristicsWord;
 
   DllCharacteristicsWord;
 
   MachineWord;
 
   ImageContainsCodeBoolean;
 
   ImageFlagsByte;
 
   LoaderFlagsLongWord;
 
   ImageFileSizeLongWord;
 
   CheckSumLongWord;
 
 end;

 
 TIoStatusBlock packed record
    Status      
NTSTATUS;
 
   Information ULONG;
 
 end;
 
 IO_STATUS_BLOCK TIoStatusBlock;
 
 P_IO_STATUS_BLOCK = ^TIoStatusBlock;

 
 TUnicodeString packed record
    Length
WORD;
 
   MaximumLengthWORD;
 
   BufferPWideChar;
 
 end;
 
 PUnicodeString = ^TUnicodeString;
 
 TUNICODE_STRING TUnicodeString;
 
 UNICODE_STRING TUnicodeString;
 
 PUNICODE_STRING PUnicodeString;

 
 POBJECT_ATTRIBUTES = ^OBJECT_ATTRIBUTES;
 
 OBJECT_ATTRIBUTES packed record
    Length
ULONG;
 
   RootDirectoryTHandle;
 
   ObjectNamePUNICODE_STRING;
 
   AttributesULONG;
 
   SecurityDescriptorPVOID       // Points to type SECURITY_DESCRIPTOR
 
   SecurityQualityOfServicePVOID // Points to type SECURITY_QUALITY_OF_SERVICE
 
 end;

function 
NtOpenFile(FileHandlePHANDLEDesiredAccessACCESS_MASKObjectAttributesPOBJECT_ATTRIBUTESIoStatusBlockP_IO_STATUS_BLOCKShareAccessULONGOpenOptionsULONG): LongIntstdcallexternal  'ntdll.dll';
function 
NtCreateSection(SectionHandlePHANDLEDesiredAccessACCESS_MASKObjectAttributesPOBJECT_ATTRIBUTESSectionSizePLARGE_INTEGERProtectULONGAttributesULONGFileHandleTHandle): LongIntstdcallexternal  'ntdll.dll';
function 
NtClose(Handle THandle): LongIntstdcallexternal  'ntdll.dll';
function 
ZwQuerySection(SectionHandle THandleSectionInformationClass SECTION_INFORMATION_CLASSSectionInformationPVOIDSectionInformationLengthULONGResultLengthPULONG): LongIntstdcallexternal  'ntdll.dll';
procedure RtlInitUnicodeString(DestinationStringPUNICODE_STRINGSourceStringPWideChar); stdcallexternal 'ntdll.dll';

procedure InitializeObjectAttributes(pPOBJECT_ATTRIBUTESnPUNICODE_STRING;
 
 aULONGrHANDLEsPVOID{PSECURITY_DESCRIPTOR});
begin
  p
^.Length := SizeOf(OBJECT_ATTRIBUTES);
 
 p^.RootDirectory := r;
 
 p^.Attributes := a;
 
 p^.ObjectName := n;
 
 p^.SecurityDescriptor := s;
 
 p^.SecurityQualityOfService := nil;
end;

function 
ImageDynamicallyRelocated(siiTSectionImageInformation): Boolean;
asm
  MOVZX EAX
BYTE PTR SS:[sii.ImageFlags]
 
 SHR AL2
  AND EAX
1
end
;

function 
CheckASLR(const FileNameWideStringout bASLRBoolean): NTSTATUS;
var
 
 statusNTSTATUS;
 
 hFilehSectionTHandle;
 
 iosbIO_STATUS_BLOCK;
 
 oaOBJECT_ATTRIBUTES;
 
 usTUnicodeString;
 
 siiTSectionImageInformation;
begin
  RtlInitUnicodeString
(@usPWideChar('\??\' + FileName));
  InitializeObjectAttributes(@oa, @us, OBJ_CASE_INSENSITIVE, 0, nil);
  status := NtOpenFile(@hFile, FILE_GENERIC_READ, @oa, @iosb, FILE_SHARE_VALID_FLAGS, FILE_SYNCHRONOUS_IO_NONALERT);
  if 0 <= status then
  begin
    status := NtCreateSection(@hSection, SECTION_QUERY, 0, 0, PAGE_READONLY, SEC_IMAGE, hFile);
    NtClose(hFile);
    if 0 <= status then
    begin
      status := ZwQuerySection(hSection, SectionImageInformation, @sii, sizeof(sii), 0);
      NtClose(hSection);
      if 0 <= status then
      begin
        bASLR := ImageDynamicallyRelocated(sii);
      end;
    end;
  end;
  Result := status;
end;

end. 



RE: Check if ASLR is enabled - Agmcz - 11-06-2018

السلام عليكم
تم جمع كل الدوال في وحدة واحدة ونشرها على GitHup
PHP كود :
https://github.com/Agmcz/CheckASLR 



RE: Check if ASLR is enabled - AX302 - 11-06-2018

السلام عليكم
عمل جميل ...فقط كملاحظة عدل كلمة written في الوصف