├── .gitattributes ├── .gitignore ├── .gitmodules ├── Examples ├── ParentTokenId │ ├── ParentTokenId.dpr │ └── ParentTokenId.dproj └── Readme.md ├── Headers └── KernelBridgeApi.pas ├── KernelBridge.Memory.Mdl.pas ├── KernelBridge.Memory.pas ├── KernelBridge.Processes.Memory.pas ├── KernelBridge.Processes.pas ├── KernelBridge.Section.pas ├── KernelBridge.Threads.pas ├── KernelBridge.pas ├── LICENSE.txt └── Readme.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Uncomment these types if you want even more clean repository. But be careful. 2 | # It can make harm to an existing project source. Read explanations below. 3 | # 4 | # Resource files are binaries containing manifest, project icon and version info. 5 | # They can not be viewed as text or compared by diff-tools. Consider replacing them with .rc files. 6 | *.res 7 | # 8 | # Type library file (binary). In old Delphi versions it should be stored. 9 | # Since Delphi 2009 it is produced from .ridl file and can safely be ignored. 10 | #*.tlb 11 | # 12 | # Diagram Portfolio file. Used by the diagram editor up to Delphi 7. 13 | # Uncomment this if you are not using diagrams or use newer Delphi version. 14 | #*.ddp 15 | # 16 | # Visual LiveBindings file. Added in Delphi XE2. 17 | # Uncomment this if you are not using LiveBindings Designer. 18 | #*.vlb 19 | # 20 | # Deployment Manager configuration file for your project. Added in Delphi XE2. 21 | # Uncomment this if it is not mobile development and you do not use remote debug feature. 22 | #*.deployproj 23 | # 24 | # C++ object files produced when C/C++ Output file generation is configured. 25 | # Uncomment this if you are not using external objects (zlib library for example). 26 | #*.obj 27 | # 28 | 29 | # Delphi compiler-generated binaries (safe to delete) 30 | *.exe 31 | *.dll 32 | *.bpl 33 | *.bpi 34 | *.dcp 35 | *.so 36 | *.apk 37 | *.drc 38 | *.map 39 | *.dres 40 | *.rsm 41 | *.tds 42 | *.dcu 43 | *.lib 44 | *.a 45 | *.o 46 | *.ocx 47 | 48 | # Delphi autogenerated files (duplicated info) 49 | *.cfg 50 | *.hpp 51 | *Resource.rc 52 | 53 | # Delphi local files (user-specific info) 54 | *.local 55 | *.identcache 56 | *.projdata 57 | *.tvsconfig 58 | *.dsk 59 | 60 | # Delphi history and backups 61 | __history/ 62 | __recovery/ 63 | *.~* 64 | 65 | # Castalia statistics file (since XE7 Castalia is distributed with Delphi) 66 | *.stat 67 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "NtUtils"] 2 | path = NtUtils 3 | url = https://github.com/diversenok/NtUtilsLibrary 4 | -------------------------------------------------------------------------------- /Examples/ParentTokenId/ParentTokenId.dpr: -------------------------------------------------------------------------------- 1 | program ParentTokenId; 2 | 3 | {$APPTYPE CONSOLE} 4 | 5 | {$R *.res} 6 | 7 | uses 8 | Winapi.WinNt, 9 | Ntapi.ntstatus, 10 | Ntapi.ntpsapi, 11 | Ntapi.ntseapi, 12 | NtUtils, 13 | NtUtils.Tokens, 14 | NtUtils.Tokens.Query, 15 | NtUtils.Objects, 16 | NtUtils.Objects.Snapshots, 17 | NtUtils.SysUtils, 18 | DelphiUiLib.Strings, 19 | NtUiLib.Errors, 20 | KernelBridgeApi, 21 | KernelBridge, 22 | KernelBridge.Processes, 23 | KernelBridge.Threads, 24 | KernelBridge.Memory; 25 | 26 | // Determine the kernel address of an object's body 27 | function GetObjectAddress( 28 | hObject: THandle; 29 | out Address: Pointer 30 | ): TNtxStatus; 31 | var 32 | Handles: TArray; 33 | HandleEntry: TSystemHandleEntry; 34 | begin 35 | // Snapshot all handles on the system 36 | Result := NtxEnumerateHandles(Handles); 37 | 38 | if not Result.IsSuccess then 39 | Exit; 40 | 41 | // Find the entry for the specified handle 42 | Result := NtxFindHandleEntry(Handles, NtCurrentProcessId, hObject, 43 | HandleEntry); 44 | 45 | if Result.IsSuccess then 46 | Address := HandleEntry.PObject; 47 | end; 48 | 49 | // Determine the offset of the ParentTokenId field in KTOKEN 50 | function GetParentTokenIdOffset(out Offset: Cardinal): TNtxStatus; 51 | var 52 | Statistics: TTokenStatistics; 53 | hxToken: IHandle; 54 | Address: Pointer; 55 | Buffer: array [0..63] of Cardinal; 56 | i: Integer; 57 | begin 58 | // Figure out our token's ID 59 | Result := NtxToken.Query(NtCurrentProcessToken, TokenStatistics, Statistics); 60 | 61 | if not Result.IsSuccess then 62 | Exit; 63 | 64 | // Create a child token with a known parent 65 | Result := NtxFilterToken(hxToken, NtCurrentProcessToken, 0); 66 | 67 | if not Result.IsSuccess then 68 | Exit; 69 | 70 | // Determine the address of the object in kernel memory 71 | Result := GetObjectAddress(hxToken.Handle, Address); 72 | 73 | if not Result.IsSuccess then 74 | Exit; 75 | 76 | // Read the beggining of the structure 77 | Result := KbxMemory.Read(Address, Buffer); 78 | 79 | if not Result.IsSuccess then 80 | Exit; 81 | 82 | // Search for the offset with a matching value 83 | for i := 0 to Pred(High(Buffer)) do 84 | if PLuid(@Buffer[i])^ = Statistics.TokenId then 85 | begin 86 | Offset := i * SizeOf(Cardinal); 87 | Result.Status := STATUS_SUCCESS; 88 | Exit; 89 | end; 90 | 91 | Result.Location := 'GetParentTokenIdOffset'; 92 | Result.Status := STATUS_NOT_FOUND; 93 | end; 94 | 95 | // Read the ID of the parent token from kernel memory 96 | function QueryParentTokenId( 97 | out ParentTokenId: TLuid; 98 | hToken: THandle 99 | ): TNtxStatus; 100 | var 101 | Address: Pointer; 102 | Offset: Cardinal; 103 | begin 104 | // Determine the address of the kernel object 105 | Result := GetObjectAddress(hToken, Address); 106 | 107 | if not Result.IsSuccess then 108 | Exit; 109 | 110 | // Determine the offset for the Parent Token ID field 111 | Result := GetParentTokenIdOffset(Offset); 112 | 113 | if not Result.IsSuccess then 114 | Exit; 115 | 116 | // Read its content 117 | Result := KbxMemory.Read(PByte(Address) + Offset, ParentTokenId); 118 | end; 119 | 120 | // Ask a user for a process ID and open its token 121 | function GetProcessToken(out hxToken: IHandle): TNtxStatus; 122 | var 123 | PID: TProcessId32; 124 | hxProcess: IHandle; 125 | begin 126 | write('PID: '); 127 | readln(PID); 128 | 129 | // Use Kernel Bridge to open the process 130 | Result := KbxOpenProcess(hxProcess, PID, PROCESS_QUERY_LIMITED_INFORMATION); 131 | 132 | if not Result.IsSuccess then 133 | Exit; 134 | 135 | // Get the token 136 | Result := NtxOpenProcessToken(hxToken, hxProcess.Handle, MAXIMUM_ALLOWED); 137 | end; 138 | 139 | // Ask a user for a thread ID and open its token 140 | function GetThreadToken(out hxToken: IHandle): TNtxStatus; 141 | var 142 | TID: TProcessId32; 143 | hxThread: IHandle; 144 | begin 145 | write('TID: '); 146 | readln(TID); 147 | 148 | // Use Kernel Bridge to open the thread 149 | Result := KbxOpenThread(hxThread, TID, THREAD_QUERY_LIMITED_INFORMATION); 150 | 151 | if not Result.IsSuccess then 152 | Exit; 153 | 154 | // Get the token 155 | Result := NtxOpenThreadToken(hxToken, hxThread.Handle, MAXIMUM_ALLOWED); 156 | end; 157 | 158 | // Copy a token handle from a process 159 | function CopyTokenFrom(out hxToken: IHandle): TNtxStatus; 160 | var 161 | PID: TProcessId32; 162 | HandleValue: THandle; 163 | hxProcess: IHandle; 164 | TokenTypeIndex: Integer; 165 | TypeInfo: TObjectTypeInfo; 166 | begin 167 | write('PID: '); 168 | readln(PID); 169 | write('Handle value: '); 170 | readln(HandleValue); 171 | 172 | // Use Kernel Bridge to open the process 173 | Result := KbxOpenProcess(hxProcess, PID, PROCESS_DUP_HANDLE); 174 | 175 | if not Result.IsSuccess then 176 | Exit; 177 | 178 | // Duplicate the handle 179 | Result := NtxDuplicateHandleFrom(hxProcess.Handle, HandleValue, hxToken); 180 | 181 | if not Result.IsSuccess then 182 | Exit; 183 | 184 | // Determine the index of the Token object type 185 | Result := NtxFindType('Token', TokenTypeIndex); 186 | 187 | if not Result.IsSuccess then 188 | Exit; 189 | 190 | // Determine the type of the object we got 191 | Result := NtxQueryTypeObject(hxToken.Handle, TypeInfo); 192 | 193 | if not Result.IsSuccess then 194 | Exit; 195 | 196 | if TypeInfo.Other.TypeIndex <> TokenTypeIndex then 197 | begin 198 | // This is not a token 199 | Result.Location := 'CopyTokenFrom'; 200 | Result.Status := STATUS_OBJECT_TYPE_MISMATCH; 201 | end; 202 | end; 203 | 204 | function Main: TNtxStatus; 205 | var 206 | Driver: IAutoReleasable; 207 | Option: Cardinal; 208 | hxToken: IHandle; 209 | ParentId: TLuid; 210 | begin 211 | writeln('Example program for determining parent token IDs via Kernel Bridge'); 212 | 213 | Result := KbxLoadAsDriver(Driver, RtlxExtractPath(ParamStr(0)) + '\' + 214 | KernelBridgeSys); 215 | 216 | if not Result.IsSuccess then 217 | Exit; 218 | 219 | write('1 - open process token, 2 - open thread token, 3 - copy handle: '); 220 | readln(Option); 221 | 222 | case Option of 223 | 1: Result := GetProcessToken(hxToken); 224 | 2: Result := GetThreadToken(hxToken); 225 | 3: Result := CopyTokenFrom(hxToken); 226 | else 227 | Result.Location := 'Main'; 228 | Result.Status := STATUS_INVALID_PARAMETER; 229 | end; 230 | 231 | if not Result.IsSuccess then 232 | Exit; 233 | 234 | // See the main logic in ParentTokenId.Helper.pas 235 | Result := QueryParentTokenId(ParentId, hxToken.Handle); 236 | 237 | if not Result.IsSuccess then 238 | Exit; 239 | 240 | writeln('Parent Token ID = ', IntToHexEx(ParentId, 8)); 241 | end; 242 | 243 | function Report(const Status: TNtxStatus): String; 244 | begin 245 | if Status.IsSuccess then 246 | Result := 'Success' 247 | else 248 | Result := Status.Location + ': ' + RtlxNtStatusName(Status); 249 | end; 250 | 251 | begin 252 | writeln(Report(Main)); 253 | readln; 254 | end. 255 | -------------------------------------------------------------------------------- /Examples/ParentTokenId/ParentTokenId.dproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | {2FA301B6-CD9F-4DB3-B42F-758264C65E14} 4 | 18.8 5 | None 6 | ParentTokenId.dpr 7 | True 8 | Release 9 | Win64 10 | 3 11 | Console 12 | 13 | 14 | true 15 | 16 | 17 | true 18 | Base 19 | true 20 | 21 | 22 | true 23 | Base 24 | true 25 | 26 | 27 | true 28 | Base 29 | true 30 | 31 | 32 | true 33 | Cfg_1 34 | true 35 | true 36 | 37 | 38 | true 39 | Cfg_1 40 | true 41 | true 42 | 43 | 44 | true 45 | Base 46 | true 47 | 48 | 49 | true 50 | Cfg_2 51 | true 52 | true 53 | 54 | 55 | .\$(Platform)\$(Config) 56 | .\$(Platform)\$(Config) 57 | false 58 | false 59 | false 60 | false 61 | false 62 | System;Xml;Data;Datasnap;Web;Soap;$(DCC_Namespace) 63 | ParentTokenId 64 | ..\..\NtUtils;..\..\NtUtils\Headers;..\..\Headers;..\..;$(DCC_UnitSearchPath) 65 | 1049 66 | CompanyName=;FileDescription=An example application in Delphi for using the Kernel-Bridge framework.;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= 67 | 68 | 69 | DBXSqliteDriver;IndyIPCommon;RESTComponents;bindcompdbx;DBXInterBaseDriver;vcl;IndyIPServer;vclactnband;vclFireDAC;IndySystem;tethering;svnui;dsnapcon;FireDACADSDriver;FireDACMSAccDriver;fmxFireDAC;vclimg;FireDAC;vcltouch;vcldb;bindcompfmx;svn;FireDACSqliteDriver;FireDACPgDriver;inetdb;soaprtl;DbxCommonDriver;fmx;FireDACIBDriver;fmxdae;xmlrtl;soapmidas;fmxobj;vclwinx;rtl;DbxClientDriver;CustomIPTransport;vcldsnap;dbexpress;IndyCore;vclx;bindcomp;appanalytics;dsnap;FireDACCommon;IndyIPClient;bindcompvcl;RESTBackendComponents;VCLRESTComponents;soapserver;dbxcds;VclSmp;adortl;vclie;bindengine;DBXMySQLDriver;CloudService;dsnapxml;FireDACMySQLDriver;dbrtl;IndyProtocols;inetdbxpress;FireDACCommonODBC;FireDACCommonDriver;inet;fmxase;$(DCC_UsePackage) 70 | Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace) 71 | Debug 72 | CompanyName=;FileDescription=$(MSBuildProjectName);FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= 73 | 1033 74 | true 75 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png 76 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png 77 | 78 | 79 | DBXSqliteDriver;IndyIPCommon;RESTComponents;bindcompdbx;DBXInterBaseDriver;vcl;IndyIPServer;vclactnband;vclFireDAC;IndySystem;tethering;dsnapcon;FireDACADSDriver;FireDACMSAccDriver;fmxFireDAC;vclimg;FireDAC;vcltouch;vcldb;bindcompfmx;FireDACSqliteDriver;FireDACPgDriver;inetdb;soaprtl;DbxCommonDriver;fmx;FireDACIBDriver;fmxdae;xmlrtl;soapmidas;fmxobj;vclwinx;rtl;DbxClientDriver;CustomIPTransport;vcldsnap;dbexpress;IndyCore;vclx;bindcomp;appanalytics;dsnap;FireDACCommon;IndyIPClient;bindcompvcl;RESTBackendComponents;VCLRESTComponents;soapserver;dbxcds;VclSmp;adortl;vclie;bindengine;DBXMySQLDriver;CloudService;dsnapxml;FireDACMySQLDriver;dbrtl;IndyProtocols;inetdbxpress;FireDACCommonODBC;FireDACCommonDriver;inet;fmxase;$(DCC_UsePackage) 80 | true 81 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_44.png 82 | $(BDS)\bin\Artwork\Windows\UWP\delphi_UwpDefault_150.png 83 | Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;$(DCC_Namespace) 84 | Debug 85 | CompanyName=;FileDescription=An example application in Delphi for using the Kernel-Bridge framework.;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProgramID=com.embarcadero.$(MSBuildProjectName);ProductName=$(MSBuildProjectName);ProductVersion=1.0.0.0;Comments= 86 | 1033 87 | true 88 | (None) 89 | 90 | 91 | DEBUG;$(DCC_Define) 92 | true 93 | false 94 | true 95 | true 96 | true 97 | 98 | 99 | false 100 | 101 | 102 | 1033 103 | (None) 104 | 105 | 106 | false 107 | RELEASE;$(DCC_Define) 108 | 0 109 | 0 110 | 111 | 112 | true 113 | 1033 114 | 115 | 116 | 117 | MainSource 118 | 119 | 120 | Cfg_2 121 | Base 122 | 123 | 124 | Base 125 | 126 | 127 | Cfg_1 128 | Base 129 | 130 | 131 | 132 | Delphi.Personality.12 133 | Application 134 | 135 | 136 | 137 | ParentTokenId.dpr 138 | 139 | 140 | Microsoft Office 2000 Sample Automation Server Wrapper Components 141 | Microsoft Office XP Sample Automation Server Wrapper Components 142 | 143 | 144 | 145 | 146 | 147 | ParentTokenId.exe 148 | true 149 | 150 | 151 | 152 | 153 | true 154 | 155 | 156 | 157 | 158 | true 159 | 160 | 161 | 162 | 163 | true 164 | 165 | 166 | 167 | 168 | ParentTokenId.exe 169 | true 170 | 171 | 172 | 173 | 174 | 1 175 | 176 | 177 | Contents\MacOS 178 | 1 179 | 180 | 181 | 0 182 | 183 | 184 | 185 | 186 | classes 187 | 1 188 | 189 | 190 | classes 191 | 1 192 | 193 | 194 | 195 | 196 | res\xml 197 | 1 198 | 199 | 200 | res\xml 201 | 1 202 | 203 | 204 | 205 | 206 | library\lib\armeabi-v7a 207 | 1 208 | 209 | 210 | 211 | 212 | library\lib\armeabi 213 | 1 214 | 215 | 216 | library\lib\armeabi 217 | 1 218 | 219 | 220 | 221 | 222 | library\lib\armeabi-v7a 223 | 1 224 | 225 | 226 | 227 | 228 | library\lib\mips 229 | 1 230 | 231 | 232 | library\lib\mips 233 | 1 234 | 235 | 236 | 237 | 238 | library\lib\armeabi-v7a 239 | 1 240 | 241 | 242 | library\lib\arm64-v8a 243 | 1 244 | 245 | 246 | 247 | 248 | library\lib\armeabi-v7a 249 | 1 250 | 251 | 252 | 253 | 254 | res\drawable 255 | 1 256 | 257 | 258 | res\drawable 259 | 1 260 | 261 | 262 | 263 | 264 | res\values 265 | 1 266 | 267 | 268 | res\values 269 | 1 270 | 271 | 272 | 273 | 274 | res\values-v21 275 | 1 276 | 277 | 278 | res\values-v21 279 | 1 280 | 281 | 282 | 283 | 284 | res\values 285 | 1 286 | 287 | 288 | res\values 289 | 1 290 | 291 | 292 | 293 | 294 | res\drawable 295 | 1 296 | 297 | 298 | res\drawable 299 | 1 300 | 301 | 302 | 303 | 304 | res\drawable-xxhdpi 305 | 1 306 | 307 | 308 | res\drawable-xxhdpi 309 | 1 310 | 311 | 312 | 313 | 314 | res\drawable-ldpi 315 | 1 316 | 317 | 318 | res\drawable-ldpi 319 | 1 320 | 321 | 322 | 323 | 324 | res\drawable-mdpi 325 | 1 326 | 327 | 328 | res\drawable-mdpi 329 | 1 330 | 331 | 332 | 333 | 334 | res\drawable-hdpi 335 | 1 336 | 337 | 338 | res\drawable-hdpi 339 | 1 340 | 341 | 342 | 343 | 344 | res\drawable-xhdpi 345 | 1 346 | 347 | 348 | res\drawable-xhdpi 349 | 1 350 | 351 | 352 | 353 | 354 | res\drawable-mdpi 355 | 1 356 | 357 | 358 | res\drawable-mdpi 359 | 1 360 | 361 | 362 | 363 | 364 | res\drawable-hdpi 365 | 1 366 | 367 | 368 | res\drawable-hdpi 369 | 1 370 | 371 | 372 | 373 | 374 | res\drawable-xhdpi 375 | 1 376 | 377 | 378 | res\drawable-xhdpi 379 | 1 380 | 381 | 382 | 383 | 384 | res\drawable-xxhdpi 385 | 1 386 | 387 | 388 | res\drawable-xxhdpi 389 | 1 390 | 391 | 392 | 393 | 394 | res\drawable-xxxhdpi 395 | 1 396 | 397 | 398 | res\drawable-xxxhdpi 399 | 1 400 | 401 | 402 | 403 | 404 | res\drawable-small 405 | 1 406 | 407 | 408 | res\drawable-small 409 | 1 410 | 411 | 412 | 413 | 414 | res\drawable-normal 415 | 1 416 | 417 | 418 | res\drawable-normal 419 | 1 420 | 421 | 422 | 423 | 424 | res\drawable-large 425 | 1 426 | 427 | 428 | res\drawable-large 429 | 1 430 | 431 | 432 | 433 | 434 | res\drawable-xlarge 435 | 1 436 | 437 | 438 | res\drawable-xlarge 439 | 1 440 | 441 | 442 | 443 | 444 | res\values 445 | 1 446 | 447 | 448 | res\values 449 | 1 450 | 451 | 452 | 453 | 454 | 1 455 | 456 | 457 | Contents\MacOS 458 | 1 459 | 460 | 461 | 0 462 | 463 | 464 | 465 | 466 | Contents\MacOS 467 | 1 468 | .framework 469 | 470 | 471 | Contents\MacOS 472 | 1 473 | .framework 474 | 475 | 476 | 0 477 | 478 | 479 | 480 | 481 | 1 482 | .dylib 483 | 484 | 485 | 1 486 | .dylib 487 | 488 | 489 | 1 490 | .dylib 491 | 492 | 493 | Contents\MacOS 494 | 1 495 | .dylib 496 | 497 | 498 | Contents\MacOS 499 | 1 500 | .dylib 501 | 502 | 503 | 0 504 | .dll;.bpl 505 | 506 | 507 | 508 | 509 | 1 510 | .dylib 511 | 512 | 513 | 1 514 | .dylib 515 | 516 | 517 | 1 518 | .dylib 519 | 520 | 521 | Contents\MacOS 522 | 1 523 | .dylib 524 | 525 | 526 | Contents\MacOS 527 | 1 528 | .dylib 529 | 530 | 531 | 0 532 | .bpl 533 | 534 | 535 | 536 | 537 | 0 538 | 539 | 540 | 0 541 | 542 | 543 | 0 544 | 545 | 546 | 0 547 | 548 | 549 | 0 550 | 551 | 552 | Contents\Resources\StartUp\ 553 | 0 554 | 555 | 556 | Contents\Resources\StartUp\ 557 | 0 558 | 559 | 560 | 0 561 | 562 | 563 | 564 | 565 | 1 566 | 567 | 568 | 1 569 | 570 | 571 | 1 572 | 573 | 574 | 575 | 576 | 1 577 | 578 | 579 | 1 580 | 581 | 582 | 1 583 | 584 | 585 | 586 | 587 | 1 588 | 589 | 590 | 1 591 | 592 | 593 | 1 594 | 595 | 596 | 597 | 598 | 1 599 | 600 | 601 | 1 602 | 603 | 604 | 1 605 | 606 | 607 | 608 | 609 | 1 610 | 611 | 612 | 1 613 | 614 | 615 | 1 616 | 617 | 618 | 619 | 620 | 1 621 | 622 | 623 | 1 624 | 625 | 626 | 1 627 | 628 | 629 | 630 | 631 | 1 632 | 633 | 634 | 1 635 | 636 | 637 | 1 638 | 639 | 640 | 641 | 642 | 1 643 | 644 | 645 | 1 646 | 647 | 648 | 1 649 | 650 | 651 | 652 | 653 | 1 654 | 655 | 656 | 1 657 | 658 | 659 | 1 660 | 661 | 662 | 663 | 664 | 1 665 | 666 | 667 | 1 668 | 669 | 670 | 1 671 | 672 | 673 | 674 | 675 | 1 676 | 677 | 678 | 1 679 | 680 | 681 | 1 682 | 683 | 684 | 685 | 686 | 1 687 | 688 | 689 | 1 690 | 691 | 692 | 1 693 | 694 | 695 | 696 | 697 | 1 698 | 699 | 700 | 1 701 | 702 | 703 | 1 704 | 705 | 706 | 707 | 708 | 1 709 | 710 | 711 | 1 712 | 713 | 714 | 1 715 | 716 | 717 | 718 | 719 | 1 720 | 721 | 722 | 1 723 | 724 | 725 | 1 726 | 727 | 728 | 729 | 730 | 1 731 | 732 | 733 | 1 734 | 735 | 736 | 1 737 | 738 | 739 | 740 | 741 | 1 742 | 743 | 744 | 1 745 | 746 | 747 | 1 748 | 749 | 750 | 751 | 752 | 1 753 | 754 | 755 | 1 756 | 757 | 758 | 1 759 | 760 | 761 | 762 | 763 | 1 764 | 765 | 766 | 1 767 | 768 | 769 | 1 770 | 771 | 772 | 773 | 774 | 1 775 | 776 | 777 | 1 778 | 779 | 780 | 1 781 | 782 | 783 | 784 | 785 | 1 786 | 787 | 788 | 1 789 | 790 | 791 | 1 792 | 793 | 794 | 795 | 796 | 1 797 | 798 | 799 | 1 800 | 801 | 802 | 1 803 | 804 | 805 | 806 | 807 | 1 808 | 809 | 810 | 1 811 | 812 | 813 | 1 814 | 815 | 816 | 817 | 818 | 1 819 | 820 | 821 | 1 822 | 823 | 824 | 1 825 | 826 | 827 | 828 | 829 | 1 830 | 831 | 832 | 1 833 | 834 | 835 | 836 | 837 | ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF 838 | 1 839 | 840 | 841 | ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF 842 | 1 843 | 844 | 845 | 846 | 847 | 1 848 | 849 | 850 | 1 851 | 852 | 853 | 854 | 855 | ..\ 856 | 1 857 | 858 | 859 | ..\ 860 | 1 861 | 862 | 863 | 864 | 865 | 1 866 | 867 | 868 | 1 869 | 870 | 871 | 1 872 | 873 | 874 | 875 | 876 | 1 877 | 878 | 879 | 1 880 | 881 | 882 | 1 883 | 884 | 885 | 886 | 887 | ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF 888 | 1 889 | 890 | 891 | 892 | 893 | ..\ 894 | 1 895 | 896 | 897 | ..\ 898 | 1 899 | 900 | 901 | 902 | 903 | Contents 904 | 1 905 | 906 | 907 | Contents 908 | 1 909 | 910 | 911 | 912 | 913 | Contents\Resources 914 | 1 915 | 916 | 917 | Contents\Resources 918 | 1 919 | 920 | 921 | 922 | 923 | library\lib\armeabi-v7a 924 | 1 925 | 926 | 927 | library\lib\arm64-v8a 928 | 1 929 | 930 | 931 | 1 932 | 933 | 934 | 1 935 | 936 | 937 | 1 938 | 939 | 940 | 1 941 | 942 | 943 | Contents\MacOS 944 | 1 945 | 946 | 947 | Contents\MacOS 948 | 1 949 | 950 | 951 | 0 952 | 953 | 954 | 955 | 956 | library\lib\armeabi-v7a 957 | 1 958 | 959 | 960 | 961 | 962 | 1 963 | 964 | 965 | 1 966 | 967 | 968 | 969 | 970 | Assets 971 | 1 972 | 973 | 974 | Assets 975 | 1 976 | 977 | 978 | 979 | 980 | Assets 981 | 1 982 | 983 | 984 | Assets 985 | 1 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | True 1001 | True 1002 | 1003 | 1004 | 12 1005 | 1006 | 1007 | 1008 | 1009 | 1010 | -------------------------------------------------------------------------------- /Examples/Readme.md: -------------------------------------------------------------------------------- 1 | # Example Projects 2 | 3 | ## ParentTokenId 4 | 5 | The project demonstrates retrieving the content of the `ParentTokenId` field from the `TOKEN` kernel structure. We first need to determine its offset since it can change depending on the version of Windows. We craft a token with a known parent, retrieve the first 256 bytes of the object's body from the kernel memory, and scan it, searching for the known unique pattern. After that, we can read the value from other objects using the same offset. The program also demonstrates opening primary/impersonation tokens and copying handles from other processes. 6 | 7 | Used functions from Kernel Bridge: 8 | - `KbOpenProcess` — via `KbxOpenProcess` 9 | - `KbOpenThread` — via `KbxOpenThread` 10 | - `KbCopyMoveMemory` — via `KbxMemory.Read` 11 | - `KbLoadAsDriver` — via `KbxLoadAsDriver` 12 | - `KbUnload` — used internally by `IAutoReleaseable` 13 | - `KbCloseHandle` — used internally by `IAutoReleaseable` 14 | -------------------------------------------------------------------------------- /Headers/KernelBridgeApi.pas: -------------------------------------------------------------------------------- 1 | unit KernelBridgeApi; 2 | 3 | {$MINENUMSIZE 4} 4 | 5 | interface 6 | 7 | uses 8 | Winapi.WinNt, Ntapi.ntdef, Ntapi.ntpsapi, Ntapi.ntrtl, Ntapi.ntmmapi, 9 | DelphiApi.Reflection; 10 | 11 | const 12 | kernelbridgesys = 'Kernel-Bridge.sys'; 13 | userbridge = 'User-Bridge.dll'; 14 | 15 | type 16 | TCpuidInfo = record 17 | Eax: Cardinal; 18 | Ebx: Cardinal; 19 | Ecx: Cardinal; 20 | Edx: Cardinal; 21 | end; 22 | PCpuidInfo = ^TCpuidInfo; 23 | 24 | PMdl = Pointer; 25 | PEProcess = Pointer; 26 | PEThread = Pointer; 27 | 28 | {$MINENUMSIZE 1} 29 | [NamingStyle(nsCamelCase)] 30 | TProcessorMode = ( 31 | KernelMode = 0, 32 | UserMode = 1 33 | ); 34 | {$MINENUMSIZE 4} 35 | 36 | [NamingStyle(nsCamelCase, 'Io', 'Access')] 37 | TLockOperation = ( 38 | IoReadAccess = 0, 39 | IoWriteAccess = 1, 40 | IoModifyAccess = 2 41 | ); 42 | 43 | TMemoryCachingType = ( 44 | MmNonCached = 0, 45 | MmCached = 1, 46 | MmWriteCombined = 2, 47 | MmHardwareCoherentCached = 3, 48 | MmNonCachedUnordered = 4, 49 | MmUSWCCached = 5, 50 | MmMaximumCacheType = 6, 51 | MmNotMapped = -1 52 | ); 53 | 54 | TMappingInfo = record 55 | MappedAddress: Pointer; 56 | Mdl: PMdl; 57 | end; 58 | PMappingInfo = ^TMappingInfo; 59 | 60 | TUserApcProc = procedure (Argument: Pointer); stdcall; 61 | 62 | // You can obtain any function address from ntoskrnl.exe/hal.dll 63 | TGetKernelProcAddress = function (RoutineName: PWideChar): Pointer; stdcall; 64 | 65 | TShellCode = function ( 66 | GetKernelProcAddress: TGetKernelProcAddress; 67 | Argument: Pointer 68 | ): NTSTATUS; stdcall; 69 | 70 | [NamingStyle(nsCamelCase, 'KbLdr')] 71 | TKbLdrStatus = ( 72 | KbLdrSuccess, 73 | KbLdrImportNotResolved, 74 | KbLdrOrdinalImportNotSupported, 75 | KbLdrKernelMemoryNotAllocated, 76 | KbLdrTransitionFailure, 77 | KbLdrCreationFailure 78 | ); 79 | 80 | { KbLoader } 81 | 82 | function KbLoadAsDriver( 83 | DriverPath: PWideChar 84 | ): LongBool; stdcall; external userbridge; 85 | 86 | function KbLoadAsFilter( 87 | DriverPath: PWideChar; 88 | Altitude: PWideChar 89 | ): LongBool; stdcall; external userbridge; 90 | 91 | function KbUnload: LongBool; stdcall; external userbridge; 92 | 93 | function KbGetDriverApiVersion: Cardinal; stdcall; external userbridge; 94 | 95 | function KbGetUserApiVersion: Cardinal; stdcall; external userbridge; 96 | 97 | function KbGetHandlesCount( 98 | out Count: Cardinal 99 | ): LongBool; stdcall; external userbridge; 100 | 101 | { IO.Beeper } 102 | 103 | function KbSetBeeperRegime: LongBool; stdcall; external userbridge; 104 | function KbStartBeeper: LongBool; stdcall; external userbridge; 105 | function KbStopBeeper: LongBool; stdcall; external userbridge; 106 | function KbSetBeeperIn: LongBool; stdcall; external userbridge; 107 | function KbSetBeeperOut: LongBool; stdcall; external userbridge; 108 | 109 | function KbSetBeeperDivider( 110 | Divider: Word 111 | ): LongBool; stdcall; external userbridge; 112 | 113 | function KbSetBeeperFrequency( 114 | Frequency: Word 115 | ): LongBool; stdcall; external userbridge; 116 | 117 | { IO.RW } 118 | 119 | function KbReadPortByte( 120 | PortNumber: Word; 121 | out Value: Byte 122 | ): LongBool; stdcall; external userbridge; 123 | 124 | function KbReadPortWord( 125 | PortNumber: Word; 126 | out Value: Word 127 | ): LongBool; stdcall; external userbridge; 128 | 129 | function KbReadPortDword( 130 | PortNumber: Word; 131 | out Value: Cardinal 132 | ): LongBool; stdcall; external userbridge; 133 | 134 | function KbReadPortByteString( 135 | PortNumber: Word; 136 | Count: Cardinal; 137 | ByteString: PByte; 138 | ByteStringSizeInBytes: Cardinal 139 | ): LongBool; stdcall; external userbridge; 140 | 141 | function KbReadPortWordString( 142 | PortNumber: Word; 143 | Count: Cardinal; 144 | ByteString: PWord; 145 | WordStringSizeInBytes: Cardinal 146 | ): LongBool; stdcall; external userbridge; 147 | 148 | function KbReadPortDwordString( 149 | PortNumber: Word; 150 | Count: Cardinal; 151 | DwordString: PCardinal; 152 | DwordStringSizeInBytes: Cardinal 153 | ): LongBool; stdcall; external userbridge; 154 | 155 | function KbWritePortByte( 156 | PortNumber: Word; 157 | Value: Byte 158 | ): LongBool; stdcall; external userbridge; 159 | 160 | function KbWritePortWord( 161 | PortNumber: Word; 162 | Value: Word 163 | ): LongBool; stdcall; external userbridge; 164 | 165 | function KbWritePortDword( 166 | PortNumber: Word; 167 | Value: Cardinal 168 | ): LongBool; stdcall; external userbridge; 169 | 170 | function KbWritePortByteString( 171 | PortNumber: Word; 172 | Count: Cardinal; 173 | ByteString: PByte; 174 | ByteStringSizeInBytes: Cardinal 175 | ): LongBool; stdcall; external userbridge; 176 | 177 | function KbWritePortWordString( 178 | PortNumber: Word; 179 | Count: Cardinal; 180 | WordString: PWord; 181 | WordStringSizeInBytes: Cardinal 182 | ): LongBool; stdcall; external userbridge; 183 | 184 | function KbWritePortDwordString( 185 | PortNumber: Word; 186 | Count: Cardinal; 187 | DwordString: PCardinal; 188 | DwordStringSizeInBytes: Cardinal 189 | ): LongBool; stdcall; external userbridge; 190 | 191 | { IO.Iopl } 192 | 193 | // Allows to use 'in/out/cli/sti' in usermode 194 | function KbRaiseIopl: LongBool; stdcall; external userbridge; 195 | function KbResetIopl: LongBool; stdcall; external userbridge; 196 | 197 | { CPU } 198 | 199 | function KbCli: LongBool; stdcall; external userbridge; 200 | function KbSti: LongBool; stdcall; external userbridge; 201 | function KbHlt: LongBool; stdcall; external userbridge; 202 | 203 | function KbReadMsr( 204 | Index: Cardinal; 205 | out MsrValue: UInt64 206 | ): LongBool; stdcall; external userbridge; 207 | 208 | function KbWriteMsr( 209 | Index: Cardinal; 210 | MsrValue: UInt64 211 | ): LongBool; stdcall; external userbridge; 212 | 213 | function KbCpuid( 214 | FunctionIdEax: Cardinal; 215 | out CpuidInfo: TCpuidInfo 216 | ): LongBool; stdcall; external userbridge; 217 | 218 | function KbCpuidEx( 219 | FunctionIdEax: Cardinal; 220 | SubfunctionIdEcx: Cardinal; 221 | out CpuidInfo: TCpuidInfo 222 | ): LongBool; stdcall; external userbridge; 223 | 224 | function KbReadPmc( 225 | Counter: Cardinal; 226 | out PmcValue: UInt64 227 | ): LongBool; stdcall; external userbridge; 228 | 229 | function KbReadTsc( 230 | out TscValue: UInt64 231 | ): LongBool; stdcall; external userbridge; 232 | 233 | function KbReadTscp( 234 | out TscValue: UInt64; 235 | out TscAux: Cardinal 236 | ): LongBool; stdcall; external userbridge; 237 | 238 | { VirtualMemory } 239 | 240 | // Supports both user- and kernel-memory in context of current process 241 | 242 | function KbAllocKernelMemory( 243 | Size: Cardinal; 244 | Executable: Boolean; 245 | out KernelAddress: Pointer 246 | ): LongBool; stdcall; external userbridge; 247 | 248 | function KbFreeKernelMemory( 249 | KernelAddress: Pointer 250 | ): LongBool; stdcall; external userbridge; 251 | 252 | function KbAllocNonCachedMemory( 253 | Size: Cardinal; 254 | out KernelAddress: Pointer 255 | ): LongBool; stdcall; external userbridge; 256 | 257 | function KbFreeNonCachedMemory( 258 | KernelAddress: Pointer; 259 | Size: Cardinal 260 | ): LongBool; stdcall; external userbridge; 261 | 262 | function KbCopyMoveMemory( 263 | Dest: Pointer; 264 | Src: Pointer; 265 | Size: Cardinal; 266 | Intersects: Boolean 267 | ): LongBool; stdcall; external userbridge; 268 | 269 | function KbFillMemory( 270 | Address: Pointer; 271 | Filler: Byte; 272 | Size: Cardinal 273 | ): LongBool; stdcall; external userbridge; 274 | 275 | function KbEqualMemory( 276 | Src: Pointer; 277 | Dest: Pointer; 278 | Size: Cardinal; 279 | out Equals: Boolean 280 | ): LongBool; stdcall; external userbridge; 281 | 282 | { Mdl } 283 | 284 | function KbAllocateMdl( 285 | VirtualAddress: Pointer; 286 | Size: Cardinal; 287 | out Mdl: PMdl 288 | ): LongBool; stdcall; external userbridge; 289 | 290 | function KbProbeAndLockPages( 291 | ProcessId: TProcessId32; 292 | Mdl: PMdl; 293 | ProcessorMode: TProcessorMode; 294 | LockOperation: TLockOperation 295 | ): LongBool; stdcall; external userbridge; 296 | 297 | function KbMapMdl( 298 | out MappedMemory: Pointer; 299 | SrcProcessId: TProcessId; 300 | DestProcessId: TProcessId; 301 | Mdl: PMdl; 302 | NeedProbeAndLock: Boolean; 303 | MapToAddressSpace: TProcessorMode = UserMode; 304 | Protect: Cardinal = PAGE_READWRITE; 305 | CacheType: TMemoryCachingType = MmNonCached; 306 | UserRequestedAddress: Pointer = nil 307 | ): LongBool; stdcall; external userbridge; 308 | 309 | function KbProtectMappedMemory( 310 | Mdl: PMdl; 311 | Protect: Cardinal 312 | ): LongBool; stdcall; external userbridge; 313 | 314 | function KbUnmapMdl( 315 | Mdl: PMdl; 316 | MappedMemory: Pointer; 317 | NeedUnlock: Boolean 318 | ): LongBool; stdcall; external userbridge; 319 | 320 | function KbUnlockPages( 321 | Mdl: PMdl 322 | ): LongBool; stdcall; external userbridge; 323 | 324 | function KbFreeMdl( 325 | Mdl: PMdl 326 | ): LongBool; stdcall; external userbridge; 327 | 328 | function KbMapMemory( 329 | out MappingInfo: TMappingInfo; 330 | SrcProcessId: TProcessId; 331 | DestProcessId: TProcessId; 332 | VirtualAddress: Pointer; 333 | Size: Cardinal; 334 | MapToAddressSpace: TProcessorMode = UserMode; 335 | Protect: Cardinal = PAGE_READWRITE; 336 | CacheType: TMemoryCachingType = MmNonCached; 337 | UserRequestedAddress: Pointer = nil 338 | ): LongBool; stdcall; external userbridge; 339 | 340 | function KbUnmapMemory( 341 | const MappingInfo: TMappingInfo 342 | ): LongBool; stdcall; external userbridge; 343 | 344 | { PhysicalMemory } 345 | 346 | // Allocates contiguous physical memory in the specified range 347 | function KbAllocPhysicalMemory( 348 | LowestAcceptableAddress: Pointer; 349 | HighestAcceptableAddress: Pointer; 350 | BoundaryAddressMultiple: Pointer; 351 | Size: Cardinal; 352 | CachingType: TMemoryCachingType; 353 | out Address: Pointer 354 | ): LongBool; stdcall; external userbridge; 355 | 356 | // Maps physical memory to a KERNEL address-space; to work with it in usermode, 357 | // map it to usermode by KbMapMemory 358 | function KbFreePhysicalMemory( 359 | Address: Pointer 360 | ): LongBool; stdcall; external userbridge; 361 | 362 | // Maps physical memory to a KERNEL address-space; to work with it in usermode, 363 | // you should map it to usermode by KbMapMemory 364 | function KbMapPhysicalMemory( 365 | PhysicalAddress: Pointer; 366 | Size: Cardinal; 367 | CachingType: TMemoryCachingType; 368 | out VirtualAddress: Pointer 369 | ): LongBool; stdcall; external userbridge; 370 | 371 | function KbUnmapPhysicalMemory( 372 | VirtualAddress: Pointer; 373 | Size: Cardinal 374 | ): LongBool; stdcall; external userbridge; 375 | 376 | // Obtains physical address for specified virtual address in context of target 377 | function KbGetPhysicalAddress( 378 | Process: PEProcess; 379 | VirtualAddress: Pointer; 380 | out PhysicalAddress: Pointer 381 | ): LongBool; stdcall; external userbridge; 382 | 383 | function KbGetVirtualForPhysical( 384 | PhysicalAddress: Pointer; 385 | out VirtualAddress: Pointer 386 | ): LongBool; stdcall; external userbridge; 387 | 388 | // Reads and writes raw physical memory to buffer in context of current process 389 | function KbReadPhysicalMemory( 390 | PhysicalAddress: Pointer; 391 | Buffer: Pointer; 392 | Size: Cardinal; 393 | CachingType: TMemoryCachingType = MmNonCached 394 | ): LongBool; stdcall; external userbridge; 395 | 396 | function KbWritePhysicalMemory( 397 | PhysicalAddress: Pointer; 398 | Buffer: Pointer; 399 | Size: Cardinal; 400 | CachingType: TMemoryCachingType = MmNonCached 401 | ): LongBool; stdcall; external userbridge; 402 | 403 | function KbReadDmiMemory( 404 | DmiMemory: Pointer; 405 | BufferSize: Cardinal 406 | ): LongBool; stdcall; external userbridge; 407 | 408 | { Processes.Descriptors } 409 | 410 | function KbGetEprocess( 411 | ProcessId: TProcessId32; 412 | out Process: PEProcess // dereferece with KbDereferenceObject 413 | ): LongBool; stdcall; external userbridge; 414 | 415 | function KbGetEthread( 416 | ThreadId: TThreadId32; 417 | out Thread: PEThread // dereferece with KbDereferenceObject 418 | ): LongBool; stdcall; external userbridge; 419 | 420 | function KbOpenProcess( 421 | ProcessId: Cardinal; 422 | out hProcess: THandle; // close with KbCloseHandle 423 | Access: TProcessAccessMask = PROCESS_ALL_ACCESS; 424 | Attributes: TObjectAttributesFlags = OBJ_KERNEL_HANDLE 425 | ): LongBool; stdcall; external userbridge; 426 | 427 | function KbOpenProcessByPointer( 428 | Process: PEProcess; 429 | out hProcess: THandle; 430 | Access: TProcessAccessMask = PROCESS_ALL_ACCESS; 431 | Attributes: TObjectAttributesFlags = OBJ_KERNEL_HANDLE; 432 | ProcessorMode: TProcessorMode = KernelMode 433 | ): LongBool; stdcall; external userbridge; 434 | 435 | function KbOpenThread( 436 | ThreadId: TThreadId32; 437 | out hThread: THandle; // close with KbCloseHandle 438 | Access: TThreadAccessMask = THREAD_ALL_ACCESS; 439 | Attributes: TObjectAttributesFlags = OBJ_KERNEL_HANDLE 440 | ): LongBool; stdcall; external userbridge; 441 | 442 | function KbOpenThreadByPointer( 443 | Thread: PEThread; 444 | out hThread: THandle; 445 | Access: TThreadAccessMask = THREAD_ALL_ACCESS; 446 | Attributes: TObjectAttributesFlags = OBJ_KERNEL_HANDLE; 447 | ProcessorMode: TProcessorMode = KernelMode 448 | ): LongBool; stdcall; external userbridge; 449 | 450 | function KbDereferenceObject( 451 | pObject: Pointer 452 | ): LongBool; stdcall; external userbridge; 453 | 454 | function KbCloseHandle( 455 | Handle: THandle 456 | ): LongBool; stdcall; external userbridge; 457 | 458 | { Processes.Information } 459 | 460 | function KbQueryInformationProcess( 461 | hProcess: THandle; 462 | ProcessInfoClass: TProcessInfoClass; 463 | Buffer: Pointer; 464 | Size: Cardinal; 465 | ReturnLength: PCardinal 466 | ): LongBool; stdcall; external userbridge; 467 | 468 | function KbSetInformationProcess( 469 | hProcess: THandle; 470 | ProcessInfoClass: TProcessInfoClass; 471 | Buffer: Pointer; 472 | Size: Cardinal 473 | ): LongBool; stdcall; external userbridge; 474 | 475 | function KbQueryInformationThread( 476 | hThread: THandle; 477 | ThreadInfoClass: TThreadInfoClass; 478 | Buffer: Pointer; 479 | Size: Cardinal; 480 | ReturnLength: PCardinal 481 | ): LongBool; stdcall; external userbridge; 482 | 483 | function KbSetInformationThread( 484 | hThread: THandle; 485 | ThreadInfoClass: TThreadInfoClass; 486 | Buffer: Pointer; 487 | Size: Cardinal 488 | ): LongBool; stdcall; external userbridge; 489 | 490 | { Processes.Threads } 491 | 492 | function KbCreateUserThread( 493 | ProcessId: TProcessId32; 494 | ThreadRoutine: TUserThreadStartRoutine; 495 | Argument: Pointer; 496 | CreateSuspended: LongBool; 497 | ClientId: PClientId; 498 | out hThread: THandle 499 | ): LongBool; stdcall; external userbridge; 500 | 501 | function KbCreateSystemThread( 502 | ProcessId: TProcessId32; 503 | ThreadRoutine: TUserThreadStartRoutine; 504 | Argument: Pointer; 505 | ClientId: PClientId; 506 | out hThread: THandle 507 | ): LongBool; stdcall; external userbridge; 508 | 509 | function KbSuspendProcess( 510 | ProcessId: TProcessId32 511 | ): LongBool; stdcall; external userbridge; 512 | 513 | function KbResumeProcess( 514 | ProcessId: TProcessId32 515 | ): LongBool; stdcall; external userbridge; 516 | 517 | function KbGetThreadContext( 518 | ThreadId: TThreadId32; 519 | Context: PContext; 520 | ContextSize: Cardinal; 521 | ProcessorMode: TProcessorMode = UserMode 522 | ): LongBool; stdcall; external userbridge; 523 | 524 | function KbSetThreadContext( 525 | ThreadId: TThreadId32; 526 | Context: PContext; 527 | ContextSize: Cardinal; 528 | ProcessorMode: TProcessorMode = UserMode 529 | ): LongBool; stdcall; external userbridge; 530 | 531 | { Processes.MemoryManagement } 532 | 533 | function KbAllocUserMemory( 534 | ProcessId: TProcessId32; 535 | Protect: Cardinal; 536 | Size: Cardinal; 537 | out BaseAddress: Pointer 538 | ): LongBool; stdcall; external userbridge; 539 | 540 | function KbFreeUserMemory( 541 | ProcessId: TProcessId32; 542 | BaseAddress: Pointer 543 | ): LongBool; stdcall; external userbridge; 544 | 545 | function KbSecureVirtualMemory( 546 | ProcessId: TProcessId32; 547 | BaseAddress: Pointer; 548 | Size: Cardinal; 549 | ProtectRights: Cardinal; 550 | out SecureHandle: THandle 551 | ): LongBool; stdcall; external userbridge; 552 | 553 | function KbUnsecureVirtualMemory( 554 | ProcessId: TProcessId32; 555 | SecureHandle: THandle 556 | ): LongBool; stdcall; external userbridge; 557 | 558 | function KbReadProcessMemory( 559 | ProcessId: TProcessId32; 560 | BaseAddress: Pointer; 561 | Buffer: Pointer; 562 | Size: Cardinal 563 | ): LongBool; stdcall; external userbridge; 564 | 565 | function KbWriteProcessMemory( 566 | ProcessId: TProcessId32; 567 | BaseAddress: Pointer; 568 | Buffer: Pointer; 569 | Size: Cardinal; 570 | PerformCopyOnWrite: Boolean = True 571 | ): LongBool; stdcall; external userbridge; 572 | 573 | function KbTriggerCopyOnWrite( 574 | ProcessId: TProcessId32; 575 | PageVirtualAddress: Pointer 576 | ): LongBool; stdcall; external userbridge; 577 | 578 | function KbGetProcessCr3Cr4( 579 | ProcessId: TProcessId32; 580 | Cr3: PUInt64; 581 | Cr4: PUInt64 582 | ): LongBool; stdcall; external userbridge; 583 | 584 | { Processes.Apc } 585 | 586 | function KbQueueUserApc( 587 | ThreadId: TThreadId32; 588 | ApcProc: TUserApcProc; 589 | Argument: Pointer 590 | ): LongBool; stdcall; external userbridge; 591 | 592 | { Sections } 593 | 594 | function KbCreateSection( 595 | out hSection: THandle; 596 | Name: PWideChar; 597 | MaximumSize: UInt64; 598 | DesiredAccess: TSectionAccessMask; 599 | SecObjFlags: TObjectAttributesFlags; 600 | SecPageProtection: Cardinal; // SEC_*** 601 | AllocationAttributes: Cardinal; 602 | hFile: THandle 603 | ): LongBool; stdcall; external userbridge; 604 | 605 | function KbOpenSection( 606 | out hSection: THandle; 607 | Name: PWideChar; 608 | DesiredAccess: TSectionAccessMask; 609 | SecObjFlags: TObjectAttributesFlags 610 | ): LongBool; stdcall; external userbridge; 611 | 612 | function KbMapViewOfSection( 613 | hSection: THandle; 614 | hProcess: THandle; 615 | var BaseAddress: Pointer; 616 | CommitSize: Cardinal; 617 | SectionOffset: PUInt64 = nil; 618 | ViewSize: PUInt64 = nil; 619 | SectionInherit: TSectionInherit = ViewUnmap; 620 | AllocationType: Cardinal = MEM_RESERVE; 621 | Win32Protect: Cardinal = PAGE_READWRITE 622 | ): LongBool; stdcall; external userbridge; 623 | 624 | function KbUnmapViewOfSection( 625 | hProcess: THandle; 626 | BaseAddress: Pointer 627 | ): LongBool; stdcall; external userbridge; 628 | 629 | { KernelShells } 630 | 631 | // Execute the specified function in Ring0 in a SEH-section with a FPU-safe 632 | // context in the context of the current process 633 | function KbExecuteShellCode( 634 | ShellCode: TShellCode; 635 | Argument: Pointer; 636 | out Result: NTSTATUS 637 | ): LongBool; stdcall; external userbridge; 638 | 639 | { LoadableModules } 640 | 641 | function KbCreateDriver( 642 | DriverName: PWideChar; 643 | DriverEntry: Pointer 644 | ): LongBool; stdcall; external userbridge; 645 | 646 | function KbLoadModule( 647 | hModule: HMODULE; 648 | ModuleName: PWideChar; 649 | OnLoad: Pointer = nil; 650 | OnUnload: Pointer = nil; 651 | OnDeviceControl: Pointer = nil 652 | ): LongBool; stdcall; external userbridge; 653 | 654 | function KbUnloadModule( 655 | hModule: HMODULE 656 | ): LongBool; stdcall; external userbridge; 657 | 658 | function KbGetModuleHandle( 659 | ModuleName: PWideChar; 660 | out hModule: HMODULE 661 | ): LongBool; stdcall; external userbridge; 662 | 663 | function KbCallModule( 664 | hModule: HMODULE; 665 | CtlCode: Cardinal; 666 | Argument: Pointer = nil 667 | ): LongBool; stdcall; external userbridge; 668 | 669 | { Hypervisor } 670 | 671 | function KbVmmEnable: LongBool; stdcall; external userbridge; 672 | 673 | function KbVmmDisable: LongBool; stdcall; external userbridge; 674 | 675 | function KbVmmInterceptPage( 676 | PhysicalAddress: Pointer; 677 | OnReadPhysicalAddress: Pointer; 678 | OnWritePhysicalAddress: Pointer; 679 | OnExecutePhysicalAddress: Pointer; 680 | OnExecuteReadPhysicalAddress: Pointer; 681 | OnExecuteWritePhysicalAddress: Pointer 682 | ): LongBool; stdcall; external userbridge; 683 | 684 | function KbVmmDeinterceptPage( 685 | PhysicalAddress: Pointer 686 | ): LongBool; stdcall; external userbridge; 687 | 688 | { Stuff } 689 | 690 | function KbGetKernelProcAddress( 691 | RoutineName: PWideChar; 692 | out KernelAddress: Pointer 693 | ): LongBool; stdcall; external userbridge; 694 | 695 | function KbStallExecutionProcessor( 696 | Microseconds: Cardinal 697 | ): LongBool; stdcall; external userbridge; 698 | 699 | function KbBugCheck(Status: NTSTATUS): LongBool; stdcall; external userbridge; 700 | 701 | function KbFindSignature( 702 | ProcessId: TProcessId32; 703 | Memory: Pointer; // Both user and kernel 704 | Size: Cardinal; 705 | Signature: PByte; // "\x11\x22\x33\x00\x44" 706 | Mask: PByte; // "...?." 707 | out FoundAddress: Pointer 708 | ): LongBool; stdcall; external userbridge; 709 | 710 | { Rtl } 711 | 712 | function KbRtlMapDriverMemory( 713 | DriverImage: Pointer; // raw *.sys file data 714 | DriverName: PWideChar // '\Driver\YourDriverName' 715 | ): TKbLdrStatus; stdcall; external userbridge; 716 | 717 | function KbRtlMapDriverFile( 718 | DriverPath: PWideChar; 719 | DriverName: PWideChar 720 | ): TKbLdrStatus; stdcall; external userbridge; 721 | 722 | function KbRtlLoadModuleMemory( 723 | ModuleImage: Pointer; // raw *.sys file data 724 | ModuleName: PWideChar; // custom unique name for the loadable module 725 | out hModule: HMODULE 726 | ): TKbLdrStatus; stdcall; external userbridge; 727 | 728 | function KbRtlLoadModuleFile( 729 | ModulePath: PWideChar; 730 | ModuleName: PWideChar; 731 | out hModule: HMODULE 732 | ): TKbLdrStatus; stdcall; external userbridge; 733 | 734 | implementation 735 | 736 | end. 737 | -------------------------------------------------------------------------------- /KernelBridge.Memory.Mdl.pas: -------------------------------------------------------------------------------- 1 | unit KernelBridge.Memory.Mdl; 2 | 3 | interface 4 | 5 | uses 6 | Winapi.WinNt, Ntapi.ntmmapi, KernelBridgeApi, NtUtils; 7 | 8 | type 9 | IMdl = interface (IAutoReleasable) 10 | function GetMdl: Pointer; 11 | property Mdl: Pointer read GetMdl; 12 | end; 13 | 14 | IMappedMdl = interface (IMemory) 15 | function GetMdl: Pointer; 16 | property Mdl: Pointer read GetMdl; 17 | end; 18 | 19 | // Allocate a Memory Descriptor List for the specified memory region 20 | function KbxAllocateMdl( 21 | out Mdl: IMdl; 22 | VirtualAddress: Pointer; 23 | Size: Cardinal 24 | ): TNtxStatus; 25 | 26 | // Probe and lock pages from a Memory Descriptor List 27 | function KbxProbeAndLockPages( 28 | out Lock: IAutoReleasable; 29 | ProcessId: TProcessId32; 30 | Mdl: IMdl; 31 | LockOperation: TLockOperation; 32 | ProcessorMode: TProcessorMode 33 | ): TNtxStatus; 34 | 35 | // Map a Memory Descriptor List into a process 36 | function KbxMapMdl( 37 | out MappedMemory: IMappedMdl; 38 | SrcProcessId: TProcessId; 39 | DestProcessId: TProcessId; 40 | Mdl: IMdl; 41 | NeedProbeAndLock: Boolean; 42 | MapToAddressSpace: TProcessorMode = UserMode; 43 | Protect: Cardinal = PAGE_READWRITE; 44 | CacheType: TMemoryCachingType = MmNonCached; 45 | UserRequestedAddress: Pointer = nil 46 | ): TNtxStatus; 47 | 48 | // Protect memory mapped for a Memory Descriptor List 49 | function KbxProtectMappedMemory( 50 | Mdl: PMdl; 51 | Protect: Cardinal 52 | ): TNtxStatus; 53 | 54 | // Map memory into a process throught a Memory Descriptor List 55 | function KbxMapMemory( 56 | out MappingMemory: IMappedMdl; 57 | SrcProcessId: TProcessId; 58 | DestProcessId: TProcessId; 59 | VirtualAddress: Pointer; 60 | Size: Cardinal; 61 | MapToAddressSpace: TProcessorMode = UserMode; 62 | Protect: Cardinal = PAGE_READWRITE; 63 | CacheType: TMemoryCachingType = MmNonCached; 64 | UserRequestedAddress: Pointer = nil 65 | ): TNtxStatus; 66 | 67 | implementation 68 | 69 | uses 70 | DelphiUtils.AutoObject; 71 | 72 | type 73 | TKbAutoMdl = class (TCustomAutoReleasable, IMdl) 74 | FMdl: Pointer; 75 | function GetMdl: Pointer; 76 | constructor Capture(pMdl: Pointer); 77 | procedure Release; override; 78 | end; 79 | 80 | TKbAutoMdlLock = class (TCustomAutoReleasable, IAutoReleasable) 81 | FMdl: IMdl; 82 | constructor Create(Mdl: IMdl); 83 | procedure Release; override; 84 | end; 85 | 86 | TKbMappedAutoMdl = class (TCustomAutoMemory, IMappedMdl) 87 | FMdl: IMdl; 88 | FNeedUnlock: Boolean; 89 | function GetMdl: Pointer; 90 | constructor Capture(Mdl: IMdl; MappedAddress: Pointer; NeedUnlock: Boolean); 91 | procedure Release; override; 92 | end; 93 | 94 | TKbMappedAutoMemory = class (TCustomAutoMemory, IMappedMdl) 95 | FMdl: PMdl; 96 | function GetMdl: Pointer; 97 | constructor Capture(MappingInfo: TMappingInfo); 98 | procedure Release; override; 99 | end; 100 | 101 | constructor TKbAutoMdl.Capture; 102 | begin 103 | inherited Create; 104 | FMdl := pMdl; 105 | end; 106 | 107 | procedure TKbAutoMdl.Release; 108 | begin 109 | KbFreeMdl(FMdl); 110 | inherited; 111 | end; 112 | 113 | function TKbAutoMdl.GetMdl; 114 | begin 115 | Result := FMdl; 116 | end; 117 | 118 | constructor TKbAutoMdlLock.Create; 119 | begin 120 | inherited Create; 121 | FMdl := Mdl; 122 | end; 123 | 124 | procedure TKbAutoMdlLock.Release; 125 | begin 126 | KbUnlockPages(FMdl.Mdl); 127 | inherited; 128 | end; 129 | 130 | constructor TKbMappedAutoMdl.Capture; 131 | begin 132 | inherited Capture(MappedAddress, 0); 133 | FMdl := Mdl; 134 | FNeedUnlock := NeedUnlock; 135 | end; 136 | 137 | procedure TKbMappedAutoMdl.Release; 138 | begin 139 | KbUnmapMdl(FMdl.Mdl, FAddress, FNeedUnlock); 140 | inherited; 141 | end; 142 | 143 | function TKbMappedAutoMdl.GetMdl; 144 | begin 145 | Result := FMdl; 146 | end; 147 | 148 | constructor TKbMappedAutoMemory.Capture; 149 | begin 150 | inherited Capture(MappingInfo.MappedAddress, 0); 151 | FMdl := MappingInfo.Mdl; 152 | end; 153 | 154 | procedure TKbMappedAutoMemory.Release; 155 | var 156 | MappingInfo: TMappingInfo; 157 | begin 158 | MappingInfo.MappedAddress := FAddress; 159 | MappingInfo.Mdl := FMdl; 160 | KbUnmapMemory(MappingInfo); 161 | inherited; 162 | end; 163 | 164 | function TKbMappedAutoMemory.GetMdl: Pointer; 165 | begin 166 | Result := FMdl; 167 | end; 168 | 169 | function KbxAllocateMdl; 170 | var 171 | pMdl: Pointer; 172 | begin 173 | Result.Location := 'KbAllocateMdl'; 174 | Result.Win32Result := KbAllocateMdl(VirtualAddress, Size, pMdl); 175 | 176 | if Result.IsSuccess then 177 | Mdl := TKbAutoMdl.Capture(pMdl); 178 | end; 179 | 180 | function KbxProbeAndLockPages; 181 | begin 182 | Result.Location := 'KbProbeAndLockPages'; 183 | Result.Win32Result := KbProbeAndLockPages(ProcessId, Mdl.Mdl, ProcessorMode, 184 | LockOperation); 185 | 186 | if Result.IsSuccess then 187 | Lock := TKbAutoMdlLock.Create(Mdl); 188 | end; 189 | 190 | function KbxMapMdl; 191 | var 192 | Address: Pointer; 193 | begin 194 | Result.Location := 'KbMapMdl'; 195 | Result.Win32Result := KbMapMdl(Address, SrcProcessId, DestProcessId, 196 | Mdl, NeedProbeAndLock, MapToAddressSpace, Protect, CacheType, 197 | UserRequestedAddress); 198 | 199 | if Result.IsSuccess then 200 | MappedMemory := TKbMappedAutoMdl.Capture(Mdl, Address, NeedProbeAndLock); 201 | end; 202 | 203 | function KbxProtectMappedMemory; 204 | begin 205 | Result.Location := 'KbProtectMappedMemory'; 206 | Result.Win32Result := KbProtectMappedMemory(Mdl, Protect); 207 | end; 208 | 209 | function KbxMapMemory; 210 | var 211 | MappingInfo: TMappingInfo; 212 | begin 213 | Result.Location := 'KbMapMemory'; 214 | Result.Win32Result := KbMapMemory(MappingInfo, SrcProcessId, DestProcessId, 215 | VirtualAddress, Size, MapToAddressSpace, Protect, CacheType, 216 | UserRequestedAddress); 217 | 218 | if Result.IsSuccess then 219 | MappingMemory := TKbMappedAutoMemory.Capture(MappingInfo); 220 | end; 221 | 222 | end. 223 | -------------------------------------------------------------------------------- /KernelBridge.Memory.pas: -------------------------------------------------------------------------------- 1 | unit KernelBridge.Memory; 2 | 3 | interface 4 | 5 | uses 6 | Winapi.WinNt, Ntapi.ntmmapi, KernelBridgeApi, NtUtils; 7 | 8 | { ------------------------------ VirtualMemory ------------------------------ } 9 | 10 | // Allocate memory in the kernel space 11 | function KbxAllocKernelMemory( 12 | out Memory: IMemory; 13 | Size: Cardinal; 14 | Executable: Boolean = False 15 | ): TNtxStatus; 16 | 17 | // Allocate non-cached memory in the kernel space 18 | function KbxAllocNonCachedMemory( 19 | out Memory: IMemory; 20 | Size: Cardinal 21 | ): TNtxStatus; 22 | 23 | // Copy user- or kernel- memory in the context of current process 24 | function KbxCopyMoveMemory( 25 | Dest: Pointer; 26 | Src: Pointer; 27 | Size: Cardinal; 28 | Intersects: Boolean = False 29 | ): TNtxStatus; 30 | 31 | // Fill user- or kernel- memory in the context of current process 32 | function KbxFillMemory( 33 | Address: Pointer; 34 | Filler: Byte; 35 | Size: Cardinal 36 | ): TNtxStatus; 37 | 38 | // Compare user- or kernel- memory in the context of the current process 39 | function KbxEqualMemory( 40 | Src: Pointer; 41 | Dest: Pointer; 42 | Size: Cardinal; 43 | out Equals: Boolean 44 | ): TNtxStatus; 45 | 46 | type 47 | KbxMemory = class abstract 48 | // Copy memory from an address to a buffer 49 | class function Read( 50 | Address: Pointer; 51 | out Buffer: T; 52 | Intersects: Boolean = False 53 | ): TNtxStatus; static; 54 | 55 | // Copy memory from a buffer to an address 56 | class function Write( 57 | Address: Pointer; 58 | const Buffer: T; 59 | Intersects: Boolean = False 60 | ): TNtxStatus; static; 61 | end; 62 | 63 | { ----------------------------- Physical Memory ----------------------------- } 64 | 65 | // Allocate contiguous physical memory in the specified range 66 | function KbxAllocPhysicalMemory( 67 | out Memory: IMemory; 68 | LowestAcceptableAddress: Pointer; 69 | HighestAcceptableAddress: Pointer; 70 | BoundaryAddressMultiple: Pointer; 71 | Size: Cardinal; 72 | CachingType: TMemoryCachingType 73 | ): TNtxStatus; 74 | 75 | // Map physical memory to the kernel address space; 76 | // to work with it in user-mode, map it with Mdl.KbxMapMemory 77 | function KbxMapPhysicalMemory( 78 | out VirtualMemory: IMemory; 79 | PhysicalMemory: IMemory; 80 | Size: Cardinal; 81 | CachingType: TMemoryCachingType 82 | ): TNtxStatus; 83 | 84 | // Convert a virtual to a physical address in a context of a process 85 | function KbxGetPhysicalAddress( 86 | out PhysicalAddress: Pointer; 87 | VirtualAddress: Pointer; 88 | Process: PEProcess 89 | ): TNtxStatus; 90 | 91 | // Convery a physica to a virtual address 92 | function KbxGetVirtualForPhysical( 93 | out VirtualAddress: Pointer; 94 | PhysicalAddress: Pointer 95 | ): TNtxStatus; 96 | 97 | // Read content of physical memory into a buffer 98 | function KbxReadPhysicalMemory( 99 | PhysicalAddress: Pointer; 100 | Buffer: Pointer; 101 | Size: Cardinal; 102 | CachingType: TMemoryCachingType = MmNonCached 103 | ): TNtxStatus; 104 | 105 | // Write content of a buffer into physical memory 106 | function KbxWritePhysicalMemory( 107 | PhysicalAddress: Pointer; 108 | Buffer: Pointer; 109 | Size: Cardinal; 110 | CachingType: TMemoryCachingType = MmNonCached 111 | ): TNtxStatus; 112 | 113 | type 114 | KbPhysicalMemory = class abstract 115 | // Read content of physical memory into a buffer 116 | class function Read( 117 | PhysicalAddress: Pointer; 118 | out Buffer: T; 119 | CachingType: TMemoryCachingType = MmNonCached 120 | ): TNtxStatus; static; 121 | 122 | // Write content of a buffer into physical memory 123 | class function Write( 124 | PhysicalAddress: Pointer; 125 | const Buffer: T; 126 | CachingType: TMemoryCachingType = MmNonCached 127 | ): TNtxStatus; static; 128 | end; 129 | 130 | implementation 131 | 132 | uses 133 | DelphiUtils.AutoObject; 134 | 135 | { VirtualMemory } 136 | 137 | type 138 | TKbAutoMemory = class (TCustomAutoMemory, IMemory) 139 | procedure Release; override; 140 | end; 141 | 142 | TKbNonCachedAutoMemory = class (TCustomAutoMemory, IMemory) 143 | procedure Release; override; 144 | end; 145 | 146 | procedure TKbAutoMemory.Release; 147 | begin 148 | KbFreeKernelMemory(FAddress); 149 | inherited; 150 | end; 151 | 152 | procedure TKbNonCachedAutoMemory.Release; 153 | begin 154 | KbFreeNonCachedMemory(FAddress, FSize); 155 | inherited; 156 | end; 157 | 158 | function KbxAllocKernelMemory; 159 | var 160 | Address: Pointer; 161 | begin 162 | Result.Location := 'KbAllocKernelMemory'; 163 | Result.Win32Result := KbAllocKernelMemory(Size, Executable, Address); 164 | 165 | if Result.IsSuccess then 166 | Memory := TKbAutoMemory.Capture(Address, Size); 167 | end; 168 | 169 | function KbxAllocNonCachedMemory; 170 | var 171 | Address: Pointer; 172 | begin 173 | Result.Location := 'KbAllocNonCachedMemory'; 174 | Result.Win32Result := KbAllocNonCachedMemory(Size, Address); 175 | 176 | if Result.IsSuccess then 177 | Memory := TKbNonCachedAutoMemory.Capture(Address, Size); 178 | end; 179 | 180 | function KbxCopyMoveMemory; 181 | begin 182 | Result.Location := 'KbCopyMoveMemory'; 183 | Result.Win32Result := KbCopyMoveMemory(Dest, Src, Size, Intersects); 184 | end; 185 | 186 | function KbxFillMemory; 187 | begin 188 | Result.Location := 'KbFillMemory'; 189 | Result.Win32Result := KbFillMemory(Address, Filler, Size) 190 | end; 191 | 192 | function KbxEqualMemory; 193 | begin 194 | Result.Location := 'KbEqualMemory'; 195 | Result.Win32Result := KbEqualMemory(Src, Dest, Size, Equals); 196 | end; 197 | 198 | class function KbxMemory.Read; 199 | begin 200 | Result.Location := 'KbCopyMoveMemory'; 201 | Result.Win32Result := KbCopyMoveMemory(@Buffer, Address, SizeOf(Buffer), 202 | Intersects); 203 | end; 204 | 205 | class function KbxMemory.Write; 206 | begin 207 | Result.Location := 'KbCopyMoveMemory'; 208 | Result.Win32Result := KbCopyMoveMemory(Address, @Buffer, SizeOf(Buffer), 209 | Intersects); 210 | end; 211 | 212 | { Physical Memory } 213 | 214 | type 215 | TKbPhysicalAutoMemory = class (TCustomAutoMemory, IMemory) 216 | procedure Release; override; 217 | end; 218 | 219 | TKbMappedPhysicalAutoMemory = class (TCustomAutoMemory, IMemory) 220 | procedure Release; override; 221 | end; 222 | 223 | procedure TKbPhysicalAutoMemory.Release; 224 | begin 225 | KbFreePhysicalMemory(FAddress); 226 | inherited; 227 | end; 228 | 229 | procedure TKbMappedPhysicalAutoMemory.Release; 230 | begin 231 | KbUnmapPhysicalMemory(FAddress, FSize); 232 | inherited; 233 | end; 234 | 235 | function KbxAllocPhysicalMemory; 236 | var 237 | Address: Pointer; 238 | begin 239 | Result.Location := 'KbAllocPhysicalMemory'; 240 | Result.Win32Result := KbAllocPhysicalMemory(LowestAcceptableAddress, 241 | HighestAcceptableAddress, BoundaryAddressMultiple, Size, CachingType, 242 | Address); 243 | 244 | if Result.IsSuccess then 245 | Memory := TKbPhysicalAutoMemory.Capture(Address, Size); 246 | end; 247 | 248 | function KbxMapPhysicalMemory; 249 | var 250 | Address: Pointer; 251 | begin 252 | Result.Location := 'KbMapPhysicalMemory'; 253 | Result.Win32Result := KbMapPhysicalMemory(PhysicalMemory.Data, Size, 254 | CachingType, Address); 255 | 256 | if Result.IsSuccess then 257 | VirtualMemory := TKbMappedPhysicalAutoMemory.Capture(Address, Size); 258 | end; 259 | 260 | function KbxGetPhysicalAddress; 261 | begin 262 | Result.Location := 'KbGetPhysicalAddress'; 263 | Result.Win32Result := KbGetPhysicalAddress(Process, VirtualAddress, 264 | PhysicalAddress); 265 | end; 266 | 267 | function KbxGetVirtualForPhysical; 268 | begin 269 | Result.Location := 'KbGetVirtualForPhysical'; 270 | Result.Win32Result := KbGetVirtualForPhysical(PhysicalAddress, 271 | VirtualAddress); 272 | end; 273 | 274 | function KbxReadPhysicalMemory; 275 | begin 276 | Result.Location := 'KbReadPhysicalMemory'; 277 | Result.Win32Result := KbReadPhysicalMemory(PhysicalAddress, Buffer, Size, 278 | CachingType); 279 | end; 280 | 281 | function KbxWritePhysicalMemory; 282 | begin 283 | Result.Location := 'KbWritePhysicalMemory'; 284 | Result.Win32Result := KbWritePhysicalMemory(PhysicalAddress, Buffer, Size, 285 | CachingType); 286 | end; 287 | 288 | class function KbPhysicalMemory.Read; 289 | begin 290 | Result := KbxReadPhysicalMemory(PhysicalAddress, @Buffer, SizeOf(Buffer), 291 | CachingType); 292 | end; 293 | 294 | class function KbPhysicalMemory.Write; 295 | begin 296 | Result := KbxWritePhysicalMemory(PhysicalAddress, @Buffer, SizeOf(Buffer), 297 | CachingType); 298 | end; 299 | 300 | end. 301 | -------------------------------------------------------------------------------- /KernelBridge.Processes.Memory.pas: -------------------------------------------------------------------------------- 1 | unit KernelBridge.Processes.Memory; 2 | 3 | interface 4 | 5 | uses 6 | Winapi.WinNt, Ntapi.ntmmapi, KernelBridgeApi, NtUtils; 7 | 8 | // Allocare user-mode memory in a process 9 | function KbxAllocUserMemory( 10 | out Memory: IMemory; 11 | ProcessId: TProcessId32; 12 | Size: Cardinal; 13 | Protect: Cardinal = PAGE_READWRITE 14 | ): TNtxStatus; 15 | 16 | // Block attempts to change memory protection 17 | function KbxSecureVirtualMemory( 18 | out SecureHandle: IHandle; 19 | ProcessId: TProcessId32; 20 | BaseAddress: Pointer; 21 | Size: Cardinal; 22 | ProtectRights: Cardinal 23 | ): TNtxStatus; 24 | 25 | // Read memory from a process 26 | function KbxReadProcessMemory( 27 | ProcessId: TProcessId32; 28 | BaseAddress: Pointer; 29 | Buffer: Pointer; 30 | Size: Cardinal 31 | ): TNtxStatus; 32 | 33 | // Write memory to a process 34 | function KbxWriteProcessMemory( 35 | ProcessId: TProcessId32; 36 | BaseAddress: Pointer; 37 | Buffer: Pointer; 38 | Size: Cardinal; 39 | PerformCopyOnWrite: Boolean = True 40 | ): TNtxStatus; 41 | 42 | type 43 | KbxProcessMemory = class abstract 44 | // Read memory from a process 45 | class function Read( 46 | ProcessId: TProcessId32; 47 | BaseAddress: Pointer; 48 | out Buffer: T 49 | ): TNtxStatus; static; 50 | 51 | // Write memory to a process 52 | class function Write( 53 | ProcessId: TProcessId32; 54 | BaseAddress: Pointer; 55 | const Buffer: T; 56 | PerformCopyOnWrite: Boolean = True 57 | ): TNtxStatus; static; 58 | end; 59 | 60 | // Trigger Copy-On-Write on a specific page in a process 61 | function KbxTriggerCopyOnWrite( 62 | ProcessId: TProcessId32; 63 | PageVirtualAddress: Pointer 64 | ): TNtxStatus; 65 | 66 | implementation 67 | 68 | uses 69 | DelphiUtils.AutoObject; 70 | 71 | type 72 | TKbAutoUserMemory = class (TCustomAutoMemory, IMemory) 73 | FProcessId: TProcessId32; 74 | procedure Release; override; 75 | constructor Capture(ProcessId: TProcessId32; Address: Pointer; 76 | Size: NativeUInt); 77 | end; 78 | 79 | TKbAutoSecureMemory = class (TCustomAutoHandle, IHandle) 80 | FProcessId: TProcessId32; 81 | procedure Release; override; 82 | constructor Capture(ProcessId: TProcessId32; SecureHandle: THandle); 83 | end; 84 | 85 | constructor TKbAutoUserMemory.Capture(ProcessId: TProcessId32; Address: Pointer; 86 | Size: NativeUInt); 87 | begin 88 | inherited Capture(Address, Size); 89 | FProcessId := ProcessId; 90 | end; 91 | 92 | procedure TKbAutoUserMemory.Release; 93 | begin 94 | KbFreeUserMemory(FProcessId, FAddress); 95 | inherited; 96 | end; 97 | 98 | constructor TKbAutoSecureMemory.Capture(ProcessId: TProcessId32; 99 | SecureHandle: THandle); 100 | begin 101 | inherited Capture(SecureHandle); 102 | FProcessId := ProcessId; 103 | end; 104 | 105 | procedure TKbAutoSecureMemory.Release; 106 | begin 107 | KbUnsecureVirtualMemory(FProcessId, FHandle); 108 | inherited; 109 | end; 110 | 111 | function KbxAllocUserMemory; 112 | var 113 | Address: Pointer; 114 | begin 115 | Result.Location := 'KbAllocUserMemory'; 116 | Result.Win32Result := KbAllocUserMemory(ProcessId, Protect, Size, Address); 117 | 118 | if Result.IsSuccess then 119 | Memory := TKbAutoUserMemory.Capture(ProcessId, Address, Size); 120 | end; 121 | 122 | function KbxSecureVirtualMemory; 123 | var 124 | hSecureHandle: THandle; 125 | begin 126 | Result.Location := 'KbSecureVirtualMemory'; 127 | Result.Win32Result := KbSecureVirtualMemory(ProcessId, BaseAddress, Size, 128 | ProtectRights, hSecureHandle); 129 | 130 | if Result.IsSuccess then 131 | SecureHandle := TKbAutoSecureMemory.Capture(ProcessId, hSecureHandle); 132 | end; 133 | 134 | function KbxReadProcessMemory; 135 | begin 136 | Result.Location := 'KbReadProcessMemory'; 137 | Result.Win32Result := KbReadProcessMemory(ProcessId, BaseAddress, Buffer, 138 | Size); 139 | end; 140 | 141 | function KbxWriteProcessMemory; 142 | begin 143 | Result.Location := 'KbWriteProcessMemory'; 144 | Result.Win32Result := KbWriteProcessMemory(ProcessId, BaseAddress, Buffer, 145 | Size, PerformCopyOnWrite); 146 | end; 147 | 148 | class function KbxProcessMemory.Read; 149 | begin 150 | Result.Location := 'KbReadProcessMemory'; 151 | Result.Win32Result := KbReadProcessMemory(ProcessId, BaseAddress, @Buffer, 152 | SizeOf(Buffer)); 153 | end; 154 | 155 | class function KbxProcessMemory.Write; 156 | begin 157 | Result.Location := 'KbWriteProcessMemory'; 158 | Result.Win32Result := KbWriteProcessMemory(ProcessId, BaseAddress, @Buffer, 159 | SizeOf(Buffer), PerformCopyOnWrite); 160 | end; 161 | 162 | function KbxTriggerCopyOnWrite; 163 | begin 164 | Result.Location := 'KbTriggerCopyOnWrite'; 165 | Result.Win32Result := KbTriggerCopyOnWrite(ProcessId, PageVirtualAddress); 166 | end; 167 | 168 | end. 169 | -------------------------------------------------------------------------------- /KernelBridge.Processes.pas: -------------------------------------------------------------------------------- 1 | unit KernelBridge.Processes; 2 | 3 | interface 4 | 5 | uses 6 | Winapi.WinNt, Ntapi.ntdef, Ntapi.ntpsapi, Ntapi.ntrtl, KernelBridgeApi, 7 | NtUtils; 8 | 9 | // Determine the address of EPROCESS structure for a process 10 | function KbxGetEprocess( 11 | out eProcess: IMemory; 12 | ProcessId: TProcessId32 13 | ): TNtxStatus; 14 | 15 | // Open a process by ID 16 | function KbxOpenProcess( 17 | out hxProcess: IHandle; 18 | ProcessId: TProcessId32; 19 | Access: TProcessAccessMask; 20 | Attributes: TObjectAttributesFlags = 0 21 | ): TNtxStatus; 22 | 23 | // Open a process by a pointer to EPROCESS 24 | function KbxOpenProcessByPointer( 25 | out hxProcess: IHandle; 26 | Address: PEProcess; 27 | Access: TProcessAccessMask; 28 | Attributes: TObjectAttributesFlags = 0; 29 | ProcessorMode: TProcessorMode = KernelMode 30 | ): TNtxStatus; 31 | 32 | // Query variable-size information for a process 33 | function KbxQueryInformationProcess( 34 | hProcess: THandle; 35 | InfoClass: TProcessInfoClass; 36 | out xMemory: IMemory; 37 | InitialBuffer: Cardinal = 0; 38 | GrowthMethod: TBufferGrowthMethod = nil 39 | ): TNtxStatus; 40 | 41 | // Set variable-size information for a process 42 | function KbxSetInformationProcess( 43 | hProcess: THandle; 44 | InfoClass: TProcessInfoClass; 45 | Buffer: Pointer; 46 | Size: Cardinal 47 | ): TNtxStatus; 48 | 49 | type 50 | KbxProcess = class abstract 51 | // Query constant-size information for a process 52 | class function Query( 53 | hProcess: THandle; 54 | InfoClass: TProcessInfoClass; 55 | out Buffer: T 56 | ): TNtxStatus; static; 57 | 58 | // Set constant-size information for a process 59 | class function &Set( 60 | hProcess: THandle; 61 | InfoClass: TProcessInfoClass; 62 | const Buffer: T 63 | ): TNtxStatus; static; 64 | end; 65 | 66 | // Suspend all threads in a process 67 | function KbxSuspendProcess( 68 | ProcessId: TProcessId32 69 | ): TNtxStatus; 70 | 71 | // Resume all threads in a process 72 | function KbxResumeProcess( 73 | ProcessId: TProcessId32 74 | ): TNtxStatus; 75 | 76 | implementation 77 | 78 | uses 79 | KernelBridge, DelphiUtils.AutoObject; 80 | 81 | function KbxGetEprocess; 82 | var 83 | Address: PEProcess; 84 | begin 85 | Result.Location := 'KbGetEprocess'; 86 | Result.Win32Result := KbGetEprocess(ProcessId, Address); 87 | 88 | if Result.IsSuccess then 89 | eProcess := TKbAutoObject.Capture(Address, 0); 90 | end; 91 | 92 | function KbxOpenProcess; 93 | var 94 | hProcess: THandle; 95 | begin 96 | Result.Location := 'KbOpenProcess'; 97 | Result.LastCall.AttachAccess(Access); 98 | Result.Win32Result := KbOpenProcess(ProcessId, hProcess, Access, Attributes); 99 | 100 | if Result.IsSuccess then 101 | hxProcess := TKbAutoHandle.Capture(hProcess); 102 | end; 103 | 104 | function KbxOpenProcessByPointer; 105 | var 106 | hProcess: THandle; 107 | begin 108 | Result.Location := 'KbOpenProcessByPointer'; 109 | Result.LastCall.AttachAccess(Access); 110 | Result.Win32Result := KbOpenProcessByPointer(Address, hProcess, Access, 111 | Attributes, ProcessorMode); 112 | 113 | if Result.IsSuccess then 114 | hxProcess := TKbAutoHandle.Capture(hProcess); 115 | end; 116 | 117 | function KbxQueryInformationProcess; 118 | var 119 | Required: Cardinal; 120 | begin 121 | Result.Location := 'KbQueryInformationProcess'; 122 | Result.LastCall.AttachInfoClass(InfoClass); 123 | 124 | xMemory := TAutoMemory.Allocate(InitialBuffer); 125 | repeat 126 | Required := 0; 127 | Result.Win32Result := KbQueryInformationProcess(hProcess, InfoClass, 128 | xMemory.Data, xMemory.Size, @Required); 129 | until not NtxExpandBufferEx(Result, xMemory, Required, GrowthMethod); 130 | end; 131 | 132 | function KbxSetInformationProcess; 133 | begin 134 | Result.Location := 'KbSetInformationProcess'; 135 | Result.LastCall.AttachInfoClass(InfoClass); 136 | Result.Win32Result := KbSetInformationProcess(hProcess, InfoClass, Buffer, 137 | Size); 138 | end; 139 | 140 | class function KbxProcess.Query; 141 | begin 142 | Result.Location := 'KbQueryInformationProcess'; 143 | Result.LastCall.AttachInfoClass(InfoClass); 144 | Result.Win32Result := KbQueryInformationProcess(hProcess, InfoClass, 145 | @Buffer, SizeOf(Buffer), nil); 146 | end; 147 | 148 | class function KbxProcess.&Set; 149 | begin 150 | Result.Location := 'KbSetInformationProcess'; 151 | Result.LastCall.AttachInfoClass(InfoClass); 152 | Result.Win32Result := KbSetInformationProcess(hProcess, InfoClass, @Buffer, 153 | SizeOf(Buffer)); 154 | end; 155 | 156 | function KbxSuspendProcess; 157 | begin 158 | Result.Location := 'KbSuspendProcess'; 159 | Result.Win32Result := KbSuspendProcess(ProcessId); 160 | end; 161 | 162 | function KbxResumeProcess; 163 | begin 164 | Result.Location := 'KbResumeProcess'; 165 | Result.Win32Result := KbResumeProcess(ProcessId); 166 | end; 167 | 168 | end. 169 | -------------------------------------------------------------------------------- /KernelBridge.Section.pas: -------------------------------------------------------------------------------- 1 | unit KernelBridge.Section; 2 | 3 | interface 4 | 5 | uses 6 | Ntapi.ntdef, Ntapi.ntmmapi, KernelBridgeApi, NtUtils; 7 | 8 | // Create a section object 9 | function KbxCreateSection( 10 | out hxSection: IHandle; 11 | hFile: THandle; 12 | MaximumSize: UInt64; 13 | Name: String = ''; 14 | DesiredAccess: TSectionAccessMask = SECTION_ALL_ACCESS; 15 | SecPageProtection: Cardinal = PAGE_READWRITE; 16 | AllocationAttributes: Cardinal = SEC_COMMIT; 17 | SecObjFlags: TObjectAttributesFlags = 0 18 | ): TNtxStatus; 19 | 20 | // Open a section object by name 21 | function KbxOpenSection( 22 | out hxSection: IHandle; 23 | Name: String; 24 | DesiredAccess: TSectionAccessMask; 25 | SecObjFlags: TObjectAttributesFlags = 0 26 | ): TNtxStatus; 27 | 28 | // Map a section into a memory of a process 29 | function KbxMapViewOfSection( 30 | out MappedSection: IAutoReleasable; 31 | hSection: THandle; 32 | hxProcess: IHandle; 33 | var BaseAddress: Pointer; 34 | CommitSize: Cardinal; 35 | Win32Protect: Cardinal = PAGE_READWRITE; 36 | ViewSize: PUInt64 = nil; 37 | SectionOffset: UInt64 = 0; 38 | AllocationType: Cardinal = MEM_RESERVE; 39 | SectionInherit: TSectionInherit = ViewUnmap 40 | ): TNtxStatus; 41 | 42 | // Forsibly unmap a view of a section from process's address space 43 | function KbxUnmapViewOfSection( 44 | hProcess: THandle; 45 | BaseAddress: Pointer 46 | ): TNtxStatus; 47 | 48 | implementation 49 | 50 | uses 51 | KernelBridge, DelphiUtils.AutoObject; 52 | 53 | function RefStrOrNil(const S: String): PWideChar; 54 | begin 55 | if S <> '' then 56 | Result := PWideChar(S) 57 | else 58 | Result := nil; 59 | end; 60 | 61 | function KbxCreateSection; 62 | var 63 | hSection: THandle; 64 | begin 65 | Result.Location := 'KbCreateSection'; 66 | Result.Win32Result := KbCreateSection(hSection, RefStrOrNil(Name), 67 | MaximumSize, DesiredAccess, SecObjFlags, SecPageProtection, 68 | AllocationAttributes, hFile); 69 | 70 | if Result.IsSuccess then 71 | hxSection := TKbAutoHandle.Capture(hSection); 72 | end; 73 | 74 | function KbxOpenSection; 75 | var 76 | hSection: THandle; 77 | begin 78 | Result.Location := 'KbOpenSection'; 79 | Result.Win32Result := KbOpenSection(hSection, PWideChar(Name), DesiredAccess, 80 | SecObjFlags); 81 | 82 | if Result.IsSuccess then 83 | hxSection := TKbAutoHandle.Capture(hSection); 84 | end; 85 | 86 | type 87 | TKbAutoSection = class (TCustomAutoReleasable, IAutoReleasable) 88 | FBaseAddress: Pointer; 89 | FProcess: IHandle; 90 | procedure Release; override; 91 | constructor Create(hxProcess: IHandle; BaseAddress: Pointer); 92 | end; 93 | 94 | constructor TKbAutoSection.Create; 95 | begin 96 | inherited Create; 97 | FBaseAddress := BaseAddress; 98 | FProcess := hxProcess; 99 | end; 100 | 101 | procedure TKbAutoSection.Release; 102 | begin 103 | if Assigned(FProcess) then 104 | KbUnmapViewOfSection(FProcess.Handle, FBaseAddress); 105 | 106 | inherited; 107 | end; 108 | 109 | function KbxMapViewOfSection; 110 | begin 111 | Result.Location := 'KbMapViewOfSection'; 112 | Result.Win32Result := KbMapViewOfSection(hSection, hxProcess.Handle, 113 | BaseAddress, CommitSize, @SectionOffset, ViewSize, SectionInherit, 114 | AllocationType, Win32Protect); 115 | 116 | if Result.IsSuccess then 117 | MappedSection := TKbAutoSection.Create(hxProcess, BaseAddress); 118 | end; 119 | 120 | function KbxUnmapViewOfSection; 121 | begin 122 | Result.Location := 'KbUnmapViewOfSection'; 123 | Result.Win32Result := KbUnmapViewOfSection(hProcess, BaseAddress); 124 | end; 125 | 126 | end. 127 | -------------------------------------------------------------------------------- /KernelBridge.Threads.pas: -------------------------------------------------------------------------------- 1 | unit KernelBridge.Threads; 2 | 3 | interface 4 | 5 | uses 6 | Winapi.WinNt, Ntapi.ntdef, Ntapi.ntpsapi, Ntapi.ntrtl, KernelBridgeApi, 7 | NtUtils, NtUtils.Threads; 8 | 9 | type 10 | IThreadHandle = interface (IHandle) 11 | function GetClientId: TClientId; 12 | property ClientId: TClientId read GetClientId; 13 | end; 14 | 15 | // Determine the address of ETHREAD structure for a thread 16 | function KbxGetEthread( 17 | out eThread: IMemory; 18 | ThreadId: TThreadId32 19 | ): TNtxStatus; 20 | 21 | // Open a thread by ID 22 | function KbxOpenThread( 23 | out hxThread: IHandle; 24 | ThreadId: TProcessId32; 25 | Access: TThreadAccessMask; 26 | Attributes: TObjectAttributesFlags = 0 27 | ): TNtxStatus; 28 | 29 | // Open a thread by a pointer to ETHREAD 30 | function KbxOpenThreadByPointer( 31 | out hxThread: IHandle; 32 | Address: PEThread; 33 | Access: TThreadAccessMask; 34 | Attributes: TObjectAttributesFlags = 0; 35 | ProcessorMode: TProcessorMode = KernelMode 36 | ): TNtxStatus; 37 | 38 | // Query variable-size information for a thread 39 | function KbxQueryInformationThread( 40 | hThread: THandle; 41 | InfoClass: TThreadInfoClass; 42 | out xMemory: IMemory; 43 | InitialBuffer: Cardinal = 0; 44 | GrowthMethod: TBufferGrowthMethod = nil 45 | ): TNtxStatus; 46 | 47 | // Set variable-size information for a process 48 | function KbxSetInformationThread( 49 | hThread: THandle; 50 | InfoClass: TThreadInfoClass; 51 | Buffer: Pointer; 52 | Size: Cardinal 53 | ): TNtxStatus; 54 | 55 | type 56 | KbxThread = class abstract 57 | // Query constant-size information for a thread 58 | class function Query( 59 | hThread: THandle; 60 | InfoClass: TThreadInfoClass; 61 | out Buffer: T 62 | ): TNtxStatus; static; 63 | 64 | // Set constant-size information for a thread 65 | class function &Set( 66 | hThread: THandle; 67 | InfoClass: TThreadInfoClass; 68 | const Buffer: T 69 | ): TNtxStatus; static; 70 | end; 71 | 72 | // Create a user-mode thread in a process 73 | function KbxCreateUserThread( 74 | out Thread: IThreadHandle; 75 | ProcessId: TProcessId32; 76 | ThreadRoutine: TUserThreadStartRoutine; 77 | Argument: Pointer; 78 | CreateSuspended: Boolean 79 | ): TNtxStatus; 80 | 81 | // Create a system thread in a process 82 | function KbxCreateSystemThread( 83 | out Thread: IThreadHandle; 84 | ProcessId: TProcessId32; 85 | ThreadRoutine: TUserThreadStartRoutine; 86 | Argument: Pointer 87 | ): TNtxStatus; 88 | 89 | // Get a context of a thread 90 | function KbxGetThreadContext( 91 | ThreadId: TThreadId32; 92 | out Context: IContext; 93 | ProcessorMode: TProcessorMode = UserMode 94 | ): TNtxStatus; 95 | 96 | // Set a context of thread 97 | function KbxSetThreadContext( 98 | ThreadId: TThreadId32; 99 | Context: PContext; 100 | ProcessorMode: TProcessorMode = UserMode 101 | ): TNtxStatus; 102 | 103 | // Queue a user APC for a thread 104 | function KbxQueueUserApc( 105 | ThreadId: TThreadId32; 106 | ApcProc: TUserApcProc; 107 | Argument: Pointer 108 | ): TNtxStatus; 109 | 110 | implementation 111 | 112 | uses 113 | KernelBridge; 114 | 115 | function KbxGetEthread; 116 | var 117 | Address: PEThread; 118 | begin 119 | Result.Location := 'KbGetEthread'; 120 | Result.Win32Result := KbGetEthread(ThreadId, Address); 121 | 122 | if Result.IsSuccess then 123 | eThread := TKbAutoObject.Capture(Address, 0); 124 | end; 125 | 126 | function KbxOpenThread; 127 | var 128 | hThread: THandle; 129 | begin 130 | Result.Location := 'KbOpenThread'; 131 | Result.LastCall.AttachAccess(Access); 132 | Result.Win32Result := KbOpenThread(ThreadId, hThread, Access, Attributes); 133 | 134 | if Result.IsSuccess then 135 | hxThread := TKbAutoHandle.Capture(hThread); 136 | end; 137 | 138 | function KbxOpenThreadByPointer; 139 | var 140 | hThread: THandle; 141 | begin 142 | Result.Location := 'KbOpenThreadByPointer'; 143 | Result.LastCall.AttachAccess(Access); 144 | Result.Win32Result := KbOpenThreadByPointer(Address, hThread, Access, 145 | Attributes, ProcessorMode); 146 | 147 | if Result.IsSuccess then 148 | hxThread := TKbAutoHandle.Capture(hThread); 149 | end; 150 | 151 | function KbxQueryInformationThread; 152 | var 153 | Required: Cardinal; 154 | begin 155 | Result.Location := 'KbQueryInformationThread'; 156 | Result.LastCall.AttachInfoClass(InfoClass); 157 | 158 | xMemory := TAutoMemory.Allocate(InitialBuffer); 159 | repeat 160 | Required := 0; 161 | Result.Win32Result := KbQueryInformationThread(hThread, InfoClass, 162 | xMemory.Data, xMemory.Size, @Required); 163 | until not NtxExpandBufferEx(Result, xMemory, Required, GrowthMethod); 164 | end; 165 | 166 | function KbxSetInformationThread; 167 | begin 168 | Result.Location := 'KbSetInformationThread'; 169 | Result.LastCall.AttachInfoClass(InfoClass); 170 | Result.Win32Result := KbSetInformationThread(hThread, InfoClass, Buffer, 171 | Size); 172 | end; 173 | 174 | class function KbxThread.Query; 175 | begin 176 | Result.Location := 'KbQueryInformationThread'; 177 | Result.LastCall.AttachInfoClass(InfoClass); 178 | Result.Win32Result := KbQueryInformationThread(hThread, InfoClass, 179 | @Buffer, SizeOf(Buffer), nil); 180 | end; 181 | 182 | class function KbxThread.&Set; 183 | begin 184 | Result.Location := 'KbSetInformationThread'; 185 | Result.LastCall.AttachInfoClass(InfoClass); 186 | Result.Win32Result := KbSetInformationThread(hThread, InfoClass, @Buffer, 187 | SizeOf(Buffer)); 188 | end; 189 | 190 | type 191 | TKbAutoThread = class (TKbAutoHandle, IThreadHandle) 192 | FClientId: TClientId; 193 | function GetClientId: TClientId; 194 | constructor Capture(hThread: THandle; ClientId: TClientId); 195 | end; 196 | 197 | constructor TKbAutoThread.Capture; 198 | begin 199 | inherited Capture(hThread); 200 | FClientId := ClientId; 201 | end; 202 | 203 | function TKbAutoThread.GetClientId: TClientId; 204 | begin 205 | Result := FClientId; 206 | end; 207 | 208 | function KbxCreateUserThread; 209 | var 210 | ClientId: TClientId; 211 | hThread: THandle; 212 | begin 213 | Result.Location := 'KbCreateUserThread'; 214 | Result.Win32Result := KbCreateUserThread(ProcessId, ThreadRoutine, Argument, 215 | CreateSuspended, @ClientId, hThread); 216 | 217 | if Result.IsSuccess then 218 | Thread := TKbAutoThread.Capture(hThread, ClientId); 219 | end; 220 | 221 | function KbxCreateSystemThread; 222 | var 223 | ClientId: TClientId; 224 | hThread: THandle; 225 | begin 226 | Result.Location := 'KbCreateSystemThread'; 227 | Result.Win32Result := KbCreateSystemThread(ProcessId, ThreadRoutine, Argument, 228 | @ClientId, hThread); 229 | 230 | if Result.IsSuccess then 231 | Thread := TKbAutoThread.Capture(hThread, ClientId); 232 | end; 233 | 234 | function KbxGetThreadContext; 235 | begin 236 | IMemory(Context) := TAutoMemory.Allocate(SizeOf(TContext)); 237 | 238 | Result.Location := 'KbGetThreadContext'; 239 | Result.Win32Result := KbGetThreadContext(ThreadId, Context.Data, Context.Size, 240 | ProcessorMode); 241 | 242 | if not Result.IsSuccess then 243 | Context := nil; 244 | end; 245 | 246 | function KbxSetThreadContext; 247 | begin 248 | Result.Location := 'KbSetThreadContext'; 249 | Result.Win32Result := KbSetThreadContext(ThreadId, Context, SizeOf(TContext), 250 | ProcessorMode); 251 | end; 252 | 253 | function KbxQueueUserApc; 254 | begin 255 | Result.Location := 'KbQueueUserApc'; 256 | Result.Win32Result := KbQueueUserApc(ThreadId, ApcProc, Argument); 257 | end; 258 | 259 | end. 260 | -------------------------------------------------------------------------------- /KernelBridge.pas: -------------------------------------------------------------------------------- 1 | unit KernelBridge; 2 | 3 | interface 4 | 5 | uses 6 | KernelBridgeApi, NtUtils, DelphiUtils.AutoObject; 7 | 8 | type 9 | TKbAutoObject = class (TCustomAutoMemory, IMemory) 10 | procedure Release; override; 11 | end; 12 | 13 | TKbAutoHandle = class (TCustomAutoHandle, IHandle) 14 | procedure Release; override; 15 | end; 16 | 17 | // Load Kernel Bridge as a driver 18 | function KbxLoadAsDriver( 19 | out Driver: IAutoReleasable; 20 | DriverPath: String 21 | ): TNtxStatus; 22 | 23 | // Load Kernel Bridge as a minifilter 24 | function KbxLoadAsFilter( 25 | out Driver: IAutoReleasable; 26 | DriverPath: String; 27 | Altitude: String = '260000' 28 | ): TNtxStatus; 29 | 30 | // Execute the specified function in Ring0 in a SEH-section with a FPU-safe 31 | // context in the context of the current process 32 | function KbxExecuteShellCode( 33 | ShellCode: TShellCode; 34 | Argument: Pointer; 35 | LocationMessage: String = 'Shellcode' 36 | ): TNtxStatus; 37 | 38 | implementation 39 | 40 | uses 41 | Ntapi.ntdef; 42 | 43 | type 44 | TKbAutoDriver = class (TCustomAutoReleasable, IAutoReleasable) 45 | procedure Release; override; 46 | end; 47 | 48 | procedure TKbAutoDriver.Release; 49 | begin 50 | KbUnload; 51 | inherited; 52 | end; 53 | 54 | procedure TKbAutoObject.Release; 55 | begin 56 | KbDereferenceObject(FAddress); 57 | inherited; 58 | end; 59 | 60 | procedure TKbAutoHandle.Release; 61 | begin 62 | KbCloseHandle(FHandle); 63 | inherited; 64 | end; 65 | 66 | function KbxLoadAsDriver; 67 | begin 68 | Result.Location := 'KbLoadAsDriver'; 69 | Result.Win32Result := KbLoadAsDriver(PWideChar(DriverPath)); 70 | 71 | if Result.IsSuccess then 72 | Driver := TKbAutoDriver.Create; 73 | end; 74 | 75 | function KbxLoadAsFilter; 76 | begin 77 | Result.Location := 'KbLoadAsFilter'; 78 | Result.Win32Result := KbLoadAsFilter(PWideChar(DriverPath), 79 | PWideChar(Altitude)); 80 | 81 | if Result.IsSuccess then 82 | Driver := TKbAutoDriver.Create; 83 | end; 84 | 85 | function KbxExecuteShellCode; 86 | var 87 | Status: NTSTATUS; 88 | begin 89 | Result.Location := 'KbExecuteShellCode'; 90 | Result.Win32Result := KbExecuteShellCode(ShellCode, Argument, Status); 91 | 92 | if Result.IsSuccess then 93 | begin 94 | // Forward the result 95 | Result.Location := LocationMessage; 96 | Result.Status := Status; 97 | end; 98 | end; 99 | 100 | end. 101 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Kernel-Bridge Delphi Connector 2 | 3 | This project is a library for using the user-mode side of the API provided by **[Kernel Bridge](https://github.com/HoShiMin/Kernel-Bridge)** with Delphi. It allows manipulating processes and threads from kernel-mode, contains functions for directly manipulating kernel and physical memory, and more. 4 | 5 | ## Content 6 | 7 | The library includes a single header file — **KernelBridgeApi.pas** — a translated version of definitions for the functions exported by **User-Bridge.dll**. Although this file depends on the headers from NtUtils, it should be simple to inline and remove those dependencies if necessary. 8 | 9 | The rest files are the wrappers that allow better integration of the functionality into the language and automated resource lifetime management. These modules are an extension of my **[NtUtils library](https://github.com/diversenok/NtUtilsLibrary)**. 10 | 11 | File | Description 12 | ------------------------------------- | ------------ 13 | **KernelBridge.pas** | Loader for the Kernel Bridge Driver, kernel shellcode injection. 14 | **KernelBridge.Processes.pas** | Process manipulation 15 | **KernelBridge.Processes.Memory.pas** | Reading, writing, allocating, and protecting process memory. 16 | **KernelBridge.Threads.pas** | Thread creation and manipulation, APC queueing. 17 | **KernelBridge.Section.pas** | Section manipulation. 18 | **KernelBridge.Memory.pas** | Operations with virtual and physical memory. 19 | **KernelBridge.Memory.Mdl.pas** | Operations with Memory Descriptor Lists. 20 | 21 | The functions in these modules are similar to those of the API in the corresponding categories. The main difference is that operations that require a cleanup return instances of IAutoReleasable (or its descendants). This interface ensures that the underlying resources get automatically released when the last reference goes out of scope. 22 | 23 | ## Examples 24 | 25 | See the [examples](Examples) folder for more details. 26 | --------------------------------------------------------------------------------