halborn_ctf.network.filters package

Submodules

halborn_ctf.network.filters.json_rpc module

halborn_ctf.network.filters.json_rpc.filter_methods(methods=[], *, listen_port, to_port, to_host='127.0.0.1')[source]

Proxy filter that allows filtering JSON RPC method

The proxy will expose a service that will be listening on listen_port and redirect all traffic to to_host:to_port.

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 methods parameter does support regex on each of the elements:

# Disable all methods starting with `anvil_` and `evm_`
filter_methods(["anvil_.*", "evm_.*"], listen_port=8545, to_port=8546)
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 [].

  • listen_port (int) – The port that will expose the proxy filter server

  • to_port (int) – The port that all traffic will be redirected to

  • to_host (str, optional) – The host that all traffic will be redirected to. Defaults to ‘127.0.0.1’.

halborn_ctf.network.filters.json_rpc.whitelist_methods(methods=[], *, listen_port, to_port, to_host='127.0.0.1')[source]

Proxy filter that allows whitelisting JSON RPC methods

The proxy will expose a service that will be listening on listen_port and redirect all traffic to to_host:to_port.

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 methods parameter does support regex on each of the elements:

# Allowing all methods starting with `eth_` and `net_`
whitelist_methods(["eth_.*", "net_.*"], listen_port=8545, to_port=8546)
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 [].

  • listen_port (int) – The port that will expose the proxy filter server

  • to_port (int) – The port that all traffic will be redirected to

  • to_host (str, optional) – The host that all traffic will be redirected to. Defaults to ‘127.0.0.1’.

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
self.path_mapping = {
    '/': {
            'port': 8545,
            'path': '/',
            'methods': ['POST']
    },
}

...

# Expose anvil to an internal port that will is not being exposed
shell.run('anvil -p 9999')
network.filters.json_rpc.filter_methods(['evm_*'], listen_port=8545, to_port=9999)

# We can also use the whitelist variant to only allow those methods
# network.filters.json_rpc.whitelist_methods(['net_*', 'eth_*'...], listen_port=8545, to_port=9999)

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).

Example

Once the script is created it can be executed using run_script:

run_script('./filter.py', listen_port=8545, to_port=9999, custom='More data')
...
# The script can access the extra **kwargs using ``ctx.options.[kwargname]`` and JSON decoding it
custom_data = json.loads(ctx.options.custom)
halborn_ctf.network.filters.run_script(script, listen_port, to_port, to_host='127.0.0.1', **kwargs)[source]

Allows running an arbitrary mitmdump script as a background shell process.

The mitmdump will be used in upstream mode against http://{to_host}:{to_port} and listen under listen_port. Any extra arguments provided to the run_script will be transfered to the script using the --set command flag on the command line of mitmdump as JSON encoded string which can be accessed on the script using mitmproxy.ctx.options.[varname].

Example

Once the script is created it can be executed using run_script:

run_script('./filter.py', listen_port=8545, to_port=9999, custom='More data')
...
# The script can access the extra **kwargs using ``ctx.options.[kwargname]`` and JSON decoding it
custom_data = json.loads(ctx.options.custom)
Parameters:
  • script (str) – Path of the script to execute

  • listen_port (int) – Port that mitmdump will listen on

  • to_port (int) – The upstream port to proxy traffic to

  • to_host (str) – The upstream host to proxy traffic to. Defaults to ‘127.0.0.1’.