-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhostKnowledge.py
More file actions
1066 lines (869 loc) · 36.8 KB
/
hostKnowledge.py
File metadata and controls
1066 lines (869 loc) · 36.8 KB
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python
"""
hostknowledge.py - contains a fuzzy logic representation of what I know about a host
"""
#for uint32
from exploitutils import *
#for saving/loading hosts
import cPickle
from threading import Thread
#for translations
from gettext import gettext as _
import gettext
gettext.bindtextdomain("CANVAS",localedir = "gui/locale/")
gettext.textdomain("CANVAS")
import os.path
import socket
import time
import sys
import copy
from canvaserror import *
import logging
class lineList:
def __init__(self, parent):
self.parent = parent
self.children = []
self._text = ""
self._activated_text = ""
self.pix = ""
self.gui = None
self.engine = None
self.activated = 0
self.amselected = self.activated # XXX
self.activate_text()
#text depends on our activation state, so we cannot pickle it
self.text = ""
self.pickledefaults = {"text": "Just Unpickled", "parent": None,
"gui": None, "activated": 0, "amselected": 0,
"_activated_text": "", "engine": None}
def __getstate__(self):
"""
We don't want to show the "(current callback)" part if we've
just unpickled, because it's untrue, so we manually set self.text here in
our pickle dictionary.
"""
self.pickledefaults["text"] = self._text
dontpickle = self.pickledefaults.keys()
newdict = filterdict(dontpickle, self.__dict__)
for key in dontpickle:
newdict[key] = self.pickledefaults[key]
return newdict
def set_all_parents(self, obj):
self.parent = obj
self.gui = obj.gui
self.engine = obj.engine
for c in self.children:
c.set_all_parents(self)
def old__setstate__(self, state):
"""
takes in a tuple state from the pickle operation
"""
p, c, t, pix = state
self.parent = p
self.children = c
self.text = t
self.pix = pix
self.engine = None
self._text = t
self.activated = 0
self.amselected = 0
self.gui = None
def activate_text(self):
"""
Construct self.text which is the line shown by the GUI
"""
if self.activated:
self.text = self._text + self._activated_text
else:
self.text = self._text
def get_pix(self):
return self.pix
def update_pix(self):
"""
used to set a different picture if we're busy, etc
"""
pass
def get_text(self):
return self.text
def get_children(self):
return self.children
def get_menu(self):
"""
Gets a list of strings which will be made into a menu
"""
return []
def menu_response(self, widget, astring):
pass
def add(self, child):
self.children += [child]
child.parent = self
def delete(self, child):
"""
Deletes a child line from the GUI
Should be safe to call from any thread since we just use gui_queue. Of course
this means the actual delete may get postponed until the Main thread runs.
This may not be what you really intended, but there's no easy way around it. We
could sleep() to trigger the main thread, I guess.
"""
try:
index = self.children.index(child)
except ValueError:
devlog("hostKnowledge", "child is not in the list of children????")
return
del self.children[index]
if self.engine:
if self.gui:
self.engine.gui.gui_queue_append("deleteLine", [ child ])
time.sleep(0.1)
if isinstance(child, hostKnowledge):
self.engine.new_event('host deleted', {'node' : self.parent.getname(),
'ip' : child.interface})
def update_gui(self):
"""
It's perfectly ok to call this from any thread - update_gui just
adds to the gui_queue such that the GUI updates whatever line we are
"""
#check to see if this has changed because we may have recently become
#activated or loaded from a pickle
self.activate_text()
if self.gui:
#self.gui is a newgui reference. We don't want to call this directly
#self.gui.update_object(self) #never do this because of threading issues
devlog("gui","Updating gui for %s %s"%(self.text,self.pix))
self.engine.update(self)
else:
pass
if self.parent and self.parent != self:
self.parent.update_gui()
def set_engine(self, engine):
self.engine = engine
from libs.dnslookup import dnslookup
from exploitutils import check_reserved
class hostKnowledge(lineList):
"""
A list of knowledge primitives. Contained within a knowledgeContainer
"""
def __init__(self, interface, parent, doDNS=True, resolved_from=None):
"""
The goal of resolved_from is to provide a place to store a virtual
host. For example, if you do CANVASNode.new_host("www.cnn.com"),
then resolved_from will be "www.cnn.com", even though everything
else will work by ip address internally. There are some cases where
you want the hostname you used to resolve this IP address - web
exploits in particular.
"""
self.additional = False
if not resolved_from:
self.resolved_from = interface
else:
self.resolved_from = resolved_from
#interface is the interface from the current node. 127.0.0.1 is, of course, the localhost
devlog("hostKnowledge", "interface=%s"%interface)
if interface == None:
print "Interface is set to none, which should not happen!"
import traceback
traceback.print_exc(file=sys.stderr)
sys.exit(1)
lineList.__init__(self,parent)
# XXX: ipv6 mod
if ":" in interface:
pass
else:
# XXX: end
#not IPv6 - so doing IPv4 resolution
if doDNS:
try:
interface2 = socket.gethostbyname(interface)
except Exception:
interface2 = "127.0.0.2" #ERROR
interface = interface2 #swap them
self.interface = interface
##OK now we have an interface set lets try and get a DNS and add it to the knowledgebase if its not a private range
#TODO CHECK COVERTNESS ?
if (not check_reserved(interface)
and interface != "::1"
and interface != ":::1"
and doDNS):
lookup = dnslookup(self)
lookup.start()
lookup.join()
self.pix = ""
self._activated_text = " (current target)"
self.pickledefaults["_activated_text"] = self._activated_text
self._text = "Host: %s" % self.interface
self.activate_text()
def __str__(self):
return "%s" % self._text
def get_sort_value(self):
return self.interface
def get_pix(self):
for c in self.children:
#knowledge about the OS is used to set our icon
known = str(c.known)
if c.tag!="OS":
continue
#we don't differentiate versions yet
if known.count("Windows"):
return "Win32Host"
elif known.count("Linux"):
return "LinuxHost"
elif known.count("Solaris"):
return "SolarisHost"
elif known.count("Embedded"):
return "EmbeddedHost"
def get_knowledge(self, tag, defaultret=None):
"""
Get information from the hostKnowledge - we return an object, not a string
O(N) operation here - could be fixed with a dictionary
"""
for c in self.children:
if c.tag == tag:
return c
return defaultret
def get_all_knowledge_as_text(self):
"""
Every so often you'll want to get all the knowledge in easy
to print out form, and this is how
"""
return "\n".join(map(str, self.children))
def get_all_knowledge_as_list(self):
"""
Every so often you'll want to get all the knowledge in easy
to print out form, and this is how
"""
return [c for c in self.children]
def get_menu(self):
"""
Get menu strings for hostKnowledge - select as target, etc
"""
menu = ["Forget this host knowledge", "Save host to file","Add note to host"]
#if we're not already a target, let's add these options to the front
if not self.activated:
menu = ["Set as additional target host"] + menu
menu = ["Set as target host"] + menu
if self.additional:
menu+=["Unset as targeted host"]
if self.get_knowledge("MOSDEFService",None):
#we have a MOSDEFService installed on this box, so we should offer the user the ability
#to connect to it
menu+=["Connect to MOSDEF Service"]
return menu
def set_as_target(self,t=1):
"""
Sets or unsets myself as a target and updates
the engine and gui to know such a thing - doesn't
actually remove from the engine's self.target_hosts list
You probably should not be calling this directly. This is for the
engine to use.
"""
self.activated=t
self.activate_text()
self.update_gui()
self.update_engine()
def unset_as_target(self):
"""
Remove myself from the engine's target_hosts list
and unset myself as an additional target or target.
You probably should not be calling this directly. This is for the
engine to use.
"""
self.additional = False
if self.engine:
self.engine.unset_target_host(self)
self.set_as_target(0)
def update_engine(self):
if self.engine:
if self.activated:
if self.additional:
#additional target
self.engine.set_additional_target_host(self)
else:
#primary target
self.engine.set_target_host(self)
def save_state(self):
"""
Uses pickle to save this HostKnowledge object to a file
We don't want to save self.parent though, since that will include a lot of
information we don't need. We don't want to save self.gui or self.engine either.
We don't need activated or amselected. These would in fact be bad to store.
We do want self.children, which is all our knowledge (if we have any)
These saves are per sesion and go in the session directory in "SavedState"
"""
self.pickledefaults["activated"]=self.activated
if self.parent:
node=self.parent.parent #get our parent node for its name
nodename=node.get_name()
else:
nodename="standalone"
hostname=nodename+"_"+self.interface #construct a unique name
##Saved Hosts goes in the appropriate directory for the current session
dirname = self.engine.create_new_session_output_dir("SavedState", subdir="Hosts")
filename = os.path.join(dirname, hostname)
try:
cPickle.dump(self,file(filename,"wb"))
self.engine.log( "Saved state of %s to %s"%(hostname, filename))
except Exception, err:
self.engine.log( "Problem saving state of host %s: %s"%(hostname, err) )
def menu_response(self, widget, astring):
"""
Handles all the menu responses (sent to us a string such as "Save to File")
"""
#print "Got %s"%astring
if astring==_("Set as target host"):
self.additional=False
self.set_as_target()
elif astring==_("Set as additional target host"):
#don't set as additional host if we already are
#either a primary or secondary host
if not self.activated:
self.additional=True
self.set_as_target()
elif astring==_("Unset as targeted host"):
#only do this is we are the secondary target since we
#always have at least ONE target selected
if self.additional:
self.unset_as_target()
elif astring==_("Forget this host knowledge"):
if self.interface=="127.0.0.1":
self.engine.log(_("Don't try to delete the loopback interface, please"))
else:
self.parent.delete(self)
elif astring==_("Save host to file"):
#print "Not yet supported, sorry"
if 1:
self.save_state()
elif astring==_("Add note to host"):
if self.gui:
#self.gui is newgui.
self.gui.engine.gui.gui_queue_append("add note to host", [self])
else:
print "Unknown string in menu_response: %s"%astring
def forget(self, tag):
"""
Forgets a tag, if we have it
returns True if we've found it, false if it was not here
"""
for c in self.children:
if c.tag == tag:
self.delete(c)
if self.engine:
self.engine.new_event('knowledge deleted', {'tag' : tag,
'node' : self.parent.parent.getname(),
'host' : self.interface,})
return True
return False
def replace_knowledge(self,tag,knowledge,percentage,invisible=0):
"""
If knowledge is already known replaces it, otherwise, adds it
"""
for c in self.children:
if c.tag == tag:
if not c.invisible and self.engine:
self.engine.deleteLine(c)
c.known = knowledge
c.known_text = knowledge
c.percentage = percentage
c.invisible = invisible
if not invisible and self.engine:
self.engine.addLine(c)
c.update_gui()
if self.engine:
self.engine.new_event('knowledge replaced', {'tag' : tag,
'knowledge' : knowledge,
'percentage' : percentage,
'node' : self.parent.parent.getname(),
'host' : self.interface,})
return c
return self.add_knowledge(tag,knowledge,percentage,invisible=invisible)
def add_knowledge(self, tag, knowledge, percentage, invisible=0):
"adds knowledge but does not replace it"
devlog('hostKnowledge::add_knowledge', "%s %s %s"%(tag,knowledge,invisible))
for c in self.children:
if tag == c.tag:
#we already know something about this - we need to adjust it,
#but for now we'll replace it
devlog('hostKnowledge::add_knowledge',"replacing %s knowledge in gui"%tag)
return self.replace_knowledge(tag,knowledge,percentage,invisible)
thing=knowledgePrimitive(self, tag, knowledge,percentage)
thing.invisible=invisible
self.add(thing)
if self.engine and not invisible:
devlog('hostKnowledge::add_knowledge',"adding %s knowledge to gui"%tag)
self.engine.addLine(thing)
else:
devlog('hostKnowledge::add_knowledge',"Not adding %s knowledge to gui. Self.engine: %s"%(tag,self.engine))
thing.update_gui()
self.update_gui()
if self.engine:
self.engine.new_event('knowledge added', {'tag' : tag,
'knowledge' : knowledge,
'percentage' : percentage,
'node' : self.parent.parent.getname(),
'host' : self.interface,})
return thing
def add_to_knowledge(self, tag, newknowledge):
"adds a fact to a knowledge line (such as a port)"
#print "add_to_knowledge(%s,%s)"%(tag,newknowledge)
knowledge=self.get_knowledge(tag)
if knowledge==None:
#add it anew
#print "add knowledge about to be called"
self.add_knowledge(tag,newknowledge,100)
return
#print "knowledge.known=%s"%knowledge.known
knowledge.known+=newknowledge
knowledge.known=uniquelist(knowledge.known)
#print "Replace knowledge about to be called"
self.replace_knowledge(tag,knowledge.known,100)
return
def open_tcpport(self,port):
"Returns 1 if the port is open on this host, else, zero"
#quick TCP function
ports=self.get_knowledge("TCPPORTS",[])
#I have no idea why ports would not be a list
#but if it's not, we don't want to error out
if not ports or type(ports) != type([]):
return 0
if port in ports:
return 1
return 0
def add_note(self,note):
self.replace_knowledge("Note",note,100)
def get_note(self):
ret = self.get_knowledge("Note","")
if ret: ret = ret.known
return ret
def add(self, thing):
lineList.add(self, thing)
class knowledgeContainer(lineList):
"""
A list of hosts we know about, typically my parent is a Node, my children are hostKnowledge objects
"""
def __init__(self,parent):
lineList.__init__(self,parent)
self._text = "Knowledge"
def get_menu(self):
#return ["Add new host", "Forget all knowledge", "Load host from file", "Load all hosts"]
return ["Add new host", "Forget all knowledge", "Add hosts from file"]
def save_state_all(self):
"""
For each host in this container save it's state
"""
for host in self.children:
host.save_state()
def restore_state(self, session_dir, state_type, prefix="", superseed_existing=True):
"""
Now loads state from the specified session dir
"""
dirname = os.path.join(session_dir, "SavedState",state_type)
try:
hostlist=os.listdir(dirname)
except:
self.engine.log( "No Saved State to load" )
return
##Delete existing host knowledge -
## Rich : right this is so much nasier than it has to be as the knowledge base was seemingly not designed with doing anything more than adding or deleting a singleton
## the delete metjhod in linelist alters self.children, thus breaking any for loop - but deletes based on object ID so we have to loop until we are complete
## - GOD DAMN FILTHY but its the only way as the rest of the code relies on the self.children modification ...... this whole knowledgebase needs recoding to be designed and scalable etc etc
while len(self.children) >0:
for x in self.children:
self.delete(x)
for f in hostlist:
#skip all hosts that don't start with 0_ for localNode
if prefix and f[:len(prefix)]!=prefix:
#print "did not match prefix: Prefix=*%s* f[:len(prefix)]=*%s*"%(prefix,f[:len(prefix)])
continue
try:
self.engine.log( "Restoring host state of: %s"%(f) )
newhost=cPickle.load(file(os.path.join(dirname,f)))
except (IOError, EOFError), err:
##Not a good pickle
logging.error("Could not load host '%s' because '%s' - Probably not a not a pickle file" % (f, err))
continue
#set up all the children to have the correct parent again
newhost.set_all_parents(self.parent.hostsknowledge)
# We do not want to perform dns resolution on session import
self.parent.add_host(newhost, lookup=False)
##Retarget previously targetted host upon restore
if newhost.activated:
newhost.additional=False
newhost.set_as_target()
for x in newhost.children:
##Now force the classic view to show this knowledge
self.engine.addLine(x)
return
def menu_response(self, widget, astring):
#print "Got %s"%astring
if astring=="Add new host":
if self.gui:
#self.gui is newgui.
self.gui.engine.gui.gui_queue_append("add host", [self])
#if astring=="Load host from file":
if astring == "Add hosts from file":
#pop up a dialog box and select the file (from the gui)
self.gui.engine.gui.gui_queue_append("Add hosts from file", [self])
#Depreciated with session support
#if astring=="Load all hosts":
#self.restore_state()
def get_all_known_hosts(self):
"""
returns a list of all the hosts I know about
This is used by the engine to maintain uniqueness
of the hosts in the container
"""
return [c.interface for c in self.children]
def forget(self, tag):
"""
Forgets information from a tag in our localhost
"""
localhost = self.get_localhost()
localhost.forget(tag)
def get_first_known_host(self):
if not self.children: return None
return self.children[0]
def get_localhost(self):
"""
Returns the local host in this container - essentially 127.0.0.1
"""
#should always exist
return self.get_known_host("127.0.0.1")
def get_known_host(self, ip):
"""
Returns a hostKnowledge or None if none found that matched that ip
"""
for c in self.children:
if c.interface==ip:
return c
return None
def add(self, data):
lineList.add(self, data)
class knowledgePrimitive(lineList):
"""
Each host has many of these
"""
def __init__(self,parent, tag, known, percentage):
lineList.__init__(self, parent)
self.tag = tag
self.known = known
self.percentage = percentage
self.invisible = 0
known_text = str_from_object(self.known)
devlog("hostKnowledge", "Known Text: %s"%known_text)
self.all_text="" #used only when we call self.get_all_text()
self.known_text=known_text #just the portion of text we use for the known value
self._text="Known: %s: %s <%s%%>"%(self.tag, self.known_text ,self.percentage)
def __str__(self):
self._text="Known: %s: %s <%s%%>"%(self.tag, self.known_text ,self.percentage)
return self._text
def old__getstate__(self):
"""used for pickling"""
state=(lineList.__getstate__(self),self.tag,self.known,self.percentage,self.invisible,self.text)
return state
def get_known_text(self):
"""
Returns only the text for the known string - not the percentage of certainty.
Has to handle the case when our known is a list or a string, essentially
"""
known=str_from_object(self.known)
devlog("hostKnowledge","get_known_text returning: %s"%known)
self.known_text=known
return known
def get_text(self):
"""
Gets the text representation of this known value, including the percentage
of certainty, and then formats it for the screen. Also assigns some internal
variables for use by people hooking this object.
If all you want to use is the known text for parsing or whatever, we also have
get_known_text() available, which will just return the known text.
Also see "get_all_text()" which does not restrict the length of the known text to
50 characters (and self.all_text).
"""
known=self.get_known_text()
#self.text is truncated to fit into a screen nicely.
self.text="Known: %s: %s <%s%%>"%(self.tag, str(known)[:50],self.percentage)
#self.all_text is used by some people who want to parse text instead of access the self.known object directly.
self.all_text="Known: %s: %s <%s%%>"%(self.tag, str(known),self.percentage)
return self.text
def get_all_text(self):
"""
Calls self.get_text() to set internal variables, then returns self.all_text - a longish
representation of what we know.
"""
self.get_text()
return self.all_text
def get_menu(self):
return ["Forget this knowledge", "Print knowledge"]
def menu_response(self, widget, astring):
if astring=="Forget this knowledge":
#done in self.parent.delete() - should be thread safe
#self.gui.engine.gui.gui_queue_append("deleteLine",[self])
self.parent.delete(self)
elif astring=="Print knowledge":
self.engine.log("Knowledge: %s"%self.get_all_text())
class interfaceLine(lineList):
def __init__(self, ifc, nat, startport, endport, parent):
lineList.__init__(self,parent)
self.interface = ifc[0]
self.ip = ifc[1]
self.netmask = nmask = ifc[2]
# ideally the following check shouldn't exist
# but there is a discrepancy in the format of the ifc list
# localNode defines netmask to be a string and pretty much
# all the other nodes use numeric netmasks
if isinstance(nmask, (int, long)):
res = ''
res += '%d.' % ((nmask & 0xff000000) >> 24)
res += '%d.' % ((nmask & 0xff0000) >> 16)
res += '%d.' % ((nmask & 0xff00) >> 8)
res += '%d' % (nmask & 0xff)
nmask = res
self._text="%s %s (%s)" % (self.interface, self.ip, nmask)
self.activate_text()
self.isNAT=nat
#for NAT's these can be a smaller range of portforwarded ports
self.startport=startport
self.endport=endport
self._activated_text = " (current callback)"
self.pickledefaults["_activated_text"]=self._activated_text
self.pickledefaults["children"]= []
self.pickledefaults["parent"]= None
self.pickledefaults["activated"]= self.activated
##For state save/restore
self.listeners_that_are_listening = []
def __str__(self):
"""
Return the IP - possibly would be better to return a "%s %s %s"%(self.ip,self.netmask,self.isNAT) or something...
"""
return str(self.ip)
def isSpecial(self):
"""
Return true if we are a special kind of interface (NAT, for example)
The other kind of special interface is one that's not local to a LocalNode.
If this returns True, the engine will not choose a different interface
when doing auto-interface selection.
See canvasengine::autoListener()
"""
if self.isNAT:
return True
if self.parent.parent.nodetype!="LocalNode":
return True
return False
def get_menu(self):
return ["Set as callback interface"]
def set_as_callback(self,t=1):
self.activated=t
self.activate_text()
self.update_gui()
self.update_engine()
def unset_as_callback(self):
self.set_as_callback(0)
def update_engine(self):
if self.engine:
self.engine.set_callback_interface(self)
def menu_response(self, widget, astring):
#print "Got %s"%astring
if astring==_("Set as callback interface"):
self.set_as_callback()
def getListenerBySock(self,sock):
#print "in getListenerBySock for listener %s"%self.text
#print "Number of listeners: %s"%len(self.children)
for c in self.children:
#print "Comparing %s to %s"%(sock,c.sock)
if sock==c.sock:
return c
return None
def save_state(self):
"""
Uses pickle to save this interface object to a file
We don't want to save self.parent though, since that will include a lot of
information we don't need. We don't want to save self.gui or self.engine either.
We don't need activated or amselected. These would in fact be bad to store.
We do want self.children, which is all our knowledge (if we have any)
These saves are per sesion and go in the session directory in "SavedState"
"""
##Make sure we don't pickle the children ...... this gets reset on multiple saves for some reason?
self.pickledefaults["children"] = []
self.pickledefaults["_activated_text"]=self._activated_text
self.pickledefaults["activated"]= self.activated
if self.parent:
node=self.parent.parent #get our parent node for its name
nodename=node.get_name()
else:
nodename="standalone"
ifacename=nodename+"_"+self._text.replace(" ","_") #construct a unique name
##Saved Hosts goes in the appropriate directory for the current session
dirname = self.engine.create_new_session_output_dir("SavedState", subdir="Interfaces")
filename = os.path.join(dirname, ifacename)
try:
cPickle.dump(self,file(filename,"wb"))
self.engine.log( "Saved state of %s to %s"%(ifacename, filename))
except Exception, err:
self.engine.log( "Problem saving state of host %s: %s"%(ifacename, err) )
class interfacesList(lineList):
"""
Parent is usually a CANVASNode
"""
def __init__(self,parent):
lineList.__init__(self,parent)
self._text="Interfaces"
self.activate_text()
def all_ips(self):
"""
Returns a list of the ip addresses
"""
return [inter.ip for inter in self.children]
def all_interfaces(self):
"""
Returns a list of the ip interfaces
"""
return [inter.interface for inter in self.children]
def all_interface_objects(self):
return self.children
def get_ip(self,ip):
"""
If we can find an interface in our list that matches
that IP, return it, otherwise return None
"""
devlog("hostKnowledge", 'interfacesList::get_ip', "ip = %s" % ip)
for interfs in self.children:
devlog("hostKnowledge", 'interfacesList::get_ip', "intefs=%s %s"%(interfs.interface,interfs.ip))
if ip==interfs.ip:
devlog("engine", 'interfacesList::get_ip', "Found...%s"%ip)
return interfs
return None
def get_interface(self, iface):
for child in self.children:
if iface == child.interface:
return child
return None
def get_interesting(self):
"""
Will return the first interesting interface it finds (as a string)
This is used by the CANVAS Nodes to make their display more
useful to the user
"""
ret=""
for child in self.children:
if child.ip not in ["127.0.0.1", "0.0.0.0"]:
return child.ip
return "" #nothing found?
def get_callback_interface(self):
"""
Returns the current callback interface object
"""
for child in self.children:
if child.activated:
return child
return None #nothing found?
def add_ip(self,ifc,nat=0,startport=1,endport=65535):
"""
An ifc (interface, ip, netmask) is given to me, I make it an object and then add it to my children
and update the model
"""
interface=interfaceLine(ifc,nat,startport,endport,self)
# We shouldnt be adding interfaces that already exist in the host knowledge
for x in self.children:
if x == interface:
devlog("hostKnowledge", "Found interface: %s NOT adding again!"%(interface))
return
self.add(interface)
if self.engine:
self.engine.addLine(interface)
interface = ifc[0]
ip = ifc[1]
netmask = ifc[2]
self.engine.new_event('interface added', {'interface' : interface,
'ip' : ip,
'netmask' : netmask,
'NAT' : True if nat else False,
'node' : self.parent.getname()})
def get_last(self, addrType=None):
"""Pass in an addrType to get the last ipv4 or ipv6 interface, otherwise you get the last interface, whatever it is"""
#strange error condition
if self.children==[]:
return None
if addrType == None:
return self.children[-1]
else:
if addrType not in ["ipv4", "ipv6"]:
raise CANVASError("Valid addrType values are 'ipv4' or 'ipv6'")
for i in reversed(self.children):
if addrType == "ipv4":
if "." in i.ip:
return i
elif addrType == "ipv6":
if ":" in i.ip:
return i
def get_menu(self):
return ["Add interface"]
def menu_response(self, widget, astring):
#print "Got %s"%astring
if astring=="Add interface":
if self.gui:
#self.gui is newgui.
self.gui.engine.gui.gui_queue_append("add interface", [self])
def restore_state(self, session_dir, prefix="", superseed_existing=True, engine=None):
dirname = os.path.join(session_dir, "SavedState","Interfaces")
if not engine:
engine = self.engine
try:
iflist=os.listdir(dirname)
except:
engine.log("No Saved State to load in %s"%(dirname))
return
#engine.log("Deleting current interface state..")
for intface in self.children:
self.engine.deleteLine(intface)
##Close any listners present
for lner in intface.children:
lner.closeme()