Skip to content

Commit 7a7fac5

Browse files
committed
Code overhaul to support dynamic additions via static files or external files passed by argument
1 parent d571223 commit 7a7fac5

20 files changed

+1623
-1039
lines changed

README.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ sudo ./setup.sh
3232
usage: redirect_rules.py [-h] [-d DESTINATION]
3333
[--exclude EXCLUDE [EXCLUDE ...]]
3434
[--exclude-file EXCLUDE_FILE] [--exclude-list]
35+
[--ip-file IP_FILE [IP_FILE ...]]
36+
[--asn-file ASN_FILE [ASN_FILE ...]]
37+
[--hostname-file HOSTNAME_FILE [HOSTNAME_FILE ...]]
38+
[--useragent-file USERAGENT_FILE [USERAGENT_FILE ...]]
3539
[--verbose]
3640
37-
Dynamically generate redirect.rules file -- v1.2.1
41+
Dynamically generate redirect.rules file -- v1.2.2
3842
3943
optional arguments:
4044
-h, --help show this help message and exit
@@ -51,7 +55,18 @@ optional arguments:
5155
File containing items/group keywords to exclude (line
5256
separated).
5357
--exclude-list List all possible exclusions.
58+
--ip-file IP_FILE [IP_FILE ...]
59+
Provide one or more IP files to use as source data.
60+
--asn-file ASN_FILE [ASN_FILE ...]
61+
Provide one or more ASN files to use as source data.
62+
--hostname-file HOSTNAME_FILE [HOSTNAME_FILE ...]
63+
Provide one or more Hostname files to use as source
64+
data.
65+
--useragent-file USERAGENT_FILE [USERAGENT_FILE ...]
66+
Provide one or more User-Agent files to use as source
67+
data.
5468
--verbose Enable verbose output.
69+
5570
```
5671

5772
#### Example Run
@@ -60,13 +75,14 @@ optional arguments:
6075
6176
----------------------------------
6277
Redirect Rules Generation Tool
63-
v1.2.1
78+
v1.2.2
6479
----------------------------------
6580
6681
[*] Pulling @curi0usJack's redirect rules...
6782
[*] Writing @curi0usJack's redirect rules...
6883
[*] Adding conditions for bad User-Agents...
69-
[*] Adding Hostnames and IPs obtained via Malware Kit...
84+
[*] Adding static IPs obtained via Malware Kit's and other sources...
85+
[*] Adding static Hostnames obtained via Malware Kit's and other sources...
7086
[*] Pulling TOR exit node list...
7187
[*] Pulling AWS IP/Network list...
7288
[*] Pulling Google Cloud IP/network list...

core/source.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,41 @@
44
# https://github.com/0xdade/sephiroth/blob/master/providers/provider.py
55

66
# Import source modules
7+
from core.sources.ip import IP
78
from core.sources.tor import Tor
89
from core.sources.asn import RADB, BGPView
910
from core.sources.misc import Misc
1011
from core.sources.amazon import AWS
1112
from core.sources.oracle import OracleCloud
1213
from core.sources.google import GoogleCloud
1314
from core.sources.htaccess import HTAccess
15+
from core.sources.hostname import Hostname
1416
from core.sources.microsoft import Azure, Office365
1517
from core.sources.useragents import UserAgents
16-
from core.sources.malwarekit import MalwareKit
18+
19+
# Import module to read external sources
20+
from core.sources.external import IPFile, HostnameFile, UserAgentFile, ASNFile
1721

1822

1923
source_map = {
24+
'ips': IP,
2025
'tor': Tor,
2126
'aws': AWS,
2227
'radb': RADB,
2328
'misc': Misc,
2429
'azure': Azure,
2530
'bgpview': BGPView,
2631
'htaccess': HTAccess,
32+
'hostnames': Hostname,
2733
'office365': Office365,
28-
'malwarekit': MalwareKit,
2934
'user-agents': UserAgents,
3035
'oraclecloud': OracleCloud,
31-
'googlecloud': GoogleCloud
36+
'googlecloud': GoogleCloud,
37+
# External sources
38+
'ip-file': IPFile,
39+
'asn-file': ASNFile,
40+
'hostname-file': HostnameFile,
41+
'useragent-file': UserAgentFile
3242
}
3343

3444
class Source(object):

core/sources/asn.py

+47-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3+
import os
34
import re
45
import requests
56
import subprocess
@@ -10,7 +11,6 @@
1011
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
1112

1213
# Import static data
13-
from core.static import asns
1414
from core.support import REWRITE
1515

1616
# Import parent class
@@ -34,11 +34,26 @@ def __init__(self, workingfile, ip_list, args):
3434
self.return_data = self._process_source()
3535

3636

37+
def _get_source(self):
38+
# Read in static source file from static/ dir
39+
asn_list = []
40+
pwd = os.path.dirname(os.path.realpath(__file__))
41+
with open(pwd + '/../static/asns.txt', 'r') as _file:
42+
for line in _file.readlines():
43+
line = line.strip()
44+
if line != '' and not line.startswith('#'):
45+
asn_list.append(line)
46+
47+
return asn_list
48+
49+
3750
def _process_source(self):
38-
# Individual Company ASNs
39-
# -- @curi0usJack and @violentlydave
40-
# :Format: CompanyName_AS12345
41-
asn_list = asns.asns
51+
try:
52+
# Get the source data
53+
asn_list = self._get_source()
54+
except:
55+
return self.ip_list
56+
4257
asn_list = [x.upper() for x in asn_list]
4358

4459
for asn in asn_list:
@@ -109,15 +124,19 @@ def __init__(self, workingfile, headers, timeout, ip_list, args):
109124
self.return_data = self._process_source()
110125

111126

112-
def _get_source(self, asn):
113-
# Write comments to working file
114-
print("[*]\tPulling %s -- %s via BGPView..." % (asn[1], asn[0]))
115-
self.workingfile.write("\n\n\t# Live copy of %s ips based on BGPView ASN %s: %s\n" % (
116-
asn[0],
117-
asn[1],
118-
datetime.now().strftime("%Y%m%d-%H:%M:%S")
119-
))
127+
def _get_source(self):
128+
# Read in static source file from static/ dir
129+
asn_list = []
130+
with open('../static/asns.txt', 'r') as _file:
131+
for line in _file.readlines():
132+
line = line.strip()
133+
if line != '' and not line.startswith('#'):
134+
asn_list.append(line)
135+
136+
return asn_list
137+
120138

139+
def _get_data(self, asn):
121140
asn_data = requests.get(
122141
'https://api.bgpview.io/asn/%s/prefixes' % asn[1],
123142
headers=self.headers,
@@ -130,10 +149,12 @@ def _get_source(self, asn):
130149

131150

132151
def _process_source(self):
133-
# Individual Company ASNs
134-
# -- @curi0usJack and @violentlydave
135-
# :Format: CompanyName_AS12345
136-
asn_list = asns.asns
152+
try:
153+
# Get the source data
154+
asn_list = self._get_source()
155+
except:
156+
return self.ip_list
157+
137158
asn_list = [x.upper() for x in asn_list]
138159

139160
for asn in asn_list:
@@ -144,10 +165,18 @@ def _process_source(self):
144165

145166
try:
146167
# Get the source data
147-
asn_data = self._get_source(asn)
168+
asn_data = self._get_data(asn)
148169
except:
149170
continue
150171

172+
# Write comments to working file
173+
print("[*]\tPulling %s -- %s via BGPView..." % (asn[1], asn[0]))
174+
self.workingfile.write("\n\n\t# Live copy of %s ips based on BGPView ASN %s: %s\n" % (
175+
asn[0],
176+
asn[1],
177+
datetime.now().strftime("%Y%m%d-%H:%M:%S")
178+
))
179+
151180
try:
152181
count = 0
153182
for network in asn_data['data']['ipv4_prefixes']:

0 commit comments

Comments
 (0)