Skip to content

Commit 8591c31

Browse files
committed
1.5.7 pwntools 4.14.0 improvements
+ removed pwntools debug patch (pwntools==4.14.0) + fixed ssh port forwarding for alpine + fixed libc debug symbols (added new sysroot_debug args) + added Optional type hints
1 parent 0e79aab commit 8591c31

12 files changed

+180
-303
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "vagd"
3-
version = "1.5.5"
3+
version = "1.5.6"
44
authors = [{ name = "0x6fe1be2" }]
55
description = "VirtuAlization GDb integrations in pwntools"
66
readme = "README.md"

requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
pwntools
1+
pwntools==4.14.0
22
docker
3-
typer
3+
typer

src/vagd/cli.py

+22-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import stat
44
import sys
5-
from typing import Dict, List, Optional
5+
from typing import Dict, List, Optional, Any
66

77
import typer
88
from rich.console import Console
@@ -16,14 +16,14 @@
1616
from vagd.virts.vagd import Vagd
1717

1818
DOGD_BOX = "Box.DOCKER_UBUNTU"
19-
DOGD = "vm = Dogd(BINARY, image={box}, {args}) # Docker"
19+
DOGD = "vm = Dogd(BINARY, image=BOX, {args}) # Docker"
2020
QEGD_BOX = "Box.QEMU_UBUNTU"
21-
QEGD = "vm = Qegd(BINARY, img={box}, {args}) # Qemu"
21+
QEGD = "vm = Qegd(BINARY, img=BOX, {args}) # Qemu"
2222
SHGD = "vm = Shgd(BINARY, user='user', host='localhost', port=22, {args}) # SSH"
2323

2424
# deprecated
2525
VAGD_BOX = "Box.VAGRANT_JAMMY64"
26-
VAGD = "vm = Vagd(BINARY, {box}, {args}) # Vagrant"
26+
VAGD = "vm = Vagd(BINARY, BOX, {args}) # Vagrant"
2727

2828
AD_ENV = """# ad envs
2929
IS_AD = os.getenv('TARGET_IP') is not None # running on ad
@@ -35,7 +35,9 @@
3535
err_console = Console(stderr=True)
3636
console = Console()
3737

38-
quote = lambda x: f"'{x}'"
38+
39+
def quote(x: str):
40+
return f"'{x}'"
3941

4042

4143
def _version(value: bool) -> None:
@@ -65,12 +67,11 @@ def add_virt(
6567
dependency: str,
6668
template: str,
6769
args: Dict[str, str],
68-
multi=False,
69-
box="",
70+
multi: bool = False,
7071
):
7172
dependencies.append(dependency)
7273
args_str = ", ".join(f"{k}={v}" for k, v in args.items())
73-
vm = template.format(box=box, args=args_str)
74+
vm = template.format(args=args_str)
7475
vms.append(("# " if multi else "") + vm)
7576

7677

@@ -162,14 +163,14 @@ def template(
162163
if sum((dogd, qegd, vagd, shgd)) > 1:
163164
multi = True
164165

165-
env = {}
166+
env: Dict[str, str] = dict()
166167

167168
if libc:
168169
files.append(libc)
169170

170-
dependencies = []
171-
vms = []
172-
args = dict()
171+
dependencies: list[str] = list()
172+
vms: list[str] = list()
173+
args: Dict[str, Any] = dict()
173174

174175
if root:
175176
args["user"] = "'root'"
@@ -188,13 +189,17 @@ def template(
188189

189190
args["ex"] = "True"
190191
args["fast"] = "True"
192+
box = ""
191193

192194
if dogd:
193-
add_virt(dependencies, vms, "Dogd", DOGD, args, box=image)
195+
box = image
196+
add_virt(dependencies, vms, "Dogd", DOGD, args)
194197
if qegd:
195-
add_virt(dependencies, vms, "Qegd", QEGD, args, multi, box=img)
198+
box = img
199+
add_virt(dependencies, vms, "Qegd", QEGD, args, multi)
196200
if vagd:
197-
add_virt(dependencies, vms, "Vagd", VAGD, args, multi, box=vbox)
201+
box = vbox
202+
add_virt(dependencies, vms, "Vagd", VAGD, args, multi)
198203
if shgd:
199204
add_virt(dependencies, vms, "Shgd", SHGD, args, multi)
200205

@@ -226,6 +231,7 @@ def template(
226231
ip=quote(ip),
227232
port=str(port),
228233
env=repr(env),
234+
box=box,
229235
ad_env=AD_ENV if ad else "",
230236
vms=("\n" + " " * 4).join(vms),
231237
libc=quote(libc),
@@ -267,7 +273,7 @@ def _get_type() -> str:
267273
exit(1)
268274

269275

270-
def _exec(cmd: str, env: Dict = None):
276+
def _exec(cmd: str, env: Optional[Dict[str, str]] = None):
271277
if env is None:
272278
env = os.environ
273279
else:

src/vagd/res/aliases.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ linfo = lambda x, *a: log.info(x, *a)
77
lwarn = lambda x, *a: log.warn(x, *a)
88
lerr = lambda x, *a: log.error(x, *a)
99
lprog = lambda x, *a: log.progress(x, *a)
10-
lhex = lambda x, y="leak": linfo(f"0x{x:016x} <- {y}")
11-
phex = lambda x, y="leak": print(f"0x{x:016x} <- {y}")
10+
lhex = lambda x, y="leak": linfo(f"{x:#016x} <- {y}")
11+
phex = lambda x, y="leak": print(f"{x:#016x} <- {y}")
1212

1313
# type manipulation
1414
byt = lambda x: x if isinstance(x, (bytes, bytearray)) else f"{x}".encode()
@@ -41,7 +41,7 @@ ss = (
4141
if len(x) < s
4242
else se(x, *a, **kw)
4343
if len(x) == s
44-
else lerr("ss to big: 0x%x > 0x%x", len(x), s)
44+
else lerr(f"ss to big: {len(x):#x} > {s:#x}")
4545
)
4646
sla = lambda x, y, t=None, *a, **kw: gt(t).sendlineafter(
4747
byt(x), byt(y), *a, **kw
@@ -52,7 +52,7 @@ sas = (
5252
if len(y) < s
5353
else sa(x, y, *a, **kw)
5454
if len(y) == s
55-
else lerr("sas to big: 0x%x > 0x%x", len(y), s)
55+
else lerr(f"ss to big: {len(x):#x} > {s:#x}")
5656
)
5757
ra = lambda t=None, *a, **kw: gt(t).recvall(*a, **kw)
5858
rl = lambda t=None, *a, **kw: gt(t).recvline(*a, **kw)

src/vagd/res/template.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ PORT = {port:<44s} # remote PORT
1111
BINARY = {binary:<44s} # PATH to local binary
1212
ARGS = [] # ARGS supplied to binary
1313
ENV = {env:<44s} # ENV supplied to binary
14+
BOX = {box:<44s} # Docker box image
1415
{ad_env}
1516
# GDB SCRIPT, executed at start of GDB session (e.g. set breakpoints here)
1617
GDB = f"""
@@ -25,7 +26,7 @@ context.aslr = {aslr:<5s} # ASLR enabled (only
2526

