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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
use crate::{
    error::Error,
    map::MappedFile,
    offsets::{
        offset_client::OffsetClient, offset_server::Offset, OffsetsRequest, OffsetsResponse,
    },
    primitives::{protected_write, rtl_rb_tree_insert, ProtectionGuard, RtlMutex},
    server::OffsetHandler,
    util::to_wide,
};
use log::debug;
use ntapi::{
    ntldr::{
        LDR_DATA_TABLE_ENTRY_u1, LDR_DATA_TABLE_ENTRY_u2, LDR_DDAG_NODE_u, LdrModulesReadyToRun,
        LDRP_CSLIST, LDR_DATA_TABLE_ENTRY, LDR_DDAG_NODE, LDR_DDAG_STATE, PLDR_INIT_ROUTINE,
    },
    ntpsapi::NtCurrentPeb,
    ntrtl::{
        InsertTailList, RemoveEntryList, RtlHashUnicodeString, RtlInitUnicodeString,
        RtlRbRemoveNode, HASH_STRING_ALGORITHM_DEFAULT, RTL_RB_TREE,
    },
};
use std::{
    ffi::{c_void, CStr, OsString},
    os::windows::prelude::OsStrExt,
    os::windows::prelude::*,
    path::Path,
    pin::Pin,
    ptr,
    ptr::null_mut,
};
#[cfg(feature = "tls")]
use tonic::transport::Certificate;
use tonic::transport::Channel;
#[cfg(feature = "tls")]
use tonic::transport::ClientTlsConfig;
use winapi::{
    shared::{
        guiddef::GUID,
        minwindef::HMODULE,
        ntdef::{
            LARGE_INTEGER, LIST_ENTRY, RTL_BALANCED_NODE, SINGLE_LIST_ENTRY, ULONGLONG,
            UNICODE_STRING,
        },
        ntstatus::STATUS_SUCCESS,
        winerror::ERROR_FILE_NOT_FOUND,
    },
    um::{
        libloaderapi::{GetModuleHandleW, GetProcAddress, LoadLibraryA},
        winnt::{
            RtlAddFunctionTable, RtlDeleteFunctionTable, DLL_PROCESS_ATTACH, DLL_PROCESS_DETACH,
            IMAGE_DEBUG_DIRECTORY, IMAGE_DEBUG_TYPE_CODEVIEW, IMAGE_DIRECTORY_ENTRY_DEBUG,
            IMAGE_DIRECTORY_ENTRY_EXCEPTION, IMAGE_DIRECTORY_ENTRY_IMPORT,
            IMAGE_DIRECTORY_ENTRY_TLS, IMAGE_DOS_HEADER, IMAGE_FILE_MACHINE_AMD64,
            IMAGE_IMPORT_BY_NAME, IMAGE_IMPORT_DESCRIPTOR, IMAGE_NT_HEADERS64,
            IMAGE_NT_OPTIONAL_HDR64_MAGIC, IMAGE_ORDINAL_FLAG, IMAGE_RUNTIME_FUNCTION_ENTRY,
            IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_WRITE, IMAGE_SECTION_HEADER, IMAGE_THUNK_DATA,
            IMAGE_TLS_DIRECTORY, PAGE_EXECUTE_READ, PAGE_READONLY, PAGE_READWRITE, PVOID,
            RTL_SRWLOCK,
        },
    },
};

/// The context of internal Nt functions and statics that are
/// required for the mapper to work
#[allow(non_snake_case)]
#[derive(Clone)]
pub struct NtContext {
    LdrpHashTable: RtlMutex<[LIST_ENTRY; 32]>,
    LdrpHandleTlsData: unsafe extern "stdcall" fn(*mut LDR_DATA_TABLE_ENTRY) -> i32,
    LdrpReleaseTlsEntry: unsafe extern "stdcall" fn(*mut LDR_DATA_TABLE_ENTRY, null: usize) -> i32,
    LdrpMappingInfoIndex: RtlMutex<RTL_RB_TREE>,
    LdrpModuleBaseAddressIndex: RtlMutex<RTL_RB_TREE>,
}

unsafe impl Send for NtContext {}

/// This allows us to hold a pointer across a wait point
struct Module(*const u8);

unsafe impl Send for Module {}

