-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
150 lines (145 loc) · 5.72 KB
/
Copy pathProgram.cs
File metadata and controls
150 lines (145 loc) · 5.72 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
using System;
using System.IO;
using System.Text;
using System.Threading;
/// <summary>
/// SA:MP cmd .NET namespace
/// </summary>
namespace SAMPcmdNET
{
/// <summary>
/// Program class
/// </summary>
public class Program
{
/// <summary>
/// Exe directory
/// </summary>
public static string ExeDir
{
get
{
return Directory.GetCurrentDirectory();
}
}
/// <summary>
/// GTA San Andread Exe
/// </summary>
public static string GTASAExePath
{
get
{
return ExeDir + "\\gta_sa.exe";
}
}
/// <summary>
/// SA:MP DLL path
/// </summary>
public static string SAMPDLLPath
{
get
{
return ExeDir + "\\samp.dll";
}
}
/// <summary>
/// Is SA:MP available
/// </summary>
public static bool IsSAMPAvailable
{
get
{
return (File.Exists(GTASAExePath) && File.Exists(SAMPDLLPath));
}
}
/// <summary>
/// Main
/// </summary>
/// <param name="args">Arguments</param>
static void Main(string[] args)
{
if (IsSAMPAvailable)
{
StringBuilder arguments = new StringBuilder();
bool first = true;
foreach (string arg in args)
{
if (first)
{
first = false;
}
else
{
arguments.Append(" ");
}
arguments.Append(arg);
}
IntPtr mh = Kernel32.GetModuleHandle("kernel32.dll");
if (mh != IntPtr.Zero)
{
IntPtr pa = Kernel32.GetProcAddress(mh, "LoadLibraryW");
if (pa != IntPtr.Zero)
{
Kernel32.PROCESS_INFORMATION process_info;
Kernel32.STARTUPINFO startup_info = new Kernel32.STARTUPINFO();
if (Kernel32.CreateProcess(GTASAExePath, arguments.ToString(), IntPtr.Zero, IntPtr.Zero, false, /* DETACHED_PROCESS */ 0x8 | /* CREATE_SUSPENDED */ 0x4, IntPtr.Zero, ExeDir, ref startup_info, out process_info))
{
IntPtr ptr = Kernel32.VirtualAllocEx(process_info.hProcess, IntPtr.Zero, (uint)(SAMPDLLPath.Length + 1) * 2U, Kernel32.AllocationType.Reserve | Kernel32.AllocationType.Commit, Kernel32.MemoryProtection.ReadWrite);
if (ptr != IntPtr.Zero)
{
int nobw = 0;
byte[] p = Encoding.Unicode.GetBytes(SAMPDLLPath);
byte[] nt = Encoding.Unicode.GetBytes("\0");
if (Kernel32.WriteProcessMemory(process_info.hProcess, ptr, p, (uint)(p.Length), out nobw) && Kernel32.WriteProcessMemory(process_info.hProcess, new IntPtr(ptr.ToInt64() + p.LongLength), nt, (uint)(nt.Length), out nobw))
{
uint tid = 0U;
IntPtr rt = Kernel32.CreateRemoteThread(process_info.hProcess, IntPtr.Zero, 0U, pa, ptr, /* CREATE_SUSPENDED */ 0x4, out tid);
if (rt != IntPtr.Zero)
{
Kernel32.ResumeThread(rt);
unchecked
{
Console.WriteLine("SA:MP module injected. Waiting process to finish...");
Kernel32.WaitForSingleObject(rt, (uint)(Timeout.Infinite));
}
}
else
{
Console.Error.WriteLine("Failed to create remote thread with \"CreateRemoteThread\".");
}
}
else
{
Console.Error.WriteLine("Failed to write into process memory with \"WriteProcessMemory\".");
}
Kernel32.VirtualFreeEx(process_info.hProcess, ptr, 0, Kernel32.AllocationType.Release);
}
else
{
Console.Error.WriteLine("Failed to allocate memory with \"VirtualAllocEx\".");
}
Kernel32.ResumeThread(process_info.hThread);
Kernel32.CloseHandle(process_info.hProcess);
}
else
{
Console.Error.WriteLine("Failed to create process \"" + GTASAExePath + "\"");
}
}
else
{
Console.Error.WriteLine("Function LoadLibraryW not found.");
}
}
else
{
Console.Error.WriteLine("Module kernel32.dll not found.");
}
}
else
{
Console.Error.WriteLine("SA:MP is not installed on \"" + ExeDir + "\".");
}
}
}
}