forked from cl4u2/ninuxoo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sambodrone.py
94 lines (85 loc) · 2.3 KB
/
sambodrone.py
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
#!/usr/bin/env python2
# vim: fileencoding=utf-8:nomodified
import smbc
import threading
import time
import socket
from resources import Resource
from dbmanager import *
SOCKTIMEOUT = 20
socket.setdefaulttimeout(SOCKTIMEOUT)
class SambaDancer(threading.Thread):
def __init__(self, target, silos, dancemanager):
threading.Thread.__init__(self)
self.ctx = smbc.Context()
self.silos = silos
self.dancemanager = dancemanager
self.uri = "smb://" + target
self.target = target
def dance(self, smburl, depth=0):
print "[%d] %s" % (self.ident, smburl)
if depth > 10: #maximum recursion depth
return
try:
entries = self.ctx.opendir(smburl).getdents()
except:
return
for e in entries:
try:
if e.smbc_type < 0 or e.name.startswith('.'):
continue
except:
continue
#3L: file share 7L: directory
if e.smbc_type == smbc.FILE_SHARE or e.smbc_type == 7L:
try:
r = Resource()
r.uri = smburl
r.server = self.target
r.comment = e.comment
r.filetype = "directory"
try:
self.silos.addRes(r)
except:
print r
self.dance(smburl + "/" + e.name, depth+1)
except:
pass
#raise
elif e.smbc_type == 8:
try:
r = Resource()
r.uri = smburl + "/" + e.name
r.server = self.target
r.filesize = self.ctx.stat(r.uri)[6]
try:
self.silos.addRes(r)
except:
print r
except:
pass
#raise
def run(self):
print "[%d] start" % self.ident
time.sleep(1)
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cr = s.connect_ex((self.target, 139))
if cr != 0:
print "%s: port closed" % self.target
s.close()
print "[%d] dying" % self.ident
self.dancemanager.dyingDancer()
return
print "%s: port open" % self.target
s.close()
self.dance(self.uri)
print "results for %s gathered" % self.target
except:
print "%s error" % self.target
raise
print "[%d] finish" % self.ident
self.dancemanager.dyingDancer()
if __name__ == "__main__":
s = SambaDancer("10.176.0.176", None, None)
s.run()