impl NtContext {
    /// Gets the loaded NTDLL hash
    ///
    /// # Arguments
    ///
    /// `ntdll`: The NTDLL instance pointer
    fn get_ntdll_hash(ntdll: *const u8) -> Result<String, Error> {
        unsafe {
            let dos_header = ntdll as *const IMAGE_DOS_HEADER;
            if dos_header.is_null() {
                return Err(Error::NtDllNotLoaded);
            }
            let nt_headers = &*((dos_header as *const u8).offset((*dos_header).e_lfanew as isize)
                as *const IMAGE_NT_HEADERS64);
            let debug_entry = &*((dos_header as *const u8).offset(
                nt_headers.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG as usize]
                    .VirtualAddress as isize,
            ) as *const IMAGE_DEBUG_DIRECTORY);
            if debug_entry.Type != IMAGE_DEBUG_TYPE_CODEVIEW {
                return Err(Error::NtDllDebugType);
            }
            let codeview_entry = &*((dos_header as *const u8)
                .offset((*debug_entry).AddressOfRawData as isize)
                as *const IMAGE_DEBUG_CODEVIEW);
            if !codeview_entry
                .rsds_signature
                .eq_ignore_ascii_case("RSDS".as_bytes())
            {
                return Err(Error::NtDllRsdsSig);
            }
            Ok(format!(
                "{:08X}{:04X}{:04X}{}{:X}",
                codeview_entry.guid.Data1,
                codeview_entry.guid.Data2,
                codeview_entry.guid.Data3,
                codeview_entry
                    .guid
                    .Data4
                    .iter()
                    .map(|b| format!("{:02X}", b))
                    .collect::<Vec<String>>()
                    .join(""),
                codeview_entry.age
            ))
        }
    }

    /// Processes an offsets response
    ///
    /// # Arguments
    ///
    /// `response`: The response to process
    fn process_response(response: OffsetsResponse, ntdll: *const u8) -> NtContext {
        unsafe {
            NtContext {
                LdrpHashTable: RtlMutex::from_ref(
                    &mut *(ntdll.offset(response.ldrp_hash_table as isize)
                        as *mut [LIST_ENTRY; 32]),
                    &mut *(ntdll.offset(response.ldrp_module_datatable_lock as isize)
                        as *mut RTL_SRWLOCK),
                ),
                LdrpHandleTlsData: std::mem::transmute(
                    ntdll.offset(response.ldrp_handle_tls_data as isize),
                ),
                LdrpReleaseTlsEntry: std::mem::transmute(
                    ntdll.offset(response.ldrp_release_tls_entry as isize),
                ),
                LdrpMappingInfoIndex: RtlMutex::from_ref(
                    &mut *(ntdll.offset(response.ldrp_mapping_info_index as isize)
                        as *mut RTL_RB_TREE),
                    &mut *(ntdll.offset(response.ldrp_module_datatable_lock as isize)
                        as *mut RTL_SRWLOCK),
                ),
                LdrpModuleBaseAddressIndex: RtlMutex::from_ref(
                    &mut *(ntdll.offset(response.ldrp_module_base_address_index as isize)
                        as *mut RTL_RB_TREE),
                    &mut *(ntdll.offset(response.ldrp_module_datatable_lock as isize)
                        as *mut RTL_SRWLOCK),
                ),
            }
        }
    }

    /// Resolves the context used by the mapper
    ///
    /// # Arguments
    ///
    /// `server_hostname`: The hostname of the endpoint of the PDB server
    ///
    /// `server_port`: The port of the endpoint of the PDB server
    pub async fn resolve<S: AsRef<str>>(
        server_hostname: S,
        server_port: u16,
    ) -> anyhow::Result<NtContext> {
        let channel = Channel::from_shared(format!(
            "http://{}:{}",
            server_hostname.as_ref(),
            server_port
        ))?;
        let mut client = OffsetClient::new(channel.connect().await?);
        // wrap this in a `Module` so we can send it across await points
        let ntdll = Module(unsafe { GetModuleHandleW(to_wide("ntdll").as_ptr()) as *const u8 });
        let request = tonic::Request::new(OffsetsRequest {
            ntdll_hash: NtContext::get_ntdll_hash(ntdll.0)?,
        });
        let response = client.get_offsets(request).await?.into_inner();
        Ok(NtContext::process_response(response, ntdll.0))
    }

    /// Resolves the context used by the mapper, over a secure TLS connection
    ///
    /// # Arguments
    ///
    /// `server_hostname`: The hostname of the endpoint of the PDB server
    ///
    /// `server_port`: The port of the endpoint of the PDB server
    ///
    /// `ca_cert`: A custom CA certificate. This is necessary if you have a
    /// self-signed certificate on the other end. If not specified, webPKI
    /// will use their store to verify the endpoint
    ///
    /// `domain`: The domain name to be verified
    #[cfg(feature = "tls")]
    pub async fn resolve_tls<S: AsRef<str>>(
        server_hostname: S,
        server_port: u16,
        ca_cert: Option<Certificate>,
        domain: Option<S>,
    ) -> anyhow::Result<NtContext> {
        let mut tls_config = ClientTlsConfig::new();
        // add a CA certificate in case a self-signed cert is used
        if let Some(ca_cert) = ca_cert {
            tls_config = tls_config.ca_certificate(ca_cert);
        }
        // add a pinned domain in case domain name verification is wanted
        if let Some(domain) = domain {
            tls_config = tls_config.domain_name(domain.as_ref());
        }
        let channel = Channel::from_shared(format!(
            "http://{}:{}",
            server_hostname.as_ref(),
            server_port
        ))?
        .tls_config(tls_config)?;
        let mut client = OffsetClient::new(channel.connect().await?);
        // wrap this in a `Module` so we can send it across await points
        let ntdll = Module(unsafe { GetModuleHandleW(to_wide("ntdll").as_ptr()) as *const u8 });
        let request = tonic::Request::new(OffsetsRequest {
            ntdll_hash: NtContext::get_ntdll_hash(ntdll.0)?,
        });
        let response = client.get_offsets(request).await?.into_inner();
        Ok(NtContext::process_response(response, ntdll.0))
    }

    /// Resolves the context used by the mapper
    ///
    /// # Arguments
    ///
    /// `handler`: The local handler
    pub async fn resolve_local(handler: &OffsetHandler) -> anyhow::Result<NtContext> {
        // wrap this in a `Module` so we can send it across await points
        let ntdll = Module(unsafe { GetModuleHandleW(to_wide("ntdll").as_ptr()) as *const u8 });
        let request = tonic::Request::new(OffsetsRequest {
            ntdll_hash: NtContext::get_ntdll_hash(ntdll.0)?,
        });
        let response = handler.get_offsets(request).await?.into_inner();
        Ok(NtContext::process_response(response, ntdll.0))
    }
}

