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
|
// Copyright 2025-2026 Dillution <[email protected]>.
//
// This file is part of DPIBreak.
//
// DPIBreak is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// DPIBreak 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 General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with DPIBreak. If not, see <https://www.gnu.org/licenses/>.
use anyhow::Result;
use windivert::{
WinDivert,
layer::NetworkLayer
};
use std::sync::{atomic::Ordering, LazyLock, Mutex, MutexGuard};
use crate::{log::LogLevel, log_println, splash};
use crate::opt;
fn windivert_filter() -> String {
let base = "(outbound and tcp and tcp.DstPort == 443 \
and tcp.Payload[0] == 22 \
and tcp.Payload[5] == 1)";
if crate::opt::fake_autottl() {
let synack = "(!outbound and tcp and tcp.SrcPort == 443 \
and tcp.Syn and tcp.Ack)";
format!("({base} or {synack}) and !impostor")
} else {
format!("{base} and !impostor")
}
}
pub static WINDIVERT_HANDLE: LazyLock<Mutex<WinDivert<NetworkLayer>>> = LazyLock::new(|| {
use windivert::*;
let filter = windivert_filter();
let h = match WinDivert::network(&filter, 0, prelude::WinDivertFlags::new()) {
Ok(h) => {
log_println!(LogLevel::Info, "windivert: HANDLE constructed for {}", filter);
h
},
Err(e) => {
log_println!(LogLevel::Error, "windivert: {}", e);
std::process::exit(1);
}
};
Mutex::new(h)
});
fn lock_handle() -> MutexGuard<'static, WinDivert<NetworkLayer>> {
WINDIVERT_HANDLE.lock().expect("mutex poisoned")
}
pub fn bootstrap() -> Result<()> {
if opt::daemonize() {
service_main();
}
Ok(())
}
pub fn cleanup() -> Result<()> {
// FIXME: `CloseAction::Uninstall' fail with `ERR_INVALID_NAME' here.
// (maybe crate windivert problem, which sending not-null-terminating
// "WinDivert" string to `OpenServiceA' with "WinDivert".as_ptr())
// So just closing the handle here instead.
//
// User might want to run `sc stop windivert' on administrator shell
// after terminating the dpibreak.
lock_handle().close(windivert::CloseAction::Nothing)?;
log_println!(LogLevel::Info, "windivert: HANDLE closed");
Ok(())
}
pub fn send_to_raw(pkt: &[u8]) -> Result<()> {
use windivert::*;
let mut p = unsafe { packet::WinDivertPacket::<NetworkLayer>::new(pkt.to_vec()) };
p.address.set_outbound(true);
p.address.set_ip_checksum(true); // TODO: test if this is needed
p.address.set_tcp_checksum(false); // For badsum; anyway it is already calculated
p.address.set_impostor(true); // to prevent inf loop
lock_handle().send(&p)?;
Ok(())
}
use crate::RUNNING;
pub fn run() -> Result<()> {
use crate::{handle_packet, MESSAGE_AT_RUN};
use super::PACKET_SIZE_CAP;
let mut windivert_buf = vec![0u8; 65536];
let mut buf = Vec::<u8>::with_capacity(PACKET_SIZE_CAP);
splash!("{MESSAGE_AT_RUN}");
while RUNNING.load(Ordering::SeqCst) {
let pkt = lock_handle().recv(Some(&mut windivert_buf))?;
handle_packet!(
&pkt.data,
&mut buf,
handled => {},
rejected => { lock_handle().send(&pkt)?; }
);
}
Ok(())
}
fn service_run() -> Result<()> {
let opt = opt::Opt::from_args()?;
opt.set_opt()?;
bootstrap()?;
run()?;
cleanup()?;
Ok(())
}
fn service_run_1() {
if service_main().is_err() {
std::process::exit(1);
}
std::process::exit(0);
}
fn service_main() {
use windows_services::Command;
match windows_services::Service::new()
.can_stop()
.run(|_, command| {
match command {
Command::Start => {
std::thread::spawn(|| service_main_1());
}
Command::Stop => {
RUNNING.store(false, Ordering::SeqCst);
}
_ => {}
}
}) {
Ok(_) => {}
Err(e) => {
println!("{e}");
std::process::exit(1);
}
};
}
|