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
|
// 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 iptables::IPTables;
use std::sync::{
atomic::{AtomicBool, Ordering},
Mutex,
LazyLock
};
use std::process::{Command, Stdio};
use std::io::Write;
use anyhow::{Result, Error, Context, anyhow};
use crate::{log::LogLevel, log_println, splash, MESSAGE_AT_RUN};
pub static IS_U32_SUPPORTED: AtomicBool = AtomicBool::new(false);
pub static IS_XT_U32_LOADED_BY_US: AtomicBool = AtomicBool::new(false);
static IS_NFT_NOT_SUPPORTED: AtomicBool = AtomicBool::new(false);
const DPIBREAK_CHAIN: &str = "DPIBREAK";
const INJECT_MARK: u32 = 0xD001;
fn exec_process(args: &[&str], input: Option<&str>) -> Result<()> {
if args.is_empty() {
return Err(anyhow!("command args cannot be empty"));
}
let program = args[0];
let stdin_mode = if input.is_some() { Stdio::piped() } else { Stdio::null() };
let mut child = Command::new(program)
.args(&args[1..])
.stdin(stdin_mode)
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.with_context(|| format!("failed to spawn {}", program))?;
if let Some(data) = input {
if let Some(mut stdin) = child.stdin.take() {
stdin.write_all(data.as_bytes())
.with_context(|| format!("failed to write input to {}", program))?;
}
}
let output = child.wait_with_output()
.with_context(|| format!("failed to wait for {}", program))?;
match output.status.code() {
Some(0) => Ok(()),
Some(code) => Err(anyhow!("{} exited with status {}: {}", program, code,
String::from_utf8_lossy(&output.stderr))),
None => Err(anyhow!("{} terminated by signal", program))
}
}
/// Apply json format nft rules with `nft_command() -j -f -`.
fn apply_nft_rules(rule: &str) -> Result<()> {
exec_process(&[crate::opt::nft_command(), "-j", "-f", "-"], Some(rule))
}
fn is_xt_u32_loaded() -> bool {
std::fs::read_to_string("/proc/modules")
.map(|s| s.lines().any(|l| l.starts_with("xt_u32 ")))
.unwrap_or(false)
}
fn ensure_xt_u32() -> Result<()> {
let before = is_xt_u32_loaded();
Command::new("modprobe").args(&["-q", "xt_u32"]).status()?;
let after = is_xt_u32_loaded();
if !before && after {
IS_XT_U32_LOADED_BY_US.store(true, Ordering::Relaxed);
}
Ok(())
}
fn is_u32_supported(ipt: &IPTables) -> bool {
if IS_U32_SUPPORTED.load(Ordering::Relaxed) {
return true;
}
if ensure_xt_u32().is_err() {
log_println!(LogLevel::Warning, "xt_u32 not supported");
return false;
}
log_println!(LogLevel::Info, "xt_u32 loaded");
let rule = "-m u32 --u32 \'0x0=0x0\' -j RETURN";
match ipt.insert("raw", "PREROUTING", rule, 1) {
Ok(_) => {
_ = ipt.delete("raw", "PREROUTING", rule);
IS_U32_SUPPORTED.store(true, Ordering::Relaxed);
true
}
Err(_) => false
}
}
fn iptables_err(e: impl ToString) -> Error {
Error::msg(format!("iptables: {}", e.to_string()))
}
fn install_iptables_rules(ipt: &IPTables) -> Result<()> {
let base = format!("-p tcp --dport 443 -j NFQUEUE --queue-num {} --queue-bypass",
crate::opt::queue_num());
let rule = if is_u32_supported(ipt) {
const U32: &str = "-m u32 --u32 \
\'0>>22&0x3C @ 12>>26&0x3C @ 0>>24&0xFF=0x16 && \
0>>22&0x3C @ 12>>26&0x3C @ 2>>24&0xFF=0x01\'";
format!("{} {}", base, U32)
} else {
base
};
ipt.new_chain("mangle", DPIBREAK_CHAIN).map_err(iptables_err)?;
// prevent inf loop
ipt.insert(
"mangle",
DPIBREAK_CHAIN,
&format!("-m mark --mark {:#x} -j RETURN", INJECT_MARK),
1
).map_err(iptables_err)?;
ipt.append("mangle", DPIBREAK_CHAIN, &rule).map_err(iptables_err)?;
log_println!(LogLevel::Info, "{}: new chain {} on table mangle", ipt.cmd, DPIBREAK_CHAIN);
ipt.insert("mangle", "POSTROUTING",
&format!("-j {}", DPIBREAK_CHAIN), 1).map_err(iptables_err)?;
log_println!(LogLevel::Info, "{}: add jump to {} chain on POSTROUTING", ipt.cmd, DPIBREAK_CHAIN);
Ok(())
}
fn cleanup_iptables_rules(ipt: &IPTables) -> Result<()> {
if ipt.delete("mangle", "POSTROUTING", &format!("-j {}", DPIBREAK_CHAIN)).is_ok() {
log_println!(LogLevel::Info, "{}: deleted jump from POSTROUTING", ipt.cmd);
}
if ipt.flush_chain("mangle", DPIBREAK_CHAIN).is_ok() {
log_println!(LogLevel::Info, "{}: flush chain {}", ipt.cmd, DPIBREAK_CHAIN);
}
if ipt.delete_chain("mangle", DPIBREAK_CHAIN).is_ok() {
log_println!(LogLevel::Info, "{}: delete chain {}", ipt.cmd, DPIBREAK_CHAIN);
}
Ok(())
}
const DPIBREAK_TABLE: &str = "dpibreak";
fn install_nft_rules() -> Result<()> {
let rule = serde_json::json!(
{
"nftables": [
{"add": {"table": {"family": "inet", "name": DPIBREAK_TABLE}}},
{
"add": {
"chain": {
"family": "inet",
"table": DPIBREAK_TABLE,
"name": "OUTPUT",
"type": "filter",
"hook": "output",
"prio": 0,
"policy": "accept",
}
}
},
{
"add": {
"chain": {
"family": "inet",
"table": DPIBREAK_TABLE,
"name": DPIBREAK_CHAIN
}
}
},
// prevent inf loop
{
"add": {
"rule": {
"family": "inet",
"table": DPIBREAK_TABLE,
"chain": DPIBREAK_CHAIN,
"expr": [
{
"match": {
"left": { "meta": { "key": "mark" }},
"op": "==",
"right": INJECT_MARK
}
},
{ "return": null }
]
}
}
},
{
"add": {
"rule": {
"family": "inet",
"table": DPIBREAK_TABLE,
"chain": "OUTPUT",
"expr": [{ "jump": { "target": DPIBREAK_CHAIN }}]
}
}
},
{
"add": {
"rule": {
"family": "inet",
"table": DPIBREAK_TABLE,
"chain": DPIBREAK_CHAIN,
"expr": [
{
"match": {
"left": {"payload": { "protocol": "tcp", "field": "dport" }},
"op": "==",
"right": 443
}
},
// TLS ContentType == 0x16 (Handshake)
{
"match": {
"left": { "payload": { "base": "ih", "offset": 0, "len": 8 } },
"op": "==",
"right": 0x16
}
},
// HandshakeType == 0x01 (ClientHello)
{
"match": {
// Note: offset and len are both "bit" unit not byte
"left": { "payload": { "base": "ih", "offset": 40, "len": 8 } },
"op": "==",
"right": 0x01
}
},
{
"queue": {
"num": crate::opt::queue_num(),
"flags": [ "bypass" ]
}
}
]
}
}
}
]
}
);
apply_nft_rules(&serde_json::to_string(&rule)?)?;
// clienthello filtered by nft
IS_U32_SUPPORTED.store(true, Ordering::Relaxed);
log_println!(LogLevel::Info, "nftables: create table inet {DPIBREAK_TABLE}");
Ok(())
}
fn install_rules() -> Result<()> {
match install_nft_rules() {
Ok(_) => {},
Err(e) => {
IS_NFT_NOT_SUPPORTED.store(true, Ordering::Relaxed);
log_println!(LogLevel::Warning, "nftables: {}", e.to_string());
log_println!(LogLevel::Warning, "fallback to iptables");
let ipt = iptables::new(false).map_err(iptables_err)?;
let ip6 = iptables::new(true).map_err(iptables_err)?;
install_iptables_rules(&ipt)?;
// FIXME: using xt_u32 on ipv6 is not supported; (even if it does,
// the rule should be different)
install_iptables_rules(&ip6)?;
}
}
Ok(())
}
fn cleanup_rules() -> Result<()> {
if IS_NFT_NOT_SUPPORTED.load(Ordering::Relaxed) {
let ipt = iptables::new(false).map_err(iptables_err)?;
let ip6 = iptables::new(true).map_err(iptables_err)?;
cleanup_iptables_rules(&ipt)?;
cleanup_iptables_rules(&ip6)?;
} else {
// nft delete table inet dpibreak
let rule = serde_json::json!({
"nftables": [
{"delete": {"table": {"family": "inet", "name": DPIBREAK_TABLE}}}
]
});
apply_nft_rules(&serde_json::to_string(&rule)?)?;
log_println!(LogLevel::Info, "cleanup: nftables: delete table inet {}", DPIBREAK_TABLE);
}
Ok(())
}
pub fn cleanup() -> Result<()> {
cleanup_rules()?;
if IS_XT_U32_LOADED_BY_US.load(Ordering::Relaxed) {
exec_process(&["modprobe", "-q", "-r", "xt_u32"], None)?;
log_println!(LogLevel::Info, "cleanup: unload xt_u32");
}
Ok(())
}
pub fn bootstrap() -> Result<()> {
_ = cleanup(); // In case the previous execution was not cleaned properly
install_rules()
}
use socket2::{Domain, Protocol, Socket, Type};
static RAW4: LazyLock<Mutex<Socket>> = LazyLock::new(|| {
let sock = Socket::new(Domain::IPV4, Type::RAW, Some(Protocol::TCP))
.expect("create raw4");
sock.set_header_included_v4(true).expect("IP_HDRINCL");
sock.set_mark(INJECT_MARK).expect("SO_MARK");
Mutex::new(sock)
});
static RAW6: LazyLock<Mutex<Socket>> = LazyLock::new(|| {
let sock = Socket::new(Domain::IPV6, Type::RAW, Some(Protocol::TCP))
.expect("create raw6");
sock.set_header_included_v6(true).expect("IP_HDRINCL");
sock.set_mark(INJECT_MARK).expect("SO_MARK");
Mutex::new(sock)
});
pub fn send_to_raw(pkt: &[u8]) -> Result<()> {
use std::net::*;
match pkt[0] >> 4 {
4 => { // IPv4
if pkt.len() < 20 {
return Err(anyhow!("invalid ipv4 packet"));
}
let dst = Ipv4Addr::new(pkt[16], pkt[17], pkt[18], pkt[19]);
let addr = SocketAddr::from((dst, 0u16));
if let Ok(sock) = RAW4.lock() {
sock.send_to(pkt, &addr.into())?;
}
}
6 => { // IPv6
if pkt.len() < 40 {
return Err(anyhow!("invalid ipv6 packet"));
}
if let Ok(bytes) = <[u8; 16]>::try_from(&pkt[24..40]) {
let dst = Ipv6Addr::from(bytes);
let addr = SocketAddr::from((dst, 0u16));
if let Ok(sock) = RAW6.lock() {
sock.send_to(pkt, &addr.into())?;
}
}
}
_ => {}
}
Ok(())
}
pub fn run() -> Result<()> {
use std::os::fd::{AsRawFd, AsFd};
use nix::{
fcntl::{fcntl, FcntlArg, OFlag},
poll::{poll, PollFd, PollFlags},
errno::Errno
};
use nfq::Queue;
use crate::handle_packet;
use super::PACKET_SIZE_CAP;
let mut q = Queue::open()?;
q.bind(crate::opt::queue_num())?;
log_println!(LogLevel::Info, "nfqueue: bound to queue number {}",
crate::opt::queue_num());
{ // to check inturrupts
let raw_fd = q.as_raw_fd();
let flags = fcntl(raw_fd, FcntlArg::F_GETFL)?;
let new_flags = OFlag::from_bits_truncate(flags) | OFlag::O_NONBLOCK;
fcntl(raw_fd, FcntlArg::F_SETFL(new_flags))?;
}
splash!("{MESSAGE_AT_RUN}");
let mut buf = Vec::<u8>::with_capacity(PACKET_SIZE_CAP);
while crate::RUNNING.load(Ordering::SeqCst) {
{
let fd = q.as_fd();
let mut fds = [PollFd::new(&fd, PollFlags::POLLIN)];
match poll(&mut fds, -1) {
Ok(_) => {},
// Why should input ^C twice to halt when this is `continue'?
// Seems like there is some kind of race in first inturrupt...
// (maybe ctrlc problem)
Err(e) if e == Errno::EINTR => break,
Err(e) => return Err(e.into()),
}
} // restore BorrowdFd to q
// flush queue
while let Ok(mut msg) = q.recv() {
let verdict = handle_packet!(
&msg.get_payload(),
&mut buf,
handled => nfq::Verdict::Drop,
rejected => nfq::Verdict::Accept,
);
msg.set_verdict(verdict);
q.verdict(msg)?;
}
}
q.unbind(crate::opt::queue_num())?;
Ok(())
}
|