#[allow(non_camel_case_types)]
#[repr(C)]
struct IMAGE_DEBUG_CODEVIEW {
    rsds_signature: [u8; 4],
    guid: GUID,
    age: u32,
}

/// Sets the image as the primary image. Useful
/// if you want `GetModuleHandle(null)` to return
/// the handle to the mapped image. Returns the
/// previous primary image base. The caller must ensure
/// that the module is valid while the primary image is
/// set, and must restore the old image base before it goes out
/// of scope
///
/// # Arguments
///
/// `image_base`: The base address of the image
unsafe fn set_as_primary_image(image_base: PVOID) -> PVOID {
    let peb = NtCurrentPeb();
    let old_base = (*peb).ImageBaseAddress;
    (*peb).ImageBaseAddress = image_base;
    old_base
}

/// A portable executable that is all nicely wrapped up in a class
pub struct PortableExecutable<'a> {
    file: MappedFile,
    file_name: Vec<u16>,
    file_path: Vec<u16>,
    nt_headers: &'a IMAGE_NT_HEADERS64,
    section_headers: &'a [IMAGE_SECTION_HEADER],
    entry_point: PLDR_INIT_ROUTINE,
    section_protections: Vec<ProtectionGuard>,
    loader_entry: Pin<Box<LDR_DATA_TABLE_ENTRY>>,
    ddag_node: Pin<Box<LDR_DDAG_NODE>>,
    context: NtContext,
    added_to_hash_tbl: bool,
    added_to_index: bool,
    called_entry_point: bool,
    last_primary: Option<PVOID>,
}

macro_rules! from_field_mut {
    ($parent: path, $field: tt, $field_ptr: expr) => {
        ($field_ptr as usize - memoffset::offset_of!($parent, $field)) as *mut $parent
    };
}

impl<'a> PortableExecutable<'a> {
    /// Adds the module to the loader hash table, enabling functions like
    /// `GetModuleHandle` to work
    fn add_to_hash_table(&mut self) {
        // insert the entry into the hash table and other relevant structures
        unsafe {
            InsertTailList(
                &mut self.context.LdrpHashTable.lock()
                    [(self.loader_entry.BaseNameHashValue & 0x1f) as usize],
                &mut self.loader_entry.HashLinks,
            );
            self.added_to_hash_tbl = true;
        }
    }

    /// Removes the module from the loader hash table
    fn remove_from_hash_table(&mut self) {
        // remove hash table and linked list entries
        let mut hash_table = self.context.LdrpHashTable.lock();
        // find our entry
        let first_entry = &mut hash_table[(self.loader_entry.BaseNameHashValue & 0x1f) as usize];
        let mut entry = first_entry.Flink;
        // if the next entry is the first one, we are done
        while !ptr::eq(entry as *const LIST_ENTRY, first_entry) {
            if ptr::eq(entry as *const _, &self.loader_entry.HashLinks as *const _) {
                // remove the entry
                unsafe {
                    RemoveEntryList(entry.as_mut().unwrap());
                }
            }
            entry = unsafe { (*entry).Flink };
        }
        debug!("Failed to find reference");
    }

    /// Adds the module to the index red-black trees
    fn add_to_index(&mut self) {
        // add it to the mapping information table
        unsafe {
            rtl_rb_tree_insert(
                &mut self.context.LdrpMappingInfoIndex.lock(),
                &mut self.loader_entry.MappingInfoIndexNode,
                |l, r| {
                    let l = from_field_mut!(LDR_DATA_TABLE_ENTRY, MappingInfoIndexNode, l);
                    let r = from_field_mut!(LDR_DATA_TABLE_ENTRY, MappingInfoIndexNode, r);
                    (*l).TimeDateStamp < (*r).TimeDateStamp
                        || ((*l).TimeDateStamp <= (*r).TimeDateStamp
                            && (*l).SizeOfImage < (*r).SizeOfImage)
                },
            )
        }
        unsafe {
            rtl_rb_tree_insert(
                &mut self.context.LdrpModuleBaseAddressIndex.lock(),
                &mut self.loader_entry.BaseAddressIndexNode,
                |l, r| {
                    let l = from_field_mut!(LDR_DATA_TABLE_ENTRY, BaseAddressIndexNode, l);
                    let r = from_field_mut!(LDR_DATA_TABLE_ENTRY, BaseAddressIndexNode, r);
                    (*l).DllBase < (*r).DllBase
                },
            )
        }
        self.added_to_index = true;
    }

