|
| 1 | +# RSA Signing Script for Zed Attack Proxy - ZAP |
| 2 | +# HelpAddOn Script - HTTPSender |
| 3 | +# Michal Walkowski - https://mwalkowski.github.io/ |
| 4 | +# https://github.com/mwalkowski |
| 5 | +# |
| 6 | +# Tested with Jython 14 beta and ZAP 2.14.0 |
| 7 | +# For RSA Signing Process: https://httpwg.org/http-extensions/draft-ietf-httpbis-message-signatures.html#name-rsassa-pkcs1-v1_5-using-sha |
| 8 | +# Based On: https://mwalkowski.github.io/post/using-burp-python-scripts-to-sign-requests-with-rsa-keys/ |
| 9 | + |
| 10 | +import urlparse |
| 11 | +import uuid |
| 12 | +import datetime |
| 13 | +import base64 |
| 14 | +import subprocess |
| 15 | + |
| 16 | +# path to private.key |
| 17 | +PRIVATE_KEY = "private.key" |
| 18 | +SIGNATURE_HEADER = 'X-Signature' |
| 19 | +NONCE_HEADER = 'X-Nonce-Value' |
| 20 | +NONCE_CREATED_AT_HEADER = 'X-Nonce-Created-At' |
| 21 | + |
| 22 | + |
| 23 | +def sign(signature_input): |
| 24 | + print('signature_input', signature_input) |
| 25 | + signature_input_b64 = base64.standard_b64encode(signature_input.encode()).decode() |
| 26 | + print('signature_input_b64', signature_input_b64) |
| 27 | + |
| 28 | + cmd = """printf %s "{}" | openssl dgst -sha256 -sign {}| openssl base64""".format(signature_input_b64, PRIVATE_KEY) |
| 29 | + print(cmd) |
| 30 | + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 31 | + |
| 32 | + output, err = process.communicate() |
| 33 | + if err.decode() != "": |
| 34 | + raise Exception(err) |
| 35 | + |
| 36 | + return output.decode().replace("\n", "") |
| 37 | + |
| 38 | +def sendingRequest(msg, initiator, helper): |
| 39 | + method = msg.getRequestHeader().getMethod() |
| 40 | + path = urlparse.urlparse(msg.getRequestHeader().getURI().toString()).path |
| 41 | + body = msg.getRequestBody().toString() |
| 42 | + print(msg.getRequestBody().toString()) |
| 43 | + |
| 44 | + nonce_value = str(uuid.uuid4()) |
| 45 | + nonce_created_at = '{}+00:00'.format(datetime.datetime.utcnow().isoformat()) |
| 46 | + signature = sign("{}{}{}{}{}".format(method, path, nonce_value, nonce_created_at, body)) |
| 47 | + |
| 48 | + print('Adding new {}: {}'.format(SIGNATURE_HEADER, signature)) |
| 49 | + msg.getRequestHeader().setHeader(SIGNATURE_HEADER, signature) |
| 50 | + |
| 51 | + print('Adding new {}: {}'.format(NONCE_HEADER, nonce_value)) |
| 52 | + msg.getRequestHeader().setHeader(NONCE_HEADER, nonce_value) |
| 53 | + |
| 54 | + print('Adding new {}: {}'.format(NONCE_CREATED_AT_HEADER, nonce_created_at)) |
| 55 | + msg.getRequestHeader().setHeader(NONCE_CREATED_AT_HEADER, nonce_created_at) |
| 56 | + |
| 57 | + |
| 58 | +def responseReceived(msg, initiator, helper): |
| 59 | + pass |
0 commit comments