-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenos.py
204 lines (167 loc) · 6.31 KB
/
genos.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
#!/usr/bin/python3
'''
Author : Saeed
github : github.com/saeed0x1
'''
import requests
from jsbeautifier import beautify
import sys,re,os
import urllib3
import argparse
from urllib.parse import urlparse
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
example_commands ='''
Example commands:
python genos.py --url https://example.com/script.js -o w output.txt
- Generates a wordlist from a single URL and saves it in 'output.txt' using write mode.
python genos.py --file script.js
- Generates a wordlist from a single JS file and prints it in the terminal.
python genos.py --url https://example.com/main.css --nojs
- Generates a wordlist from a non-js URL
$ echo "http://example.com/main.js" | python genos.py --stdin
- Taking standard input
$ cat urls.txt | python genos.py --stdin
- The output of cat command will be the input of genos
'''
parser = argparse.ArgumentParser(
prog='Genos',
description='Generate wordlist from Files and URLs',
epilog=example_commands,formatter_class=argparse.RawDescriptionHelpFormatter)
def separated_list(arg_value):
try:
values = [item for item in arg_value.split(",")]
return values
except ValueError:
raise argparse.ArgumentTypeError("Invalid list format. Please provide comma-separated file names.")
parser.add_argument('--url',help="Give a single url with .js endpoint",required=False,type=str)
parser.add_argument('--list',help="Give a list of URLs (urls.txt)",required=False,type=str)
parser.add_argument('--file',help="Give one or more files (comma separated)",required=False,type=separated_list)
parser.add_argument('-o',choices=['w','a'],help="Output mode: 'w' (write) or 'a' (append)",required=False,type=str)
parser.add_argument('output_filename', nargs="?",help="Name of the output file")
parser.add_argument("--nojs", action="store_true", help="Mention if the provided URLs are non-js URLs")
parser.add_argument("--no-num", dest="exnum", action="store_true", help="Set to exclude numbers from the wordlist")
parser.add_argument("--min-len", dest="minlen", help="Set the minimum length of a word",required=False,type=int)
parser.add_argument("--stdin",dest="stdin",action="store_true",help="Take standard input",required=False)
args = parser.parse_args()
# check if the given url is valid
def is_valid_url(url):
try:
result = urlparse(url)
return all([result.scheme, result.netloc])
except ValueError:
return False
# check if the url is valid + ends with js
def is_valid_js_url(url):
try:
result = urlparse(url)
if all([result.scheme, result.netloc, result.path.endswith(".js")]):
return True
else:
return False
except ValueError:
return False
def main(js_file):
if is_valid_js_url(js_file):
send_req(js_file)
else:
print('Bad URL: {}, please check your URL !!\nProvide "--nojs" argument if it is non js URL'.format(js_file))
sys.exit(0)
# sending request to the server
def send_req(file):
try:
req = requests.get(file, verify=False, timeout=5)
req.raise_for_status()
content = req.text
words = get_words(content)
print("\n".join(words))
if args.o:
data = "\n".join(words)
filename = args.output_filename
outputfile(data,filename, args.o)
except requests.exceptions.RequestException as err:
sys.exit(print(err))
except Exception as err:
sys.exit(print(err))
# handling single/multiple files
def single_files(f):
with open(f, 'r') as openedfile:
content = openedfile.read()
words = get_words(content)
print("\n".join(words))
if args.o:
data = "\n".join(words)
filename = args.output_filename
outputfile(data,filename, args.o)
# extracting words
def get_words(content: str):
word_list = set()
content = beautify(content)
words = re.findall(r'\b\w+\b', content)
if args.exnum:
result = re.sub(r'\b\d+\b', '', " ".join(words))
# if minimum length is set
if args.minlen:
_result = length_check(result.split())
word_list.update(_result)
return word_list
word_list.update(result.split())
return word_list
if args.minlen:
print(words)
_result = length_check(words)
word_list.update(_result)
return word_list
word_list.update(words)
return word_list
def length_check(wordlist):
_wordlist = set(wordlist)
for word in _wordlist.copy():
if len(word) < args.minlen:
_wordlist.remove(word)
return _wordlist
# file output
def outputfile(word,filename,mode):
with open(filename,mode) as f:
if os.path.exists(filename) and mode=="a":
f.write("\n"+word)
else:
f.write(word)
# using the arguments
if args.file:
for f in args.file:
if os.path.exists(f):
single_files(f)
else:
print("'{}' file doesn't exist".format(f))
elif args.url:
if args.nojs:
if is_valid_url(args.url):
send_req(args.url)
else:
print('Bad URL: {}, please check your URL'.format(args.url))
else:
main(args.url)
elif args.list:
if os.path.exists(args.list):
with open(args.list, 'r') as urllist:
lines = urllist.readlines()
for line in lines:
line = line.strip()
if args.nojs:
if is_valid_url(line):
send_req(line)
else:
print('Bad URL: {}, please check your URL'.format(line))
else:
main(line)
else:
print("'{}' file doesn't exist".format(args.list))
elif args.stdin:
for line in sys.stdin.readlines():
if args.nojs:
if is_valid_url(line.strip()):
send_req(line.strip())
else:
main(line.strip())
else:
print(parser.format_help())