    /// Removes the module from the index red-black trees
    fn remove_from_index(&mut self) {
        unsafe {
            RtlRbRemoveNode(
                &mut (*self.context.LdrpMappingInfoIndex.lock()),
                &mut self.loader_entry.MappingInfoIndexNode,
            );
            RtlRbRemoveNode(
                &mut (*self.context.LdrpModuleBaseAddressIndex.lock()),
                &mut self.loader_entry.BaseAddressIndexNode,
            );
        }
    }

    /// Calls the function entry point
    ///
    /// # Arguments
    ///
    /// `reason`: The reason for the call. Ex: `DLL_PROCESS_ATTACH`
    unsafe fn call_entry_point(&mut self, reason: u32) -> u8 {
        if let Some(entry_point) = &self.entry_point {
            entry_point(self.file.contents_mut(), reason, null_mut())
        } else {
            0
        }
    }

    /// Enables exception handling for the module
    fn enable_exceptions(&mut self) -> Result<(), Error> {
        // get the exception table
        let exception_table = self.get_exception_table()?;
        if exception_table.is_empty() {
            return Ok(());
        }
        // add the table to the process
        if unsafe {
            RtlAddFunctionTable(
                exception_table.as_mut_ptr(),
                exception_table.len() as u32,
                self.file.contents() as u64,
            ) == false as u8
        } {
            return Err(Error::ExceptionTableEntry);
        }
        Ok(())
    }

    /// Disables exception handling for the module
    fn disable_exceptions(&mut self) -> Result<(), Error> {
        // get the exception table
        let exception_table = self.get_exception_table()?;
        if exception_table.is_empty() {
            return Ok(());
        }
        // remove the table from the process
        if unsafe { RtlDeleteFunctionTable(exception_table.as_mut_ptr()) == false as u8 } {
            return Err(Error::ExceptionTableEntry);
        }
        Ok(())
    }

