python3-10.pyscalpel.burp_utils
1from typing import TypeVar, cast 2from functools import singledispatch 3from collections.abc import Iterable 4 5import pyscalpel._globals 6from pyscalpel.java.burp.http_request import IHttpRequest, HttpRequest 7from pyscalpel.java.burp.http_response import IHttpResponse, HttpResponse 8from pyscalpel.java.burp.byte_array import IByteArray, ByteArray 9from pyscalpel.java.burp.http_parameter import IHttpParameter, HttpParameter 10from pyscalpel.java.bytes import JavaBytes 11from pyscalpel.java.scalpel_types.utils import PythonUtils 12from pyscalpel.encoding import always_bytes, urldecode, urlencode_all 13 14 15ctx = pyscalpel._globals.ctx 16 17 18HttpRequestOrResponse = TypeVar("HttpRequestOrResponse", IHttpRequest, IHttpResponse) 19 20ByteArraySerialisable = TypeVar("ByteArraySerialisable", IHttpRequest, IHttpResponse) 21 22ByteArrayConvertible = TypeVar( 23 "ByteArrayConvertible", bytes, JavaBytes, list[int], str, bytearray 24) 25 26 27@singledispatch 28def new_response(obj: ByteArrayConvertible) -> IHttpResponse: # pragma: no cover 29 """Create a new HttpResponse from the given bytes""" 30 return HttpResponse.httpResponse(byte_array(obj)) 31 32 33@new_response.register 34def _new_response(obj: IByteArray) -> IHttpResponse: # pragma: no cover 35 return HttpResponse.httpResponse(obj) 36 37 38@singledispatch 39def new_request(obj: ByteArrayConvertible) -> IHttpRequest: # pragma: no cover 40 """Create a new HttpRequest from the given bytes""" 41 return HttpRequest.httpRequest(byte_array(obj)) 42 43 44@new_request.register 45def _new_request(obj: IByteArray) -> IHttpRequest: # pragma: no cover 46 return HttpRequest.httpRequest(obj) 47 48 49@singledispatch 50def byte_array( 51 _bytes: bytes | JavaBytes | list[int] | bytearray, 52) -> IByteArray: # pragma: no cover 53 """Create a new :class:`IByteArray` from the given bytes-like obbject""" 54 # Handle buggy bytes casting 55 # This is needed because Python will _sometimes_ try 56 # to interpret bytes as a an integer when passing to ByteArray.byteArray() and crash like this: 57 # 58 # TypeError: Error converting parameter 1: 'bytes' object cannot be interpreted as an integer 59 # 60 # Restarting Burp fixes the issue when it happens, so to avoid unstable behaviour 61 # we explcitely convert the bytes to a PyJArray of Java byte 62 cast_value = cast(JavaBytes, PythonUtils.toJavaBytes(bytes(_bytes))) 63 return ByteArray.byteArray(cast_value) 64 65 66@byte_array.register 67def _byte_array_str(string: str) -> IByteArray: # pragma: no cover 68 return ByteArray.byteArray(string) 69 70 71def get_bytes(array: IByteArray) -> bytes: # pragma: no cover 72 return to_bytes(array.getBytes()) 73 74 75def to_bytes(obj: ByteArraySerialisable | JavaBytes) -> bytes: # pragma: no cover 76 # Handle java signed bytes 77 if isinstance(obj, Iterable): 78 # Convert java signed bytes to python unsigned bytes 79 return bytes([b & 0xFF for b in cast(JavaBytes, obj)]) 80 81 return get_bytes(cast(ByteArraySerialisable, obj).toByteArray())
ctx =
{}
@singledispatch
def
new_response( obj: ~ByteArrayConvertible) -> pyscalpel.java.burp.http_response.IHttpResponse:
28@singledispatch 29def new_response(obj: ByteArrayConvertible) -> IHttpResponse: # pragma: no cover 30 """Create a new HttpResponse from the given bytes""" 31 return HttpResponse.httpResponse(byte_array(obj))
Create a new HttpResponse from the given bytes
@singledispatch
def
new_request( obj: ~ByteArrayConvertible) -> pyscalpel.java.burp.http_request.IHttpRequest:
39@singledispatch 40def new_request(obj: ByteArrayConvertible) -> IHttpRequest: # pragma: no cover 41 """Create a new HttpRequest from the given bytes""" 42 return HttpRequest.httpRequest(byte_array(obj))
Create a new HttpRequest from the given bytes
@singledispatch
def
byte_array( _bytes: bytes | pyscalpel.java.bytes.JavaBytes | list[int] | bytearray) -> pyscalpel.java.burp.byte_array.IByteArray:
50@singledispatch 51def byte_array( 52 _bytes: bytes | JavaBytes | list[int] | bytearray, 53) -> IByteArray: # pragma: no cover 54 """Create a new :class:`IByteArray` from the given bytes-like obbject""" 55 # Handle buggy bytes casting 56 # This is needed because Python will _sometimes_ try 57 # to interpret bytes as a an integer when passing to ByteArray.byteArray() and crash like this: 58 # 59 # TypeError: Error converting parameter 1: 'bytes' object cannot be interpreted as an integer 60 # 61 # Restarting Burp fixes the issue when it happens, so to avoid unstable behaviour 62 # we explcitely convert the bytes to a PyJArray of Java byte 63 cast_value = cast(JavaBytes, PythonUtils.toJavaBytes(bytes(_bytes))) 64 return ByteArray.byteArray(cast_value)
Create a new IByteArray
from the given bytes-like obbject
def
get_bytes(array: pyscalpel.java.burp.byte_array.IByteArray) -> bytes:
def
to_bytes( obj: Union[~ByteArraySerialisable, pyscalpel.java.bytes.JavaBytes]) -> bytes:
76def to_bytes(obj: ByteArraySerialisable | JavaBytes) -> bytes: # pragma: no cover 77 # Handle java signed bytes 78 if isinstance(obj, Iterable): 79 # Convert java signed bytes to python unsigned bytes 80 return bytes([b & 0xFF for b in cast(JavaBytes, obj)]) 81 82 return get_bytes(cast(ByteArraySerialisable, obj).toByteArray())