#include-once
#include <WinApi.au3>
#include <Security.au3>
#region _Memory
;==================================================================================
; AutoIt Version: 3.1.127 (beta)
; Language: English
; Platform: All Windows
; Author: Nomad
; Requirements: These functions will only work with beta.
;==================================================================================
; Credits: wOuter - These functions are based on his original _Mem() functions.
; But they are easier to comprehend and more reliable. These
; functions are in no way a direct copy of his functions. His
; functions only provided a foundation from which these evolved.
;==================================================================================
;
; Functions:
;
;==================================================================================
;Funzione: _GetPID($Mode, $Name)
;Descrizione: Ritorna il PID (Process Id) del processo specificato.
;Parametri: $Mode - Modalità . Impostare a 1 per ricavare il pid tramite nome
; del processo (es Metin2.bin). Impostare a 2 per ricavare
; il pid tramite nome della finestra (es METIN2)
; $Name - Se $Mode = 1, settare $Name col nome del processo
; Se $Mode = 2 settare $Name con nome della finestra
;==================================================================================
func _GetPID($Mode, $Name)
if $mode=1 Then
$PID=ProcessExists($Name)
Else
$PID = WinGetProcess($Name)
EndIf
If @error Or $PID = 0 Then Return SetError(-1)
Return $PID
EndFunc
;==================================================================================
; Function: _MemoryOpen($iv_Pid[, $iv_DesiredAccess[, $iv_InheritHandle]])
; Description: Opens a process and enables all possible access rights to the
; process. The Process ID of the process is used to specify which
; process to open. You must call this function before calling
; _MemoryClose(), _MemoryRead(), or _MemoryWrite().
; Parameter(s): $iv_Pid - The Process ID of the program you want to open.
; $iv_DesiredAccess - (optional) Set to 0x1F0FFF by default, which
; enables all possible access rights to the
; process specified by the Process ID.
; $iv_InheritHandle - (optional) If this value is TRUE, all processes
; created by this process will inherit the access
; handle. Set to 1 (TRUE) by default. Set to 0
; if you want it FALSE.
; $debugprivilege - (opzionale) True = Privilegi di debugging attivi
; aggiunto da SnFede
; Requirement(s): None.
; Return Value(s): On Success - Returns an array containing the Dll handle and an
; open handle to the specified process.
; On Failure - Returns 0
; @Error - 0 = No error.
; 1 = Invalid $iv_Pid.
; 2 = Failed to open Kernel32.dll.
; 3 = Failed to open the specified process.
; Author(s): Nomad
; Note(s):
;==================================================================================
Func _MemoryOpen($iv_Pid, $iv_DesiredAccess = 0x1F0FFF, $iv_InheritHandle = 1, $debugprivilege = False)
If Not ProcessExists($iv_Pid) Then
SetError(1)
Return 0
EndIf
Local $ah_Handle[2] = [DllOpen('kernel32.dll')]
If @Error Then
SetError(2)
Return 0
EndIf
If $debugprivilege Then
$hToken = _Security__OpenThreadTokenEx(BitOR($__WINAPCONSTANT_TOKEN_ADJUST_PRIVILEGES, $__WINAPCONSTANT_TOKEN_QUERY))
_WinAPI_Check("_WinAPI_OpenProcess:OpenThreadTokenEx", @error, @extended)
_Security__SetPrivilege($hToken, "SeDebugPrivilege", True)
_WinAPI_Check("_WinAPI_OpenProcess:SetPrivilege:Enable", @error, @extended)
Local $writing = DllCall($OpenedProcess[0], 'int', 'OpenProcess', 'int', 0x1F0FFF, 'int', 1, 'int', $ProcessID)
_Security__SetPrivilege($hToken, "SeDebugPrivilege", False)
_WinAPI_Check("_WinAPI_OpenProcess:SetPrivilege:Disable", @error, @extended)
_WinAPI_CloseHandle($hToken)
Else
Local $writing = DllCall($OpenedProcess[0], 'int', 'OpenProcess', 'int', 0x1F0FFF, 'int', 1, 'int', $ProcessID)
EndIf
If @Error Then
DllClose($ah_Handle[0])
SetError(3)
Return 0
EndIf
$ah_Handle[1] = $av_OpenProcess[0]
Return $ah_Handle
EndFunc
;==================================================================================
; Function: _MemoryRead($iv_Address, $ah_Handle[, $sv_Type])
; Description: Reads the value located in the memory address specified.
; Parameter(s): $iv_Address - The memory address you want to read from. It must
; be in hex format (0x00000000).
; $ah_Handle - An array containing the Dll handle and the handle
; of the open process as returned by _MemoryOpen().
; $sv_Type - (optional) The "Type" of value you intend to read.
; This is set to 'dword'(32bit(4byte) signed integer)
; by default. See the help file for DllStructCreate
; for all types. An example: If you want to read a
; word that is 15 characters in length, you would use
; 'char[16]' since a 'char' is 8 bits (1 byte) in size.
; Return Value(s): On Success - Returns the value located at the specified address.
; On Failure - Returns 0
; @Error - 0 = No error.
; 1 = Invalid $ah_Handle.
; 2 = $sv_Type was not a string.
; 3 = $sv_Type is an unknown data type.
; 4 = Failed to allocate the memory needed for the DllStructure.
; 5 = Error allocating memory for $sv_Type.
; 6 = Failed to read from the specified process.
; Author(s): Nomad
; Note(s): Values returned are in Decimal format, unless specified as a
; 'char' type, then they are returned in ASCII format. Also note
; that size ('char[size]') for all 'char' types should be 1
; greater than the actual size.
;==================================================================================
Func _MemoryRead($iv_Address, $ah_Handle, $sv_Type = 'dword')
If Not IsArray($ah_Handle) Then
SetError(1)
Return 0
EndIf
Local $v_Buffer = DllStructCreate($sv_Type)
If @Error Then
SetError(@Error + 1)
Return 0
EndIf
DllCall($ah_Handle[0], 'int', 'ReadProcessMemory', 'int', $ah_Handle[1], 'int', $iv_Address, 'ptr', DllStructGetPtr($v_Buffer), 'int', DllStructGetSize($v_Buffer), 'int', '')
If Not @Error Then
Local $v_Value = DllStructGetData($v_Buffer, 1)
Return $v_Value
Else
SetError(6)
Return 0
EndIf
EndFunc
;==================================================================================
; Function: _MemoryWrite($iv_Address, $ah_Handle, $v_Data[, $sv_Type])
; Description: Writes data to the specified memory address.
; Parameter(s): $iv_Address - The memory address which you want to write to.
; It must be in hex format (0x00000000).
; $ah_Handle - An array containing the Dll handle and the handle
; of the open process as returned by _MemoryOpen().
; $v_Data - The data to be written.
; $sv_Type - (optional) The "Type" of value you intend to write.
; This is set to 'dword'(32bit(4byte) signed integer)
; by default. See the help file for DllStructCreate
; for all types. An example: If you want to write a
; word that is 15 characters in length, you would use
; 'char[16]' since a 'char' is 8 bits (1 byte) in size.
; Return Value(s): On Success - Returns 1
; On Failure - Returns 0
; @Error - 0 = No error.
; 1 = Invalid $ah_Handle.
; 2 = $sv_Type was not a string.
; 3 = $sv_Type is an unknown data type.
; 4 = Failed to allocate the memory needed for the DllStructure.
; 5 = Error allocating memory for $sv_Type.
; 6 = $v_Data is not in the proper format to be used with the
; "Type" selected for $sv_Type, or it is out of range.
; 7 = Failed to write to the specified process.
; Author(s): Nomad
; Note(s): Values sent must be in Decimal format, unless specified as a
; 'char' type, then they must be in ASCII format. Also note
; that size ('char[size]') for all 'char' types should be 1
; greater than the actual size.
;==================================================================================
Func _MemoryWrite($iv_Address, $ah_Handle, $v_Data, $sv_Type = 'dword')
If Not IsArray($ah_Handle) Then
SetError(1)
Return 0
EndIf
Local $v_Buffer = DllStructCreate($sv_Type)
If @Error Then
SetError(@Error + 1)
Return 0
Else
DllStructSetData($v_Buffer, 1, $v_Data)
If @Error Then
SetError(6)
Return 0
EndIf
EndIf
DllCall($ah_Handle[0], 'int', 'WriteProcessMemory', 'int', $ah_Handle[1], 'int', $iv_Address, 'ptr', DllStructGetPtr($v_Buffer), 'int', DllStructGetSize($v_Buffer), 'int', '')
If Not @Error Then
Return 1
Else
SetError(7)
Return 0
EndIf
EndFunc
;==================================================================================
; Function: _MemoryClose($ah_Handle)
; Description: Closes the process handle opened by using _MemoryOpen().
; Parameter(s): $ah_Handle - An array containing the Dll handle and the handle
; of the open process as returned by _MemoryOpen().
; Return Value(s): On Success - Returns 1
; On Failure - Returns 0
; @Error - 0 = No error.
; 1 = Invalid $ah_Handle.
; 2 = Unable to close the process handle.
; Author(s): Nomad
; Note(s):
;==================================================================================
Func _MemoryClose($ah_Handle)
If Not IsArray($ah_Handle) Then
SetError(1)
Return 0
EndIf
DllCall($ah_Handle[0], 'int', 'CloseHandle', 'int', $ah_Handle[1])
If Not @Error Then
DllClose($ah_Handle[0])
Return 1
Else
DllClose($ah_Handle[0])
SetError(2)
Return 0
EndIf
EndFunc
Func _MemoryPointerWrite($IV_ADDRESS, $AH_HANDLE, $AV_OFFSET, $V_DATA, $SV_TYPE = "dword")
If IsArray($AV_OFFSET) Then
If IsArray($AH_HANDLE) Then
Local $IV_POINTERCOUNT = UBound($AV_OFFSET) - 1
Else
SetError(2)
Return 0
EndIf
Else
SetError(1)
Return 0
EndIf
Local $IV_STRUCTDATA, $I
Local $V_BUFFER = DllStructCreate("dword")
For $I = 0 To $IV_POINTERCOUNT
If $I = $IV_POINTERCOUNT Then
$V_BUFFER = DllStructCreate($SV_TYPE)
If @error Then
SetError(@error + 3)
Return 0
EndIf
DllStructSetData($V_BUFFER, 1, $V_DATA)
If @error Then
SetError(8)
Return 0
EndIf
$IV_ADDRESS = "0x" & Hex($IV_STRUCTDATA + $AV_OFFSET[$I])
DllCall($AH_HANDLE[0], "int", "WriteProcessMemory", "int", $AH_HANDLE[1], "int", $IV_ADDRESS, "ptr", DllStructGetPtr($V_BUFFER), "int", DllStructGetSize($V_BUFFER), "int", "")
If @error Then
SetError(9)
Return 0
Else
Return $IV_ADDRESS
EndIf
ElseIf $I = 0 Then
DllCall($AH_HANDLE[0], "int", "ReadProcessMemory", "int", $AH_HANDLE[1], "int", $IV_ADDRESS, "ptr", DllStructGetPtr($V_BUFFER), "int", DllStructGetSize($V_BUFFER), "int", "")
If @error Then
SetError(3)
Return 0
EndIf
$IV_STRUCTDATA = DllStructGetData($V_BUFFER, 1)
Else
$IV_ADDRESS = "0x" & Hex($IV_STRUCTDATA + $AV_OFFSET[$I])
DllCall($AH_HANDLE[0], "int", "ReadProcessMemory", "int", $AH_HANDLE[1], "int", $IV_ADDRESS, "ptr", DllStructGetPtr($V_BUFFER), "int", DllStructGetSize($V_BUFFER), "int", "")
If @error Then
SetError(3)
Return 0
EndIf
$IV_STRUCTDATA = DllStructGetData($V_BUFFER, 1)
EndIf
Next
EndFunc
Func _MemoryPointerRead($IV_ADDRESS, $AH_HANDLE, $AV_OFFSET, $SV_TYPE = "dword")
If IsArray($AV_OFFSET) Then
If IsArray($AH_HANDLE) Then
Local $IV_POINTERCOUNT = UBound($AV_OFFSET) - 1
Else
SetError(2)
Return 0
EndIf
Else
SetError(1)
Return 0
EndIf
Local $IV_DATA[2], $I
Local $V_BUFFER = DllStructCreate("dword")
For $I = 0 To $IV_POINTERCOUNT
If $I = $IV_POINTERCOUNT Then
$V_BUFFER = DllStructCreate($SV_TYPE)
If @error Then
SetError(@error + 2)
Return 0
EndIf
$IV_ADDRESS = "0x" & Hex($IV_DATA[1] + $AV_OFFSET[$I])
DllCall($AH_HANDLE[0], "int", "ReadProcessMemory", "int", $AH_HANDLE[1], "int", $IV_ADDRESS, "ptr", DllStructGetPtr($V_BUFFER), "int", DllStructGetSize($V_BUFFER), "int", "")
If @error Then
SetError(7)
Return 0
EndIf
$IV_DATA[1] = DllStructGetData($V_BUFFER, 1)
ElseIf $I = 0 Then
DllCall($AH_HANDLE[0], "int", "ReadProcessMemory", "int", $AH_HANDLE[1], "int", $IV_ADDRESS, "ptr", DllStructGetPtr($V_BUFFER), "int", DllStructGetSize($V_BUFFER), "int", "")
If @error Then
SetError(7)
Return 0
EndIf
$IV_DATA[1] = DllStructGetData($V_BUFFER, 1)
Else
$IV_ADDRESS = "0x" & Hex($IV_DATA[1] + $AV_OFFSET[$I])
DllCall($AH_HANDLE[0], "int", "ReadProcessMemory", "int", $AH_HANDLE[1], "int", $IV_ADDRESS, "ptr", DllStructGetPtr($V_BUFFER), "int", DllStructGetSize($V_BUFFER), "int", "")
If @error Then
SetError(7)
Return 0
EndIf
$IV_DATA[1] = DllStructGetData($V_BUFFER, 1)
EndIf
Next
$IV_DATA[0] = $IV_ADDRESS
Return $IV_DATA
EndFunc
Func WriteBytes($Address, $OpenedProcess, $Data, $AddyType = 'dword');$Opened process è la variabile assegnata a OpenProcess(...) || $Data è il valore da scrivere
If Not IsArray($OpenedProcess) Then Return False
Local $Buffer = DllStructCreate($AddyType)
If @Error Then
Return False
Else
DllStructSetData($Buffer, 1, $Data)
If @Error Then
Return False
Else
DllCall($OpenedProcess[0], 'int', 'WriteProcessMemory', 'int', $OpenedProcess[1], 'int', $Address, 'ptr', DllStructGetPtr($Buffer), 'Int', DllStructGetSize($Buffer), 'byte', '')
If Not @Error Then
Return True
Else
Return False
EndIf
EndIf
EndIf
EndFunc
#endregion