    /// Execute TLS callbacks
    ///
    /// # Arguments
    ///
    /// `reason`: The reason for the call to the callbacks
    fn execute_tls_callbacks(&mut self, reason: u32) -> Result<(), Error> {
        let tls_directory =
            &self.nt_headers.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS as usize];
        // navigate to the tls directory if it exists
        if tls_directory.VirtualAddress == 0 {
            return Ok(());
        }
        let tls_directory = self
            .file
            .get_rva::<IMAGE_TLS_DIRECTORY>(tls_directory.VirtualAddress as isize)
            .ok_or(Error::TLSOutOfBounds)?;
        let mut tls_callback = unsafe {
            self.file
                .get_rva::<Option<unsafe extern "stdcall" fn(PVOID, u32, PVOID)>>(
                    ((*tls_directory).AddressOfCallBacks - self.file.contents() as u64) as isize,
                )
                .ok_or(Error::CallbackOutOfBounds)?
        };
        // execute all of the TLS callbacks
        unsafe {
            while (*tls_callback).is_some() {
                debug!("Executing TLS callback at {:p}", tls_callback);
                (*tls_callback).unwrap()(self.file.contents_mut(), reason, null_mut());
                // TLS callbacks are in an array
                tls_callback = tls_callback.offset(1);
            }
        }
        Ok(())
    }

    /// Frees all TLS data
    fn free_tls_data(&mut self) -> Result<(), Error> {
        if unsafe { (self.context.LdrpReleaseTlsEntry)(self.loader_entry.as_mut().get_mut(), 0) }
            != STATUS_SUCCESS
        {
            Err(Error::TLSData)
        } else {
            Ok(())
        }
    }

    /// Handle TLS data
    fn handle_tls_data(&mut self) -> Result<(), Error> {
        if unsafe { (self.context.LdrpHandleTlsData)(self.loader_entry.as_mut().get_mut()) }
            != STATUS_SUCCESS
        {
            Err(Error::TLSData)
        } else {
            Ok(())
        }
    }

    /// Gets the exception table for the module, and its size
    fn get_exception_table(&mut self) -> Result<&'a mut [IMAGE_RUNTIME_FUNCTION_ENTRY], Error> {
        let exception_dir =
            &self.nt_headers.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION as usize];
        if exception_dir.VirtualAddress == 0 {
            // there is no exception table
            return Ok(&mut []);
        }
        let exception_table = self
            .file
            .get_rva_mut::<IMAGE_RUNTIME_FUNCTION_ENTRY>(exception_dir.VirtualAddress as isize)
            .ok_or(Error::ExceptionTableOutOfBounds)?;
        return Ok(unsafe {
            std::slice::from_raw_parts_mut(
                exception_table,
                exception_dir.Size as usize / std::mem::size_of::<IMAGE_RUNTIME_FUNCTION_ENTRY>(),
            )
        });
    }

    /// Initializes the loader entry
    fn init_ldr_entry(&mut self) -> Result<(), Error> {
        self.loader_entry.DllBase = self.file.contents_mut();
        self.loader_entry.DdagNode = self.ddag_node.as_mut().get_mut();
        unsafe {
            RtlInitUnicodeString(&mut self.loader_entry.BaseDllName, self.file_name.as_ptr());
            RtlInitUnicodeString(&mut self.loader_entry.FullDllName, self.file_path.as_ptr());
        }
        // add the module hash value to the loader entry
        let mut hash = 0;
        unsafe {
            if RtlHashUnicodeString(
                &self.loader_entry.BaseDllName,
                true as u8,
                HASH_STRING_ALGORITHM_DEFAULT,
                &mut hash,
            ) != STATUS_SUCCESS
            {
                return Err(Error::LdrEntry);
            }
        }
        self.loader_entry.BaseNameHashValue = hash;
        // set that we need to process static imports
        unsafe {
            self.loader_entry.u2.set_ProcessStaticImport(1);
        }
        // add the executable size
        self.loader_entry.TimeDateStamp = self.nt_headers.FileHeader.TimeDateStamp;
        self.loader_entry.SizeOfImage = self.nt_headers.OptionalHeader.SizeOfImage;
        self.ddag_node.State = LdrModulesReadyToRun;
        self.ddag_node.LoadCount = u32::MAX;
        // add to the loader hash table
        self.add_to_hash_table();
        // add to the red-black trees for traversal
        self.add_to_index();
        Ok(())
    }

    /// Loads the portable executable, processing any options
    ///
    /// # Arguments
    ///
    /// `path`: The path to the executable file
    ///
    /// `context`: The resolved Nt Context
    pub fn load(
        path: &str,
        context: NtContext,
    ) -> Result<PortableExecutable<'a>, anyhow::Error> {
        PortableExecutable::load_as_primary(path, context, false)
    }

    /// Loads the portable executable, processing any options
    ///
    /// # Arguments
    ///
    /// `path`: The path to the executable file
    ///
    /// `context`: The resolved Nt Context
    ///
    /// `primary`: Whether or not this module should be the primary
    /// module, and be returned upon invocation of `GetModuleHandle(null)`
    pub fn load_as_primary(
        path: &str,
        context: NtContext,
        primary: bool,
    ) -> Result<PortableExecutable<'a>, anyhow::Error> {
        // first make sure we got all of the required functions
        let mut file = MappedFile::load(path)?;
        let path = Path::new(path);
        let file_name: Vec<u16> = path
            .file_name()
            .ok_or_else(|| std::io::Error::from_raw_os_error(ERROR_FILE_NOT_FOUND as i32))?
            .encode_wide()
            .chain(std::iter::once(0))
            .collect();
        // remove the "extended length path syntax"
        let file_path: Vec<u16> = path
            .canonicalize()?
            .as_os_str()
            .encode_wide()
            .skip(4)
            .chain(std::iter::once(0))
            .collect();
        debug!(
            "Loading {} at path {}",
            OsString::from_wide(&file_name[..])
                .as_os_str()
                .to_string_lossy(),
            OsString::from_wide(&file_path[..])
                .as_os_str()
                .to_string_lossy()
        );
        let (nt_headers, section_headers) = PortableExecutable::load_headers(&mut file)?;
        let entry_point = PortableExecutable::load_entry_point(&mut file, nt_headers)?;
        let mut pe = PortableExecutable {
            file,
            file_name,
            file_path,
            nt_headers,
            section_headers,
            entry_point,
            section_protections: Vec::new(),
            loader_entry: Box::pin(LDR_DATA_TABLE_ENTRY {
                InLoadOrderLinks: LIST_ENTRY::default(),
                InMemoryOrderLinks: LIST_ENTRY::default(),
                u1: LDR_DATA_TABLE_ENTRY_u1 {
                    InInitializationOrderLinks: LIST_ENTRY::default(),
                },
                DllBase: null_mut(),
                EntryPoint: None,
                SizeOfImage: 0,
                FullDllName: UNICODE_STRING::default(),
                BaseDllName: UNICODE_STRING::default(),
                u2: LDR_DATA_TABLE_ENTRY_u2 { Flags: 0 },
                ObsoleteLoadCount: 0,
                TlsIndex: 0,
                HashLinks: LIST_ENTRY::default(),
                TimeDateStamp: 0,
                EntryPointActivationContext: null_mut(),
                Lock: null_mut(),
                DdagNode: null_mut(),
                NodeModuleLink: LIST_ENTRY::default(),
                LoadContext: null_mut(),
                ParentDllBase: null_mut(),
                SwitchBackContext: null_mut(),
                BaseAddressIndexNode: RTL_BALANCED_NODE::default(),
                MappingInfoIndexNode: RTL_BALANCED_NODE::default(),
                OriginalBase: 0,
                LoadTime: LARGE_INTEGER::default(),
                BaseNameHashValue: 0,
                LoadReason: 0,
                ImplicitPathOptions: 0,
                ReferenceCount: 0,
                DependentLoadFlags: 0,
                SigningLevel: 0,
            }),
            ddag_node: Box::pin(LDR_DDAG_NODE {
                Modules: LIST_ENTRY::default(),
                ServiceTagList: null_mut(),
                LoadCount: 0,
                LoadWhileUnloadingCount: 0,
                LowestLink: 0,
                u: LDR_DDAG_NODE_u {
                    Dependencies: LDRP_CSLIST { Tail: null_mut() },
                },
                IncomingDependencies: LDRP_CSLIST { Tail: null_mut() },
                State: LDR_DDAG_STATE::default(),
                CondenseLink: SINGLE_LIST_ENTRY::default(),
                PreorderNumber: 0,
            }),
            context,
            added_to_hash_tbl: false,
            added_to_index: false,
            called_entry_point: false,
            last_primary: None,
        };
        pe.init_ldr_entry()?;
        pe.resolve_imports()?;
        pe.protect_sections()?;
        pe.enable_exceptions()?;
        if primary {
            pe.set_self_as_primary_image();
        }
        pe.execute_tls_callbacks(DLL_PROCESS_ATTACH)?;
        pe.handle_tls_data()?;
        Ok(pe)
    }

    /// Loads the entry point from a mapped file
    ///
    /// # Arguments
    ///
    /// `file`: The mapped executable file
    fn load_entry_point(
        file: &mut MappedFile,
        nt_headers: &IMAGE_NT_HEADERS64,
    ) -> Result<PLDR_INIT_ROUTINE, Error> {
        let address_of_ep = nt_headers.OptionalHeader.AddressOfEntryPoint;
        if address_of_ep == 0 {
            // PEs can sometimes not have entry points
            return Ok(None);
        }
        unsafe {
            // we transmute here because I have no earthly idea how to return a generic function
            let entry_point: PLDR_INIT_ROUTINE = std::mem::transmute(
                file.get_rva::<u8>(address_of_ep as isize)
                    .ok_or(Error::EPOutOfBounds)?,
            );
            Ok(entry_point)
        }
    }

    /// Loads the headers from a mapped file
    ///
    /// # Arguments
    ///
    /// `file`: The mapped executable file
    fn load_headers(
        file: &mut MappedFile,
    ) -> Result<(&'a IMAGE_NT_HEADERS64, &'a [IMAGE_SECTION_HEADER]), Error> {
        unsafe {
            let dos_header = &*file
                .get_rva::<IMAGE_DOS_HEADER>(0)
                .ok_or(Error::DOSOutOfBounds)?;
            let nt_headers = &*file
                .get_rva::<IMAGE_NT_HEADERS64>(dos_header.e_lfanew as isize)
                .ok_or(Error::NTOutOfBounds)?;
            // ensure supported architecture
            if nt_headers.FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64
                || nt_headers.OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC
            {
                return Err(Error::UnsupportedArch);
            }
            // load section headers
            let section_headers = std::slice::from_raw_parts(
                file.get_rva_size_chk::<IMAGE_SECTION_HEADER>(
                    dos_header.e_lfanew as isize
                        + std::mem::size_of::<IMAGE_NT_HEADERS64>() as isize,
                    nt_headers.FileHeader.NumberOfSections as usize
                        * std::mem::size_of::<IMAGE_SECTION_HEADER>(),
                )
                .ok_or(Error::SectOutOfBounds)?,
                nt_headers.FileHeader.NumberOfSections as usize,
            );
            Ok((nt_headers, section_headers))
        }
    }

    /// Returns the handle for the loaded process
    pub fn module_handle(&mut self) -> HMODULE {
        self.file.contents() as HMODULE
    }

    /// Protects all of the sections with their specified protections
    fn protect_sections(&mut self) -> Result<(), anyhow::Error> {
        for &section in self.section_headers {
            unsafe {
                self.section_protections.push(ProtectionGuard::new(
                    self.file
                        .get_rva_mut::<c_void>(section.VirtualAddress as isize)
                        .ok_or(Error::SectOutOfBounds)?,
                    *section.Misc.VirtualSize() as usize,
                    PortableExecutable::section_flags_to_prot(section.Characteristics),
                )?)
            }
        }
        Ok(())
    }

    /// Resolves imports from the NT headers
    fn resolve_imports(&mut self) -> anyhow::Result<()> {
        unsafe {
            let iat_directory = &self.nt_headers.OptionalHeader.DataDirectory
                [IMAGE_DIRECTORY_ENTRY_IMPORT as usize];
            // if there is not an IAT, just return
            if iat_directory.VirtualAddress == 0 {
                return Ok(());
            }
            // grab the IAT from the header. ensure there is space for the entire directory
            let iat_entry = self
                .file
                .get_rva_size_chk::<IMAGE_IMPORT_DESCRIPTOR>(
                    iat_directory.VirtualAddress as isize,
                    iat_directory.Size as usize,
                )
                .ok_or(Error::IDOutOfBounds)?;
            self.resolve_import_descriptors(std::slice::from_raw_parts(
                iat_entry,
                (iat_directory.Size as usize) / std::mem::size_of::<IMAGE_IMPORT_DESCRIPTOR>(),
            ))
        }
    }

    /// Resolves imports from the specified import descriptors. This
    /// is separate because some executables have separate IATs from the
    /// NT header IATs, and it is necessary to resolve them as well
    ///
    /// # Arguments
    ///
    /// `table`: The import descriptors
    pub fn resolve_import_descriptors(
        &mut self,
        table: &[IMAGE_IMPORT_DESCRIPTOR],
    ) -> anyhow::Result<()> {
        // we know the table is not null
        for &entry in table {
            // ignore empty entries
            if entry.Name == 0 {
                continue;
            }
            // load the name of the import. here we could crash if the string was on the edge
            // of the page. but that's a waste to check for every byte
            let name = self
                .file
                .get_rva::<i8>(entry.Name as isize)
                .ok_or(Error::LibNameOutOfBounds)?;
            unsafe {
                debug!("Loading library {:?}", CStr::from_ptr(name));
                let library = LoadLibraryA(name);
                if library.is_null() {
                    return Err(std::io::Error::last_os_error().into());
                }
                // skip empty tables
                if entry.FirstThunk == 0 {
                    continue;
                }
                let mut thunk = self
                    .file
                    .get_rva_mut::<IMAGE_THUNK_DATA>(entry.FirstThunk as isize)
                    .ok_or(Error::IATOutOfBounds)?;
                while (*thunk).u1.AddressOfData() != &0 {
                    let proc_name = if ((*thunk).u1.Ordinal() & IMAGE_ORDINAL_FLAG) != 0 {
                        (*(*thunk).u1.Ordinal() & !IMAGE_ORDINAL_FLAG) as *const i8
                    } else {
                        &(*self
                            .file
                            .get_rva::<IMAGE_IMPORT_BY_NAME>(*(*thunk).u1.AddressOfData() as isize)
                            .ok_or(Error::ProcNameOutOfBounds)?)
                        .Name as *const i8
                    };
                    if ((*thunk).u1.Ordinal() & IMAGE_ORDINAL_FLAG) != 0 {
                        debug!(
                            "Loading procedure with ordinal {}",
                            (*thunk).u1.Ordinal() & !IMAGE_ORDINAL_FLAG
                        );
                    } else {
                        // if it is a pointer, make sure it is not null
                        if proc_name.is_null() {
                            return Err(Error::NullProcName.into());
                        }
                        debug!(
                            "Loading procedure with name {:?}",
                            CStr::from_ptr(proc_name)
                        );
                    }
                    // load the function
                    let func = GetProcAddress(library, proc_name);
                    if func.is_null() {
                        return Err(std::io::Error::last_os_error().into());
                    }
                    let func_ptr = (*thunk).u1.Function_mut() as *mut ULONGLONG;
                    // write the function address
                    protected_write(func_ptr, func as ULONGLONG)?;
                    thunk = thunk.add(1);
                }
            }
        }
        Ok(())
    }

    /// Runs the executable's entry point with `DLL_PROCESS_ATTACH`
    ///
    /// # Safety
    ///
    /// The safety of this function is entirely dependent on whether or not
    /// the underlying executable is safe. This function panics if the executable
    /// entry point has already been called
    pub unsafe fn run(&mut self) -> u8 {
        if self.called_entry_point {
            panic!("OEP already called")
        }
        let res = self.call_entry_point(DLL_PROCESS_ATTACH);
        self.called_entry_point = true;
        res
    }

    /// Converts section flags to page protection flags
    ///
    /// # Arguments
    ///
    /// `section_flags`: The section flags
    fn section_flags_to_prot(section_flags: u32) -> u32 {
        if section_flags & IMAGE_SCN_MEM_EXECUTE != 0 {
            PAGE_EXECUTE_READ
        } else if section_flags & IMAGE_SCN_MEM_WRITE != 0 {
            PAGE_READWRITE
        } else {
            PAGE_READONLY
        }
    }

    /// Sets this mapped image as the primary image,
    /// as if by calling `set_as_primary_image(self.module_handle())`.
    fn set_self_as_primary_image(&mut self) {
        self.last_primary =
            Some(unsafe { set_as_primary_image(self.module_handle() as *mut c_void) });
    }
}

impl<'a> Drop for PortableExecutable<'a> {
    // unload the loader entry. this is used for GetModuleHandle
    fn drop(&mut self) {
        // call each tls callback with process_detach
        if let Err(e) = self.execute_tls_callbacks(DLL_PROCESS_DETACH) {
            debug!("Failed to execute TLS callbacks on exit: {}", e.to_string())
        };
        // call the entry point with detach
        unsafe { self.call_entry_point(DLL_PROCESS_DETACH) };
        // disable exceptions afterwards in case a TLS callback uses them
        if let Err(e) = self.disable_exceptions() {
            debug!("Failed to disable exceptions: {}", e.to_string())
        }
        // remove ourselves from the index RB trees
        if self.added_to_index {
            self.remove_from_index();
        }
        // remove ourselves from the hash table
        if self.added_to_hash_tbl {
            self.remove_from_hash_table();
        }
        // free TLS data
        if let Err(e) = self.free_tls_data() {
            debug!("Failed to free TLS data: {}", e.to_string())
        }
        // ensure we are not the primary image anymore
        if let Some(last_image) = self.last_primary {
            unsafe { set_as_primary_image(last_image) };
        }
    }
}

unsafe impl<'a> Send for PortableExecutable<'a> {}

#[cfg(test)]
mod test {
    use super::*;
    use lazy_static::lazy_static;
    use serial_test::serial;
    use std::ptr::null;
    use tokio::runtime::Runtime;
    use winapi::{
        shared::winerror::{ERROR_BAD_EXE_FORMAT, ERROR_MOD_NOT_FOUND, ERROR_PROC_NOT_FOUND},
        um::libloaderapi::GetModuleFileNameW,
    };

