-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathMemoryReader.cs
More file actions
103 lines (79 loc) · 2.81 KB
/
MemoryReader.cs
File metadata and controls
103 lines (79 loc) · 2.81 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
using System;
using System.Collections.Immutable;
using System.Linq;
namespace read_memory_64_bit;
public interface IMemoryReader
{
ReadOnlyMemory<byte>? ReadBytes(ulong startAddress, int length);
}
public class MemoryReaderFromProcessSample(
IImmutableList<SampleMemoryRegion> memoryRegions)
: IMemoryReader
{
readonly IImmutableList<SampleMemoryRegion> memoryRegionsOrderedByAddress =
memoryRegions
.OrderBy(memoryRegion => memoryRegion.baseAddress)
.ToImmutableList();
public ReadOnlyMemory<byte>? ReadBytes(ulong startAddress, int length)
{
var memoryRegion =
memoryRegionsOrderedByAddress
.Where(region => region.baseAddress <= startAddress)
.LastOrDefault();
if (memoryRegion?.content is not { } memoryRegionContent)
return null;
var start =
startAddress - memoryRegion.baseAddress;
if ((int)start < 0)
return null;
if (memoryRegionContent.Length <= (int)start)
return null;
var sliceLengthBound =
memoryRegionContent.Length - (int)start;
var sliceLength =
length < sliceLengthBound ? length : sliceLengthBound;
return
memoryRegionContent.Slice(
start: (int)start,
length: sliceLength);
}
}
public class MemoryReaderFromLiveProcess(int processId) : IMemoryReader, IDisposable
{
readonly IntPtr processHandle =
WinApi.OpenProcess(
(int)(WinApi.ProcessAccessFlags.QueryInformation | WinApi.ProcessAccessFlags.VirtualMemoryRead),
false,
dwProcessId: processId);
public void Dispose()
{
if (processHandle != IntPtr.Zero)
WinApi.CloseHandle(processHandle);
}
public ReadOnlyMemory<byte>? ReadBytes(ulong startAddress, int length)
{
if (length < 0)
{
/*
* From https://www.ecma-international.org/wp-content/uploads/ECMA-335_6th_edition_june_2012.pdf
* III.4.20 newarr – create a zero-based, one-dimensional array
*
* [...]
* System.OverflowException is thrown if numElems is < 0
* */
return null;
}
var buffer = new byte[length];
UIntPtr numberOfBytesReadAsPtr = UIntPtr.Zero;
if (!WinApi.ReadProcessMemory(processHandle, startAddress, buffer, (UIntPtr)buffer.LongLength, ref numberOfBytesReadAsPtr))
return null;
var numberOfBytesRead = numberOfBytesReadAsPtr.ToUInt64();
if (numberOfBytesRead is 0)
return null;
if (int.MaxValue < numberOfBytesRead)
return null;
if (numberOfBytesRead == (ulong)buffer.LongLength)
return buffer;
return buffer;
}
}