-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Context32.cs
116 lines (100 loc) · 3.88 KB
/
Context32.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharpBlock {
public class Context32 : Context {
WinAPI.CONTEXT ctx = new WinAPI.CONTEXT();
public override ulong Ip {
get => ctx.Eip ; set => ctx.Eip = (uint)value;
}
protected override object ContextStruct { get => ctx; set => ctx = (WinAPI.CONTEXT)value; }
public Context32(ContextFlags contextFlags) {
switch (contextFlags) {
case ContextFlags.All:
ctx.ContextFlags = WinAPI.CONTEXT_FLAGS.CONTEXT_ALL;
break;
case ContextFlags.Debug:
ctx.ContextFlags = WinAPI.CONTEXT_FLAGS.CONTEXT_DEBUG_REGISTERS;
break;
}
}
public override ulong GetCurrentReturnAddress(IntPtr hProcess) {
byte[] returnAddress = new byte[4];
IntPtr bytesRead;
WinAPI.ReadProcessMemory(hProcess, new IntPtr((long)ctx.Esp), returnAddress, 4, out bytesRead);
return BitConverter.ToUInt32(returnAddress, 0);
}
public override void SetResultRegister(ulong result) {
ctx.Eax = (uint)result;
}
public override void PopStackPointer() {
ctx.Esp += 4;
}
public override void EnableBreakpoint(IntPtr address, int index) {
//Currently only supports first hardware breakpoint, could
//be expanded to support up to 4 hardware breakpoint for altering
//ETW and other potensial bypasses
ctx.Dr0 = (uint)address.ToInt32();
//Set bits 16-19 as 0, DR0 for execute HBP
ctx.Dr7 = (uint)SetBits((ulong)ctx.Dr7, 16, 4, 0);
//Set DR0 HBP as enabled
ctx.Dr7 = (uint)SetBits((ulong)ctx.Dr7, 0, 2, 3);
ctx.Dr6 = 0;
}
public override void EnableSingleStep() {
ctx.Dr0 = ctx.Dr6 = ctx.Dr7 = 0;
ctx.EFlags |= (1 << 8);
}
public override void ClearBreakpoint(int index) {
ctx.Dr0 = ctx.Dr6 = ctx.Dr7 = 0;
ctx.EFlags = 0;
}
protected override bool SetContext(IntPtr thread, IntPtr context) {
return WinAPI.SetThreadContext(thread, context);
}
protected override bool GetContext(IntPtr thread, IntPtr context) {
return WinAPI.GetThreadContext(thread, context);
}
public override void SetRegister(int index, long value) {
switch (index) {
case 0:
ctx.Eax = (uint)value;
break;
case 1:
ctx.Ebx = (uint)value;
break;
case 2:
ctx.Ecx = (uint)value;
break;
case 3:
ctx.Edx = (uint)value;
break;
default:
throw new NotImplementedException();
}
}
public override long GetRegister(int index) {
switch (index) {
case 0:
return (long)ctx.Eax;
case 1:
return (long)ctx.Ebx;
case 2:
return (long)ctx.Ecx;
case 3:
return (long)ctx.Edx;
default:
throw new NotImplementedException();
}
}
public override long GetParameter(int index, IntPtr hProcess) {
long parameterAddress = ctx.Esp + 4 + (index * 4);
byte[] parameterValue = new byte[4];
IntPtr bytesRead;
WinAPI.ReadProcessMemory(hProcess, new IntPtr(parameterAddress), parameterValue, 4, out bytesRead);
return BitConverter.ToUInt32(parameterValue, 0);
}
}
}