    lazy_static! {
        static ref NT_CONTEXT: NtContext = Runtime::new()
            .unwrap()
            .block_on(NtContext::resolve_local(
                &OffsetHandler::new("test/cache.json").unwrap()
            ))
            .unwrap();
    }

    #[test]
    #[serial]
    fn bad_dos() {
        let err: std::io::Error = PortableExecutable::load("test/baddos.exe", NT_CONTEXT.clone())
            .err()
            .unwrap()
            .downcast()
            .unwrap();
        assert_eq!(err.raw_os_error().unwrap(), ERROR_BAD_EXE_FORMAT as i32);
    }

    #[test]
    #[serial]
    fn bad_section() {
        let err: std::io::Error = PortableExecutable::load("test/badsection.exe", NT_CONTEXT.clone())
            .err()
            .unwrap()
            .downcast()
            .unwrap();
        assert_eq!(err.raw_os_error().unwrap(), ERROR_BAD_EXE_FORMAT as i32);
    }

    #[test]
    #[serial]
    fn bad_entry() {
        let err: Error = PortableExecutable::load("test/badentry.exe", NT_CONTEXT.clone())
            .err()
            .unwrap()
            .downcast()
            .unwrap();
        assert_eq!(err, Error::EPOutOfBounds);
    }

    #[test]
    #[serial]
    fn bad_mod() {
        let err: std::io::Error = PortableExecutable::load("test/badmod.exe", NT_CONTEXT.clone())
            .err()
            .unwrap()
            .downcast()
            .unwrap();
        assert_eq!(err.raw_os_error().unwrap(), ERROR_MOD_NOT_FOUND as i32);
    }

