forked from dmuhs/pastebin-scraper
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathsniff-paste.py
executable file
·705 lines (588 loc) · 26.2 KB
/
sniff-paste.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#!/usr/bin/env python3
import json
from ipaddress import IPv4Address as IPv4
from ipaddress import IPv4Network as IPv4Net
import logging
import socket
import logging.handlers
import os
import sys
import threading
import time
from datetime import datetime
from os import path
import re
import requests
from lxml import html
import nmap
import configparser
import queue
from colorlog import ColoredFormatter
from sqlalchemy import Column, Integer, String, DateTime, Boolean
from sqlalchemy.dialects.mysql import LONGTEXT
debug = False
IPStack = []
secretRegexes = {
"Slack Token": "(xox[p|b|o|a]-[0-9]{12}-[0-9]{12}-[0-9]{12}-[a-z0-9]{32})",
"RSA private key": "-----BEGIN RSA PRIVATE KEY-----",
"SSH (OPENSSH) private key": "-----BEGIN OPENSSH PRIVATE KEY-----",
"SSH (DSA) private key": "-----BEGIN DSA PRIVATE KEY-----",
"SSH (EC) private key": "-----BEGIN EC PRIVATE KEY-----",
"PGP private key block": "-----BEGIN PGP PRIVATE KEY BLOCK-----",
"Facebook Oauth": "[f|F][a|A][c|C][e|E][b|B][o|O][o|O][k|K].*['|\"][0-9a-f]{32}['|\"]",
"Twitter Oauth": "[t|T][w|W][i|I][t|T][t|T][e|E][r|R].*['|\"][0-9a-zA-Z]{35,44}['|\"]",
"GitHub": "[g|G][i|I][t|T][h|H][u|U][b|B].*[['|\"]0-9a-zA-Z]{35,40}['|\"]",
"Google Oauth": "(\"client_secret\":\"[a-zA-Z0-9-_]{24}\")",
"AWS API Key": "AKIA[0-9A-Z]{16}",
"Heroku API Key": "[h|H][e|E][r|R][o|O][k|K][u|U].*[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}",
"Generic Secret": "[s|S][e|E][c|C][r|R][e|E][t|T].*['|\"][0-9a-zA-Z]{32,45}['|\"]"
}
cryptoRegexes={
"bitcoin-address" : "[13][a-km-zA-HJ-NP-Z1-9]{25,34}" ,
"bitcoin-uri" : "bitcoin:([13][a-km-zA-HJ-NP-Z1-9]{25,34})" ,
"bitcoin-xpub-key" : "(xpub[a-km-zA-HJ-NP-Z1-9]{100,108})(\\?c=\\d*&h=bip\\d{2,3})?" ,
"monero-address": "(?:^4[0-9AB][1-9A-HJ-NP-Za-km-z]{93}$)",
"ethereum-address": "(?:^0x[a-fA-F0-9]{40}$)",
"litecoin-address":"(?:^[LM3][a-km-zA-HJ-NP-Z1-9]{26,33}$)",
"bitcoin-cash-address":"(?:^[13][a-km-zA-HJ-NP-Z1-9]{33}$)",
"dash-address":"(?:^X[1-9A-HJ-NP-Za-km-z]{33}$)",
"ripple-address":"(?:^r[0-9a-zA-Z]{33}$)",
"neo-address":"(?:^A[0-9a-zA-Z]{33}$)",
"dogecoin-address":"(?:^D{1}[5-9A-HJ-NP-U]{1}[1-9A-HJ-NP-Za-km-z]{32}$)"
}
class PasteDBConnector(object):
supported = ('MYSQL')
def __init__(self, db, **kwargs):
try:
self.logger = logging.getLogger('pastebin-scraper')
from sqlalchemy.ext.declarative import declarative_base
except ImportError:
self.logger.error('SQLAlchemy import failed. Make sure the SQLAlchemy Python library '
'is installed! To check your existing installation run: '
'python3 -c "import sqlalchemy;print(sqlalchemy.__version__)"')
self.db = db
self.Base = declarative_base()
self.engine = self._get_db_engine(**kwargs)
self.session = self._get_db_session(self.engine)
#Create tables for credentials
self.paste_model = self._get_paste_model(self.Base, **kwargs)
self.email_model = self._get_email_model(self.Base)
self.link_model = self._get_link_model(self.Base)
self.ip_model = self._get_ip_model(self.Base)
self.phone_model = self._get_phone_model(self.Base)
self.secret_model = self._get_secret_model(self.Base)
self.crypto_model = self._get_crypto_model(self.Base)
self.port_model = self._get_port_model(self.Base)
self.Base.metadata.create_all(self.engine)
#Nmap Worker
nmapper = threading.Thread(target=self._scan_network)
nmapper.setDaemon(True)
nmapper.start()
def isFiltered(self,ip):
with open("res/nmapfilter.conf") as ipFilters:
for networkString in ipFilters:
ip=IPv4(ip)
network=IPv4Net(networkString.rstrip("\r\n"))
if(ip in network):
self.logger.debug("["+str(ip)+"] is a filtered address, do not scan.")
return True
return False
def _get_db_engine(self, **kwargs):
from sqlalchemy import create_engine
if self.db == 'MYSQL':
# use the mysql-python connector
location = 'mysql+pymysql://'
location += '{username}:{password}@{host}:{port}'.format(
host=kwargs.pop('host'),
port=kwargs.pop('port'),
username=kwargs.pop('username'),
password=kwargs.pop('password'),
)
location += '/{table_name}?charset={charset}'.format(
table_name=kwargs.pop('table_name'),
charset='utf8'
)
self.logger.info('Using MySQL')
return create_engine(location)
def _get_db_session(self, engine):
from sqlalchemy.orm import sessionmaker
return sessionmaker(bind=engine)()
def _get_paste_model(self, base, **kwargs):
class Paste(base):
__tablename__ = "pastes"
id = Column(Integer, primary_key=True)
name = Column('name', String(60))
lang = Column('language', String(30))
link = Column('link', String(28)) # Assuming format http://pastebin.com/XXXXXXXX
date = Column('date', DateTime())
data = Column('data', LONGTEXT(charset='utf8'))
def __repr__(self):
return "<Paste(id=%s, name='%s', lang='%s', link='%s', date='%s', data='%s')" %\
(self.id,
self.name,
self.lang,
self.link,
str(self.date),
self.data[:10])
return Paste
def _get_email_model(self, base):
class Email(base):
__tablename__ = "emails"
id = Column(Integer, primary_key=True)
email = Column('email', String(90))
link = Column('link', String(28)) # Assuming format http://pastebin.com/XXXXXXXX
def __repr__(self):
return "<Email(id=%s, email='%s', link='%s')" %\
(self.id,
self.email,
self.link)
return Email
def _get_link_model(self, base):
class Link(base):
__tablename__ = "links"
id = Column(Integer, primary_key=True)
url = Column('url', String(60))
link = Column('link', String(28)) # Assuming format http://pastebin.com/XXXXXXXX
def __repr__(self):
return "<Link(id=%s, url='%s', link='%s')" %\
(self.id,
self.url,
self.link)
return Link
def _get_phone_model(self, base):
class Phone(base):
__tablename__ = "phones"
id = Column(Integer, primary_key=True)
phone = Column('phone', String(60))
link = Column('link', String(28)) # Assuming format http://pastebin.com/XXXXXXXX
def __repr__(self):
return "<Phone(id=%s, phone='%s', link='%s')" %\
(self.id,
self.phone,
self.link)
return Phone
def _get_secret_model(self, base):
class Secret(base):
__tablename__ = "secrets"
id = Column(Integer, primary_key=True)
secret = Column('secret', String(60))
link = Column('link', String(28)) # Assuming format http://pastebin.com/XXXXXXXX
def __repr__(self):
return "<Secret(id=%s, secret='%s', link='%s')" %\
(self.id,
self.secret,
self.link)
return Secret
def _get_crypto_model(self, base):
class Crypto(base):
__tablename__ = "cryptos"
id = Column(Integer, primary_key=True)
genre = Column('genre', String(60))
content = Column('content', String(60))
link = Column('link', String(28)) # Assuming format http://pastebin.com/XXXXXXXX
def __repr__(self):
return "<Crypto(id=%s, genre='%s', content='%s', link='%s' )" %\
(self.id,
self.genre,
self.content,
self.link
)
return Crypto
def _get_port_model(self, base):
class Port(base):
__tablename__ = "ports"
id = Column(Integer, primary_key=True)
ip = Column('ip', String(60))
port = Column('port', String(10))
service = Column('service', String(60))
status = Column('status', String(40))
version= Column('version', String(60))
def __repr__(self):
return "<Port(id=%s, ip='%s', port='%s', service='%s', status='%s', version='%s')" %\
(self.id,
self.ip,
self.port,
self.service,
self.status,
self.version
)
return Port
def _get_ip_model(self, base):
class IP(base):
__tablename__ = "ips"
id = Column(Integer, primary_key=True)
ip = Column('ip', String(60))
online = Column('online', Boolean())
link = Column('link', String(28)) # Assuming format http://pastebin.com/XXXXXXXX
def __repr__(self):
return "<IP(id=%s, ip='%s', online='%s', link='%s')" %\
(self.id,
self.ip,
self.online,
self.link)
return IP
def harvest(self,pasteLink, data):
print("Harvesting Data From: "+pasteLink)
ips = re.findall( r'[0-9]+(?:\.[0-9]+){3}', str(data ) )
emails = re.findall( r"[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+", str(data ) )
phoneNumbers= re.findall( r"\(?\b[2-9][0-9]{2}\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}\b", str(data))
urls = re.findall( 'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', str(data))
urls += re.findall( 'http?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', str(data))
secrets = None
# Remove all duplicates
ips = list(set(ips))
emails= list(set(emails))
phoneNumbers = list(set(phoneNumbers))
urls= list(set(urls))
for finding in urls:
url_model = self.link_model(
url=finding,
link=pasteLink
)
try:
self.session.add(url_model)
self.session.commit()
except:
print("Error: Url model not committed, session rollback")
self.session.rollback()
#ADD TO SQL +=url_model
for finding in ips:
#print("IP: "+finding)
try:
socket.inet_aton(finding)
IPStack.append((finding, pasteLink))
except socket.error:
print("Invalid ip: "+finding)
#ret = os.system("ping -o -c 2 -W 500 "+finding)
#pingRes = ret != 0
for finding in emails:
email_model = self.email_model(
email=finding,
link=pasteLink
)
try:
self.session.add(email_model)
self.session.commit()
except:
print("Error: Email model not committed, session rollback")
self.session.rollback()
for finding in phoneNumbers:
try:
if( ' ' in finding or '-' in finding or '(' in finding or ')' in finding):
phone_model = self.phone_model(
phone=finding,
link=pasteLink
)
try:
self.session.add(phone_model)
self.session.commit()
except Exception as e:
print("Error: Phone model not committed, session rollback: "+str(e))
self.session.rollback()
except:
print("Error: Phone number submission")
totalSecrets= 0
for key, value in secretRegexes.items():
secrets = re.findall(value, str(data))
for secret in secrets:
totalSecrets+=1
print("\t"+key+": "+secret)
secret_model = self.secret_model(
secret=key,
link=pasteLink
)
try:
self.session.add(secret_model)
self.session.commit()
except:
print("Error: Secret model not committed, session rollback")
self.session.rollback()
totalCryptoFindings= 0
for key, value in cryptoRegexes.items():
findings = re.findall(value, str(data))
for finding in findings:
totalCryptoFindings+=1
crypto_model = self.crypto_model(
genre=key,
content=finding,
link=pasteLink
)
try:
self.session.add(crypto_model)
self.session.commit()
except:
print("Error: Crypto model not committed, session rollback")
self.session.rollback()
print("IPS: "+str(len(ips)))
print("Emails: "+str(len(emails)))
print("URLS: "+str(len(urls)))
print("Phones: "+str(len(phoneNumbers)))
print("Secrets: "+str(totalSecrets))
print("Cryptos: "+str(totalCryptoFindings))
print("\n\n")
def add(self, paste, data):
paste_model = self.paste_model(
name=paste[0],
lang=paste[1],
link=paste[2],
date=datetime.now(),
data=data.content.replace(b'\\', b'\\\\').decode('unicode-escape')
)
#Start Harvest Here
self.logger.debug('Harvesting From model ' + str(paste_model))
self.harvest(paste[2], data.content.replace(b'\\', b'\\\\').decode('unicode-escape'))
try:
self.session.add(paste_model)
self.session.commit()
except:
self.logger.error(
'An error occurred while adding a paste to %s: %s' %
(self.db, sys.exc_info()[0])
)
def _scan_network(self):
nm = nmap.PortScanner()
while True:
if(IPStack): #Check for connectivity, parse nmap if valid
finding, pasteLink = IPStack.pop()
print("NMAP WORKER CALLED ["+finding+"]")
print("Left on stack: " +str(len(IPStack)))
if(self.isFiltered(finding)==False):
self.logger.debug('Nmap scan on IP: ' + finding)
try:
nm.scan(finding, arguments='-sV')
except Exception as e:
print("Nmap scan error: "+str(e))
print("Nmap Scan ["+finding+"] Complete")
try:
state= nm[finding]['status']['state']
except:
state= "down"
if("up" in state):
portScan= nm[finding]['tcp']
self.logger.debug(finding+": up\tports: "+str(portScan))
for key, value in portScan.items():
print(finding+":"+str(key))
product= value['product']
ver= value['version']
state= value['state']
name= value['name']
try:
print("Name: "+name+"\n\tProduct: "+product+"\n\tVersion: "+ver+"\n\tState: "+state)
port_model = self.port_model(
ip = finding,
port = key,
service = product,
status = state,
version = ver
)
try:
self.session.add(port_model)
self.session.commit()
except:
self.session.rollback()
print("Error: Port Commit Issue, session rollback")
except Exception as e:
print("Exception error while pushing port model: "+str(e))
try:
ip_model = self.ip_model(
ip=finding,
online= True,
link=pasteLink
)
try:
self.session.add(ip_model)
self.session.commit()
except:
self.session.rollback()
print("Error pushing to mysql: "+ str(e))
except:
print("Error: IP Model issue")
else: #Else put a normal entry in for pastebin
self.logger.debug("IP["+finding+"] down")
try:
ip_model = self.ip_model(
ip=finding,
online= False,
link=pasteLink
)
try:
self.session.add(ip_model)
self.session.commit()
except:
self.session.rollback()
print("Error: IP Commit Issue, session rollback")
except Exception as e:
print("Error pushing to mysql: "+ str(e))
else: #Else put a nmap filtered kentry for pastebin
self.logger.debug("IP["+finding+"] is a filtered address")
try:
ip_model = self.ip_model(
ip=finding,
online= False,
link=pasteLink
)
try:
self.session.add(ip_model)
self.session.commit()
except:
self.session.rollback()
print("Error: IP Commit Issue, session rollback")
except Exception as e:
print("Error pushing to mysql: "+ str(e))
class PastebinScraper(object):
def __init__(self):
# Read and split config
self.config = configparser.ConfigParser()
if(debug):
self.config.read('settings.debug.ini')
else:
self.config.read('settings.ini')
self.conf_general = self.config['GENERAL']
self.conf_logging = self.config['LOGGING']
self.conf_stdout = self.config['STDOUT']
self.conf_mysql = self.config['MYSQL']
# Internals
self.unlimited_pastes = self.conf_general.getint('PasteLimit') == 0
self.pastes = queue.Queue(maxsize=8)
self.pastes_seen = set()
# Init the logger
self.logger = logging.getLogger('pastebin-scraper')
self.logger.setLevel(logging.DEBUG)
# Set up log rotation
rotation = logging.handlers.RotatingFileHandler(
filename=self.conf_logging['RotationLog'],
maxBytes=self.conf_logging.getint('MaxRotationSize'),
backupCount=self.conf_logging.getint('RotationBackupCount')
)
rotation.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s|%(levelname)-8s| %(message)s')
rotation.setFormatter(formatter)
self.logger.addHandler(rotation)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = ColoredFormatter(
'%(log_color)s%(asctime)s|[%(levelname)-4s] %(message)s%(reset)s', '%H:%M:%S'
)
console.setFormatter(formatter)
self.logger.addHandler(console)
if not (self.conf_stdout.getboolean('Enable') or self.conf_mysql.getboolean('Enable')):
self.logger.error('No output method specified! Please set at least one output method '
'in the settings.ini to \'yes\'.')
raise RuntimeError('No output method specified!')
# DB connectors if needed
self.mysql_conn = None
if self.conf_mysql.getboolean('Enable'):
self.logger.debug('Initializing MySQL connector')
self.mysql_conn = PasteDBConnector(
db='MYSQL',
host=self.conf_mysql['Host'],
port=self.conf_mysql['Port'],
username=self.conf_mysql['Username'],
password=self.conf_mysql['Password'],
table_name=self.conf_mysql['TableName']
)
def _get_paste_data(self):
paste_limit = self.conf_general.getint('PasteLimit')
pb_link = self.conf_general['PBLINK']
paste_counter = 0
self.logger.info('No scrape limit set - scraping indefinitely' if self.unlimited_pastes
else 'Paste limit: ' + str(paste_limit))
while self.unlimited_pastes or (paste_counter < paste_limit):
page = self._handle_data_download(pb_link)
self.logger.debug('Got {} - {} from {}'.format(
page.status_code,
page.reason,
pb_link
))
tree = html.fromstring(page.content)
pastes = tree.cssselect('ul.right_menu li')
for paste in pastes:
if not self.unlimited_pastes \
and (paste_counter >= paste_limit):
# Break for limits % 8 != 0
break
name_link = paste.cssselect('a')[0]
name = name_link.text_content().strip()
href = name_link.get('href')[1:] # Get rid of leading /
data = paste.cssselect('span')[0].text_content().split('|')
language = None
if len(data) == 2:
# Got language
language = data[0].strip()
paste_data = (name, language, href)
self.logger.debug('Paste scraped: ' + str(paste_data))
if paste_data[2] not in self.pastes_seen:
# New paste detected
self.logger.debug('Scheduling new paste:' + str(paste_data))
self.pastes_seen.add(paste_data[2])
self.pastes.put(paste_data)
delay = self.conf_general.getint('NewPasteCheckInterval')
time.sleep(delay)
paste_counter += 1
self.logger.debug('Paste counter now at ' + str(paste_counter))
if paste_counter % 100 == 0:
self.logger.info('Scheduled %d pastes' % paste_counter)
def _download_paste(self):
while True:
paste = self.pastes.get() # (name, lang, href)
self.logger.debug('Fetching raw paste ' + paste[2])
link = self.conf_general['PBLink'] + 'raw/' + paste[2]
data = self._handle_data_download(link)
self.logger.debug('Fetched {} with {} - {}'.format(
link,
data.status_code,
data.reason
))
if self.conf_stdout.getboolean('Enable'):
self._write_to_stdout(paste, data)
if self.conf_mysql.getboolean('Enable'):
self._write_to_mysql(paste, data)
def _handle_data_download(self, link):
while True:
try:
data = requests.get(link)
except:
retry = self.conf_general.getint('ConnectionRetryInterval')
self.logger.debug(
'Error connecting to %s: Retry in %ss, TRACE: %s' %
(link, retry, sys.exc_info())
)
self.logger.info('Connection problems - trying again in %ss' % retry)
time.sleep(retry)
else:
if data.status_code == 403 and b'Pastebin.com has blocked your IP' in data.content:
self.logger.info('Our IP has been blocked. Trying again in an hour.')
time.sleep(self.conf_general.getint('IPBlockedWaitTime'))
return data
def _assemble_output(self, conf, paste, data):
output = ''
if conf.getboolean('ShowName'):
output += 'Name: %s\n' % paste[0]
if conf.getboolean('ShowLang'):
output += 'Lang: %s\n' % paste[1]
if conf.getboolean('ShowLink'):
output += 'Link: %s\n' % (self.conf_general['PBLink'] + paste[2])
if conf.getboolean('ShowData'):
encoding = conf['DataEncoding']
limit = conf.getint('ContentDisplayLimit')
if limit > 0:
output += '\n%s\n\n' % data.content.decode(encoding)[:limit]
else:
output += '\n%s\n\n' % data.content.decode(encoding)
return output
def _write_to_stdout(self, paste, data):
output = self._assemble_output(self.conf_stdout, paste, data)
sys.stdout.write(output)
def _write_to_mysql(self, paste, data):
self.mysql_conn.add(paste, data)
def run(self):
for i in range(self.conf_general.getint('DownloadWorkers')):
t = threading.Thread(target=self._download_paste)
t.setDaemon(True)
t.start()
s = threading.Thread(target=self._get_paste_data)
s.start()
s.join()
if __name__ == '__main__':
ps = PastebinScraper()
ps.run()