Skip to content

Instantly share code, notes, and snippets.

@rbdixon
Last active May 11, 2020 12:24
Show Gist options
  • Save rbdixon/5218a51691ee20452185d5d020845a4c to your computer and use it in GitHub Desktop.
Save rbdixon/5218a51691ee20452185d5d020845a4c to your computer and use it in GitHub Desktop.
mitmproxy script to translate WBXML

Why did I do this? See post.

#!/usr/bin/env python
# Usage: mitmdump -s wbxml.py
import sys
import traceback
from mitmproxy import ctx
import wbxml
class WBXMLTranslator:
def is_translated(self, headers):
return 'WBXMLTRANSLATE' in map(lambda x: x.upper(), headers.keys())
def is_wbxml(self, headers):
return 'application/vnd.syncml.dm+wbxml' in headers.get('Content-Type', [])
def has_headers(self, headers):
return headers is not None
def request(self, flow):
try:
if self.has_headers(flow.request.headers):
if self.is_wbxml(flow.request.headers):
if not self.is_translated(flow.request.headers):
# Request from device
ctx.log.info(
'Translating device request to server: WBXML to XML')
flow.request.headers[
'WBXMLTranslate'] = 'device request'
flow.request.content = wbxml.wbxml_to_xml(
flow.request.content)
else:
# Request going to server
ctx.log.info(
'Translating proxy request to server: XML to WBXML')
flow.request.headers.pop('WBXMLTranslate')
flow.request.content = wbxml.xml_to_wbxml(
flow.request.content)
except Exception:
ctx.log.error('Error translating request: {url}'.format(
url=flow.request.pretty_url
))
ctx.log.error(traceback.format_exc())
def response(self, flow):
try:
if self.has_headers(flow.response.headers):
if self.is_wbxml(flow.request.headers):
if not self.is_translated(flow.request.headers):
# Response from server
ctx.log.info(
'Translating server response arriving to proxy: WBXML to XML')
flow.response.headers[
'WBXMLTranslate'] = 'Server response'
flow.response.content = wbxml.wbxml_to_xml(
flow.response.content)
else:
# Response going to device
ctx.log.info(
'Translating server response departing for device: XML to WBXML')
flow.response.headers.pop('WBXMLTranslate')
flow.response.content = wbxml.xml_to_wbxml(
flow.response.content)
except Exception:
ctx.log.error('Error translating response to: {url}'.format(
url=flow.request.pretty_url
))
ctx.log.error(traceback.format_exc())
def start():
ctx.log.info('Staring WBXMLTranslator')
return WBXMLTranslator()
def done():
ctx.log.info("Shutting WBXMLTranslator script down.")
if __name__ == "__main__":
print('This script should be executed as a mitmproxy script')
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment