-
Notifications
You must be signed in to change notification settings - Fork 11
/
honeypot.py
256 lines (207 loc) · 6.65 KB
/
honeypot.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
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
#! /usr/bin/env python
import time
import threading
import os, sys, Queue
from time import gmtime, strftime
from itertools import groupby
from operator import itemgetter
import os.path
import imapfile
import logging
import honeypotconfig
import scan
import bing
import executemechanize
import malwebsites
import normalize
import updateantivirus
import yaradetection
import unquote
import argparse
import extraction
try:
import signal
from signal import SIGPIPE, SIG_IGN
signal.signal(signal.SIGPIPE, signal.SIG_IGN)
except ImportError:
pass
queue=Queue.Queue()
logger = logging.getLogger()
def worker():
urldict = queue.get()
#this is for the normal visitor output (no error)
logger.info(str(urldict["counter"]) + ",\t" + urldict["url"]+",\t"+ "Visiting")
executemechanize.executemechanize(urldict)
queue.task_done()
def threadmaker():
while True:
threadstomake = honeypotconfig.threadnum - threading.active_count()
for i in range(threadstomake):
thread = threading.Thread(target=worker)
thread.setDaemon(True)
thread.start()
time.sleep(5)
def readurl():
url = sys.argv[2]
return url
def main():
#Create the threads
thread = threading.Thread(target=threadmaker)
thread.setDaemon(True)
thread.start()
script_path = os.path.dirname(os.path.abspath( __file__ ))
parser = argparse.ArgumentParser(description="Examples:\n/honeypot.py --url www.yahoo.com\nhoneypot.py --file <file path>\n./honeypot.py --blacklist\n./honeypot.py --email\n./honeypot.py --update\n./honeypot.py --search <warez>\n./honeypot.py --local <file/directory path>", formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("--email", help="Retrieves your Spam emails from your mail server and crawls the extracted URLS. Enter your email credentials in honeypotconfig.py file!", action="store_true")
parser.add_argument("--update", help="Updates the anti-virus signatures", action="store_true")
parser.add_argument("--blacklist", help="Downloads list of suspicious malicious websites from three databases and retrieves/scans them accordingly", action="store_true")
parser.add_argument("--file", nargs=1, help="Provide an input file", action="store")
parser.add_argument("--url", nargs=1, help="Provide a url", action="store")
parser.add_argument("--search", nargs=1, help="searches Bing search engine for a keyword (1 single keyword at the moment) and returns 100 results starting from the 20th result.", action="store")
parser.add_argument("--local", nargs=1, help="scans a local file or directory for malicious signatures.", action="store")
parser.add_argument("--debug", help="Include http header", action="store_true")
parser.add_argument("--crawler", help="Crawl the sites and save any executables found", action="store_true")
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
path = script_path+"/tmp"
print path
#create the tmp folder
if not os.path.isdir(path):
os.makedirs("tmp")
#Crawler
if args.crawler:
executemechanize.crawler = True
#Logging
"""Initialize logger."""
command = "mkdir -p debug/" #create a temporary folder in your working space folder
os.system(command)
sys.stdin=open("debug/" + time.asctime(time.localtime(time.time())) +".log", "a")
logger = logging.getLogger()
sh = logging.StreamHandler()
sh.setFormatter(SpecialFormatter())
sh2 = logging.StreamHandler(sys.stdin)
sh2.setFormatter(SpecialFormatter())
logger.addHandler(sh)
logger.addHandler(sh2)
logger.setLevel(logging.INFO)
if args.debug:
logger.setLevel(logging.DEBUG)
executemechanize.set_logging_level(logging.DEBUG)
#Update antivirus signatures
if args.update:
updateantivirus.updateantivirus()
#Blacklist Databases
if args.blacklist:
try:
if not os.path.exists("list"):
os.mkdir("list")
except OSError as e:
logger.error(e)
malwebsites.domaindownload()
malwebsites.duplicateremover()
urls = open("list/malwebsites.txt", "r")
counter = 0
for line in urls:
dict={}
counter += 1
dict["url"] = line.strip()
dict["counter"] = counter
queue.put(dict)
queue.join()
scan.scanning(path)
yaradetection.listandscan(path)
unquote.unquoteDirectory(path)
#Email
if args.email:
imapfile.imap()
extraction.extracturl()#extracts urls from emails.txt file
extraction.duplicateremover() #removes the duplicate urls from crawler.txt files (which now contain extracted urls from emails.txt)
os.remove("emails.txt")
urls = open('crawler.txt', "r")
counter = 0
for line in urls:
dict={}
counter += 1
dict["url"] = line.rstrip()
dict["counter"] = counter
queue.put(dict)
queue.join()
scan.scanning(path)
yaradetection.listandscan(path)
unquote.unquoteDirectory(path)
#File
if args.file:
mylist = list()
mylist2 = list()
counter =0
fopen3 = open(sys.argv[2],"r")
for line in fopen3:
dict={}
line = line.strip()
counter += 1
if not (line.startswith("http://")) and not (line.startswith("https://")):
line = "http://"+line
dict["url"] = line
dict["counter"] = counter
queue.put(dict)
queue.join()
fopen3.close()
scan.scanning(path)
yaradetection.listandscan(path)
unquote.unquoteDirectory(path)
#URL
if args.url:
url = readurl()
url = normalize.normalizeurl(url)
dict={}
counter = 1
if not (url.startswith("http://")) and not (url.startswith("https://")):
url = "http://"+url
dict["url"] = url
dict["counter"] = counter
queue.put(dict)
queue.join()
# executemechanize.executemechanize(url)
scan.scanning(path)
yaradetection.listandscan(path)
unquote.unquoteDirectory(path)
#Search
if args.search:
keyword = sys.argv[2]
bing.searchBing(keyword)
mylist = list()
fopen = open("list/searchresult.txt","r")
for line in fopen:
line = line.strip()
if not line:
continue
mylist.append(line)
fopen.close()
counter = 0
for line in mylist:
dict={}
counter += 1
dict["url"] = line
dict["counter"] = counter
queue.put(dict)
queue.join()
scan.scanning(path)
yaradetection.listandscan(path)
unquote.unquoteDirectory(path)
#Local Scan
if args.local:
path = sys.argv[2]
scan.scanning(path)
yaradetection.listandscan(path)
unquote.unquoteDirectory(path)
class SpecialFormatter(logging.Formatter):
FORMATS = {logging.INFO : "%(name)s,\t%(levelname)s,\t%(message)s", 'DEFAULT' : "%(name)s,\t%(levelname)s,\t%(message)s"}
def formatTime(self, record, datefmt=None):
self._datefmt = time.strftime("%Y-%m-%d %H:%M:%S")
return logging.Formatter.formatTime(self, record, self._datefmt)
def format(self, record):
self._fmt = self.FORMATS.get(record.levelno, self.FORMATS['DEFAULT'])
return logging.Formatter.format(self, record)
if __name__ == "__main__":
main()