Skip to content

Commit 4f6f7fb

Browse files
committed
AMSI Update
1 parent 9a54176 commit 4f6f7fb

File tree

3 files changed

+508
-0
lines changed

3 files changed

+508
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[package]
2+
name = "Amsi_Page_Guard_Exceptions"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
dinvk = "0.4.2"
8+
windows-targets = "0.53"
9+
10+
[dependencies.windows-sys]
11+
version = "0.61.2"
12+
features = [
13+
"Win32_Foundation",
14+
"Win32_Security",
15+
"Win32_Globalization",
16+
"Win32_System_Threading",
17+
"Win32_UI_WindowsAndMessaging",
18+
"Win32_System_Memory",
19+
"Win32_System_Registry",
20+
"Win32_System_Diagnostics_Debug",
21+
"Win32_System_SystemServices",
22+
"Win32_System_Environment",
23+
"Win32_UI_Shell",
24+
"Win32_System_LibraryLoader",
25+
"Win32_System_SystemInformation",
26+
"Win32_System_WindowsProgramming",
27+
"Win32_System_Diagnostics_ToolHelp",
28+
"Win32_UI_Input_KeyboardAndMouse",
29+
"Win32_Storage_FileSystem",
30+
"Win32_System_ProcessStatus",
31+
"Win32_System_Kernel",
32+
"Win32_System_IO",
33+
"Win32_System_Diagnostics_ProcessSnapshotting",
34+
35+
"Wdk_Foundation",
36+
"Wdk_System_SystemInformation",
37+
]
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#![allow(non_camel_case_types)]
2+
#![allow(non_snake_case)]
3+
#![allow(dead_code)]
4+
5+
use std::ffi::c_void;
6+
use std::fmt;
7+
use std::ptr::null_mut;
8+
use windows_sys::{
9+
Wdk::Foundation::OBJECT_ATTRIBUTES,
10+
Win32::Foundation::HANDLE,
11+
Win32::Foundation::{NTSTATUS, UNICODE_STRING},
12+
Win32::System::Diagnostics::Debug::EXCEPTION_POINTERS,
13+
Win32::System::Memory::PAGE_PROTECTION_FLAGS,
14+
};
15+
16+
use windows_targets::link;
17+
18+
// --- LARGE_INTEGER ---
19+
20+
#[repr(C)]
21+
#[derive(Clone, Copy, Debug, Default)]
22+
pub struct LARGE_INTEGER_s {
23+
pub LowPart: u32,
24+
pub HighPart: i32,
25+
}
26+
27+
#[repr(C)]
28+
#[derive(Clone, Copy, Debug, Default)]
29+
pub struct LARGE_INTEGER_u {
30+
pub LowPart: u32,
31+
pub HighPart: i32,
32+
}
33+
34+
#[repr(C)]
35+
#[derive(Clone, Copy)]
36+
pub union LARGE_INTEGER {
37+
pub _anonymous: i64,
38+
pub s: LARGE_INTEGER_s,
39+
pub u: LARGE_INTEGER_u,
40+
pub QuadPart: i64,
41+
}
42+
43+
impl fmt::Debug for LARGE_INTEGER {
44+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45+
unsafe { write!(f, "{}", self.QuadPart) }
46+
}
47+
}
48+
49+
pub type PLARGE_INTEGER = *mut LARGE_INTEGER;
50+
51+
// --- ULARGE_INTEGER ---
52+
53+
#[repr(C)]
54+
#[derive(Clone, Copy, Debug, Default)]
55+
pub struct ULARGE_INTEGER_s {
56+
pub LowPart: u32,
57+
pub HighPart: u32,
58+
}
59+
60+
#[repr(C)]
61+
#[derive(Clone, Copy)]
62+
pub union ULARGE_INTEGER {
63+
pub _anonymous: u64,
64+
pub s: ULARGE_INTEGER_s,
65+
pub u: ULARGE_INTEGER_s,
66+
pub QuadPart: u64,
67+
}
68+
69+
impl fmt::Debug for ULARGE_INTEGER {
70+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71+
unsafe { write!(f, "{}", self.QuadPart) }
72+
}
73+
}
74+
75+
pub type PULARGE_INTEGER = *mut ULARGE_INTEGER;
76+
77+
pub type POBJECT_ATTRIBUTES = *mut OBJECT_ATTRIBUTES;
78+
pub type PUNICODE_STRING = *mut UNICODE_STRING;
79+
80+
#[inline]
81+
pub fn InitializeObjectAttributes(
82+
p: POBJECT_ATTRIBUTES,
83+
n: PUNICODE_STRING,
84+
a: u32,
85+
r: *mut core::ffi::c_void,
86+
s: *mut core::ffi::c_void,
87+
) {
88+
use core::mem::size_of;
89+
unsafe {
90+
(*p).Length = size_of::<OBJECT_ATTRIBUTES>() as u32;
91+
(*p).RootDirectory = r;
92+
(*p).Attributes = a;
93+
(*p).ObjectName = n;
94+
(*p).SecurityDescriptor = s as _;
95+
(*p).SecurityQualityOfService = null_mut();
96+
}
97+
}
98+
99+
pub const fn nt_success(nt_status: NTSTATUS) -> bool {
100+
nt_status >= 0
101+
}
102+
103+
pub type AMSI_RESULT = u32;
104+
105+
pub const fn c_hash(s: &str) -> u32 {
106+
let mut hash = 0x811c9dc5u32;
107+
let bytes = s.as_bytes();
108+
let mut i = 0;
109+
while i < bytes.len() {
110+
hash ^= bytes[i] as u32;
111+
hash = hash.wrapping_mul(0x01000193);
112+
i += 1;
113+
}
114+
hash
115+
}
116+
117+
pub const fn w_hash(s: &[u16]) -> u32 {
118+
let mut hash = 0x811c9dc5u32;
119+
let mut i = 0;
120+
while i < s.len() {
121+
hash ^= s[i] as u32;
122+
hash = hash.wrapping_mul(0x01000193);
123+
i += 1;
124+
}
125+
hash
126+
}
127+
128+
type PVECTORED_EXCEPTION_HANDLER = extern "system" fn(*mut EXCEPTION_POINTERS) -> i32;
129+
130+
link!("ntdll.dll" "system" fn RtlAddVectoredExceptionHandler(First: u32, Handler: PVECTORED_EXCEPTION_HANDLER) -> *mut c_void);
131+
link!("ntdll.dll" "system" fn RtlRemoveVectoredExceptionHandler(Handle: *mut c_void) -> u32);
132+
link!("ntdll.dll" "system" fn NtProtectVirtualMemory(ProcessHandle: HANDLE, BaseAddress: *mut *mut c_void, NumberOfBytesToProtect: *mut usize, NewAccessProtection: PAGE_PROTECTION_FLAGS, OldAccessProtection: *mut PAGE_PROTECTION_FLAGS) -> NTSTATUS);
133+
link!("ntdll.dll" "system" fn NtDelayExecution(Alertable: i32, DelayInterval: PLARGE_INTEGER) -> NTSTATUS);

0 commit comments

Comments
 (0)