From 55a9df152ca1b2aab888806abc2dc3ded3f20c7d Mon Sep 17 00:00:00 2001 From: deadly-panda Date: Mon, 22 Jul 2024 19:32:58 +0100 Subject: [PATCH] Handle `json.decoder.JSONDecodeError` exceptions. --- agent/agent_nuclei.py | 6 +++++- tests/agent_test.py | 15 +++++++++++++++ tests/invalid_nuclei_result.json | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/invalid_nuclei_result.json diff --git a/agent/agent_nuclei.py b/agent/agent_nuclei.py index ca9c079..ecdd5ed 100644 --- a/agent/agent_nuclei.py +++ b/agent/agent_nuclei.py @@ -186,7 +186,11 @@ def _parse_output(self) -> None: with open(OUTPUT_PATH, "r", encoding="UTF-8") as f: lines = f.readlines() for line in lines: - nuclei_data_dict = json.loads(line) + try: + nuclei_data_dict = json.loads(line) + except json.decoder.JSONDecodeError as e: + logger.debug("Error while trying to decode line: %s: %s", line, e) + continue technical_detail = "" matcher_status = nuclei_data_dict.get("matcher-status", False) matcher_name = nuclei_data_dict.get("matcher-name", None) diff --git a/tests/agent_test.py b/tests/agent_test.py index e0daa01..050f581 100644 --- a/tests/agent_test.py +++ b/tests/agent_test.py @@ -769,3 +769,18 @@ def testAgentNuclei_whenDescriptionIsLong_shouldNotTruncate( agent_mock[0].data["description"] == "The SSH transport protocol with certain OpenSSH extensions, found in OpenSSH before 9.6 and other products, allows remote attackers to bypass integrity checks such that some packets are omitted (from the extension negotiation message), and a client and server may consequently end up with a connection for which some security features have been downgraded or disabled, aka a Terrapin attack. This occurs because the SSH Binary Packet Protocol (BPP), implemented by these extensions, mishandles the handshake phase and mishandles use of sequence numbers. For example, there is an effective attack against SSH's use of ChaCha20-Poly1305 (and CBC with Encrypt-then-MAC). The bypass occurs in chacha20-poly1305@openssh.com and (if CBC is used) the -etm@openssh.com MAC algorithms. This also affects Maverick Synergy Java SSH API before 3.1.0-SNAPSHOT, Dropbear through 2022.83, Ssh before 5.1.1 in Erlang/OTP, PuTTY before 0.80, AsyncSSH before 2.14.2, golang.org/x/crypto before 0.17.0, libssh before 0.10.6, libssh2 through 1.11.0, Thorn Tech SFTP Gateway before 3.4.6, Tera Term before 5.1, Paramiko before 3.4.0, jsch before 0.2.15, SFTPGo before 2.5.6, Netgate pfSense Plus through 23.09.1, Netgate pfSense CE through 2.7.2, HPN-SSH through 18.2.0, ProFTPD before 1.3.8b (and before 1.3.9rc2), ORYX CycloneSSH before 2.3.4, NetSarang XShell 7 before Build 0144, CrushFTP before 10.6.0, ConnectBot SSH library before 2.2.22, Apache MINA sshd through 2.11.0, sshj through 0.37.0, TinySSH through 20230101, trilead-ssh2 6401, LANCOM LCOS and LANconfig, FileZilla before 3.66.4, Nova before 11.8, PKIX-SSH before 14.4, SecureCRT before 9.4.3, Transmit5 before 5.10.4, Win32-OpenSSH before 9.5.0.0p1-Beta, WinSCP before 6.2.2, Bitvise SSH Server before 9.32, Bitvise SSH Client before 9.33, KiTTY through 0.76.1.13, the net-ssh gem 7.2.0 for Ruby, the mscdex ssh2 module before 1.15.0 for Node.js, the thrussh library before 0.35.1 for Rust, and the Russh crate before 0.40.2 for Rust.\n" ) + + +@mock.patch("agent.agent_nuclei.OUTPUT_PATH", "./tests/invalid_nuclei_result.json") +def testAgentNuclei_whenResultIsInvalidJson_agentShouldHandleExceptionAndDoNotRaise( + scan_message: message.Message, + nuclei_agent_no_url_scope: agent_nuclei.AgentNuclei, + mocker: plugin.MockerFixture, + agent_mock: list[message.Message], +) -> None: + """Ensure the agent handles `json.decoder.JSONDecodeError` exceptions.""" + mocker.patch("subprocess.run", return_value=None) + + nuclei_agent_no_url_scope.process(scan_message) + + assert len(agent_mock) == 0 diff --git a/tests/invalid_nuclei_result.json b/tests/invalid_nuclei_result.json new file mode 100644 index 0000000..d4ef0a5 --- /dev/null +++ b/tests/invalid_nuclei_result.json @@ -0,0 +1 @@ +{"curl-command":"curl -X 'POST' -d '_=' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Host: web.com' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2866.71 Safari/537.36' 'https://web.com/'","host":"https://web.com","extracted-results": ["result1"],"info":{"author":["dwisiswant0","lu4nx"],"classification":{"cve-id":null "cvss-metrics":"CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:N","cwe-id":["cwe-200"]},"description":"A web application firewall was detected.","name":"WAF Detection","reference":["https://github.com/ekultek/whatwaf"],"severity":"critical","tags":["waf","tech","misc"]},"ip":"54.183.147.1","matched-at":"https://web.com/","matcher-name":"ats","matcher-status":true,"request":"POST / HTTP/1.1\r\nHost: ","response":"HTTP/1.1 200 OK\r\nConnection: close\r\nCache-Control: no-store, no-cache, ","timestamp":"2022-03-28T19:56:11.826498997Z","type":"http"}