Skip to content

Commit 3dbf49d

Browse files
Merge pull request #5 from stavinski/master
Adding State Functionality
2 parents d5c83c3 + d48d352 commit 3dbf49d

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

BappDescription.html

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
<p>This exension allows execution of custom Python scripts to be used with HTTP request and responses plus handling Macro messages.</p>
1+
<p>This extension allows execution of custom Python scripts to be used with HTTP request and responses plus support for handling Macro messages.</p>
22
<p>To use, type or paste a Python script into the &quot;Python Scripts&quot; tab, and use Burp in
3-
the normal way. The script will be executed for each HTTP request and response.
4-
The following variables are defined in the context of the script:</p>
3+
the normal way. The script will be executed for each HTTP request and response.</p>
4+
<p>Items placed into the 'state' dictionary will be persisted across request/responses.</p>
5+
<p>The following variables are defined in the context of the script:</p>
56
<ul>
6-
<li>extender</li>
7-
<li>callbacks</li>
8-
<li>helpers</li>
9-
<li>toolFlag</li>
10-
<li>messageIsRequest</li>
11-
<li>messageInfo</li>
12-
<li>macroItems</li>
7+
<li>extender: IBurpExtender</li>
8+
<li>callbacks: IBurpExtenderCallbacks</li>
9+
<li>helpers: IExtensionHelpers</li>
10+
<li>toolFlag: int</li>
11+
<li>messageIsRequest: bool</li>
12+
<li>messageInfo: IHttpRequestResponse</li>
13+
<li>macroItems: IHttpRequestResponse[]</li>
14+
<li>state: dict</li>
1315
</ul>

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ If there was a runtime exception these will also be captured in the `Errors` tex
3333

3434
__Scripts are automatically restored and saved on extension load and unload.__
3535

36+
## State Dictionary
37+
38+
There are scenarios when values retrieved from previous requests or responses are required for the next request, this can sometimes be performed using Macros however if this behaviour is used for every endpoint this becomes unworkable as a solution. That is why the `state` dictionary was introduced so that it is possible to persist values that can be later retrieved in a subsequent request/response.
39+
40+
An example of this could be an application that returns a unique token in the response and this must be provided into the next request, this could be handled using the following:
41+
42+
~~~python
43+
if messageIsRequest:
44+
if 'x-req' in state: # see if we have a token from prev resp
45+
req = helpers.analyzeRequest(messageInfo)
46+
headers = req.headers
47+
headers.add('X-Request: {}'.format(state['x-req'])) # put the token into the req
48+
body = messageInfo.request[req.bodyOffset:]
49+
new_req = helpers.buildHttpMessage(headers, body)
50+
messageInfo.request = new_req # replace the current req with the updated version
51+
print('Added X-Request header: {}'.format(state['x-req']))
52+
else:
53+
token = ... # perform retrieval of token (e.g. hidden form field, meta tag etc...); Macro regex generator could help with this ;)
54+
state['x-req'] = token # save in state to be used in next req
55+
print('Saved X-Request from response: {}'.format(token))
56+
~~~
3657

3758
## FAQs
3859

models.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import traceback
77

8-
DEFAULT_SCRIPT = '''# Recommended to use the pyscripterer base script found here https://github.com/lanmaster53/pyscripter-er
8+
DEFAULT_SCRIPT = '''# Recommended to use the pyscripter-er base script found here https://github.com/lanmaster53/pyscripter-er
99
# to be placed into the python environment directory
1010
1111
# from pyscripterer import BaseScript as Script
@@ -123,6 +123,7 @@ def __init__(self, extender, callbacks, helpers, title, enabled=False, content=D
123123
self.content = content
124124
self.stderr = sys.stderr
125125
self.stdout = sys.stdout
126+
self.state = dict()
126127
self._code = None
127128
self._compiled_content = content
128129
self._compilation_error = ''
@@ -152,8 +153,9 @@ def processHttpMessage(self, toolFlag, messageIsRequest, messageInfo, macroItems
152153
'toolFlag': toolFlag,
153154
'messageIsRequest': messageIsRequest,
154155
'messageInfo': messageInfo,
155-
'macroItems': macroItems
156-
}
156+
'macroItems': macroItems,
157+
'state': self.state
158+
}
157159

158160
oldstderr = sys.stderr
159161
oldstdout = sys.stdout
@@ -197,4 +199,4 @@ def from_dict(cls, val, callbacks, helpers, extender):
197199
class Properties:
198200

199201
COMPILATION_ERROR = 'compilation_error'
200-
IS_COMPILED = 'is_compiled'
202+
IS_COMPILED = 'is_compiled'

0 commit comments

Comments
 (0)