halborn_ctf.network.filters package
Submodules
halborn_ctf.network.filters.json_rpc module
- halborn_ctf.network.filters.json_rpc.filter_methods(methods=[])[source]
Proxy filter that allows filtering JSON RPC method
Each request will be checked for a valid method and if the method is on the filter list the following data will be send:
{ "jsonrpc": "2.0", "id": json_dump['id'], "error": { "code":-32601, "message":"Method not allowed" } }
Example
The
methodsparameter does support regex on each of the elements:# Disable all methods starting with `anvil_` and `evm_` filter_methods(["anvil_.*", "evm_.*"])
- Parameters:
methods (list, optional) – A list of methods to filter. Each element of the list does support regex expressions to match multiple patterns. Example:
["evm_.*"]. Defaults to [].
- halborn_ctf.network.filters.json_rpc.whitelist_methods(methods=[])[source]
Proxy filter that allows whitelisting JSON RPC methods
Each request will be checked for a valid method and if the method is not whitelisted the following data will be send:
{ "jsonrpc": "2.0", "id": json_dump['id'], "error": { "code":-32601, "message":"Method not allowed" } }
Example
The
methodsparameter does support regex on each of the elements:# Allowing all methods starting with `eth_` and `net_` whitelist_methods(["eth_.*", "net_.*"])
- Parameters:
methods (list, optional) – A list of methods to whitelist. Each element of the list does support regex expressions to match multiple patterns. Example:
["eth_.*"]. Defaults to [].
Module contents
Filters module provide an easy way to restrict functionality to an exposed server on the challenge box.
All filters are using mitmdump from mitmproxy underneath to execute an script to filter the traffic to a given port.
To do the filtering, the command should expose a different port were the standard requests will flow in. None-filtered responses will
be forwarded to the specified upstream server on each of the filters.
Example
We can run anvil on the background and have a network filter for specific JSON-RPC methods:
...
# Have port 8545 be exposed on the root of the challenge
PATH_MAPPING = {
'/': {
'port': 8545,
'path': '/',
'methods': ['POST'],
'filter': network.filters.json_rpc.whitelist_methods(['net_*', 'eth_*'...])
},
}
...
shell.run('anvil -p 8545')
It is possible to also define your own filters (which can later be exposed to the player for reference) by either referencing the current implementations or the official documentation (https://2qwesgdhjuiytyrjhtgdbf.readthedocs.io/en/latest/scripting/inlinescripts.html) and examples (https://docs.mitmproxy.org/stable/addons-examples/).
Example
Once the script is created it can be used using generic_filter:
generic_filter('./filter.py', my_args=[], extra_custom='more')
...
# The script can access the extra **kwargs using ``ctx.options.[varname]`` and JSON decoding it
custom_data = json.loads(ctx.options.[varname])
Tip
You can use the current challenge.py as the container for the filter without having to create a separated file by using python built-in __file__:
generic_filter(__file__, my_args=[], extra_custom='more')
- halborn_ctf.network.filters.generic_filter(filter_file, **kwargs)[source]
Allows running an arbitrary
mitmdumpscript as a background shell process.The
mitmdumpwill be used inupstreammode. Any extra arguments provided to thegeneric_filterwill be transfered to thescriptusing the--setcommand flag on the command line of mitmdump as JSON encoded string which can be accessed on the script usingmitmproxy.ctx.options.[varname].https://docs.mitmproxy.org/stable/addons-examples/
Example
Once the script is created it can be used on using
generic_filter:generic_filter('./filter.py', my_args=[], extra_custom='more') ... # The script can access the extra **kwargs using ``ctx.options.[varname]`` and JSON decoding it custom_data = json.loads(ctx.options.[varname])
- Parameters:
script (str) – Path of the script to execute
**kwargs – Any extra arguments that the filter script needs