|
| 1 | +# CryptoHack Requester Package |
| 2 | + |
| 3 | +This is a simple python package to ease interaction with CryptoHack's challenges over Netcat and Web API. |
| 4 | + |
| 5 | +## Installing / Getting started |
| 6 | + |
| 7 | +The project has been tested with python >= 3.6.9. It may works with previous version, but nothing sure. |
| 8 | + |
| 9 | +To install the package simply use `pip`: |
| 10 | +```shell |
| 11 | +python3 -m pip install CHRequester |
| 12 | +``` |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +CryptoHack has some challenges working with a Web API ([Block Cipher Mode challenges](https://cryptohack.org/challenges/aes/)), and some through server comunication. They generaly need automation so it is recommanded in the [FAQ](https://cryptohack.org/faq/) to use the [Requets](https://requests.readthedocs.io/en/master/) package for web challenges and [Pwntools](http://docs.pwntools.com/en/stable/) to communicate to a challenge on a server. |
| 17 | +This package offer a more user-firendly and less time-consuming way to communicate with these challenges. |
| 18 | + |
| 19 | +### Example of usage with the Web API: |
| 20 | +```python |
| 21 | +from ch_requester import URLRequester |
| 22 | + |
| 23 | +if __name__ == "__main__": |
| 24 | + BASE_URL = "http://aes.cryptohack.org/oh_snap/" |
| 25 | + |
| 26 | + R = URLRequester(BASE_URL) |
| 27 | + # adding actions to the requester |
| 28 | + # first give the name of the action |
| 29 | + # then specify a tuple of inputs that are needed for this action |
| 30 | + # then specify a tuple of outputs that can given in return |
| 31 | + |
| 32 | + # 'error' can be specified as an output if the challenge returns |
| 33 | + # useful information through an error message |
| 34 | + # else, an Exception will be raised if an error is returned |
| 35 | + # and the user does not specify it as a desired output. |
| 36 | + # inputs must be in the order of the url, e.g. |
| 37 | + # http://aes.cryptohack.org/oh_snap/ciphertext/nonce/ |
| 38 | + R.add_action('send_cmd', ('ciphertext', 'nonce'), ('msg', 'error')) |
| 39 | + |
| 40 | + # we can then execute an action, by giving its name as first parameters. |
| 41 | + # The following ordered arguments are the wanted outputs from this action. |
| 42 | + # Even if we specified multiple possible outputs for this actions ('error') |
| 43 | + # and 'msg', it is possible to retrieve a subset. If only one output is |
| 44 | + # asked, then its value is returned by the function. Else a dictionnary |
| 45 | + # is returned. |
| 46 | + # Then inputs are specified through unordered arguments. There must always |
| 47 | + # be given as bytes, the requester is in charge to send it as hex to the web |
| 48 | + # server. If some day the Web API changes, and some arguments are not expected |
| 49 | + # to be in hexadecimal, changes will be needed. |
| 50 | + error_message = req.do_action('send_cmd', 'error', ciphertext=b'\xff', nonce=b'\xff') |
| 51 | + |
| 52 | + # do awesome things to find the flag |
| 53 | +``` |
| 54 | + |
| 55 | +### Example of usage with server communication using 'option' parameters: |
| 56 | +```python |
| 57 | +from ch_requester import NCRequester |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + |
| 61 | + R = NCRequester(13397) |
| 62 | + # adding actions to the requester |
| 63 | + # first give the name of the action, and must match the option |
| 64 | + # paramete of the server-side. |
| 65 | + # then specify a tuple of inputs that are needed for this action |
| 66 | + # then specify a tuple of outputs that can given in return. |
| 67 | + # No particular ordering is needed here. |
| 68 | + R.add_action("insert_key", ("key",), ("msg")) |
| 69 | + R.add_action("unlock", (), ("msg",)) |
| 70 | + |
| 71 | + # we then open the connection (using pwntools) |
| 72 | + R.open() |
| 73 | + # some challenges print a line to welcome the challenger |
| 74 | + # we can skip it |
| 75 | + R.flush_line() |
| 76 | + |
| 77 | + # As for the Web API, we can execute action, except this time |
| 78 | + # the user is responsible to put input data into the correct |
| 79 | + # format used by the server-side. |
| 80 | + msg = req.do_action("insert_key", "msg", key=(b"\x00" * 16).hex()) |
| 81 | + |
| 82 | + print(msg) |
| 83 | + |
| 84 | + flag = req.do_action("unlock", "msg") |
| 85 | + |
| 86 | + # do awesome things to find the flag |
| 87 | + |
| 88 | + R.close() |
| 89 | +``` |
| 90 | + |
| 91 | +### Example of usage with server communication without 'option' parameters: |
| 92 | +```python |
| 93 | +from ch_requester import NCRequester |
| 94 | +from base64 import b64decode |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + |
| 98 | + R = NCRequester(13370) |
| 99 | + # some challenges don't use the 'option' parameters |
| 100 | + # so you can send your own dictionnary payload to the server |
| 101 | + |
| 102 | + # we then open the connection (using pwntools) |
| 103 | + R.open() |
| 104 | + # some challenges print a line to welcome the challenger |
| 105 | + # we can skip it |
| 106 | + R.flush_line() |
| 107 | + |
| 108 | + PAYLOAD = {'msg': 'request'} |
| 109 | + ciphertext = b64decode(req.send_recv_raw_payload(PAYLOAD, 'ciphertext')) |
| 110 | + |
| 111 | + # do awesome things to find the flag |
| 112 | + |
| 113 | + R.close() |
| 114 | +``` |
| 115 | + |
| 116 | +## Contributing |
| 117 | + |
| 118 | +If you'd like to contribute, please fork the repository and use a feature |
| 119 | +branch. Pull requests are warmly welcome. |
| 120 | + |
| 121 | +## Licensing |
| 122 | + |
| 123 | +The code in this project is licensed under MIT license. |
0 commit comments