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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
use escape_bytes;
use fancy_regex::Regex as FancyRegex;
use regex::Captures;
use regex::Regex;
use serde::Deserialize;
use serde::Serialize;
use std::fmt;
use tracing::debug;
use tracing::error;

use crate::error::PistolError;
use crate::vs::vscan::MatchX;

fn unescape_string(input: &str) -> Result<Vec<u8>, PistolError> {
    let output = match escape_bytes::unescape(input.as_bytes()) {
        Ok(o) => o,
        Err(e) => {
            return Err(PistolError::CanNotUnescapeString {
                s: input.to_string(),
                e: format!("{:?}", e),
            });
        }
    };
    Ok(output)
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum ProbeProtocol {
    Tcp,
    Udp,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VersionInfo {
    pub p: String,
    pub v: String,
    pub h: String,
    pub i: String,
    pub o: String,
    pub cpe: String,
    pub fix: String,
}

impl fmt::Display for VersionInfo {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut versioninfo_vec = Vec::new();
        if self.fix.len() == 0 {
            if self.p.len() > 0 {
                versioninfo_vec.push(format!("p:{}", self.p));
            }
            if self.v.len() > 0 {
                versioninfo_vec.push(format!("v:{}", self.v));
            }
            if self.h.len() > 0 {
                versioninfo_vec.push(format!("h:{}", self.h));
            }
            if self.i.len() > 0 {
                versioninfo_vec.push(format!("i:{}", self.i));
            }
            if self.o.len() > 0 {
                versioninfo_vec.push(format!("o:{}", self.o));
            }
            if self.cpe.len() > 0 {
                versioninfo_vec.push(format!("{}", self.cpe));
            }
            let versioninfo = versioninfo_vec.join("\n");
            write!(f, "{}", versioninfo)
        } else {
            write!(f, "{}", self.fix)
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Match {
    // This is simply the service name that the pattern matches.
    pub service: String,
    // This pattern is used to determine whether the response received matches the service given in the previous parameter.
    pub pattern: String,
    // The versioninfo section actually contains several optional fields.
    pub versioninfo: VersionInfo,
}

impl fmt::Display for Match {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.service)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SoftMatch {
    // This is simply the service name that the pattern matches.
    pub service: String,
    // This pattern is used to determine whether the response received matches the service given in the previous parameter.
    pub pattern: String,
}

impl fmt::Display for SoftMatch {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.service)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Probe {
    /// This must be either TCP or UDP. Nmap only uses probes that match the protocol of the service it is trying to scan.
    pub probeprotocol: ProbeProtocol,
    /// This is a plain English name for the probe. It is used in service fingerprints to describe which probes elicited responses.
    pub probename: String,
    /// Tells Nmap what to send.
    pub probestring: Vec<u8>,
    /// This keyword is used to instruct Nmap not to use the given probe as a protocol-specific payload during UDP port scanning.
    pub no_payload: bool,
}

impl Probe {
    pub fn empty() -> Probe {
        Probe {
            probeprotocol: ProbeProtocol::Tcp,
            probename: String::new(),
            probestring: Vec::new(),
            no_payload: false,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceProbe {
    pub probe: Probe,
    pub matchs: Vec<Match>,
    pub softmatchs: Vec<SoftMatch>,
    /// This line tells Nmap what ports the services identified by this probe are commonly found on.
    pub ports: Vec<u16>,
    /// This is the same as 'ports' directive described above, except that these ports are often used to wrap a service in SSL.
    pub sslports: Vec<u16>,
    /// This rarely necessary directive specifies the amount of time Nmap should wait before giving up on the most recently defined Probe against a particular service.
    pub totalwaitms: usize,
    /// This directive is only used for the Null probe.
    pub tcpwrappedms: usize,
    /// The rarity directive roughly corresponds to how infrequently this probe can be expected to return useful results.
    pub rarity: usize,
    /// This optional directive specifies which probes should be used as fallbacks for if there are no matches in the current Probe section.
    pub fallback: Vec<String>,
}

impl ServiceProbe {
    fn pattern_fix(&self, pattern: &str) -> String {
        // This code convert C style regex format to Rust style regex format.
        let pattern = pattern.replace(r"[\xfb-\xfe]", r"(\xfb|\xfc|\xfd|\xfe)");
        let pattern = pattern.replace(r"[\x0a-\x0b]", r"(\x0a|\x0b)");
        let pattern = pattern.replace(r"[\x5e-\x60]", r"(\x5e|\x5f|\x60)");
        let pattern = pattern.replace(r"[\x60-\x62]", r"(\x60|\x61|\x62)");
        let pattern = pattern.replace(r"[\x03-\x05]", r"(\x03|\x04|\x05)");
        let pattern = pattern.replace(r"[\x8b-\x8f]", r"(\x8b|\x8c|\x8d|\x8e|\x8f)");
        let pattern = pattern.replace(r"[\x17-\x1a]", r"(\x17|\x18|\x19|\x1a)");
        let pattern = pattern.replace(r"[\x6d-\x6f]", r"(\x6d|\x6f)");
        let pattern = pattern.replace(r"[\x6b-\x6d]", r"(\x6b|\x6c|\x6d)");
        let pattern = pattern.replace(r"[\x69-\x6b]", r"(\x69|\x6a|\x6b)");
        let pattern = pattern.replace(r"[\x4a-\x4c]", r"(\x4a|\x4b|\x4c)");
        let pattern = pattern.replace(r"[\x48-\x4a]", r"(\x48|\x49|\x4a)");
        let pattern = pattern.replace(r"[\x46-\x48]", r"(\x46|\x47|\x48)");
        // destructive match, too much values if I replace it to below format
        let pattern = pattern.replace(r"[\x40-\x90]", r"\x[a-f0-9]{2}");
        let pattern = pattern.replace(r"[\x3e-\x8e]", r"\x[a-f0-9]{2}");
        let pattern = pattern.replace(r"[\x3c-\x8c]", r"\x[a-f0-9]{2}");
        let pattern = pattern.replace(r"[\x50-\x90]", r"\x[a-f0-9]{2}");
        let pattern = pattern.replace(r"[\x4e-\x8e]", r"\x[a-f0-9]{2}");
        let pattern = pattern.replace(r"[\x4c-\x8c]", r"\x[a-f0-9]{2}");
        let pattern = pattern.replace(r"[\x90-\xdb]", r"\x[a-f0-9]{2}");
        let pattern = pattern.replace(r"[\x90-\xdb]", r"\x[a-f0-9]{2}");
        let pattern = pattern.replace(r"[\x90-\xd8]", r"\x[a-f0-9]{2}");
        let pattern = pattern.replace(r"[\xad-\xd8]", r"\x[a-f0-9]{2}");
        // The stupid developer wrote a bunch of ambiguous regular expressions,
        // and now I have to fix them one by one.
        let pattern = pattern.replace(r"[][\w._ ]+", r"[\]\[\w._\s]+");
        let pattern = pattern.replace(r"[][\w:.]+", r"[\]\[\w:.]+");
        let pattern = pattern.replace(
            r"([^[]+) \[[^]]+ ([\d.]+)\]",
            r"([^\[]+) \[[^\]]+ ([\d.]+)\]",
        );
        let pattern = pattern.replace(r"[][\w._:-]+", r"[\]\[\w._:-]+");
        let pattern = pattern.replace(r"[][\w.:]+", r"[\]\[\w.:]+");
        let pattern = pattern.replace(r"[^[]", r"[^\[]");

        let pattern = match FancyRegex::new(r"(?<!\\)\\x") {
            Ok(regex) => regex.replace_all(&pattern, r"\\x").to_string(),
            Err(e) => {
                error!("special regex failed: {}", e);
                pattern
            }
        };
        let pattern = match FancyRegex::new(r"(?<!\\)\\0") {
            Ok(regex) => regex.replace_all(&pattern, r"\\0").to_string(),
            Err(e) => {
                error!("special regex failed: {}", e);
                pattern
            }
        };
        // leave this codes here
        // let pattern = match FancyRegex::new(r"(?<!\\)\\r") {
        //     Ok(regex) => regex.replace_all(&pattern, r"\\r").to_string(),
        //     Err(e) => {
        //         error!("special regex failed: {}", e);
        //         pattern
        //     }
        // };
        // let pattern = match FancyRegex::new(r"(?<!\\)\\n") {
        //     Ok(regex) => regex.replace_all(&pattern, r"\\n").to_string(),
        //     Err(e) => {
        //         error!("special regex failed: {}", e);
        //         pattern
        //     }
        // };
        // let pattern = match FancyRegex::new(r"(?<!\\)\\t") {
        //     Ok(regex) => regex.replace_all(&pattern, r"\\t").to_string(),
        //     Err(e) => {
        //         error!("special regex failed: {}", e);
        //         pattern
        //     }
        // };

        pattern
    }
    fn match_pattern(&self, m: &Match, recv_str: &str) -> Result<Match, PistolError> {
        let new_pattern = self.pattern_fix(&m.pattern);

        let re = FancyRegex::new(&new_pattern)?;
        let captures_group = re.captures(recv_str)?;
        match captures_group {
            Some(v) => {
                let mut versioninfo_fix = m.versioninfo.to_string();
                for i in 0..v.len() {
                    let value = match v.get(i) {
                        Some(v) => v,
                        None => continue,
                    };
                    versioninfo_fix = versioninfo_fix.replace(&format!("${}", i), value.as_str());
                }
                let mut new_m = m.clone();
                new_m.versioninfo.fix = versioninfo_fix;
                Ok(new_m)
            }
            None => {
                // if m.pattern.contains(r"^HTTP/1\.[01] \d\d\d (?:[^\r\n]*\r\n(?!\r\n))*?Server: Apache[/ ](\d[-.\w]+) ([^\r\n]+)") {
                //     debug!("match {}", new_pattern);
                //     debug!("source: {}", recv_str);
                // }
                Err(PistolError::NoMatchFound) // avoid to using Option<'_> here make less code
            }
        }
    }
    fn softmatch_function(&self, sm: &SoftMatch, recv_str: &str) -> Result<SoftMatch, PistolError> {
        let new_pattern = self.pattern_fix(&sm.pattern);
        let re = FancyRegex::new(&new_pattern)?;
        if re.is_match(recv_str)? {
            Ok(sm.clone())
        } else {
            Err(PistolError::NoMatchFound)
        }
    }
    pub fn check(&self, recv_str: &str) -> Option<MatchX> {
        let mut match_ret = Vec::new();
        let mut softmatch_ret = Vec::new();

        // match
        for m in &self.matchs {
            match self.match_pattern(&m, recv_str) {
                Ok(m) => {
                    match_ret.push(m);
                    break;
                }
                Err(e) => match e {
                    PistolError::NoMatchFound => (),
                    _ => {
                        error!("match pattern error: {}", e);
                        continue;
                    }
                },
            }
        }

        if match_ret.len() == 0 {
            // softmatch
            for sm in &self.softmatchs {
                match self.softmatch_function(&sm, recv_str) {
                    Ok(sm) => {
                        softmatch_ret.push(sm);
                        break;
                    }
                    Err(e) => match e {
                        PistolError::NoMatchFound => (), // do noting for no match found
                        _ => {
                            error!("softmatch pattern error: {}", e);
                            continue;
                        }
                    },
                }
            }
        }
        debug!(
            "match: {}, softmatch: {}",
            match_ret.len(),
            softmatch_ret.len()
        );
        if match_ret.len() > 0 {
            Some(MatchX::Match(match_ret[0].clone()))
        } else if softmatch_ret.len() > 0 {
            Some(MatchX::SoftMatch(softmatch_ret[0].clone()))
        } else {
            None
        }
    }
}

fn ports_parser(ports: &str) -> Result<Vec<u16>, PistolError> {
    let mut ret = Vec::new();
    let ports_split: Vec<&str> = ports.split(",").map(|s| s.trim()).collect();
    for ps in ports_split {
        if ps.contains("-") {
            let ps_split: Vec<&str> = ps.split("-").collect();
            let ps_start: u16 = ps_split[0].parse()?;
            let ps_end: u16 = ps_split[1].parse()?;
            for p in ps_start..=ps_end {
                ret.push(p);
            }
        } else {
            let p: u16 = ps.parse()?;
            ret.push(p);
        }
    }
    Ok(ret)
}

/// Instead of getting the `Exclude` port based on the `nmap-service-probes` file,
/// we expect the user to provide a parameter to specify this value themselves.
pub fn nmap_service_probes_parser(lines: Vec<String>) -> Result<Vec<ServiceProbe>, PistolError> {
    let probe_name_reg = Regex::new(
        r"Probe (?P<probeprotocol>[^\s]+) (?P<probename>[^\s]+) q\|(?P<probestring>[^\|]+|)\|(.+)?",
    )?;
    let ports_reg = Regex::new(r"ports (?P<ports>[\d,-]+)")?;
    let sslports_reg = Regex::new(r"sslports (?P<sslports>[\d,-]+)")?;
    let totalwaitms_reg = Regex::new(r"totalwaitms (?P<totalwaitms>[\d]+)")?;
    let tcpwrappedms_reg = Regex::new(r"tcpwrappedms (?P<tcpwrappedms>[\d]+)")?;
    let rarity_reg = Regex::new(r"rarity (?P<rarity>[\d]+)")?;

    let match_split_symbol = vec![r"\|", "/", "=", "%", "@"];
    let mut match_regs = Vec::new();
    for s in &match_split_symbol {
        let p = format!(
            r"match (?P<service>.+) m{s}(?P<pattern>[^{s}]+){s}(?P<modifiers>\w+)?( p/(?P<p>[^/]+)/)?( v/(?P<v>[^/]+)/)?( h/(?P<h>[^/]+)/)?( i/(?P<i>[^/]+)/)?( o/(?P<o>[^/]+)/)?( (?P<cpe>cpe:/[^/]+)/(\w)?)?"
        );
        let reg = Regex::new(&p)?;
        match_regs.push(reg);
    }
    let mut softmatch_regs = Vec::new();
    for s in &match_split_symbol {
        let p = format!(
            r"softmatch (?P<service>[^\s]+) m{s}(?P<pattern>[^{s}]+){s}(?P<modifiers>\w+)?"
        );
        let reg = Regex::new(&p)?;
        softmatch_regs.push(reg);
    }
    // lazy code
    fn do_regex_match<'a>(regs: &'a [Regex], line: &'a str) -> Option<Captures<'a>> {
        for reg in regs {
            match reg.captures(line) {
                Some(caps) => {
                    return Some(caps);
                }
                None => (),
            }
        }
        None
    }

    let fallback_reg = Regex::new(r"fallback (?P<fallback>[\w\d,]+)")?;

    let mut ret_v: Vec<ServiceProbe> = Vec::new();
    let mut probe_v: Probe = Probe::empty();
    let mut ports_v: Vec<u16> = Vec::new();
    let mut sslports_v: Vec<u16> = Vec::new();
    let mut totalwaitms_v: usize = 0;
    let mut tcpwrappedms_v: usize = 0;
    let mut rarity_v: usize = 0;
    let mut match_v: Vec<Match> = Vec::new();
    let mut softmatch_v: Vec<SoftMatch> = Vec::new();
    let mut fallback_v: Vec<String> = Vec::new();

    let mut first_probe = true;

    for line in lines {
        if line.starts_with("#") || line.trim().len() == 0 {
            continue;
        }
        if line.starts_with("Probe") {
            if !first_probe {
                let probe = probe_v;
                let totalwaitms = totalwaitms_v;
                let tcpwrappedms = tcpwrappedms_v;
                let rarity = rarity_v;
                let ports = ports_v.clone();
                let sslports = sslports_v.clone();
                let matchs = match_v.clone();
                let softmatchs = softmatch_v.clone();
                let fallback = fallback_v.clone();

                let sp = ServiceProbe {
                    probe,
                    matchs,
                    softmatchs,
                    ports,
                    sslports,
                    totalwaitms,
                    tcpwrappedms,
                    rarity,
                    fallback,
                };
                ret_v.push(sp);

                // probe_v = Probe::empty();
                ports_v.clear();
                sslports_v.clear();
                totalwaitms_v = 0;
                tcpwrappedms_v = 0;
                rarity_v = 0;
                match_v.clear();
                softmatch_v.clear();
                fallback_v.clear();
            }

            first_probe = false;
            /* probe name part */
            let (probeprotocol, probename, probestring) = match probe_name_reg.captures(&line) {
                Some(caps) => {
                    let probeprotocol = caps.name("probeprotocol").map_or("", |m| m.as_str());
                    let probename = caps.name("probename").map_or("", |m| m.as_str());
                    let probestring = caps.name("probestring").map_or("", |m| m.as_str());
                    let probestring = unescape_string(&probestring)?;
                    (
                        probeprotocol.trim().to_string(),
                        probename.trim().to_string(),
                        probestring,
                    )
                }
                None => {
                    return Err(PistolError::ServiceProbesParseError {
                        name: String::from("Probe"),
                        line: line.to_string(),
                    });
                }
            };
            let probeprotocol = match probeprotocol.as_str() {
                "TCP" => ProbeProtocol::Tcp,
                "UDP" => ProbeProtocol::Udp,
                _ => {
                    return Err(PistolError::ServiceProbesProtocolUnknown {
                        protocol: probeprotocol,
                    });
                }
            };
            let no_payload = if line.contains("no-payload") {
                true
            } else {
                false
            };

            let probe = Probe {
                probeprotocol,
                probename,
                probestring,
                no_payload,
            };
            probe_v = probe;
        } else if line.starts_with("ports") {
            let ports_str = match ports_reg.captures(&line) {
                Some(caps) => {
                    let ports_str = caps.name("ports").map_or("", |m| m.as_str());
                    ports_str
                }
                None => {
                    return Err(PistolError::ServiceProbesParseError {
                        name: String::from("ports"),
                        line: line.to_string(),
                    });
                }
            };
            ports_v = ports_parser(ports_str)?;
        } else if line.starts_with("sslports") {
            let ports_str = match sslports_reg.captures(&line) {
                Some(caps) => {
                    let ports_str = caps.name("sslports").map_or("", |m| m.as_str());
                    ports_str
                }
                None => {
                    return Err(PistolError::ServiceProbesParseError {
                        name: String::from("ports"),
                        line: line.to_string(),
                    });
                }
            };
            sslports_v = ports_parser(ports_str)?;
        } else if line.starts_with("totalwaitms") {
            let totalwaitms = match totalwaitms_reg.captures(&line) {
                Some(caps) => {
                    let totalwaitms_str = caps.name("totalwaitms").map_or("", |m| m.as_str());
                    totalwaitms_str
                }
                None => {
                    return Err(PistolError::ServiceProbesParseError {
                        name: String::from("totalwaitms"),
                        line: line.to_string(),
                    });
                }
            };
            let totalwaitms: usize = totalwaitms.parse()?;
            totalwaitms_v = totalwaitms;
        } else if line.starts_with("tcpwrappedms") {
            let tcpwrappedms = match tcpwrappedms_reg.captures(&line) {
                Some(caps) => {
                    let tcpwrappedms_str = caps.name("tcpwrappedms").map_or("", |m| m.as_str());
                    tcpwrappedms_str
                }
                None => {
                    return Err(PistolError::ServiceProbesParseError {
                        name: String::from("tcpwrappedms"),
                        line: line.to_string(),
                    });
                }
            };
            let tcpwrappedms: usize = tcpwrappedms.parse()?;
            tcpwrappedms_v = tcpwrappedms;
        } else if line.starts_with("rarity") {
            let rarity = match rarity_reg.captures(&line) {
                Some(caps) => {
                    let rarity_str = caps.name("rarity").map_or("", |m| m.as_str());
                    rarity_str
                }
                None => {
                    return Err(PistolError::ServiceProbesParseError {
                        name: String::from("rarity"),
                        line: line.to_string(),
                    });
                }
            };
            let rarity: usize = rarity.parse()?;
            rarity_v = rarity;
        } else if line.starts_with("match") {
            let caps = match do_regex_match(&match_regs, &line) {
                Some(caps) => caps,
                None => {
                    return Err(PistolError::ServiceProbesParseError {
                        name: String::from("match"),
                        line: line.to_string(),
                    });
                }
            };

            let service_str = caps.name("service").map_or("", |m| m.as_str());
            let pattern_str = caps.name("pattern").map_or("", |m| m.as_str());
            let modifiers_str = caps.name("modifiers").map_or("", |m| m.as_str());
            let p_str = caps.name("p").map_or("", |m| m.as_str());
            let v_str = caps.name("v").map_or("", |m| m.as_str());
            let h_str = caps.name("h").map_or("", |m| m.as_str());
            let i_str = caps.name("i").map_or("", |m| m.as_str());
            let o_str = caps.name("o").map_or("", |m| m.as_str());
            let cpe_str = caps.name("cpe").map_or("", |m| m.as_str());

            let pattern_str = if modifiers_str.trim().len() > 0 {
                format!("(?{}){}", modifiers_str.trim(), pattern_str)
            } else {
                pattern_str.to_string()
            };
            // let pattern_str = pattern_fix(&pattern_str);

            let versioninfo = VersionInfo {
                p: p_str.trim().to_string(),
                v: v_str.trim().to_string(),
                h: h_str.trim().to_string(),
                i: i_str.trim().to_string(),
                o: o_str.trim().to_string(),
                cpe: cpe_str.trim().to_string(),
                fix: String::new(),
            };
            let m = Match {
                service: service_str.to_string(),
                pattern: pattern_str.to_string(),
                versioninfo,
            };
            match_v.push(m);
        } else if line.starts_with("softmatch") {
            let caps = match do_regex_match(&softmatch_regs, &line) {
                Some(caps) => caps,
                None => {
                    return Err(PistolError::ServiceProbesParseError {
                        name: String::from("match"),
                        line: line.to_string(),
                    });
                }
            };

            let service_str = caps.name("service").map_or("", |m| m.as_str());
            let pattern_str = caps.name("pattern").map_or("", |m| m.as_str());
            let modifiers_str = caps.name("modifiers").map_or("", |m| m.as_str());

            let pattern_str = if modifiers_str.trim().len() > 0 {
                format!("(?{}){}", modifiers_str.trim(), pattern_str)
            } else {
                pattern_str.to_string()
            };
            // let pattern_str = pattern_fix(&pattern_str);

            let sm = SoftMatch {
                service: service_str.trim().to_string(),
                pattern: pattern_str.trim().to_string(),
            };
            softmatch_v.push(sm);
        } else if line.starts_with("fallback") {
            let fallback = match fallback_reg.captures(&line) {
                Some(caps) => {
                    let fallback_str = caps.name("fallback").map_or("", |m| m.as_str());
                    fallback_str
                }
                None => {
                    return Err(PistolError::ServiceProbesParseError {
                        name: String::from("fallback"),
                        line: line.to_string(),
                    });
                }
            };
            fallback_v.push(fallback.trim().to_string());
        }
    }

    // last value
    let probe = probe_v;
    let totalwaitms = totalwaitms_v;
    let tcpwrappedms = tcpwrappedms_v;
    let rarity = rarity_v;
    let ports = ports_v.clone();
    let sslports = sslports_v.clone();
    let matchs = match_v.clone();
    let softmatchs = softmatch_v.clone();
    let fallback = fallback_v.clone();

    let sp = ServiceProbe {
        probe,
        matchs,
        softmatchs,
        ports,
        sslports,
        totalwaitms,
        tcpwrappedms,
        rarity,
        fallback,
    };
    ret_v.push(sp);

    Ok(ret_v)
}

#[cfg(test)]
mod tests {
    use super::*;
    use fancy_regex::Regex as FancyRegex;
    use std::fs::File;
    use std::io::Write;
    #[test]
    #[ignore]
    fn nmap_service_probes_gen() {
        let nsp_str = include_str!("../db/nmap-service-probes");
        let mut nsp_lines = Vec::new();
        for l in nsp_str.lines() {
            nsp_lines.push(l.to_string());
        }
        println!("read file finish");
        let ret = nmap_service_probes_parser(nsp_lines).unwrap();
        println!("ret len: {}", ret.len());

        let serialized = serde_json::to_string(&ret).unwrap();
        let mut file_write = File::create("nmap-service-probes.pistol").unwrap();
        file_write.write_all(serialized.as_bytes()).unwrap();
    }
    #[test]
    fn test_gen() {
        let test_str = r"[\x40-\x90]";
        let test_str = test_str.replace("]", "").replace("[", "");
        let test_split: Vec<&str> = test_str.split("-").collect();
        let start = test_split[0];
        let end = test_split[1];

        let start_ues = unescape_string(&start).unwrap();
        let end_ues = unescape_string(&end).unwrap();

        let start = start_ues[0];
        let end = end_ues[0];

        let mut ret_string = String::from("(");
        for i in start..=end {
            let tmp = format!("\\x{:02x}", i);
            ret_string += &tmp;
            ret_string += "|"
        }
        ret_string.pop();
        ret_string += ")";
        println!("{}", ret_string);
    }
    #[test]
    fn test_new_method() {
        let text = r"abc\xdef\\xghi\xjkl";
        let re = FancyRegex::new(r"(?<!\\)\\x").unwrap();
        let result = re.replace_all(text, r"\\x");
        println!("{}", result);
    }
    #[test]
    fn test_unescape() {
        let test_str = r"\x80\0\0\x28\x72\xFE\x1D\x13\0\0\0\0\0\0\0\x02\0\x01\x86\xA0\0\x01\x97\x7C\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
        let output = unescape_string(&test_str).unwrap();
        println!("{:?}", output);
    }
}