    #[test]
    #[serial]
    fn bad_proc() {
        let err: std::io::Error = PortableExecutable::load("test/badproc.exe", NT_CONTEXT.clone())
            .err()
            .unwrap()
            .downcast()
            .unwrap();
        assert_eq!(err.raw_os_error().unwrap(), ERROR_PROC_NOT_FOUND as i32);
    }

    // we only support x86-64/ARM64 for now
    #[test]
    #[serial]
    fn bad_arch() {
        let err: Error = PortableExecutable::load("test/x86.exe", NT_CONTEXT.clone())
            .err()
            .unwrap()
            .downcast()
            .unwrap();
        assert_eq!(err, Error::UnsupportedArch);
    }

    #[test]
    #[serial]
    fn basic_image() {
        let mut image = PortableExecutable::load("test/basic.exe", NT_CONTEXT.clone()).unwrap();
        unsafe {
            assert_eq!(image.run(), 23);
        }
    }

    #[test]
    #[serial]
    fn ldr_entry() {
        let mut file_name_buf = [0 as u16; 128];
        let handle;
        {
            let _image = PortableExecutable::load("test/basic.exe", NT_CONTEXT.clone()).unwrap();
            handle = unsafe { GetModuleHandleW(to_wide("basic.exe").as_ptr()) };
            assert!(!handle.is_null());
            let name_len = unsafe {
                GetModuleFileNameW(
                    handle,
                    file_name_buf.as_mut_ptr(),
                    file_name_buf.len() as u32,
                )
            };
            assert_ne!(name_len, 0);
        }
        assert!(unsafe { GetModuleHandleW(to_wide("basic.exe").as_ptr()) }.is_null());
        assert_eq!(
            unsafe {
                GetModuleFileNameW(
                    handle,
                    file_name_buf.as_mut_ptr(),
                    file_name_buf.len() as u32,
                )
            },
            0
        );
    }

    #[test]
    #[serial]
    fn tls() {
        let mut image = PortableExecutable::load("test/tls.exe", NT_CONTEXT.clone()).unwrap();
        unsafe {
            assert_eq!(image.run(), 7);
        }
    }

    #[test]
    #[serial]
    fn primary_image() {
        let original_handle = unsafe { GetModuleHandleW(null()) };
        {
            let mut image = PortableExecutable::load("test/basic.exe", NT_CONTEXT.clone()).unwrap();
            assert_ne!(image.module_handle(), unsafe { GetModuleHandleW(null()) });
            image.set_self_as_primary_image();
            assert_eq!(image.module_handle(), unsafe { GetModuleHandleW(null()) });
        }
        assert_eq!(original_handle, unsafe { GetModuleHandleW(null()) });
    }

    #[tokio::test]
    #[serial]
    #[cfg(all(feature = "tls", feature = "server"))]
    async fn tls_server() {
        use crate::server::Server;
        use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
        use tonic::transport::Identity;
        // start the server
        let endpoint = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 44443));
        // we stole these certificates from the tonic test suite
        let cert = tokio::fs::read("test/server.pem").await.unwrap();
        let key = tokio::fs::read("test/server.key").await.unwrap();
        let identity = Identity::from_pem(cert, key);
        let server = Server::new(endpoint, "test/tls_cache.json", Some(identity)).unwrap();
        // resolve
        let ca_cert = tokio::fs::read("test/ca.pem").await.unwrap();
        let ca_cert = Certificate::from_pem(ca_cert);
        tokio::select! {
            _ = server.run() => {},
            res = NtContext::resolve_tls("localhost", 44443, Some(ca_cert), None) => { res.unwrap(); }
        };
    }
}