-
Notifications
You must be signed in to change notification settings - Fork 0
/
csrf_inline.py
76 lines (57 loc) · 2.77 KB
/
csrf_inline.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
from burp import IBurpExtender
from burp import IHttpListener
from burp import IParameter
from java.net import URL
import re
import urlparse
import ssl
# Find reg ex: <meta name="csrf-token" content=".*?" />
# Replace reg ex: authenticity_token= POST param
csrfregex = re.compile(r'<input name="__RequestVerificationToken" type="hidden" value="(.*?)" />')
class BurpExtender(IBurpExtender, IHttpListener, IParameter):
# Variable to hold the token found so that it can be inserted in the next request
discoveredToken = ''
def registerExtenderCallbacks(self, callbacks):
self._callbacks = callbacks
self._helpers = callbacks.getHelpers()
callbacks.setExtensionName("RobB CSRF Token Inline")
callbacks.registerHttpListener(self)
print "Extension registered successfully."
return
def processHttpMessage(self, toolFlag, messageIsRequest, currentMessage):
if toolFlag != self._callbacks.TOOL_EXTENDER:
if messageIsRequest:
self.processRequest(toolFlag, currentMessage)
else:
self.getToken(currentMessage)
def getToken(self, currentMessage):
print "Gonna get response from body"
responseBody = currentMessage.getResponse()
print len(responseBody)
token = csrfregex.search(responseBody)
if token is None:
print "No token found in response."
else:
BurpExtender.discoveredToken = token.group(1)
print "Found a token. Fricken sweet: " + BurpExtender.discoveredToken
def processRequest(self, toolFlag, currentMessage):
request = currentMessage.getRequest()
parsedRequest = self._helpers.analyzeRequest(request)
if parsedRequest.getMethod() != 'POST':
return
if BurpExtender.discoveredToken != '' and toolFlag != self._callbacks.TOOL_PROXY:
headers = parsedRequest.getHeaders()
header_name = "__RequestVerificationToken:"
if any(header.startswith(header_name) for header in headers):
print "Header found"
new_headers = []
for i in range(0, len(headers)):
if headers[i].startswith(header_name):
print "Found header to replace"
new_headers.append(header_name + " " + BurpExtender.discoveredToken)
else:
new_headers.append(headers[i])
currentMessage.request = self._helpers.buildHttpMessage(new_headers, request[parsedRequest.getBodyOffset():])
print "Replaced the token."
else:
print "No token to replace."