Repository navigation
Expand file tree
/
Copy pathheavensgate.cpp
More file actions
executable file
·1377 lines (1178 loc) · 38.5 KB
/
Copy pathheavensgate.cpp
File metadata and controls
executable file
·1377 lines (1178 loc) · 38.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2014 ReWolf
// 2025 neonbyte
//
// http://blog.rewolf.pl/
// https://github.com/neonbyte1
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <zen/platform/heavensgate.hpp>
#if defined(ZEN_TARGET_32_BIT)
#include <zen/platform/rtl/peb.hpp>
#include <zen/nt/directories/exports.hpp>
#include <zen/nt/image.hpp>
#include <zen/core/fnv.hpp>
#include <cstdarg>
using namespace zen;
// ZEN_DECL_SYSCALL_METADATA_WITH_RETURN_VALUE
#define ZEN_DECL_HEAVENSGATE_METADATA_WITH_RETURN_VALUE(ModuleName, FunctionName, Fallback) \
static u64 function_address; \
if (!function_address) { \
constexpr static auto module_hash = zen::fnv<>::hash<true>(ModuleName); \
constexpr static auto name_hash = zen::fnv<>::hash<true>(FunctionName); \
function_address = win::x64_get_proc_address(win::x64_get_module_handle(module_hash), name_hash); \
if (!function_address) { \
return (Fallback); \
} \
}
#define ZEN_DECL_HEAVENSGATE_METADATA(ModuleName, FunctionName) \
ZEN_DECL_HEAVENSGATE_METADATA_WITH_RETURN_VALUE(ModuleName, FunctionName, status_code{static_cast<long>(0xC000001C)})
namespace {
NODISCARD
auto
x64_get_teb() noexcept -> u64
{
auto reg_rax = win::make_reg64(0);
__asm {
X64_Start();
X64_Push(X64_R12);
pop reg_rax.dw[0]
X64_End();
}
return reg_rax.u64;
}
NODISCARD
auto
x64_get_proc_address(
const u64 handle,
const u32 proc_hash,
const i32 proc_ordinal,
const bool lowercase
) noexcept -> u64
{
win::dos_header dos_hdr{};
win::nt_headers<true> nt_hdr{};
win::export_directory exp_dir{};
win::x64_memcpy(&dos_hdr, handle, sizeof(dos_hdr));
if (!dos_hdr.valid()) {
return 0;
}
win::x64_memcpy(&nt_hdr, handle + dos_hdr.next_hdr_offset(), sizeof(nt_hdr));
if (!nt_hdr.valid() || !nt_hdr.is_64_bit()) {
return 0;
}
const auto& data_dir = nt_hdr.optional_hdr().data_directories().dir.exports;
if (!data_dir.present()) {
return 0;
}
const auto exp_dir_ptr = handle + data_dir.rva();
const auto delta = exp_dir_ptr - data_dir.rva();
win::x64_memcpy(&exp_dir, exp_dir_ptr, sizeof(exp_dir));
const auto num_functions = exp_dir.num_functions();
const auto num_names = exp_dir.num_names();
if (!num_functions) {
return 0;
}
auto names = std::make_unique<u32[]>(num_names);
auto functions = std::make_unique<u32[]>(num_functions);
auto ordinals = std::make_unique<u16[]>(num_functions);
win::x64_memcpy(
ordinals.get(),
handle + exp_dir.rva_name_ordinals(),
sizeof(u16) * exp_dir.num_functions()
);
win::x64_memcpy(
functions.get(),
handle + exp_dir.rva_functions(),
sizeof(u32) * exp_dir.num_functions()
);
win::x64_memcpy(
names.get(),
handle + exp_dir.rva_names(),
sizeof(u32) * exp_dir.num_names()
);
for (u32 i{}; i < num_functions; ++i) {
const auto ordinal = proc_ordinal != -1
? static_cast<u16>(i)
: ordinals[i];
const auto found_by_ordinal
= proc_ordinal != -1
&& static_cast<u16>(proc_ordinal) == exp_dir.base() + ordinal;
bool found_by_name{};
if (!found_by_ordinal && i < num_names) {
char buffer[260]{};
win::x64_memcpy(buffer, delta + names[i], sizeof(buffer));
const std::string_view function_name{buffer};
const auto hash = lowercase
? fnv<>::get<true>(function_name)
: fnv<>::get(function_name);
found_by_name = hash == proc_hash;
}
if (!found_by_ordinal && !found_by_name) {
continue;
}
const auto function_pointer = delta + functions[ordinal];
const auto directory_begin = delta + data_dir.rva();
const auto directory_end = directory_begin + data_dir.size();
if (function_pointer >= directory_begin && function_pointer <= directory_end) {
// e.g. "api-ms-win-core-processthreads-l1-1-6.SetProcessDynamicEnforcedCetCompatibleRanges"
char forwarded_name_raw[260]{};
win::x64_memcpy(forwarded_name_raw, function_pointer, sizeof(forwarded_name_raw));
const std::string_view forwarded_name{forwarded_name_raw};
const auto dot_pos{forwarded_name.find('.')};
if (dot_pos == std::string_view::npos) {
return 0;
}
// e.g. "api-ms-win-core-processthreads-l1-1-6" + ".dll"
const auto lib_hash = fnv<>::hash(".dll", fnv<>::get<true>(forwarded_name.substr(0, dot_pos)));
const auto forwarded_handle = win::x64_get_module_handle(lib_hash);
if (!forwarded_handle) {
return 0;
}
return forwarded_handle
? x64_get_proc_address(
forwarded_handle,
// e.g. "SetProcessDynamicEnforcedCetCompatibleRanges"
fnv<>::get<true>(forwarded_name.substr(dot_pos + 1)),
-1,
true
) : 0;
}
return function_pointer;
}
return 0;
}
NODISCARD
auto
x64_to_handle(
const u64 handle
) noexcept -> void*
{
return reinterpret_cast<void*>(static_cast<iptr>(handle));
}
NODISCARD
auto
x64_nt_open_process(
const u32 pid,
const u32 desired_access,
void** const process_handle
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtOpenProcess")
auto handle{win::invalid_handle_value<u64>()};
rtl::object_attributes<true> attributes{};
rtl::client_id<true> cid{};
cid.pid(pid);
const auto status = win::x64_call(
function_address,
4,
win::x64_from_ptr(&handle),
static_cast<u64>(desired_access),
win::x64_from_ptr(&attributes),
win::x64_from_ptr(&cid)
);
*process_handle = x64_to_handle(handle);
return status_code{status};
}
NODISCARD
auto
x64_nt_open_thread(
const u32 thread_id,
const u32 desired_access,
void** const process_handle
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtOpenThread")
auto handle{win::invalid_handle_value<u64>()};
rtl::object_attributes<true> attributes{};
rtl::client_id<true> cid{};
cid.tid(thread_id);
const auto status = win::x64_call(
function_address,
4,
win::x64_from_ptr(&handle),
static_cast<u64>(desired_access),
win::x64_from_ptr(&attributes),
win::x64_from_ptr(&cid)
);
*process_handle = x64_to_handle(handle);
return status_code{status};
}
auto
x64_nt_create_thread_ex(
void** output,
const void* const process_handle,
const u64 routine,
const u64 thread_params,
const rtl::create_thread_flags flags,
const u32 desired_access,
rtl::object_attributes<true>* const attributes,
const u64 stack_zero_bits,
const u64 size_of_stack_commit,
const u64 size_of_stack_reserve,
void* const bytes_buffer
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtCreateThreadEx")
auto handle{win::invalid_handle_value<u64>()};
const auto status = win::x64_call(
0,
11,
win::x64_from_ptr(&handle),
static_cast<u64>(desired_access),
win::x64_from_ptr(attributes),
win::x64_from_handle(process_handle),
routine,
thread_params,
static_cast<u64>(flags),
stack_zero_bits,
size_of_stack_commit,
size_of_stack_reserve,
win::x64_from_handle(bytes_buffer)
);
*output = x64_to_handle(handle);
return status_code{status};
}
}
auto
win::x64_get_peb() noexcept -> u64
{
// https://en.wikipedia.org/wiki/Win32_Thread_Information_Block#Contents_of_the_TIB_on_Windows
// Type Offset (x86) Offset (x64) Description
// pointer FS:[0x30] GS:[0x60] Linear address of Process Environment Block (PEB)
u64 peb_addr{};
win::x64_memcpy(&peb_addr, x64_get_teb() + 0x60, sizeof(peb_addr));
return peb_addr;
}
auto
win::x64_get_pid() noexcept -> u32
{
// https://en.wikipedia.org/wiki/Win32_Thread_Information_Block#Contents_of_the_TIB_on_Windows
// Type Offset (x86) Offset (x64) Description
// pointer FS:[0x20] GS:[0x40] Process ID (in some Windows distributions this field is used as DebugContext)
u64 pid{};
win::x64_memcpy(&pid, x64_get_teb() + 0x40, sizeof(pid));
return static_cast<u32>(pid);
}
auto
win::x64_get_tid() noexcept -> u32
{
// https://en.wikipedia.org/wiki/Win32_Thread_Information_Block#Contents_of_the_TIB_on_Windows
// Type Offset (x86) Offset (x64) Description
// pointer FS:[0x24] GS:[0x48] Current thread ID
u64 pid{};
win::x64_memcpy(&pid, x64_get_teb() + 0x48, sizeof(pid));
return static_cast<u32>(pid);
}
#pragma warning(push)
#pragma warning(disable : 4409)
auto
win::x64_call(
u64 function,
szt num_arguments,
...
) noexcept -> u64
{
va_list args;
va_start(args, num_arguments);
auto arg_reg = [&num_arguments, &args]
{
if (num_arguments > 0) {
--num_arguments;
return make_reg64(va_arg(args, u64));
}
return make_reg64(0);
};
auto reg_rcx = arg_reg();
auto reg_rdx = arg_reg();
auto reg_r8 = arg_reg();
auto reg_r9 = arg_reg();
auto reg_rax = make_reg64(0);
auto rest_args = make_reg64(x64_from_ptr(&va_arg(args, u64)));
auto num_args = make_reg64(num_arguments);
u32 back_esp{};
u16 back_fs{};
__asm {
;// reset FS segment, to properly handle RFG
mov back_fs, fs
mov eax, 0x2B
mov fs, ax
;// keep original esp in back_esp variable
mov back_esp, esp
;// align esp to 0x10, without aligned stack some syscalls may return errors !
;// (actually, for syscalls it is sufficient to align to 8, but SSE opcodes
;// requires 0x10 alignment), it will be further adjusted according to the
;// number of arguments above 4
and esp, 0xFFFFFFF0
X64_Start();
;// below code is compiled as x86 inline asm, but it is executed as x64 code
;// that's why it need sometimes REX_W() macro, right column contains detailed
;// transcription how it will be interpreted by CPU
;// fill first four arguments
X64_REX_W mov ecx, reg_rcx.dw[0] ;// mov rcx, qword ptr [reg_rcx]
X64_REX_W mov edx, reg_rdx.dw[0] ;// mov rdx, qword ptr [reg_rdx]
push reg_r8.u64 ;// push qword ptr [reg_r8]
X64_Pop(X64_R8); ;// pop r8
push reg_r9.u64 ;// push qword ptr [reg_r9]
X64_Pop(X64_R9); ;// pop r9
X64_REX_W mov eax, num_args.dw[0] ;// mov rax, qword ptr [num_args]
;// final stack adjustment, according to the
;// number of arguments above 4
test al, 1 ;// test al, 1
jnz _no_adjust ;// jnz _no_adjust
sub esp, 8 ;// sub rsp, 8
_no_adjust:
push edi ;// push rdi
X64_REX_W mov edi, rest_args.dw[0] ;// mov rdi, qword ptr [rest_args]
;// put rest of arguments on the stack
X64_REX_W test eax, eax ;// test rax, rax
jz _ls_e ;// je _ls_e
X64_REX_W lea edi, dword ptr [edi + 8*eax - 8] ;// lea rdi, [rdi + rax*8 - 8]
_ls:
X64_REX_W test eax, eax ;// test rax, rax
jz _ls_e ;// je _ls_e
push dword ptr [edi] ;// push qword ptr [rdi]
X64_REX_W sub edi, 8 ;// sub rdi, 8
X64_REX_W sub eax, 1 ;// sub rax, 1
jmp _ls ;// jmp _ls
_ls_e:
;// create stack space for spilling registers
X64_REX_W sub esp, 0x20 ;// sub rsp, 20h
call function ;// call qword ptr [func]
;// cleanup stack
X64_REX_W mov ecx, num_args.dw[0] ;// mov rcx, qword ptr [num_args]
X64_REX_W lea esp, dword ptr [esp + 8*ecx + 0x20] ;// lea rsp, [rsp + rcx*8 + 20h]
pop edi ;// pop rdi
;// set return value
X64_REX_W mov reg_rax.dw[0], eax ;// mov qword ptr [reg_rax], rax
X64_End();
mov ax, ds
mov ss, ax
mov esp, back_esp
;// restore FS segment
mov ax, back_fs
mov fs, ax
}
return reg_rax.u64;
}
#pragma warning(pop)
auto
win::x64_memcpy(
void* dst,
u64 src,
szt num_bytes
) noexcept -> void
{
auto reg_src = make_reg64(src);
// below code is compiled as x86 inline asm, but it is executed as x64 code
// that's why it need sometimes REX_W() macro, right column contains detailed
// transcription how it will be interpreted by CPU
__asm {
X64_Start();
push edi ;// push rdi
push esi ;// push rsi
mov edi, dst ;// mov edi, dword ptr [dst] ; high part of RDI is zeroed
X64_REX_W mov esi, reg_src.dw[0] ;// mov rsi, qword ptr [reg_src]
mov ecx, num_bytes ;// mov ecx, dword ptr [size] ; high part of RCX is zeroed
mov eax, ecx ;// mov eax, ecx
and eax, 3 ;// and eax, 3
shr ecx, 2 ;// shr ecx, 2
rep movsd ;// rep movs dword ptr [rdi], dword ptr [rsi]
test eax, eax ;// test eax, eax
je _move_0 ;// je _move_0
cmp eax, 1 ;// cmp eax, 1
je _move_1 ;// je _move_1
movsw ;// movs word ptr [rdi], word ptr [rsi]
cmp eax, 2 ;// cmp eax, 2
je _move_0 ;// je _move_0
_move_1:
movsb ;// movs byte ptr [rdi], byte ptr [rsi]
_move_0:
pop esi ;// pop rsi
pop edi ;// pop rdi
X64_End();
}
}
auto
win::x64_memcmp(
const void* dst,
u64 src,
szt num_bytes
) noexcept -> bool
{
bool result{};
auto reg_src = make_reg64(src);
// below code is compiled as x86 inline asm, but it is executed as x64 code
// that's why it need sometimes REX_W() macro, right column contains detailed
// transcription how it will be interpreted by CPU
__asm {
X64_Start();
push edi ;// push rdi
push esi ;// push rsi
mov edi, dst ;// mov edi, dword ptr [dst] ; high part of RDI is zeroed
X64_REX_W mov esi, reg_src.dw[0] ;// mov rsi, qword ptr [reg_src]
mov ecx, num_bytes ;// mov ecx, dword ptr [num_bytes] ; high part of RCX is zeroed
mov eax, ecx ;// mov eax, ecx
and eax, 3 ;// and eax, 3
shr ecx, 2 ;// shr ecx, 2
repe cmpsd ;// repe cmps dword ptr [rsi], dword ptr [rdi]
jnz _ret_false ;// jnz _ret_false
test eax, eax ;// test eax, eax
je _move_0 ;// je _move_0
cmp eax, 1 ;// cmp eax, 1
je _move_1 ;// je _move_1
cmpsw ;// cmps word ptr [rsi], word ptr [rdi]
jnz _ret_false ;// jnz _ret_false
cmp eax, 2 ;// cmp eax, 2
je _move_0 ;// je _move_0
_move_1:
cmpsb ;// cmps byte ptr [rsi], byte ptr [rdi]
jnz _ret_false ;// jnz _ret_false
_move_0:
mov result, 1 ;// mov byte ptr [result], 1
_ret_false:
pop esi ;// pop rsi
pop edi ;// pop rdi
X64_End();
}
return result;
}
auto
win::x64_get_module_handle(
const u32 name,
const bool lowercase
) noexcept -> u64
{
const auto validate_entry = [&](
const rtl::ldr_data_table_entry<true>& entry
) noexcept -> bool
{
if (!entry.base_dll_name.buffer()) {
return false;
}
if (!fnv<>::valid(name)) {
return true;
}
const auto module_name_buffer
= std::make_unique<wchar_t[]>(entry.base_dll_name.length() + 1);
x64_memcpy(
module_name_buffer.get(),
entry.base_dll_name.buffer(),
entry.base_dll_name.len()
);
const std::wstring_view module_name{
module_name_buffer.get(),
entry.base_dll_name.length()
};
const auto hash = lowercase
? fnv<>::get<true>(module_name)
: fnv<>::get(module_name);
return hash == name;
};
rtl::peb<true> peb{};
win::x64_memcpy(&peb, x64_get_peb(), sizeof(peb));
if (peb.ldr) {
rtl::peb_ldr_data<true> ldr{};
rtl::ldr_data_table_entry<true> head{};
x64_memcpy(&ldr, peb.ldr, sizeof(ldr));
const auto last_entry = peb.ldr
+ offsetof(rtl::peb_ldr_data<true>, in_load_order_module_list);
head.in_load_order_links.next = ldr.in_load_order_module_list.next;
do {
x64_memcpy(&head, head.in_load_order_links.next, sizeof(head));
if (validate_entry(head)) {
return head.dll_base;
}
} while (head.in_load_order_links.next != last_entry);
}
return 0;
}
auto
win::x64_get_module_handle(
const std::string_view name,
const bool lowercase
) noexcept -> u64
{
const u32 name_hash = !name.empty()
? (lowercase ? fnv<>::get<true>(name) : fnv<>::get(name))
: 0;
return x64_get_module_handle(name_hash, lowercase);
}
auto
win::x64_get_module_handle(
const std::wstring_view name,
const bool lowercase
) noexcept -> u64
{
const u32 name_hash = !name.empty()
? (lowercase ? fnv<>::get<true>(name) : fnv<>::get(name))
: 0;
return x64_get_module_handle(name_hash, lowercase);
}
auto
win::x64_get_proc_address(
const u64 handle,
const u32 name,
const bool lowercase
) noexcept -> u64
{
if (!handle || !fnv<>::valid(name)) {
return 0;
}
return ::x64_get_proc_address(handle, name, -1, lowercase);
}
auto
win::x64_get_proc_address(
const u64 handle,
const std::string_view name,
const bool lowercase
) noexcept -> u64
{
if (!handle || name.empty()) {
return 0;
}
const auto name_ptr = reinterpret_cast<const uptr>(name.data());
const auto is_ordinal = name_ptr <= 0xFFFF;
return ::x64_get_proc_address(
handle,
!is_ordinal
? (lowercase ? fnv<>::get<true>(name) : fnv<>::get(name))
: 0,
is_ordinal ? static_cast<i32>(name_ptr) : -1,
lowercase
);
}
auto
win::x64_nt_query_information_process(
const void* const process_handle,
const rtl::process_info id,
void* const buffer,
const u64 size,
u64* const return_length
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtQueryInformationProcess")
u64 ret_len{};
const auto status = x64_call(
function_address,
5,
x64_from_handle(process_handle),
static_cast<u64>(id),
x64_from_ptr(buffer),
size,
x64_from_ptr(&ret_len)
);
if (return_length) {
*return_length = ret_len;
}
return status_code{status};
}
auto
win::x64_nt_query_information_thread(
const void* const thread_handle,
const rtl::thread_info id,
void* const buffer,
const u64 size,
u64* const return_length
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtQueryInformationThread")
u64 ret_len{};
const auto status = x64_call(
function_address,
5,
x64_from_handle(thread_handle),
static_cast<u64>(id),
x64_from_ptr(buffer),
size,
x64_from_ptr(&ret_len)
);
if (return_length) {
*return_length = ret_len;
}
return status_code{status};
}
auto
win::x64_nt_query_system_information(
const rtl::system_info id,
void* const buffer,
const u64 size,
u64* const return_length
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtQuerySystemInformation")
u64 ret_len{};
const auto status = x64_call(
function_address,
4,
static_cast<u64>(id),
x64_from_ptr(buffer),
size,
x64_from_ptr(&ret_len)
);
if (return_length) {
*return_length = ret_len;
}
return status_code{status};
}
auto
win::x64_nt_query_object(
const void* const handle,
const rtl::object_info id,
void* const buffer,
const u64 size,
u64* const return_length
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtQueryObject")
u64 ret_len{};
const auto status = x64_call(
function_address,
5,
x64_from_handle(handle),
static_cast<u64>(id),
x64_from_ptr(buffer),
size,
x64_from_ptr(&ret_len)
);
if (return_length) {
*return_length = ret_len;
}
return status_code{status};
}
auto
win::x64_nt_query_section(
const void* const handle,
const rtl::section_info id,
void* const buffer,
const u64 size,
u64* const return_length
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtQuerySection")
u64 ret_len{};
const auto status = x64_call(
function_address,
5,
x64_from_handle(handle),
static_cast<u64>(id),
x64_from_ptr(buffer),
size,
x64_from_ptr(&ret_len)
);
if (return_length) {
*return_length = ret_len;
}
return status_code{status};
}
auto
win::x64_nt_query_virtual_memory(
const void* const handle,
const u64 base_address,
const rtl::memory_info id,
void* const buffer,
const u64 size,
u64* const return_length
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtQueryVirtualMemory")
u64 ret_len{};
const auto status = x64_call(
function_address,
6,
x64_from_handle(handle),
base_address,
static_cast<u64>(id),
x64_from_ptr(buffer),
size,
x64_from_ptr(&ret_len)
);
if (return_length) {
*return_length = ret_len;
}
return status_code{status};
}
auto
win::x64_nt_allocate_virtual_memory(
const void* const handle,
u64* base_address,
const u64 zero_bits,
const u64 size,
const allocation_type allocation,
const page_protection protection
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtAllocateVirtualMemory")
auto region_size{size};
const auto status = x64_call(
function_address,
6,
x64_from_handle(handle),
x64_from_ptr(base_address),
zero_bits,
x64_from_ptr(®ion_size),
static_cast<u64>(allocation),
static_cast<u64>(protection)
);
return status_code{status};
}
auto
win::x64_nt_free_virtual_memory(
const void* const handle,
const u64 base_address,
const u64 size,
const u32 free_type
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtFreeVirtualMemory")
auto temp_address{base_address};
auto region_size{size};
const auto status = x64_call(
function_address,
4,
x64_from_handle(handle),
x64_from_ptr(&temp_address),
x64_from_ptr(®ion_size),
static_cast<u64>(free_type)
);
return status_code{status};
}
auto
win::x64_nt_protect_virtual_memory(
const void* const handle,
const u64 base_address,
const u64 size,
const page_protection new_protection,
page_protection* const old_protection
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtProtectVirtualMemory")
auto temp_address{base_address};
auto region_size{size};
const auto status = x64_call(
function_address,
5,
x64_from_handle(handle),
x64_from_ptr(&temp_address),
x64_from_ptr(®ion_size),
static_cast<u64>(new_protection),
x64_from_ptr(old_protection)
);
return status_code{status};
}
auto
win::x64_nt_read_virtual_memory(
const void* const handle,
const u64 base_address,
void* const buffer,
const u64 size,
u64* const bytes_read
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtReadVirtualMemory")
u64 bytes_processed{};
const auto status = x64_call(
function_address,
5,
x64_from_handle(handle),
base_address,
x64_from_ptr(buffer),
size,
x64_from_ptr(&bytes_processed)
);
if (bytes_read) {
*bytes_read = bytes_processed;
}
return status_code{status};
}
auto
win::x64_nt_write_virtual_memory(
const void* const handle,
const u64 base_address,
const void* const buffer,
const u64 size,
u64* const bytes_written
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtWriteVirtualMemory")
u64 bytes_processed{};
const auto status = x64_call(
function_address,
5,
x64_from_handle(handle),
base_address,
x64_from_ptr(buffer),
size,
x64_from_ptr(&bytes_processed)
);
if (bytes_written) {
*bytes_written = bytes_processed;
}
return status_code{status};
}
auto
win::x64_nt_get_context_thread(
const void* handle,
void* context
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtGetContextThread")
return status_code{
x64_call(
function_address,
2,
x64_from_handle(handle),
x64_from_ptr(context)
)
};
}
auto
win::x64_nt_set_context_thread(
const void* handle,
const void* context
) noexcept -> status_code
{
ZEN_DECL_HEAVENSGATE_METADATA("ntdll.dll", "NtSetContextThread")
return status_code{
x64_call(
function_address,
2,
x64_from_handle(handle),