Files
php2shell-cve-2026-109618-1…/tests/test_cli.py
T

113 lines
4.0 KiB
Python

import unittest
from poc import cli
def parse(argv):
return cli.build_parser().parse_args(argv)
class SeparationTest(unittest.TestCase):
def test_lab_defaults_to_up(self):
self.assertEqual(parse(["lab"]).action, "up")
def test_lab_up_status_down(self):
for action in ("up", "down", "status"):
self.assertEqual(parse(["lab", action]).action, action)
def test_lab_rejects_unknown_action(self):
with self.assertRaises(SystemExit):
parse(["lab", "exploit"])
def test_lab_has_no_exploit_flags(self):
ns = parse(["lab", "up"])
for flag in ("proof", "retries", "dry_run"):
self.assertFalse(hasattr(ns, flag), flag)
def test_exploit_keeps_its_flags(self):
ns = parse(["exploit", "--cmd", "id>/out/x", "--retries", "1"])
self.assertEqual(ns.cmd, "id>/out/x")
self.assertEqual(ns.retries, 1)
def test_lab_actions_do_not_run_the_chain(self):
ns = parse(["lab", "up"])
self.assertIs(ns.func, cli.cmd_lab)
def test_exploit_and_leak_are_separate_commands(self):
self.assertIs(parse(["exploit"]).func, cli.cmd_exploit)
self.assertIs(parse(["leak"]).func, cli.cmd_leak)
self.assertIs(parse(["check"]).func, cli.cmd_check)
class UrlTargetTest(unittest.TestCase):
def test_defaults_without_url(self):
ns = cli.resolve_target(parse(["exploit"]))
self.assertEqual((ns.host, ns.port, ns.path), ("127.0.0.1", 18110, ""))
def test_url_sets_host_port_and_path(self):
ns = cli.resolve_target(parse(["exploit", "--url", "http://10.0.0.5:8080/app/index.php"]))
self.assertEqual(ns.host, "10.0.0.5")
self.assertEqual(ns.port, 8080)
self.assertEqual(ns.path, "/app/index.php")
def test_url_default_port_80(self):
ns = cli.resolve_target(parse(["leak", "--url", "http://lab.internal/index.php"]))
self.assertEqual(ns.port, 80)
def test_url_root_path_defaults(self):
ns = cli.resolve_target(parse(["leak", "--url", "http://lab.internal:9000"]))
self.assertEqual(ns.path, "/index.php")
def test_https_rejected(self):
with self.assertRaises(SystemExit):
cli.resolve_target(parse(["exploit", "--url", "https://lab.internal/"]))
def test_explicit_flags_win_over_url(self):
ns = cli.resolve_target(parse(["exploit", "--url", "http://a:1/x.php", "--port", "7000"]))
self.assertEqual(ns.port, 7000)
class BannerTest(unittest.TestCase):
def test_banner_carries_author_and_org(self):
import contextlib
import io
import poc
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
cli.banner()
out = buf.getvalue()
self.assertIn(poc.__author__, out)
self.assertIn(poc.__org__, out)
self.assertIn(poc.__version__, out)
class ExecWrapperTest(unittest.TestCase):
def test_wrapper_redirects_output_to_mount(self):
wrapped = cli.exec_wrapper("id")
self.assertIn("id", wrapped)
self.assertIn("/out/.poc-exec", wrapped)
self.assertTrue(wrapped.endswith("2>&1"))
self.assertIn(chr(10), wrapped)
def test_wrapper_survives_multiple_commands(self):
wrapped = cli.exec_wrapper("id; uname -a")
self.assertIn("id; uname -a", wrapped)
def test_fits_boundary(self):
self.assertTrue(cli.exec_fits("A" * 150))
self.assertFalse(cli.exec_fits("A" * 300))
def test_limit_matches_forge_carrier(self):
from poc import forge
self.assertEqual(cli.CARRIER_LIMIT, 200)
with self.assertRaises(ValueError):
forge.build_ht(0, 0, b"A" * (cli.CARRIER_LIMIT + 1))
def test_exec_command_parses_and_resolves(self):
ns = cli.resolve_target(parse(["exec", "--cmd", "id", "--url", "http://h:9/x.php"]))
self.assertIs(ns.func, cli.cmd_exec)
self.assertEqual(ns.cmd, "id")
self.assertEqual((ns.host, ns.port, ns.path), ("h", 9, "/x.php"))