• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /usr/bin/env python
2
3from __future__ import print_function
4import getopt
5
6def usage():
7    print("""Usage: check_asdis -i <pcap_file> [-o <wrong_packets.pcap>]
8    -v   increase verbosity
9    -d   hexdiff packets that differ
10    -z   compress output pcap
11    -a   open pcap file in append mode""", file=sys.stderr)
12
13def main(argv):
14    PCAP_IN = None
15    PCAP_OUT = None
16    COMPRESS=False
17    APPEND=False
18    DIFF=False
19    VERBOSE=0
20    try:
21        opts=getopt.getopt(argv, "hi:o:azdv")
22        for opt, parm in opts[0]:
23            if opt == "-h":
24                usage()
25                raise SystemExit
26            elif opt == "-i":
27                PCAP_IN = parm
28            elif opt == "-o":
29                PCAP_OUT = parm
30            elif opt == "-v":
31                VERBOSE += 1
32            elif opt == "-d":
33                DIFF = True
34            elif opt == "-a":
35                APPEND = True
36            elif opt == "-z":
37                COMPRESS = True
38
39
40        if PCAP_IN is None:
41            raise getopt.GetoptError("Missing pcap file (-i)")
42
43    except getopt.GetoptError as e:
44        print("ERROR: %s" % e, file=sys.stderr)
45        raise SystemExit
46
47
48
49    from scapy.config import conf
50    from scapy.utils import RawPcapReader,RawPcapWriter,hexdiff
51    from scapy.layers import all
52
53
54    pcap = RawPcapReader(PCAP_IN)
55    pcap_out = None
56    if PCAP_OUT:
57        pcap_out = RawPcapWriter(PCAP_OUT, append=APPEND, gz=COMPRESS, linktype=pcap.linktype)
58        pcap_out._write_header(None)
59
60    LLcls = conf.l2types.get(pcap.linktype)
61    if LLcls is None:
62        print(" Unknown link type [%i]. Can't test anything!" % pcap.linktype, file=sys.stderr)
63        raise SystemExit
64
65
66    i=-1
67    differ=0
68    failed=0
69    for p1,meta in pcap:
70        i += 1
71        try:
72            p2d = LLcls(p1)
73            p2 = str(p2d)
74        except KeyboardInterrupt:
75            raise
76        except Exception as e:
77            print("Dissection error on packet %i" % i)
78            failed += 1
79        else:
80            if p1 == p2:
81                if VERBOSE >= 2:
82                    print("Packet %i ok" % i)
83                continue
84            else:
85                print("Packet %i differs" % i)
86                differ += 1
87                if VERBOSE >= 1:
88                    print(repr(p2d))
89                if DIFF:
90                    hexdiff(p1,p2)
91        if pcap_out is not None:
92            pcap_out.write(p1)
93    i+=1
94    correct = i-differ-failed
95    print("%i total packets. %i ok, %i differed, %i failed. %.2f%% correct." % (i, correct, differ,
96                                                                                failed, i and 100.0*(correct)/i))
97
98
99if __name__ == "__main__":
100    import sys
101    try:
102        main(sys.argv[1:])
103    except KeyboardInterrupt:
104        print("Interrupted by user.", file=sys.stderr)
105