Skip to content

Instantly share code, notes, and snippets.

@ashleyprimo
Forked from theMiddleBlue/secthemall-tor.py
Last active April 9, 2018 03:19
Show Gist options
  • Save ashleyprimo/77ba51ef3f88c4c659958e405fec680b to your computer and use it in GitHub Desktop.
Save ashleyprimo/77ba51ef3f88c4c659958e405fec680b to your computer and use it in GitHub Desktop.
Script for download the SECTHEMALL Tor Reputation IPs list
#!/usr/bin/env python
'''
To run every 60 seconds (1 minute) create a crontab like the following:
* * * * * /usr/local/bin/python2.7 /opt/secthemall-tor/secthemall-tor.py
In addition you may run in to issues using Python versions above or below 2.9,
thus my recommendation is you use version 2.9
Edit's made are fairly minor just mainly allow this program to run using crontab,
instead of a loop.
'''
# ---------------- CONFIG ------------------
username = "your@secthemall username here"
apikey = "your API Key here"
size = "1000"
nginx_reload_cmd = "service nginx reload"
log_on_update="" # Example: update.log OR leave it blank to disable logging.
'''
The log function, if enabled will create a log entry every time the python script
downloads an update. Great to ensure you have configured automatic running
of this script correctly / successfully.
The log name will be as specified and stored in the base directory of the script.
If you wish to disable leave the field blank, with no spaces- as shown default.
'''
# ------------------------------------------
try:
import sys, httplib, urllib, json, os, re, base64, ssl, time
from os import path
timestamp=time.strftime("%d/%m/%Y %H:%M:%S")
basedir = os.path.dirname(os.path.realpath(__file__))
headers = {
'User-Agent':'secthemall/tor 1.0',
'Authorization':'Basic '+base64.b64encode(username+':'+apikey, None)
}
update = False
savedid = ''
conn = httplib.HTTPSConnection('secthemall.com', 443, timeout=10)
conn.request('GET', '/public-list/tor-exit-nodes/json?lastid=true', urllib.urlencode({}), headers)
res = json.loads(conn.getresponse().read())
conn.close()
if os.path.exists(basedir+'/lastid'):
f = open(basedir+'/lastid', 'r')
savedid = f.read().strip()
else:
f = open(basedir+'/lastid', 'w')
f.write(res['lastid'])
if savedid != res['lastid'].strip():
f = open(basedir+'/lastid', 'w')
f.write(res['lastid'])
update = True
try:
if log_on_update != "":
f = open(basedir+"/"+log_on_update, 'a')
f.write("\n[" + timestamp + "] Tor Exit Node Database Updated.")
except IOError:
print "Log writing error -> IOError."
except:
print "Log writing error."
if update is True:
print "Lastid changed, updating Tor exit nodes list..."
conn = httplib.HTTPSConnection('secthemall.com', 443, timeout=10)
conn.request('GET', '/public-list/tor-exit-nodes/json?size='+str(size), urllib.urlencode({}), headers)
res = json.loads(conn.getresponse().read())
# Nginx deny list
writetofile = '';
for i in res['results']:
writetofile += 'deny '+i['ip']+";\n"
f = open(basedir+'/nginx_deny_tor.txt', 'w')
f.write(writetofile.strip())
# ModSecurity IP List
writetofile = '';
for i in res['results']:
writetofile += i['ip']+"\n";
f = open(basedir+'/modsecurity_deny_tor.txt', 'w')
f.write(writetofile.strip())
conn.close()
os.system(nginx_reload_cmd)
print "Updated, exiting."
else:
print "Lastid not changed, exiting..."
exit()
except KeyboardInterrupt:
print "\nInterrupt Requested. Closing."
except ImportError:
print "\nA module does not appear to be installed on this Python Installation."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment