Skip to content

Commit 5cfc641

Browse files
committed
Add encryption to windows client
1 parent 0afb402 commit 5cfc641

File tree

4 files changed

+503
-86
lines changed

4 files changed

+503
-86
lines changed

windows/client/OctopusClient/OctopusClient.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
77
<ProjectGuid>{A1F757AD-A6B3-46B9-BD11-DE43E9ABFB80}</ProjectGuid>
8-
<OutputType>Exe</OutputType>
8+
<OutputType>WinExe</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>OctopusClient</RootNamespace>
1111
<AssemblyName>OctopusClient</AssemblyName>
@@ -48,6 +48,7 @@
4848
<ItemGroup>
4949
<Compile Include="Program.cs" />
5050
<Compile Include="Properties\AssemblyInfo.cs" />
51+
<Compile Include="XXTEA.cs" />
5152
</ItemGroup>
5253
<ItemGroup>
5354
<None Include="App.config" />
Lines changed: 115 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
63
using System.Net.Sockets;
74
using System.Net;
85
using System.IO;
96
using System.Runtime.InteropServices;
10-
7+
using Xxtea;
118

129
namespace OctopusClient
1310
{
14-
class Program
11+
class OctopusClient
1512
{
1613
[StructLayout(LayoutKind.Sequential)]
17-
public struct MOUSEINPUT {
14+
public struct MOUSEINPUT
15+
{
1816
public int dx;
1917
public int dy;
2018
public int mouseData;
@@ -23,21 +21,24 @@ public struct MOUSEINPUT {
2321
public IntPtr dwExtraInfo;
2422
}
2523
[StructLayout(LayoutKind.Sequential)]
26-
public struct KEYBDINPUT {
24+
public struct KEYBDINPUT
25+
{
2726
public ushort wVk;
2827
public ushort wScan;
2928
public uint dwFlags;
3029
public uint time;
3130
public IntPtr dwExtraInfo;
3231
}
3332
[StructLayout(LayoutKind.Sequential)]
34-
public struct HARDWAREINPUT {
33+
public struct HARDWAREINPUT
34+
{
3535
public uint uMsg;
3636
public ushort wParamL;
3737
public ushort wParamH;
3838
}
3939
[StructLayout(LayoutKind.Explicit)]
40-
public struct InputBatch {
40+
public struct InputBatch
41+
{
4142
[FieldOffset(0)]
4243
public HARDWAREINPUT Hardware;
4344
[FieldOffset(0)]
@@ -46,37 +47,41 @@ public struct InputBatch {
4647
public MOUSEINPUT Mouse;
4748
}
4849
[StructLayout(LayoutKind.Sequential)]
49-
public struct INPUT {
50+
public struct INPUT
51+
{
5052
public SendInputEventType type;
5153
public InputBatch data;
5254
}
5355
[Flags]
54-
public enum LinuxEventTypes : uint {
55-
EV_SYN = 0x00,
56-
EV_KEY = 0x01,
57-
EV_REL = 0x02,
58-
EV_ABS = 0x03,
59-
EV_MSC = 0x04,
60-
EV_SW = 0x05,
61-
EV_LED = 0x11,
62-
EV_SND = 0x12,
63-
EV_REP = 0x14,
64-
EV_FF = 0x15,
65-
EV_PWR = 0x16,
56+
public enum LinuxEventTypes : uint
57+
{
58+
EV_SYN = 0x00,
59+
EV_KEY = 0x01,
60+
EV_REL = 0x02,
61+
EV_ABS = 0x03,
62+
EV_MSC = 0x04,
63+
EV_SW = 0x05,
64+
EV_LED = 0x11,
65+
EV_SND = 0x12,
66+
EV_REP = 0x14,
67+
EV_FF = 0x15,
68+
EV_PWR = 0x16,
6669
EV_FF_STATUS = 0x17,
67-
EV_MAX = 0x1f,
68-
EV_CNT = 0x20
70+
EV_MAX = 0x1f,
71+
EV_CNT = 0x20
6972
}
70-
public enum LinuxSynCodes : uint {
71-
SYN_REPORT = 0,
72-
SYN_CONFIG = 1,
73+
public enum LinuxSynCodes : uint
74+
{
75+
SYN_REPORT = 0,
76+
SYN_CONFIG = 1,
7377
SYN_MT_REPORT = 2,
74-
SYN_DROPPED = 3,
75-
SYN_MAX = 0xf,
76-
SYN_CNT = 0x10
78+
SYN_DROPPED = 3,
79+
SYN_MAX = 0xf,
80+
SYN_CNT = 0x10
7781
}
7882

79-
public enum MouseEventFlags : uint {
83+
public enum MouseEventFlags : uint
84+
{
8085
MOUSEEVENTF_MOVE = 0x0001,
8186
MOUSEEVENTF_LEFTDOWN = 0x0002,
8287
MOUSEEVENTF_LEFTUP = 0x0004,
@@ -97,25 +102,26 @@ public enum SendInputEventType : int
97102
InputKeyboard,
98103
InputHardware
99104
}
100-
public enum UsefulConst : uint {
101-
BTN_MIN = 0x0100,
102-
BTN_MAX = 0x015f,
105+
public enum UsefulConst : uint
106+
{
107+
BTN_MIN = 0x0100,
108+
BTN_MAX = 0x015f,
103109

104110
// Supported mouse buttons
105-
BTN_LEFT = 0x110,
106-
BTN_RIGHT = 0x111,
111+
BTN_LEFT = 0x110,
112+
BTN_RIGHT = 0x111,
107113
BTN_MIDDLE = 0x112,
108-
BTN_SIDE = 0x113,
109-
BTN_EXTRA = 0x114,
114+
BTN_SIDE = 0x113,
115+
BTN_EXTRA = 0x114,
110116

111117
// Supported relative axes
112-
REL_X = 0x00,
113-
REL_Y = 0x01,
118+
REL_X = 0x00,
119+
REL_Y = 0x01,
114120
REL_HWHEEL = 0x06,
115-
REL_WHEEL = 0x08
121+
REL_WHEEL = 0x08
116122
}
117123

118-
public dictionary LinuxKeyCode2Extended = new Dictionary<uint, uint> {
124+
public Dictionary<uint, ushort> LinuxKeyCode2Extended = new Dictionary<uint, ushort> {
119125
{ 96, 0x001c }, // KEY_KPENTER
120126
{ 97, 0x001d }, // KEY_RIGHTCTRL
121127
{ 98, 0x0035 }, // KEY_KPSLASH
@@ -126,7 +132,7 @@ public enum UsefulConst : uint {
126132
{ 143, 0x0063 } // KEY_WAKEUP
127133
};
128134

129-
public dictionary LinuxKeyCode2Virtual = new Dictionary<uint, uint> {
135+
public Dictionary<uint, ushort> LinuxKeyCode2Virtual = new Dictionary<uint, ushort> {
130136
// These would need fakeshifts with scancodes, so we use virtual codes instead.
131137
{ 103, 0x0026 }, // KEY_UP
132138
{ 105, 0x0025 }, // KEY_LEFT
@@ -140,6 +146,7 @@ public enum UsefulConst : uint {
140146
{ 111, 0x002e }, // KEY_DELETE
141147
{ 125, 0x005b }, // KEY_LEFTMETA
142148
{ 126, 0x005c }, // KEY_RIGHTMETA
149+
143150
// Nonstandard scancodes, use virtual codes instead.
144151
{ 113, 0x00AD }, // KEY_MUTE
145152
{ 114, 0x00AE }, // KEY_VOLUMEDOWN
@@ -155,20 +162,20 @@ public enum UsefulConst : uint {
155162
[DllImport("user32.dll", SetLastError = true)]
156163
public static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
157164

158-
public INPUT[] stackedInputs;
165+
public List<INPUT> stackedInputs = new List<INPUT>();
159166

160-
static public int StackKbdInput(ushort wVk, ushort wScan, uint dwFlags) {
167+
public void StackKbdInput(ushort wVk, ushort wScan, uint dwFlags) {
161168
INPUT input = new INPUT { type = SendInputEventType.InputKeyboard };
162169
input.data.Keyboard = new KEYBDINPUT();
163170
input.data.Keyboard.time = 0;
164171
input.data.Keyboard.dwExtraInfo = IntPtr.Zero;
165172
input.data.Keyboard.wVk = wVk;
166173
input.data.Keyboard.wScan = wScan;
167174
input.data.Keyboard.dwFlags = dwFlags;
168-
stackedInputs.push(input);
175+
stackedInputs.Add(input);
169176
}
170177

171-
static public int StackMouseInput(int dx, int dy, int mouseData, uint dwFlags) {
178+
public void StackMouseInput(int dx, int dy, int mouseData, uint dwFlags) {
172179
INPUT input = new INPUT { type = SendInputEventType.InputMouse };
173180
input.data.Mouse = new MOUSEINPUT();
174181
input.data.Mouse.time = 0;
@@ -177,16 +184,16 @@ static public int StackMouseInput(int dx, int dy, int mouseData, uint dwFlags) {
177184
input.data.Mouse.dy = dy;
178185
input.data.Mouse.mouseData = mouseData;
179186
input.data.Mouse.dwFlags = dwFlags;
180-
stackedInputs.push(input);
187+
stackedInputs.Add(input);
181188
}
182189

183-
static public int SendStackedInputs() {
184-
SendInput(stackedInputs.Length, stackedInputs, Marshal.SizeOf(typeof(INPUT)));
185-
Array.Clear(stackedInputs, 0, stackedInputs.Length);
190+
public void SendStackedInputs() {
191+
INPUT[] inputs = stackedInputs.ToArray();
192+
SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
193+
stackedInputs.Clear();
186194
}
187195

188-
static void Main(string[] args)
189-
{
196+
public void Run(byte clientId, string key) {
190197
UdpClient socket = new UdpClient(4020);
191198
socket.JoinMulticastGroup(IPAddress.Parse("239.255.77.88"));
192199
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
@@ -197,82 +204,105 @@ static void Main(string[] args)
197204
using (BinaryReader reader = new BinaryReader(new MemoryStream(message))) {
198205

199206
byte client = reader.ReadByte();
200-
ushort type = reader.ReadUInt16();
201-
ushort code = reader.ReadUInt16();
202-
int value = reader.ReadInt32();
207+
if (client != clientId) continue;
203208

204-
// FIXME
205-
if (client != 1) continue;
209+
byte enc = reader.ReadByte();
210+
ushort type = 0;
211+
ushort code = 0;
212+
int value = 0;
206213

214+
if (enc > 0) {
215+
Byte[] encrypted = reader.ReadBytes(enc);
216+
Byte[] decrypted = XXTEA.Decrypt(encrypted, key);
217+
BinaryReader encreader = new BinaryReader(new MemoryStream(decrypted));
218+
UInt32 rnd = encreader.ReadUInt32();
219+
type = encreader.ReadUInt16();
220+
code = encreader.ReadUInt16();
221+
value = encreader.ReadInt32();
222+
}
223+
else if (enc == 0) {
224+
UInt32 rnd = reader.ReadUInt32();
225+
type = reader.ReadUInt16();
226+
code = reader.ReadUInt16();
227+
value = reader.ReadInt32();
228+
}
229+
else continue;
230+
207231
//Console.WriteLine("Type:" + type + " Code:" + code + " Value:" + value);
208-
209232
//continue;
210233

211-
if (type == LinuxEventTypes.EV_SYN) {
234+
if (type == (uint)LinuxEventTypes.EV_SYN) {
212235
// We only handle SYN_REPORT
213-
if (code == LinuxSynCodes.SYN_REPORT) {
236+
if (code == (uint)LinuxSynCodes.SYN_REPORT) {
214237
// Send buffered inputs
215238
SendStackedInputs();
216239
}
217240
}
218-
else if (type == LinuxEventTypes.EV_KEY) {
219-
220-
if (code >= UsefulConst.BTN_MIN && code <= UsefulConst.BTN_MAX) {
241+
else if (type == (uint)LinuxEventTypes.EV_KEY) {
242+
if (code >= (uint)UsefulConst.BTN_MIN && code <= (uint)UsefulConst.BTN_MAX) {
221243
// Mouse Buttons
222-
if (code == UsefulConst.BTN_LEFT) {
223-
StackMouseInput(0, 0, 0, value ? (uint)MouseEventFlags.MOUSEEVENTF_LEFTDOWN
244+
if (code == (uint)UsefulConst.BTN_LEFT) {
245+
StackMouseInput(0, 0, 0, value > 0 ? (uint)MouseEventFlags.MOUSEEVENTF_LEFTDOWN
224246
: (uint)MouseEventFlags.MOUSEEVENTF_LEFTUP);
225247
}
226-
else if (code == UsefulConst.BTN_RIGHT) {
227-
StackMouseInput(0, 0, 0, value ? (uint)MouseEventFlags.MOUSEEVENTF_RIGHTDOWN
248+
else if (code == (uint)UsefulConst.BTN_RIGHT) {
249+
StackMouseInput(0, 0, 0, value > 0 ? (uint)MouseEventFlags.MOUSEEVENTF_RIGHTDOWN
228250
: (uint)MouseEventFlags.MOUSEEVENTF_RIGHTUP);
229251
}
230-
else if(code == UsefulConst.BTN_MIDDLE) {
231-
StackMouseInput(0, 0, 0, value ? (uint)MouseEventFlags.MOUSEEVENTF_MIDDLEDOWN
252+
else if (code == (uint)UsefulConst.BTN_MIDDLE) {
253+
StackMouseInput(0, 0, 0, value > 0 ? (uint)MouseEventFlags.MOUSEEVENTF_MIDDLEDOWN
232254
: (uint)MouseEventFlags.MOUSEEVENTF_MIDDLEUP);
233255
}
234-
else if (code == UsefulConst.BTN_SIDE) {
235-
StackMouseInput(0, 0, 1, value ? (uint)MouseEventFlags.MOUSEEVENTF_XDOWN
256+
else if (code == (uint)UsefulConst.BTN_SIDE) {
257+
StackMouseInput(0, 0, 1, value > 0 ? (uint)MouseEventFlags.MOUSEEVENTF_XDOWN
236258
: (uint)MouseEventFlags.MOUSEEVENTF_XUP);
237259
}
238-
else if (code == UsefulConst.BTN_EXTRA) {
239-
StackMouseInput(0, 0, 2, value ? (uint)MouseEventFlags.MOUSEEVENTF_XDOWN
260+
else if (code == (uint)UsefulConst.BTN_EXTRA) {
261+
StackMouseInput(0, 0, 2, value > 0 ? (uint)MouseEventFlags.MOUSEEVENTF_XDOWN
240262
: (uint)MouseEventFlags.MOUSEEVENTF_XUP);
241263
}
242264
}
243265
else {
244266
// Keyboard key
245-
if (LinuxKeyCode2Virtual[code]) {
267+
if (LinuxKeyCode2Virtual.ContainsKey(code)) {
246268
// Must use wVk mechanism instead of raw scancode
247-
StackKbdInput(LinuxKeyCode2Virtual[code], 0, value ? 0x0 : 0x2);
269+
StackKbdInput(LinuxKeyCode2Virtual[code], 0, (uint)(value > 0 ? 0x0 : 0x2));
248270
}
249-
else if (LinuxKeyCode2Extended[code]) {
271+
else if (LinuxKeyCode2Extended.ContainsKey(code)) {
250272
// Extended key
251-
StackKbdInput(0, LinuxKeyCode2Extended[code], value ? 0x1 : 0x3);
273+
StackKbdInput(0, 0xe0, 0);
274+
StackKbdInput(0, LinuxKeyCode2Extended[code], (uint)(value > 0 ? 0x9 : 0xb));
252275
}
253276
else {
254277
// Raw scancode
255-
StackKbdInput(0, code, value ? 0x8 : 0xa);
278+
StackKbdInput(0, code, (uint)(value > 0 ? 0x8 : 0xa));
256279
}
257280
}
258281
}
259-
else if (type == LinuxEventTypes.EV_REL) {
260-
261-
if (code == UsefulConst.REL_X) {
282+
else if (type == (uint)LinuxEventTypes.EV_REL) {
283+
if (code == (uint)UsefulConst.REL_X) {
262284
StackMouseInput(value, 0, 0, (uint)MouseEventFlags.MOUSEEVENTF_MOVE);
263285
}
264-
else if (code == UsefulConst.REL_Y) {
286+
else if (code == (uint)UsefulConst.REL_Y) {
265287
StackMouseInput(0, value, 0, (uint)MouseEventFlags.MOUSEEVENTF_MOVE);
266288
}
267-
else if (code == UsefulConst.REL_WHEEL) {
289+
else if (code == (uint)UsefulConst.REL_WHEEL) {
268290
StackMouseInput(0, 0, value * 120, (uint)MouseEventFlags.MOUSEEVENTF_WHEEL);
269291
}
270-
else if (code == UsefulConst.REL_HWHEEL) {
292+
else if (code == (uint)UsefulConst.REL_HWHEEL) {
271293
StackMouseInput(0, 0, value * 120, (uint)MouseEventFlags.MOUSEEVENTF_HWHEEL);
272294
}
273295
}
274296
}
275297
}
276298
}
277299
}
300+
301+
class Program
302+
{
303+
static void Main(string[] args) {
304+
OctopusClient octopusClient = new OctopusClient();
305+
octopusClient.Run(byte.Parse(args[0]), args[1]);
306+
}
307+
}
278308
}

0 commit comments

Comments
 (0)