Repository navigation
Expand file tree
/
Copy pathmysql.py
More file actions
197 lines (162 loc) · 7.33 KB
/
Copy pathmysql.py
File metadata and controls
197 lines (162 loc) · 7.33 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
"""
docstring
"""
import MySQLdb
from twisted.internet import defer
from twisted.enterprise import adbapi
from twisted.python import log
import cowrie.core.output
class ReconnectingConnectionPool(adbapi.ConnectionPool):
"""
Reconnecting adbapi connection pool for MySQL.
This class improves on the solution posted at
http://www.gelens.org/2008/09/12/reinitializing-twisted-connectionpool/
by checking exceptions by error code and only disconnecting the current
connection instead of all of them.
Also see:
http://twistedmatrix.com/pipermail/twisted-python/2009-July/020007.html
"""
def _runInteraction(self, interaction, *args, **kw):
try:
return adbapi.ConnectionPool._runInteraction(
self, interaction, *args, **kw)
except MySQLdb.OperationalError as e:
if e[0] not in (2003, 2006, 2013):
raise log.msg("RCP: got error %s, retrying operation" %(e))
conn = self.connections.get(self.threadID())
self.disconnect(conn)
# Try the interaction again
return adbapi.ConnectionPool._runInteraction(
self, interaction, *args, **kw)
class Output(cowrie.core.output.Output):
"""
docstring here
"""
def __init__(self, cfg):
self.cfg = cfg
cowrie.core.output.Output.__init__(self, cfg)
def start(self):
"""
docstring here
"""
if self.cfg.has_option('output_mysql', 'port'):
port = int(self.cfg.get('output_mysql', 'port'))
else:
port = 3306
try:
self.db = ReconnectingConnectionPool('MySQLdb',
host = self.cfg.get('output_mysql', 'host'),
db = self.cfg.get('output_mysql', 'database'),
user = self.cfg.get('output_mysql', 'username'),
passwd = self.cfg.get('output_mysql', 'password'),
port = port,
cp_min = 1,
cp_max = 1)
except MySQLdb.Error as e:
log.msg("Error %d: %s" % (e.args[0], e.args[1]))
self.db.start()
def stop(self):
"""
docstring here
"""
self.db.close()
def sqlerror(self, error):
"""
docstring here
"""
log.err( 'MySQL Error:', error.value )
def simpleQuery(self, sql, args):
"""
Just run a deferred sql query, only care about errors
"""
d = self.db.runQuery(sql, args)
d.addErrback(self.sqlerror)
@defer.inlineCallbacks
def write(self, entry):
"""
docstring here
"""
if entry["eventid"] == 'cowrie.session.connect':
r = yield self.db.runQuery(
"SELECT `id` FROM `sensors` WHERE `ip` = %s", (self.sensor,))
if r:
sensorid = r[0][0]
else:
yield self.db.runQuery(
'INSERT INTO `sensors` (`ip`) VALUES (%s)', (self.sensor,))
r = yield self.db.runQuery('SELECT LAST_INSERT_ID()')
sensorid = int(r[0][0])
self.simpleQuery(
"INSERT INTO `sessions` (`id`, `starttime`, `sensor`, `ip`)"
+ " VALUES (%s, STR_TO_DATE(%s, %s), %s, %s)",
(entry["session"], entry["timestamp"], '%Y-%m-%dT%H:%i:%s.%fZ',
sensorid, entry["src_ip"]))
elif entry["eventid"] == 'cowrie.login.success':
self.simpleQuery('INSERT INTO `auth` (`session`, `success`' + \
', `username`, `password`, `timestamp`)' + \
' VALUES (%s, %s, %s, %s, STR_TO_DATE(%s, %s))',
(entry["session"], 1, entry['username'], entry['password'],
entry["timestamp"], '%Y-%m-%dT%H:%i:%s.%fZ'))
elif entry["eventid"] == 'cowrie.login.failed':
self.simpleQuery('INSERT INTO `auth` (`session`, `success`' + \
', `username`, `password`, `timestamp`)' + \
' VALUES (%s, %s, %s, %s, STR_TO_DATE(%s, %s))',
(entry["session"], 0, entry['username'], entry['password'],
entry["timestamp"], '%Y-%m-%dT%H:%i:%s.%fZ'))
elif entry["eventid"] == 'cowrie.command.success':
self.simpleQuery('INSERT INTO `input`' + \
' (`session`, `timestamp`, `success`, `input`)' + \
' VALUES (%s, STR_TO_DATE(%s, %s), %s , %s)',
(entry["session"], entry["timestamp"], '%Y-%m-%dT%H:%i:%s.%fZ',
1, entry["input"]))
elif entry["eventid"] == 'cowrie.command.failed':
self.simpleQuery('INSERT INTO `input`' + \
' (`session`, `timestamp`, `success`, `input`)' + \
' VALUES (%s, STR_TO_DATE(%s, %s), %s , %s)',
(entry["session"], entry["timestamp"], '%Y-%m-%dT%H:%i:%s.%fZ',
0, entry["input"]))
elif entry["eventid"] == 'cowrie.session.file_download':
self.simpleQuery('INSERT INTO `downloads`' + \
' (`session`, `timestamp`, `url`, `outfile`, `shasum`)' + \
' VALUES (%s, STR_TO_DATE(%s, %s), %s, %s, %s)',
(entry["session"], entry["timestamp"], '%Y-%m-%dT%H:%i:%s.%fZ',
entry['url'], entry['outfile'], entry['shasum']))
elif entry["eventid"] == 'cowrie.session.file_download':
self.simpleQuery('INSERT INTO `input`' + \
' (`session`, `timestamp`, `realm`, `input`)' + \
' VALUES (%s, STR_TO_DATE(%s, %s), %s , %s)',
(entry["session"], entry["timestamp"], '%Y-%m-%dT%H:%i:%s.%fZ',
entry["realm"], entry["input"]))
elif entry["eventid"] == 'cowrie.client.version':
r = yield self.db.runQuery(
'SELECT `id` FROM `clients` WHERE `version` = %s', \
(entry['version'],))
if r:
id = int(r[0][0])
else:
yield self.db.runQuery(
'INSERT INTO `clients` (`version`) VALUES (%s)', \
(entry['version'],))
r = yield self.db.runQuery('SELECT LAST_INSERT_ID()')
id = int(r[0][0])
self.simpleQuery(
'UPDATE `sessions` SET `client` = %s WHERE `id` = %s',
(id, entry["session"]))
elif entry["eventid"] == 'cowrie.client.size':
self.simpleQuery(
'UPDATE `sessions` SET `termsize` = %s WHERE `id` = %s',
('%sx%s' % (entry['width'], entry['height']),
entry["session"]))
elif entry["eventid"] == 'cowrie.session.closed':
self.simpleQuery(
'UPDATE `sessions` SET `endtime` = STR_TO_DATE(%s, %s)' + \
' WHERE `id` = %s', (entry["timestamp"],
'%Y-%m-%dT%H:%i:%s.%fZ', entry["session"]))
elif entry["eventid"] == 'cowrie.log.closed':
self.simpleQuery(
'INSERT INTO `ttylog` (`session`, `ttylog`, `size`) VALUES (%s, %s, %s)',
(entry["session"], entry["ttylog"], entry["size"]))
elif entry["eventid"] == 'cowrie.client.fingerprint':
self.simpleQuery(
'INSERT INTO `keyfingerprints` (`session`, `username`, `fingerprint`) VALUES (%s, %s, %s)',
(entry["session"], entry["username"], entry["fingerprint"]))