Files
pentest.intro/session1/content/site-src/build_template.py
T
quoije dcb64ac65e Restructure the repository per session
session1/ now holds the whole live lab (content, platforms, docs, answer key,
README); session2/ keeps the design for the second workshop. The root README
indexes both and states that session2 modifies session1 rather than
duplicating it.

build-bundle.sh builds from session1 and still produces a flat archive, so
the documented paths inside a deployment (platforms/<p>/lab.sh) are
unchanged. .gitignore follows the move.
2026-10-24 14:22:39 -04:00

129 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""
Turn the archived Norbourg.com home page into ready-to-open HTML.
Two outputs are produced from the same source capture:
norbourg.html one self-contained file (images inlined as data: URIs)
norbourg-static/ index.html + images/ with plain relative paths
(use this one if data: URIs are blocked by a viewer)
Both keep the original table layout, iso-8859-1 charset and the hover
JavaScript; they need no server and no network to display.
"""
import base64
import re
import shutil
import time
import urllib.error
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parent
SRC = ROOT / "source" / "raw-home.html"
ASSETS = ROOT / "norbourg-2002" / "images"
OUT_INLINE = ROOT / "norbourg.html"
OUT_STATIC = ROOT / "norbourg-static"
TS = "20020929201450"
SHARD = f"http://web.archive.org/web/{TS}id_/http://www.norbourg.com/images/"
UA = "Mozilla/5.0 (X11; Linux x86_64) norbourg-template/1.0"
MIME = {".gif": "image/gif", ".jpg": "image/jpeg", ".jpeg": "image/jpeg",
".png": "image/png", ".swf": "application/x-shockwave-flash"}
IMG_RE = re.compile(r"(?:https?://(?:www\.)?norbourg\.com)?/?images/([A-Za-z0-9_.\-]+\.(?:gif|jpe?g|png))", re.I)
LINK_RE = re.compile(r'href="(?:https?://(?:www\.)?norbourg\.com)?/?([A-Za-z0-9_.\-]+\.(?:asp|html))"', re.I)
def fetch_asset(name):
dest = ASSETS / name
if dest.is_file() and dest.stat().st_size > 0:
return dest.read_bytes()
for attempt in range(4):
try:
req = urllib.request.Request(SHARD + name, headers={"User-Agent": UA})
with urllib.request.urlopen(req, timeout=60) as r:
data = r.read()
if data:
dest.parent.mkdir(parents=True, exist_ok=True)
dest.write_bytes(data)
print(f" fetched {name} ({len(data)} bytes)")
time.sleep(0.3)
return data
except urllib.error.HTTPError as e:
print(f" {name}: HTTP {e.code}")
if e.code == 404:
return None
except Exception as e: # noqa: BLE001
print(f" {name}: {e}")
time.sleep(2 * (attempt + 1))
return None
def finalise(html):
"""Strip the 2002 hit counter and point page links at the archived copies."""
html = re.sub(r"(?is)<script[^>]*>\s*<!--\s*document\.write\('<a href=\"/webstat.*?</script>", "", html)
def fix_link(m):
page = m.group(1)
if page in ("index.asp", "index.html", ""):
return 'href="#"'
page = re.sub(r"\.html$", ".asp", page) # local mirror names -> original
return f'href="https://web.archive.org/web/{TS}/http://www.norbourg.com/{page}"'
return LINK_RE.sub(fix_link, html)
def main():
raw = SRC.read_text(encoding="latin-1")
refs = sorted({m.group(1) for m in IMG_RE.finditer(raw)})
print(f"{len(refs)} image references")
blobs = {}
for name in refs:
data = fetch_asset(name)
if data:
blobs[name] = data
else:
print(f" !! could not get {name}")
# ---------- variant 1: everything inlined -----------------------------
def inline(m):
name = m.group(1)
if name not in blobs:
return m.group(0)
mime = MIME.get(Path(name).suffix.lower(), "application/octet-stream")
b64 = base64.b64encode(blobs[name]).decode("ascii")
return f"data:{mime};base64,{b64}"
html = finalise(IMG_RE.sub(inline, raw))
# a data: URI inside the old BACKGROUND attribute is unreliable, move it to CSS
m = re.search(r'background="(data:image/[^"]+)"', html, re.I)
if m:
html = re.sub(r'\sbackground="data:image/[^"]+"', "", html, count=1, flags=re.I)
html = html.replace(
"</HEAD>", f"<style>body{{background-image:url('{m.group(1)}');}}</style>\n</HEAD>", 1
)
OUT_INLINE.write_text(html, encoding="latin-1")
print(f"wrote {OUT_INLINE.name} ({OUT_INLINE.stat().st_size/1024:.0f} KB)")
# ---------- variant 2: plain files next to the html -------------------
OUT_STATIC.mkdir(exist_ok=True)
(OUT_STATIC / "images").mkdir(exist_ok=True)
for name in blobs:
shutil.copyfile(ASSETS / name, OUT_STATIC / "images" / name)
def relative(m):
name = m.group(1)
return f"images/{name}" if name in blobs else m.group(0)
html = finalise(IMG_RE.sub(relative, raw))
(OUT_STATIC / "index.html").write_text(html, encoding="latin-1")
left = IMG_RE.findall(html)
print(f"wrote {OUT_STATIC.name}/index.html + {len(blobs)} images, unresolved refs: {len(left)}")
if __name__ == "__main__":
main()