2627
# setup vagd vm
2728
vm = None
28-
def setup() -> object | None:
29+
def setup():
2930
global vm
3031
if args.REMOTE or {is_local}{is_ad}:
3132
return None
@@ -45,7 +46,7 @@ def setup() -> object | None:
4546

4647

4748
# get target (pwnlib.tubes.tube)
48-
def get_target(**kw) -> tubes.tube:
49+
def get_target(**kw):
4950
if args.REMOTE{is_ad}:
5051
# context.log_level = 'debug'
5152
return remote(IP, PORT)

src/vagd/templates.py

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
RUN adduser -h /home/{user} -s /bin/ash -g sudo -D {user}
6464
RUN echo "{user} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/{user} && chmod 0440 /etc/sudoers.d/{user}
6565
RUN echo "{user}:{user}" | chpasswd
66+
RUN sed "s/AllowTcpForwarding no/AllowTcpForwarding yes/" -i /etc/ssh/sshd_config
6667
6768
USER {user}
6869

src/vagd/virts/dogd.py

+9-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from typing import Dict, List
2+
from typing import Dict, List, Optional
33

44
import docker
55

@@ -67,7 +67,6 @@ class Dogd(Shgd):
6767
_dockerdir: str
6868
_dockerfile: str
6969
_isalpine: bool
70-
_gdbsrvport: int
7170
_rm: bool
7271
_ex: bool
7372
_forward: Dict[str, int]
@@ -88,8 +87,8 @@ def __init__(
8887
binary: str,
8988
image: str = DEFAULT_IMAGE,
9089
user: str = DEFAULT_USER,
91-
forward: Dict[str, int] = None,
92-
packages: List[str] = None,
90+
forward: Optional[Dict[str, int]] = None,
91+
packages: Optional[List[str]] = None,
9392
symbols=True,
9493
rm=True,
9594
ex: bool = False,
@@ -116,11 +115,8 @@ def __init__(
116115
# trigger package detection in Pwngdb
117116
packages = list()
118117

119-
self._gdbsrvport = -1
120118
self._dockerdir = Dogd.DOCKERHOME + f"{self._image}/"
121-
if not (
122-
os.path.exists(Dogd.DOCKERHOME) and os.path.exists(self._dockerdir)
123-
):
119+
if not (os.path.exists(Dogd.DOCKERHOME) and os.path.exists(self._dockerdir)):
124120
os.makedirs(self._dockerdir)
125121
self._dockerfile = self._dockerdir + "Dockerfile"
126122
self._user = user
@@ -143,7 +139,6 @@ def __init__(
143139
ex=ex,
144140
fast=fast,
145141
symbols=False,
146-
gdbsrvport=self._gdbsrvport,
147142
**kwargs,
148143
)
149144

@@ -154,11 +149,7 @@ def _create_dockerfile(self):
154149

155150
if not os.path.exists(self._dockerdir + "keyfile.pub"):
156151
os.link(Pwngd.PUBKEYFILE, self._dockerdir + "keyfile.pub")
157-
template = (
158-
templates.DOCKER_ALPINE_TEMPLATE
159-
if self._isalpine
160-
else templates.DOCKER_TEMPLATE
161-
)
152+
template = templates.DOCKER_ALPINE_TEMPLATE if self._isalpine else templates.DOCKER_TEMPLATE
162153

163154
with open(self._dockerfile, "w") as dockerfile:
164155
dockerfile.write(
@@ -175,9 +166,6 @@ def _create_docker_instance(self):
175166
helper.info("starting docker instance")
176167
self._port = helper.first_free_port(Dogd.DEFAULT_PORT)
177168
self._forward.update({"22/tcp": self._port})
178-
if self._isalpine:
179-
self._gdbsrvport = helper.first_free_port(Pwngd.STATIC_GDBSRV_PORT)
180-
self._forward.update({f"{self._gdbsrvport}/tcp": self._gdbsrvport})
181169

182170
dir = os.path.dirname(os.path.realpath(__file__))
183171
with open(dir[: dir.rfind("/")] + "/res/seccomp.json", "r") as seccomp_file:
@@ -194,9 +182,7 @@ def _create_docker_instance(self):
194182
self._id = container.id
195183
helper.info(f"started docker instance {container.short_id}")
196184
with open(Dogd.LOCKFILE, "w") as lockfile:
197-
lockfile.write(
198-
f"{container.id}:{str(self._port)}:{str(self._gdbsrvport)}"
199-
)
185+
lockfile.write(f"{container.id}:{str(self._port)}")
200186

201187
def _build_image(self):
202188
build_progress = helper.progress("building docker image")
@@ -215,9 +201,7 @@ def _build_image(self):
215201
tag += "_"
216202
tag += "symbols"
217203

218-
bimage = self._client.images.build(
219-
path=os.path.dirname(self._dockerfile), tag=f"vagd/{tag}"
220-
)[0]
204+
bimage = self._client.images.build(path=os.path.dirname(self._dockerfile), tag=f"vagd/{tag}")[0]
221205

222206
build_progress.success("done")
223207

@@ -239,21 +223,15 @@ def _vm_create(self):
239223
def _vm_setup(self) -> None:
240224
self._client = docker.from_env()
241225
if not os.path.exists(Dogd.LOCKFILE):
242-
helper.info(
243-
f"No Lockfile {Dogd.LOCKFILE} found, creating new Docker Instance"
244-
)
226+
helper.info(f"No Lockfile {Dogd.LOCKFILE} found, creating new Docker Instance")
245227
self._vm_create()
246228
else:
247229
with open(Dogd.LOCKFILE, "r") as lockfile:
248230
data = lockfile.readline().split(":")
249231
self._id = data[0]
250232
self._port = int(data[1])
251-
if self._isalpine:
252-
self._gdbsrvport = int(data[2])
253233
if not self._client.containers.list(filters={"id": self._id}):
254-
helper.info(
255-
f"Lockfile {Dogd.LOCKFILE} found, container not running, creating new one"
256-
)
234+
helper.info(f"Lockfile {Dogd.LOCKFILE} found, container not running, creating new one")
257235
self._vm_create()
258236
else:
259237
helper.info(

src/vagd/virts/logd.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Iterable
1+
from typing import Iterable, Optional
22

33
import pwnlib.args
44
import pwnlib.tubes
@@ -64,7 +64,7 @@ def _install_packages(self, packages: Iterable):
6464
"""
6565
helper.error("NOT IMPLEMENTED")
6666

67-
def put(self, file: str, remote: str = None):
67+
def put(self, file: str, remote: Optional[str] = None):
6868
"""
6969
NOT IMPLEMENTED
7070
"""
@@ -78,9 +78,7 @@ def debug(self, **kwargs) -> pwnlib.tubes.process.process:
7878
"""
7979
return self.pwn_debug(**kwargs)
8080

81-
def pwn_debug(
82-
self, argv: list[str] = None, **kwargs
83-
) -> pwnlib.tubes.process.process:
81+
def pwn_debug(self, argv: Optional[list[str]] = None, **kwargs) -> pwnlib.tubes.process.process:
8482
"""
8583
run binary with gdb locally
8684
:param argv: comandline arguments for binary
@@ -89,9 +87,7 @@ def pwn_debug(
8987
"""
9088
return pwnlib.gdb.debug([self._binary] + argv, **kwargs)
9189

92-
def process(
93-
self, argv: list[str] = None, **kwargs
94-
) -> pwnlib.tubes.process.process:
90+
def process(self, argv: Optional[list[str]] = None, **kwargs) -> pwnlib.tubes.process.process:
9591
"""
9692
run binary locally
9793
:param argv: comandline arguments for binary
@@ -102,9 +98,9 @@ def process(
10298

10399
def start(
104100
self,
105-
argv: list[str] = None,
101+
argv: Optional[list[str]] = None,
106102
gdbscript: str = "",
107-
api: bool = None,
103+
api: bool = False,
108104
**kwargs,
109105
) -> pwnlib.tubes.process.process:
110106
"""

0 commit comments

Comments
 (0)