This repository was archived by the owner on Feb 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.ps1
More file actions
188 lines (148 loc) · 10.2 KB
/
Process.ps1
File metadata and controls
188 lines (148 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<# ============================================================================= *
* SA-MP Injector - PowerShell injection ↓ *
* library for SA-MP (San Andreas Multiplayer) and OMP (Open Multiplayer) *
* ============================================================================= *
* *
* Copyright (c) 2025, SPC (SA-MP Programming Community) *
* All rights reserved. *
* *
* Developed by: Calasans *
* Repository: https://github.com/spc-samp/samp-injector *
* *
* ============================================================================= *
* *
* Licensed under the MIT License (MIT); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at: *
* *
* https://opensource.org/licenses/MIT *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
* THE SOFTWARE. *
* *
* ============================================================================= #>
class Process {
[void] Close_Process_Info([Process_Info]$process_info) {
if ($null -ne $process_info) {
if ($process_info.ProcessHandle -ne [System.IntPtr]::Zero -and $process_info.ProcessHandle -ne [System.IntPtr]::new(-1)) {
[void][WinAPI]::CloseHandle($process_info.ProcessHandle)
$process_info.ProcessHandle = [System.IntPtr]::Zero
}
if ($process_info.ThreadHandle -ne [System.IntPtr]::Zero -and $process_info.ThreadHandle -ne [System.IntPtr]::new(-1)) {
[void][WinAPI]::CloseHandle($process_info.ThreadHandle)
$process_info.ThreadHandle = [System.IntPtr]::Zero
}
}
}
[Process_Info] Create_Game_Process([string]$game_path, [string]$command_args, [string]$working_dir) {
$startup_info = New-Object WinAPI+Startup_Info
$startup_info.cb = [System.Runtime.InteropServices.Marshal]::SizeOf($startup_info)
$process_information = New-Object WinAPI+Process_Information
$command_line_bytes = $null
if (-not [string]::IsNullOrEmpty($command_args)) {
$command_line_bytes = [System.Text.Encoding]::Default.GetBytes($command_args + "`0")
}
$current_directory = if ([string]::IsNullOrEmpty($working_dir)) {
$null
}
else {
$working_dir
}
$success = [WinAPI]::CreateProcessA($game_path, $command_line_bytes, [System.IntPtr]::Zero, [System.IntPtr]::Zero, $false, $global:CONSTANTS.PROCESS_CREATION_FLAGS,
[System.IntPtr]::Zero, $current_directory, [ref]$startup_info, [ref]$process_information)
if (-not $success) {
$error_code = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
$error_message = Get_System_Error_Message $error_code
Show_Error "Failed to create game process. Ensure 'gta_sa.exe' is not running and you have sufficient permissions to execute the file. System Error: $error_message" ([Inject_Type]::SAMP)
return $null
}
$result = New-Object Process_Info
$result.ProcessHandle = $process_information.hProcess
$result.ThreadHandle = $process_information.hThread
return $result
}
[bool] Inject_DLL([IntPtr]$process_handle, [string]$dll_path, [ref]$error_message) {
if ($process_handle -eq [System.IntPtr]::Zero) {
$error_message.Value = "Invalid process handle provided for DLL injection."
return $false
}
if ([string]::IsNullOrEmpty($dll_path)) {
$error_message.Value = "DLL path cannot be empty."
return $false
}
$dll_path_bytes = [System.Text.Encoding]::ASCII.GetBytes($dll_path + "`0")
$dll_path_size = $dll_path_bytes.Length
$remote_memory = [WinAPI]::VirtualAllocEx($process_handle, [System.IntPtr]::Zero, $dll_path_size, $global:CONSTANTS.MEMORY_ALLOCATION_TYPE, $global:CONSTANTS.MEMORY_PROTECTION)
if ($remote_memory -eq [System.IntPtr]::Zero) {
$error_code = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
$error_message.Value = "Failed to allocate memory in the target process. This might be due to insufficient permissions or process protection mechanisms. System Error: $(Get_System_Error_Message $error_code)"
return $false
}
try {
$bytes_written = [System.IntPtr]::Zero
$write_success = [WinAPI]::WriteProcessMemory($process_handle, $remote_memory, $dll_path_bytes, $dll_path_size, [ref]$bytes_written)
if (-not $write_success) {
$error_code = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
$error_message.Value = "Failed to write DLL path to the target process memory. Verify process permissions and ensure the DLL path is accessible. System Error: $(Get_System_Error_Message $error_code)"
return $false
}
$kernel32_handle = [WinAPI]::GetModuleHandleA($global:CONSTANTS.KERNEL32_DLL)
if ($kernel32_handle -eq [System.IntPtr]::Zero) {
$error_code = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
$error_message.Value = "Failed to obtain a handle to kernel32.dll. This is an essential system library and this error indicates a severe system issue. System Error: $(Get_System_Error_Message $error_code)"
return $false
}
$load_library_address = [WinAPI]::GetProcAddress($kernel32_handle, $global:CONSTANTS.LOAD_LIBRARY_FUNC)
if ($load_library_address -eq [System.IntPtr]::Zero) {
$error_code = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
$error_message.Value = "Failed to find the address of the LoadLibraryA function in kernel32.dll. This is critical for injecting the DLL. System Error: $(Get_System_Error_Message $error_code)"
return $false
}
$thread_id = [System.IntPtr]::Zero
$remote_thread_handle = [WinAPI]::CreateRemoteThread($process_handle, [System.IntPtr]::Zero, 0, $load_library_address, $remote_memory, 0, [ref]$thread_id)
if ($remote_thread_handle -eq [System.IntPtr]::Zero) {
$error_code = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
$error_message.Value = "Failed to create a remote thread in the target process to execute the DLL injection. This could be due to security restrictions or process state. System Error: $(Get_System_Error_Message $error_code)"
return $false
}
try {
$wait_result = [WinAPI]::WaitForSingleObject($remote_thread_handle, $global:CONSTANTS.DLL_INJECTION_TIMEOUT_MS)
if ($wait_result -ne $global:CONSTANTS.WAIT_OBJECT_0) {
$error_code = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
$wait_result_message = switch ($wait_result) {
$global:CONSTANTS.WAIT_TIMEOUT {
"timeout"
}
$global:CONSTANTS.WAIT_FAILED {
"wait failed"
}
default {
"unknown wait result ($wait_result)"
}
}
$error_message.Value = "Timeout or error waiting for DLL injection to complete ($wait_result_message). System Error: $(Get_System_Error_Message $error_code)"
return $false
}
$exit_code = 0
$get_exit_code_success = [WinAPI]::GetExitCodeThread($remote_thread_handle, [ref]$exit_code)
if (-not $get_exit_code_success -or $exit_code -eq 0) {
$error_code = [System.Runtime.InteropServices.Marshal]::GetLastWin32Error()
$error_message.Value = "DLL injection failed or returned an error. The LoadLibrary call may have failed in the target process. Exit Code: $exit_code. System Error: $(Get_System_Error_Message $error_code)"
return $false
}
return $true
}
finally {
[void][WinAPI]::CloseHandle($remote_thread_handle)
}
}
finally {
[void][WinAPI]::VirtualFreeEx($process_handle, $remote_memory, 0, $global:CONSTANTS.MEMORY_FREE_TYPE)
}
}
}