forked from BosslandGmbH/HBRelog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cs
More file actions
451 lines (396 loc) · 17.1 KB
/
Utility.cs
File metadata and controls
451 lines (396 loc) · 17.1 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
Copyright 2012 HighVoltz
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace HighVoltz.HBRelog
{
internal static class Utility
{
public static bool IsUserAdministrator()
{
//bool value to hold our return value
bool isAdmin;
try
{
//get the currently logged in user
WindowsIdentity user = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(user);
isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (UnauthorizedAccessException)
{
isAdmin = false;
}
catch (Exception)
{
isAdmin = false;
}
return isAdmin;
}
public const uint WmKeydown = 0x0100;
public const uint WmChar = 0x0102;
public const uint WmKeyup = 0x0101;
public static readonly Random Rand = new Random();
public static readonly string AssemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
public static bool HasInternetConnection
{
get
{
int state;
return NativeMethods.InternetGetConnectedState(out state, 0);
}
}
public static string EncodeToUTF8(this string text)
{
var buffer = new StringBuilder(Encoding.UTF8.GetByteCount(text)*2);
var utf8Encoded = Encoding.UTF8.GetBytes(text);
foreach (var b in utf8Encoded)
{
buffer.Append(string.Format("\\{0:D3}", b));
}
return buffer.ToString();
}
public static void UnblockFileIfZoneRestricted(string file)
{
if (!File.Exists(file))
throw new FileNotFoundException(file);
var path = file + ":Zone.Identifier";
if (NativeMethods.GetFileAttributes(path) != -1)
{
Log.Write("Removing Zone restrictions from {0}", file);
NativeMethods.DeleteFile(path);
}
}
delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
public static IEnumerable<IntPtr> EnumerateProcessWindowHandles(int processId)
{
var handles = new List<IntPtr>();
foreach (ProcessThread thread in Process.GetProcessById(processId).Threads)
EnumThreadWindows(thread.Id,
(hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);
return handles;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowTextLength(HandleRef hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(HandleRef hWnd, StringBuilder lpString, int nMaxCount);
public static void ResizeAndMoveWindow(IntPtr hWnd, int x, int y, int width, int height)
{
NativeMethods.SetWindowPos(hWnd, new IntPtr(0), x, y, width, height,
NativeMethods.SetWindowPosFlags.SWP_NOZORDER | NativeMethods.SetWindowPosFlags.SWP_NOACTIVATE);
}
public static NativeMethods.Rect GetWindowRect(IntPtr hWnd)
{
var result = new NativeMethods.Rect();
NativeMethods.GetWindowRect(hWnd, out result);
return result;
}
public static bool Is64BitProcess(Process proc)
{
bool retVal;
return Environment.Is64BitOperatingSystem &&
!(NativeMethods.IsWow64Process(proc.Handle, out retVal) && retVal);
}
public static NativeMethods.WindowInfo GetWindowInfo(IntPtr hWnd)
{
var wi = new NativeMethods.WindowInfo(true);
NativeMethods.GetWindowInfo(hWnd, ref wi);
return wi;
}
public static Process GetChildProcessByName(int parentPid, string processName)
{
var processes = Process.GetProcessesByName(processName);
return processes.FirstOrDefault(process => IsChildProcessOf(parentPid, process));
}
public static bool IsChildProcessOf(int parentPid, Process child)
{
var childPid = child.Id;
try
{
while (true)
{
var childParrentPid = NativeMethods.ParentProcessUtilities.GetParentProcessId(childPid);
if (childParrentPid <= 0) return false;
if (childParrentPid == parentPid) return true;
childPid = childParrentPid;
}
}
catch (Exception)
{
return false;
}
}
// returns base offset for main module
public static uint BaseOffset(this Process proc)
{
return (uint) proc.MainModule.BaseAddress.ToInt32();
}
public static string VersionString(this Process proc)
{
return proc.MainModule.FileVersionInfo.FileVersion;
}
/// <summary>
/// Encrpts the string using dpapi and returns a base64 string of encrypted data
/// </summary>
/// <param name="clearData">The clear data.</param>
/// <returns></returns>
public static string EncrptDpapi(string clearData)
{
var data = Encoding.Unicode.GetBytes(clearData);
data = ProtectedData.Protect(data, null, DataProtectionScope.CurrentUser);
return Convert.ToBase64String(data);
}
/// <summary>
/// Decrypts the base64 string using dpapi and returns the unencrpyted text.
/// </summary>
/// <param name="base64Data">The clear data.</param>
/// <returns></returns>
public static string DecrptDpapi(string base64Data)
{
var data = Convert.FromBase64String(base64Data);
data = ProtectedData.Unprotect(data, null, DataProtectionScope.CurrentUser);
return Encoding.Unicode.GetString(data);
}
public static string DecryptAes(string clearText, byte[] key, byte[] iv)
{
var cipherBytes = Convert.FromBase64String(clearText);
using (var algorithm = Aes.Create())
{
// algorithm.Padding = PaddingMode.None;
using (var decryptor = algorithm.CreateDecryptor(key, iv))
{
using (var m = new MemoryStream())
{
using (var c = new CryptoStream(m, decryptor, CryptoStreamMode.Write))
{
c.Write(cipherBytes, 0, cipherBytes.Length);
c.FlushFinalBlock();
}
var a = m.ToArray();
return Encoding.Unicode.GetString(a);
}
}
}
}
public static string EncryptAes(string clearText, byte[] key, byte[] iv)
{
var cipherBytes = Encoding.Unicode.GetBytes(clearText);
using (var algorithm = Aes.Create())
{
// algorithm.Padding = PaddingMode.None;
using (var decryptor = algorithm.CreateEncryptor(key, iv))
{
using (var m = new MemoryStream())
{
using (var c = new CryptoStream(m, decryptor, CryptoStreamMode.Write))
{
c.Write(cipherBytes, 0, cipherBytes.Length);
c.FlushFinalBlock();
}
var a = m.ToArray();
return Convert.ToBase64String(a);
}
}
}
}
#region Key and Mouse sending utility functions.
public static bool SendBackgroundKey(IntPtr hWnd, char key, bool useVmChar = true)
{
var scanCode = NativeMethods.MapVirtualKey(key, 0);
var lParam = (UIntPtr) (0x00000001 | (scanCode << 16));
if (useVmChar)
return SendMessage(hWnd, NativeMethods.Message.VM_CHAR, key, lParam);
return SendMessage(hWnd, NativeMethods.Message.KEY_DOWN, key, lParam) &&
SendMessage(hWnd, NativeMethods.Message.KEY_UP, key, lParam);
}
public static void SendBackgroundString(IntPtr hWnd, string str, bool downUp = true)
{
foreach (var chr in str)
{
SendBackgroundKey(hWnd, chr, downUp);
}
}
public static void PostBackgroundKey(IntPtr hWnd, char key, bool useVmChar = true)
{
var scanCode = NativeMethods.MapVirtualKey(key, 0);
var lParam = (UIntPtr) (0x00000001 | (scanCode << 16));
if (useVmChar)
{
PostMessage(hWnd, NativeMethods.Message.VM_CHAR, key, lParam);
}
else
{
PostMessage(hWnd, NativeMethods.Message.KEY_DOWN, key, lParam);
PostMessage(hWnd, NativeMethods.Message.VM_CHAR, key, lParam);
PostMessage(hWnd, NativeMethods.Message.KEY_UP, key, lParam);
}
}
public static void PostBackgroundString(IntPtr hWnd, string str, bool downUp = true)
{
foreach (var chr in str)
{
PostBackgroundKey(hWnd, chr);
}
}
private static bool SendMessage(IntPtr hWnd, NativeMethods.Message msg, char key, UIntPtr lParam)
{
for (var cnt = 0; cnt < 4; cnt++)
{
if (NativeMethods.SendMessage(hWnd, (uint) msg, (IntPtr) key, lParam) != IntPtr.Zero)
continue;
return true;
}
return false;
}
private static void PostMessage(IntPtr hWnd, NativeMethods.Message msg, char key, UIntPtr lParam)
{
NativeMethods.PostMessage(hWnd, (uint) msg, (IntPtr) key, lParam);
}
private const int SizeOfInput = 28;
public static void LeftClickAtPos(
IntPtr hWnd, int x, int y, bool doubleClick = false, bool restore = true, Func<bool> restoreCondition = null)
{
var wndBounds = GetWindowRect(hWnd);
double fScreenWidth = NativeMethods.GetSystemMetrics(NativeMethods.SystemMetric.SM_CXSCREEN) - 1;
double fScreenHeight = NativeMethods.GetSystemMetrics(NativeMethods.SystemMetric.SM_CYSCREEN) - 1;
var fx = (wndBounds.Left + x)*(65535.0f/fScreenWidth);
var fy = (wndBounds.Top + y)*(65535.0f/fScreenHeight);
var structInput = new NativeMethods.Input {type = NativeMethods.SendInputEventType.InputMouse};
structInput.mkhi.mi.dwFlags = NativeMethods.MouseEventFlags.ABSOLUTE | NativeMethods.MouseEventFlags.MOVE |
NativeMethods.MouseEventFlags.LEFTDOWN |
NativeMethods.MouseEventFlags.LEFTUP;
structInput.mkhi.mi.dx = (int) fx;
structInput.mkhi.mi.dy = (int) fy;
var forefroundWindow = NativeMethods.GetForegroundWindow();
if (restore)
SaveForegroundWindowAndMouse();
try
{
NativeMethods.BlockInput(true);
for (var num = 0; forefroundWindow != hWnd && num < 1000; num++)
{
NativeMethods.SetForegroundWindow(hWnd);
Thread.Sleep(10);
forefroundWindow = NativeMethods.GetForegroundWindow();
}
NativeMethods.SendInput(1, ref structInput, SizeOfInput);
Thread.Sleep(100);
if (doubleClick)
{
NativeMethods.SendInput(1, ref structInput, SizeOfInput);
Thread.Sleep(100);
}
}
finally
{
if (restore)
{
try
{
if (restoreCondition != null)
{
while (!restoreCondition())
Thread.Sleep(1);
}
else
{
Thread.Sleep(100);
}
RestoreForegroundWindowAndMouse();
}
catch
{
}
}
NativeMethods.BlockInput(false);
}
}
/// <summary>
/// Sleeps until condition becomes true or after timeout has been reached.
/// </summary>
/// <param name="condition">The until condition.</param>
/// <param name="maxSleepTime">The max sleep time.</param>
/// <returns>returns true if condition returned true before reaching timeout</returns>
public static bool SleepUntil(Func<bool> condition, TimeSpan maxSleepTime)
{
var sleepStart = DateTime.Now;
var timeOut = false;
while (!condition() && (timeOut = DateTime.Now - sleepStart >= maxSleepTime) == false)
Thread.Sleep(10);
return !timeOut;
}
private static IntPtr _originalForegroundWindow;
private static Point _originalMousePos;
public static void SaveForegroundWindowAndMouse()
{
_originalForegroundWindow = NativeMethods.GetForegroundWindow();
NativeMethods.GetCursorPos(out _originalMousePos);
}
public static void RestoreForegroundWindowAndMouse()
{
var forefroundWindow = NativeMethods.GetForegroundWindow();
for (var num = 0; forefroundWindow != _originalForegroundWindow && num < 1000; num++)
{
if (!NativeMethods.SetForegroundWindow(_originalForegroundWindow))
break;
Thread.Sleep(1);
forefroundWindow = NativeMethods.GetForegroundWindow();
}
var structInput = new NativeMethods.Input {type = NativeMethods.SendInputEventType.InputMouse};
double fScreenWidth = NativeMethods.GetSystemMetrics(NativeMethods.SystemMetric.SM_CXSCREEN) - 1;
double fScreenHeight = NativeMethods.GetSystemMetrics(NativeMethods.SystemMetric.SM_CYSCREEN) - 1;
structInput.mkhi.mi.dwFlags = NativeMethods.MouseEventFlags.ABSOLUTE | NativeMethods.MouseEventFlags.MOVE;
structInput.mkhi.mi.dx = (int) (_originalMousePos.X*(65535.0f/fScreenWidth));
structInput.mkhi.mi.dy = (int) (_originalMousePos.Y*(65535.0f/fScreenHeight));
NativeMethods.SendInput(1, ref structInput, SizeOfInput);
}
#endregion
public static async Task CloseBotProcessAsync(Process proc, CharacterProfile profile)
{
var procName = proc.ProcessName;
profile.Log("Attempting to close {0}", procName);
proc.CloseMainWindow();
if (await WaitForProcessToExitAsync(proc, TimeSpan.FromSeconds(10)))
{
profile.Log("Successfully closed {0} gracefully", procName);
return;
}
profile.Log("Killing {0}", procName);
proc.Kill();
}
private static async Task<bool> WaitForProcessToExitAsync(Process process, TimeSpan waitTimespan)
{
var sw = Stopwatch.StartNew();
while (sw.Elapsed < waitTimespan)
{
if (process.HasExitedSafe())
return true;
await Task.Delay(100);
}
return false;
}
}
}