root/scapy/ansmachine.py

Revision 862:52d055e522bd, 3.9 kB (checked in by Phil <phil@secdev.org>, 5 months ago)

Merged with one-file scapy

Line 
1 ## This file is part of Scapy
2 ## See http://www.secdev.org/projects/scapy for more informations
3 ## Copyright (C) Philippe Biondi <phil@secdev.org>
4 ## This program is published under a GPLv2 license
5
6 ########################
7 ## Answering machines ##
8 ########################
9
10 from sendrecv import send,sendp,sniff
11 from config import conf
12 from error import log_interactive
13
14 class ReferenceAM(type):
15     def __new__(cls, name, bases, dct):
16         o = super(ReferenceAM, cls).__new__(cls, name, bases, dct)
17         if o.function_name:
18             globals()[o.function_name] = lambda o=o,*args,**kargs: o(*args,**kargs)()
19         return o
20
21
22 class AnsweringMachine(object):
23     __metaclass__ = ReferenceAM
24     function_name = ""
25     filter = None
26     sniff_options = { "store":0 }
27     sniff_options_list = [ "store", "iface", "count", "promisc", "filter", "type", "prn" ]
28     send_options = { "verbose":0 }
29     send_options_list = ["iface", "inter", "loop", "verbose"]
30     send_function = staticmethod(send)
31    
32    
33     def __init__(self, **kargs):
34         self.mode = 0
35         if self.filter:
36             kargs.setdefault("filter",self.filter)
37         kargs.setdefault("prn", self.reply)
38         self.optam1 = {}
39         self.optam2 = {}
40         self.optam0 = {}
41         doptsend,doptsniff = self.parse_all_options(1, kargs)
42         self.defoptsend = self.send_options.copy()
43         self.defoptsend.update(doptsend)
44         self.defoptsniff = self.sniff_options.copy()
45         self.defoptsniff.update(doptsniff)
46         self.optsend,self.optsniff = [{},{}]
47
48     def __getattr__(self, attr):
49         for d in [self.optam2, self.optam1]:
50             if attr in d:
51                 return d[attr]
52         raise AttributeError,attr
53                
54     def __setattr__(self, attr, val):
55         mode = self.__dict__.get("mode",0)
56         if mode == 0:
57             self.__dict__[attr] = val
58         else:
59             [self.optam1, self.optam2][mode-1][attr] = val
60
61     def parse_options(self):
62         pass
63
64     def parse_all_options(self, mode, kargs):
65         sniffopt = {}
66         sendopt = {}
67         for k in kargs.keys():           
68             if k in self.sniff_options_list:
69                 sniffopt[k] = kargs[k]
70             if k in self.send_options_list:
71                 sendopt[k] = kargs[k]
72             if k in self.sniff_options_list+self.send_options_list:
73                 del(kargs[k])
74         if mode != 2 or kargs:
75             if mode == 1:
76                 self.optam0 = kargs
77             elif mode == 2 and kargs:
78                 k = self.optam0.copy()
79                 k.update(kargs)
80                 self.parse_options(**k)
81                 kargs = k
82             omode = self.__dict__.get("mode",0)
83             self.__dict__["mode"] = mode
84             self.parse_options(**kargs)
85             self.__dict__["mode"] = omode
86         return sendopt,sniffopt
87
88     def is_request(self, req):
89         return 1
90
91     def make_reply(self, req):
92         return req
93
94     def send_reply(self, reply):
95         self.send_function(reply, **self.optsend)
96
97     def print_reply(self, req, reply):
98         print "%s ==> %s" % (req.summary(),reply.summary())
99
100     def reply(self, pkt):
101         if not self.is_request(pkt):
102             return
103         reply = self.make_reply(pkt)
104         self.send_reply(reply)
105         if conf.verb >= 0:
106             self.print_reply(pkt, reply)
107
108     def run(self, *args, **kargs):
109         log_interactive.warning("run() method deprecated. The intance is now callable")
110         self(*args,**kargs)
111
112     def __call__(self, *args, **kargs):
113         optsend,optsniff = self.parse_all_options(2,kargs)
114         self.optsend=self.defoptsend.copy()
115         self.optsend.update(optsend)
116         self.optsniff=self.defoptsniff.copy()
117         self.optsniff.update(optsniff)
118
119         try:
120             self.sniff()
121         except KeyboardInterrupt:
122             print "Interrupted by user"
123        
124     def sniff(self):
125         sniff(**self.optsniff)
126
Note: See TracBrowser for help on using the browser.