-
Notifications
You must be signed in to change notification settings - Fork 2
/
cookies.py
274 lines (249 loc) · 10.6 KB
/
cookies.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
from bs4 import BeautifulSoup
from optparse import OptionParser, OptionGroup
from urllib.parse import urlparse
import streamlit as st
import sys
import xml.etree.ElementTree as ET
import requests
import json
import datetime
import time
import re
user_agent = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0' }
requests.packages.urllib3.disable_warnings()
white = "\033[0m"
blue = "\033[94m"
green = "\033[0;32m"
red = "\033[91m"
def headersURL(line, info, nocolor, formatoutput, delay, timeout):
""" page load and print data"""
url = line.strip()
if (urlparse(url).scheme == ''):
url = 'http://%s'%url
try:
time.sleep(delay)
r = requests.get(url, verify=False, allow_redirects=False, timeout=timeout)
if (r.status_code == 302) and (len(r.cookies) == 0):
r = requests.get(url, verify=False, allow_redirects=True, timeout=timeout)
if (formatoutput == "normal"):
printJson(line, r.cookies, info)
elif (formatoutput == "xml"):
printXML(line, r.cookies, info)
elif (formatoutput == "csv"):
if 1:
st.write("url,cookie name,secure,httponly,value,path,expires")
else:
st.write("url,cookie name,secure,httponly")
printCsv(line, r.cookies, info)
elif (formatoutput == "grepable"):
printGrepable(line, r.cookies, info)
except:
if (formatoutput == "normal"):
st.write("[ERR] %s - Connection failed." % url)
else:
pass
def readFile(filename, info, nocolor, formatoutput, delay, timeout):
""" Read file with the url list """
try:
with open(filename, "r") as f:
for line in f:
headersURL(line, info, nocolor, formatoutput, delay, timeout)
except FileNotFoundError:
st.write("[ERR] File not found.")
def printNormal(line, cookies, nocolor, info):
if nocolor:
color_blue = white
color_red = white
color_green = white
else:
color_blue = blue
color_red = red
color_green = green
st.write("%s[*] URL: %s%s"%(color_blue,line.strip(),white))
for cookie in cookies:
name = cookie.name
secure = cookie.secure
httponly = cookie.has_nonstandard_attr("HttpOnly")
if not httponly:
httponlyResult = '%sHttpOnly: %s%s' % (color_red, str(httponly), white)
else:
httponlyResult = '%sHttpOnly: %s%s' % (color_green, str(httponly), white)
if not secure:
secureResult = '%ssecure: %s%s' % (color_red, str(secure), white)
else:
secureResult = '%sSecure: %s' % (color_green, str(secure))
st.write("%s[*] Name: %s\n\t%s\n\t%s%s%s" % (white, name, secureResult, white, httponlyResult, white))
if info:
if cookie.expires is not None:
expires = datetime.datetime.fromtimestamp(cookie.expires).strftime('%Y-%m-%d %H:%M:%S')
else:
expires = "Never"
st.write("\tValue: %s\n\tPath: %s\n\tExpire: %s" % (cookie.value, cookie.path, expires))
def printGrepable(line, cookies, info):
for cookie in cookies:
name = cookie.name
secure = cookie.secure
httponly = cookie.has_nonstandard_attr("HttpOnly")
if not httponly:
httponlyResult = "NO"
else:
httponlyResult = "YES"
if not secure:
secureResult = "NO"
else:
secureResult = "YES"
if info:
if cookie.expires is not None:
expires = datetime.datetime.fromtimestamp(cookie.expires).strftime('%Y-%m-%d %H:%M:%S')
else:
expires = "Never"
st.write("URL: %s: Cookie: %s : Secure: %s : Httponly: %s : value: %s : path: %s : expires: %s" % (line.strip(), name, secureResult, httponlyResult, cookie.value, cookie.path, expires))
else:
st.write("URL: %s: Cookie: %s : Secure: %s : Httponly: %s" % (line.strip(), name, secureResult, httponlyResult))
def indent(elem, level=0):
""" XML pretty print"""
i = "\n" + level * " "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level + 1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
def printXML(line, cookies, info):
allxml = ET.Element('url', {'site': line.strip()})
for cookie in cookies:
child = ET.SubElement(allxml, 'cookie')
secure = cookie.secure
httponly = cookie.has_nonstandard_attr("HttpOnly")
if not httponly:
httponlyResult = "NO"
else:
httponlyResult = "YES"
if not secure:
secureResult = "NO"
else:
secureResult = "YES"
ET.SubElement(child, 'name').text = cookie.name
ET.SubElement(child, 'secure').text = secureResult
ET.SubElement(child, 'httponly').text = httponlyResult
if info:
if cookie.expires is not None:
expires = datetime.datetime.fromtimestamp(cookie.expires).strftime('%Y-%m-%d %H:%M:%S')
else:
expires = "Never"
ET.SubElement(child, 'value').text = cookie.value
ET.SubElement(child, 'path').text = cookie.path
ET.SubElement(child, 'expires').text = expires
indent(allxml)
ET.dump(allxml)
def printJson(line, cookies, info):
cookies_output = []
for cookie in cookies:
secure = cookie.secure
httponly = cookie.has_nonstandard_attr("HttpOnly")
if not httponly:
httponlyResult = "NO"
else:
httponlyResult = "YES"
if not secure:
secureResult = "NO"
else:
secureResult = "YES"
data = {
'name': cookie.name,
'secure': secureResult,
'httponly': httponlyResult
}
cookies_output.append(data)
if info:
if cookie.expires is not None:
expires = datetime.datetime.fromtimestamp(cookie.expires).strftime('%Y-%m-%d %H:%M:%S')
else:
expires = "Never"
data['value'] = cookie.value
data['path'] = cookie.path
data['expire'] = expires
json_output = {
'url': line.strip(),
'cookies': cookies_output
}
st.json(json.dumps(json_output, indent=4, separators=(',', ': ')))
def printCsv(line, cookies, info):
for cookie in cookies:
name = cookie.name
secure = cookie.secure
httponly = cookie.has_nonstandard_attr("HttpOnly")
if not httponly:
httponlyResult = "NO"
else:
httponlyResult = "YES"
if not secure:
secureResult = "NO"
else:
secureResult = "YES"
# If info option entered, print all
if info:
if cookie.expires is not None:
expires = datetime.datetime.fromtimestamp(cookie.expires).strftime('%Y-%m-%d %H:%M:%S')
else:
expires = "Never"
st.write("%s,\"%s\",%s,%s,\"%s\",%s,%s" % (line.strip(), name, secureResult, httponlyResult, cookie.value, cookie.path, expires))
else:
st.write("%s,\"%s\",%s,%s" % (line.strip(), name, secureResult, httponlyResult))
def googleSearch(domain, info, nocolor, formatoutput, delay, timeout):
""" Google search, find subdomains and load pages"""
g_url = "http://www.google.com/search?hl=es&q=site:%s&btnG=Google+Search" % domain
r = requests.get(g_url, verify=False, headers=user_agent)
soup = BeautifulSoup(r.text, "html.parser")
domains = []
g_pages = []
top = soup.find('tr', attrs={'valign': 'top'})
for page in top.find_all('a', attrs={'class': 'fl'}):
g_pages.append('https://www.google.com%s'% page.get('href'))
for site in soup.find_all('cite'):
if re.match('^([a-z0-9]*)(.?)%s$' % domain, urlparse('//%s'% site.text).netloc) is not None:
domains.append(urlparse('//%s' % site.text).netloc)
for sites in g_pages:
r = requests.get(sites, verify=False, headers=user_agent)
soup2 = BeautifulSoup(r.text, "html.parser")
for site in soup2.find_all('cite'):
if re.match('^([a-z0-9]*)(.?)%s$' % domain, urlparse('//%s'% site.text).netloc) is not None:
domains.append(urlparse('//%s' % site.text).netloc)
for url in set(domains):
headersURL(url, info, nocolor, formatoutput, delay, timeout)
def opciones():
st.write('----------------------Cookie Details------------------------')
parser = OptionParser("usage: %prog [options] \nExample: ./%prog -i ips.txt")
parser.add_option("-i", "--input",
action="store", type="string", dest="input", help="File input with the list of webservers")
parser.add_option("-l", "--link",
action="store", type="string", dest="url", help="URL")
parser.add_option("-f", "--format",
action="store", type="string", dest="format", default="normal", help="Output format (json, xml, csv, normal, grepable)")
parser.add_option("-g", "--google",
action="store", dest="google", help="Search in google by domain")
parser.add_option("--nocolor",
action="store_true", dest="nocolor", default=False, help="Disable color output")
parser.add_option("-n", "--info",
action="store_true", dest="info", default=False, help="Information")
group = OptionGroup(parser, "Performance")
group.add_option("-t", type="float", dest="timeout", default=1.0, help="Response Timeout")
group.add_option("-d", type="float", dest="delay", default=0.0, help="Delay between requests")
parser.add_option_group(group)
(options, args) = parser.parse_args()
if (len(sys.argv) == 1):
parser.print_help()
elif (options.input is not None):
readFile(options.input, options.info, options.nocolor, options.format, options.delay, options.timeout)
elif (options.url is not None):
headersURL(options.url, options.info, options.nocolor, options.format, options.delay, options.timeout)
elif (options.google is not None):
googleSearch(options.google, options.info, options.nocolor, options.format, options.delay, options.timeout)
if __name__ == "__main__":
opciones()