REST-JSON API client
(host=RESTAPI_DEFAULT_ADDRESS, port=RESTAPI_DEFAULT_PORT, username=None, password=None)
| 759 | return text |
| 760 | |
| 761 | def client(host=RESTAPI_DEFAULT_ADDRESS, port=RESTAPI_DEFAULT_PORT, username=None, password=None): |
| 762 | """ |
| 763 | REST-JSON API client |
| 764 | """ |
| 765 | |
| 766 | DataStore.username = username |
| 767 | DataStore.password = password |
| 768 | |
| 769 | dbgMsg = "Example client access from command line:" |
| 770 | dbgMsg += "\n\t$ taskid=$(curl http://%s:%d/task/new 2>1 | grep -o -I '[a-f0-9]\\{16\\}') && echo $taskid" % (host, port) |
| 771 | dbgMsg += "\n\t$ curl -H \"Content-Type: application/json\" -X POST -d '{\"url\": \"http://testphp.vulnweb.com/artists.php?artist=1\"}' http://%s:%d/scan/$taskid/start" % (host, port) |
| 772 | dbgMsg += "\n\t$ curl http://%s:%d/scan/$taskid/data" % (host, port) |
| 773 | dbgMsg += "\n\t$ curl http://%s:%d/scan/$taskid/log" % (host, port) |
| 774 | logger.debug(dbgMsg) |
| 775 | |
| 776 | addr = "http://%s:%d" % (host, port) |
| 777 | logger.info("Starting REST-JSON API client to '%s'..." % addr) |
| 778 | |
| 779 | try: |
| 780 | _client(addr) |
| 781 | except Exception as ex: |
| 782 | if not isinstance(ex, _urllib.error.HTTPError) or ex.code == _http_client.UNAUTHORIZED: |
| 783 | errMsg = "There has been a problem while connecting to the " |
| 784 | errMsg += "REST-JSON API server at '%s' " % addr |
| 785 | errMsg += "(%s)" % getSafeExString(ex) |
| 786 | logger.critical(errMsg) |
| 787 | return |
| 788 | |
| 789 | commands = ("help", "new", "use", "data", "log", "status", "option", "stop", "kill", "list", "flush", "version", "exit", "bye", "quit") |
| 790 | colors = ('red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'lightgrey', 'lightred', 'lightgreen', 'lightyellow', 'lightblue', 'lightmagenta', 'lightcyan') |
| 791 | autoCompletion(AUTOCOMPLETE_TYPE.API, commands=commands) |
| 792 | |
| 793 | taskid = None |
| 794 | logger.info("Type 'help' or '?' for list of available commands") |
| 795 | |
| 796 | while True: |
| 797 | try: |
| 798 | color = colors[int(taskid or "0", 16) % len(colors)] |
| 799 | command = _input("api%s> " % (" (%s)" % setColor(taskid, color) if taskid else "")).strip() |
| 800 | command = re.sub(r"\A(\w+)", lambda match: match.group(1).lower(), command) |
| 801 | except (EOFError, KeyboardInterrupt): |
| 802 | print() |
| 803 | break |
| 804 | |
| 805 | if command in ("data", "log", "status", "stop", "kill"): |
| 806 | if not taskid: |
| 807 | logger.error("No task ID in use") |
| 808 | continue |
| 809 | raw = _client("%s/scan/%s/%s" % (addr, taskid, command)) |
| 810 | res = dejsonize(raw) |
| 811 | if not res["success"]: |
| 812 | logger.error("Failed to execute command %s" % command) |
| 813 | dataToStdout("%s\n" % raw) |
| 814 | |
| 815 | elif command.startswith("option"): |
| 816 | if not taskid: |
| 817 | logger.error("No task ID in use") |
| 818 | continue |
no test coverage detected
searching dependent graphs…