-
Notifications
You must be signed in to change notification settings - Fork 1
/
RPCChannel.cs
172 lines (146 loc) · 5.99 KB
/
RPCChannel.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Squared.Task;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace Squared.PE {
public class RPCResponseChannel : NativeWindow, IDisposable {
protected int WM_RPC_MESSAGE;
private const int WS_EX_NOACTIVATE = 0x08000000;
protected Process _Process;
protected Dictionary<UInt32, Future<byte[]>> _AwaitingResponses = new Dictionary<uint, Future<byte[]>>();
protected BlockingQueue<byte[]> _Messages = new BlockingQueue<byte[]>();
protected Random _Random = new Random();
public UInt32 RemoteThreadId = 0;
public RPCResponseChannel (Process process, string messageName)
: base() {
_Process = process;
WM_RPC_MESSAGE = Win32.RegisterWindowMessage(messageName);
var cp = new CreateParams {
Caption = "Squared.PE.RPCChannel",
X = 0,
Y = 0,
Width = 0,
Height = 0,
Style = 0,
ExStyle = WS_EX_NOACTIVATE,
Parent = new IntPtr(-3)
};
CreateHandle(cp);
try {
if (!Win32.ChangeWindowMessageFilterEx(
this.Handle, WM_RPC_MESSAGE, MessageFilterFlag.AllowMessage, IntPtr.Zero
)) {
var error = Win32.GetLastError();
throw new Exception(String.Format("Error changing window message filter: {0:x8}", error));
}
} catch (EntryPointNotFoundException) {
try {
if (!Win32.ChangeWindowMessageFilter(
WM_RPC_MESSAGE, MessageFilterFlag.AllowMessage
)) {
var error = Win32.GetLastError();
throw new Exception(String.Format("Error changing window message filter: {0:x8}", error));
}
} catch (EntryPointNotFoundException) {
}
}
}
protected unsafe byte[] ReadRemoteData (RemoteMemoryRegion region, out UInt32 messageId) {
using (var handle = region.OpenHandle(ProcessAccessFlags.VMRead)) {
messageId = BitConverter.ToUInt32(
region.ReadBytes(handle, 0, 4), 0
);
return region.ReadBytes(handle, 4, region.Size - 4);
}
}
protected override void WndProc (ref Message m) {
if (m.Msg == WM_RPC_MESSAGE) {
byte[] messageData = null;
UInt32 messageID = 0;
if ((m.WParam != IntPtr.Zero) && (m.LParam != IntPtr.Zero))
using (var region = RemoteMemoryRegion.Existing(_Process, m.WParam, (uint)m.LParam.ToInt64()))
messageData = ReadRemoteData(region, out messageID);
Future<byte[]> fResult;
Monitor.Enter(_AwaitingResponses);
if (_AwaitingResponses.TryGetValue(messageID, out fResult)) {
_AwaitingResponses.Remove(messageID);
Monitor.Exit(_AwaitingResponses);
fResult.SetResult(messageData, null);
} else {
Debug.Assert(messageID == 0);
Monitor.Exit(_AwaitingResponses);
_Messages.Enqueue(messageData);
}
} else {
base.WndProc(ref m);
}
}
public Future<byte[]> Receive () {
return _Messages.Dequeue();
}
public UInt32 GetMessageID () {
var buf = new byte[4];
_Random.NextBytes(buf);
return BitConverter.ToUInt32(buf, 0);
}
public Future<byte[]> WaitForMessage (UInt32 messageID) {
Future<byte[]> result;
lock (_AwaitingResponses) {
if (!_AwaitingResponses.TryGetValue(messageID, out result))
_AwaitingResponses[messageID] = result = new Future<byte[]>();
}
return result;
}
public UInt32 ChannelID {
get {
return (UInt32)Handle.ToInt64();
}
}
public void Dispose () {
foreach (var f in _AwaitingResponses.Values)
f.Dispose();
_AwaitingResponses.Clear();
DestroyHandle();
}
}
public class RPCChannel : RPCResponseChannel {
public RPCChannel (Process process, string messageName)
: base(process, messageName) {
}
public unsafe void Send (byte[] message) {
if (_Process == null)
throw new Exception("No remote process");
if (RemoteThreadId == 0)
throw new Exception("No remote thread");
using (var handle = Win32.OpenProcessHandle(ProcessAccessFlags.VMWrite | ProcessAccessFlags.VMOperation, false, _Process.Id)) {
RemoteMemoryRegion region;
UInt32 regionSize = (UInt32)message.Length;
// leaked on purpose
region = RemoteMemoryRegion.Allocate(
_Process, handle, regionSize
);
fixed (byte* pData = message) {
try {
region.Write(handle, 0, regionSize, pData);
} catch {
try {
region.Dispose();
} catch {
}
throw;
}
}
if (!Win32.PostThreadMessage(RemoteThreadId, WM_RPC_MESSAGE, region.Address, region.Size)) {
var error = Win32.GetLastError();
region.Dispose();
throw new Exception(String.Format("Error posting thread message: {0:x8}", error));
}
}
}
}
}