-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhoneypot_db.py
153 lines (137 loc) · 5.7 KB
/
honeypot_db.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
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
#!/usr/bin/env python
from coret_config import *
import imp
import socket
import subprocess
try:
imp.find_module('sqlite3')
USE_DB = True
except ImportError:
print "sqlite3 module wasn't found, skipping it."
print "Maybe try:"
print "pip install sqlite3"
USE_DB = False
if USE_DB:
import sqlite3
class HoneypotDB:
_dberr = False
# Singleton implementation
def __call__(self):
return self
def __init__(self):
if USE_DB:
try:
self.conn = sqlite3.connect('/opt/kojoney/kojoney.sqlite3')
except Exception as err:
print "ERROR: SQLite error in HoneypotDB.__init__() " , err
self._dberr = True
return None;
def __del(self):
self.connection.close()
def check_recent(self, username):
'Get recent login attempts with a username to limit valid passwords for a set time'
#added by Josh Bauer <[email protected]>
if not self._dberr:
try:
cursor = self.conn.cursor()
sql = """select password from login_attempts
where time > date('now','-1 day')
and username = ? order by time desc LIMIT 1"""
cursor.execute(sql, (str(username),))
retval = cursor.fetchone()
cursor.close()
return retval
except Exception as err:
print "ERROR: SQLite error in HoneypotDB.checkRecentAttempts() " , err
return False
def log_command(self, command, ip):
global WHITELIST
#whitelist functionality added by Josh Bauer <[email protected]>
if ip in WHITELIST:
print 'command database entry skipped due to whitelisted ip: '+ip
elif not self._dberr:
try:
sql = """INSERT INTO executed_commands
(time, command, ip, ip_numeric, sensor_id)
VALUES
(CURRENT_TIMESTAMP, ?, ?, ?, ?)"""
cursor = self.conn.cursor()
cursor.execute(sql , (command, ip, socket.inet_aton(ip), SENSOR_ID))
self.conn.commit()
cursor.close()
except sqlite3.Error as msg:
print "ERROR: SQLite error in HoneypotDB.log_command() ", msg
def log_download(self,ip, url, filemd5, filename, filetype, SENSOR_ID):
if not self._dberr:
try:
sql = """INSERT INTO downloads (time, ip, ip_numeric, url, md5sum, filename, filetype, sensor_id)
VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?, ?)"""
cursor = self.conn.cursor()
cursor.execute(sql , (ip, socket.inet_aton(ip), url, filemd5, filename, filetype, SENSOR_ID))
self.conn.commit()
cursor.close()
except sqlite3.Error as msg:
print "ERROR: SQLite error in HoneypotDB.log_download() ", msg
def log_login(self, ip, username, password):
if not self._dberr:
try:
sql = """INSERT INTO login_attempts
(time, ip, ip_numeric, username, password, sensor_id)
VALUES
(CURRENT_TIMESTAMP, ?, ?, ?, ?, ?)"""
cursor = self.conn.cursor()
cursor.execute(sql , (ip, socket.inet_aton(ip), username, password, SENSOR_ID))
self.conn.commit()
cursor.close()
except sqlite3.Error as msg:
print "ERROR: SQLite error in HoneypotDB.log_login() ", msg
#add missing tables to the database
#added by Josh Bauer <[email protected]>
def update_db(self):
try:
sql = """CREATE TABLE IF NOT EXISTS `login_attempts` (
`id` INTEGER PRIMARY KEY,
`time` TIMESTAMP,
`ip` VARCHAR(15),
`username` VARCHAR(16),
`password` VARCHAR(20),
`ip_numeric` INTEGER,
`sensor_id` INTEGER
);
CREATE TABLE IF NOT EXISTS `executed_commands` (
`id` INTEGER PRIMARY KEY,
`time` TIMESTAMP,
`ip` VARCHAR(15),
`command` VARCHAR(100),
`ip_numeric` INTEGER,
`sensor_id` INTEGER
);
CREATE TABLE IF NOT EXISTS `downloads` (
`id` INTEGER PRIMARY KEY,
`time` TIMESTAMP,
`ip` VARCHAR(15),
`ip_numeric` INTEGER,
`url` VARCHAR(100),
`filename` TEXT,
`md5sum` VARCHAR(32),
`filetype` VARCHAR(255),
`clamsig` TEXT,
`sensor_id` INTEGER
`file` LONGBLOB
);
-- nmap_scans table added by Josh Bauer <[email protected]>
CREATE TABLE IF NOT EXISTS `nmap_scans` (
`id` INTEGER PRIMARY KEY,
`time` TIMESTAMP,
`ip` VARCHAR(15),
`ip_numeric` INTEGER,
`sensor_id` INTEGER,
`nmap_output` TEXT
);"""
cursor = self.conn.cursor()
cursor.executescript(sql)
self.conn.commit()
cursor.close()
#subprocess.Popen('mysql -u %s --password=%s -h %s < create_tables.sql' % (DATABASE_USER, DATABASE_PASS, DATABASE_HOST) , stdout=subprocess.PIPE, shell=True)
except sqlite3.Error as e:
print "ERROR: SQLite error in HoneypotDB.update_db() ", e.args[0]