Skip to content

Commit f4298c7

Browse files
committed
feat: block dll policy
1 parent 50197f9 commit f4298c7

File tree

5 files changed

+204
-0
lines changed

5 files changed

+204
-0
lines changed

Block_DLL_Policy/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Block_DLL_Policy/Cargo.lock

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Block_DLL_Policy/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "block_dll_policy"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
windows = { version = "0.52.0", features = ["Win32_System_Threading", "Win32_Foundation", "Win32_Security", "Win32_System_Memory", "Win32_System_SystemServices"] }

Block_DLL_Policy/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Block DLL Policy 🦀
2+
3+
<p align="left">
4+
<a href="https://www.rust-lang.org/"><img src="https://img.shields.io/badge/made%20with-Rust-red"></a>
5+
<a href="#"><img src="https://img.shields.io/badge/platform-windows-blueviolet"></a>
6+
</p>
7+
8+
- [Overview](#overview)
9+
- [Usage](#usage)
10+
11+
# Overview
12+
The "Block DLL Policy" technique is an effective strategy for preventing non-Microsoft-signed DLLs from being loaded into system processes. This policy can be applied both when creating new processes and implemented in our local process.
13+
14+
# Usage
15+
16+
You can run with cargo run or the compiled binary directly:
17+
```sh
18+
cargo run
19+
```
20+
```sh
21+
target/release/block_dll_policy.exe
22+
```

Block_DLL_Policy/src/main.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use std::{ptr::null_mut, ffi::c_void};
2+
use windows::core::PSTR;
3+
use windows::Win32::System::{
4+
Memory::{GetProcessHeap, HeapAlloc, HEAP_ZERO_MEMORY},
5+
SystemServices::{
6+
PROCESS_MITIGATION_BINARY_SIGNATURE_POLICY, PROCESS_MITIGATION_BINARY_SIGNATURE_POLICY_0,
7+
},
8+
Threading::{
9+
CreateProcessA, DeleteProcThreadAttributeList, InitializeProcThreadAttributeList,
10+
ProcessSignaturePolicy, SetProcessMitigationPolicy, UpdateProcThreadAttribute,
11+
EXTENDED_STARTUPINFO_PRESENT, LPPROC_THREAD_ATTRIBUTE_LIST, PROCESS_INFORMATION,
12+
PROCESS_MITIGATION_POLICY, PROC_THREAD_ATTRIBUTE_MITIGATION_POLICY, STARTUPINFOEXA,
13+
STARTUPINFOW_FLAGS,
14+
},
15+
};
16+
17+
const PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON: u64 = 0x00000001u64 << 44;
18+
19+
fn main() {
20+
create_process_block_dll();
21+
// current_process_block_dll();
22+
}
23+
24+
fn current_process_block_dll() {
25+
unsafe {
26+
let mut policy = PROCESS_MITIGATION_BINARY_SIGNATURE_POLICY {
27+
Anonymous: PROCESS_MITIGATION_BINARY_SIGNATURE_POLICY_0 { Flags: 0 },
28+
};
29+
policy.Anonymous.Flags |= 1 << 0;
30+
let _ = SetProcessMitigationPolicy(
31+
PROCESS_MITIGATION_POLICY(ProcessSignaturePolicy.0),
32+
&policy as *const _ as *const _,
33+
std::mem::size_of_val(&policy),
34+
);
35+
}
36+
}
37+
38+
fn create_process_block_dll() {
39+
let mut process_information = PROCESS_INFORMATION::default();
40+
let mut startup_info = STARTUPINFOEXA::default();
41+
startup_info.StartupInfo.cb = std::mem::size_of::<STARTUPINFOEXA>() as u32;
42+
startup_info.StartupInfo.dwFlags = STARTUPINFOW_FLAGS(EXTENDED_STARTUPINFO_PRESENT.0);
43+
let mut attr_size: usize = 0;
44+
unsafe {
45+
let _ = InitializeProcThreadAttributeList(
46+
LPPROC_THREAD_ATTRIBUTE_LIST(null_mut()),
47+
1,
48+
0,
49+
&mut attr_size,
50+
);
51+
52+
let attr_list = LPPROC_THREAD_ATTRIBUTE_LIST(HeapAlloc(
53+
GetProcessHeap().unwrap(),
54+
HEAP_ZERO_MEMORY,
55+
attr_size,
56+
));
57+
58+
let _ = InitializeProcThreadAttributeList(attr_list, 1, 0, &mut attr_size);
59+
60+
let policy = PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON;
61+
let _ = UpdateProcThreadAttribute(
62+
attr_list,
63+
0,
64+
PROC_THREAD_ATTRIBUTE_MITIGATION_POLICY as usize,
65+
Some(&policy as *const _ as *const c_void),
66+
std::mem::size_of::<u64>(),
67+
None,
68+
None,
69+
);
70+
71+
let windir = std::env::var("WINDIR").unwrap() + "\\System32\\SystemSettingsBroker.exe";
72+
startup_info.lpAttributeList = attr_list;
73+
let _ = CreateProcessA(
74+
None,
75+
PSTR(windir.as_ptr() as _),
76+
None,
77+
None,
78+
false,
79+
EXTENDED_STARTUPINFO_PRESENT,
80+
None,
81+
None,
82+
&startup_info.StartupInfo,
83+
&mut process_information,
84+
);
85+
86+
DeleteProcThreadAttributeList(attr_list);
87+
}
88+
}

0 commit comments

Comments
 (0)