Skip to content

Commit

Permalink
Add proxy of filepointer; more robust overlap_structure
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketMaDev committed Aug 20, 2024
1 parent 958a7a9 commit b5a1ca8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
2 changes: 2 additions & 0 deletions pwnlib/filepointer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This is a fallback support for filepointer mudule
from pwnlib.file.filepointer import *
27 changes: 13 additions & 14 deletions pwnlib/util/packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,14 +1202,15 @@ def _decode(b):
del name, routine, mod

def overlap_structure(*structs):
"""overlap_structure(*structs: tuple[bytes_like]) -> bytes
r"""overlap_structure(*structs: tuple[bytes_like]) -> bytes
Unpacks bytes_like structures and overlap them up. Check the example
below for more detail.
Parameters:
structs: ``str``, ``bytes`` and ``list[int]`` are all acceptable,
as long as the argument is "len-able", iterable and serving ints.
Note ``str``s will be used as ``bytes`` object.
Raises:
ValueError: 2 non-zero values on the same index. Carry `msg` of
Expand All @@ -1222,32 +1223,30 @@ def overlap_structure(*structs):
Examples:
>>> x = bytes.fromhex('41 42 43 44')
>>> y = '\0\0\0\0\0\0\0\0\0xyz'
>>> z = [0, 0, 0, 0, 33, 34, 35, 36]
>>> x = p32(0x7ffffe30)
>>> y = [0, 0, 0, 0, 33, 34, 35, 36]
>>> overlap_structure(x, y)
b'0\xfe\xff\x7f!"#$'
>>> z = p32(0xbeef)
>>> overlap_structure(x, y, z)
<stdin>:1: BytesWarning: Text is not bytes; assuming ASCII, no guarantees. See https://docs.pwntools.com/#bytes
b'ABCD!"#$\x00xyz'
>>> w = p32(0xbeef)
>>> overlap_structure(w, x, y, z)
Traceback (most recent call last):
...
ValueError: Conflict values at index 0, 1
"""
if len(structs) == 1:
return structs[0]

maxlen = max(len(e) for e in structs)
# convert str to bytes first to calc accurate length
itr = [_need_bytes(s) if isinstance(s, str) else s for s in structs]
maxlen = max(len(e) for e in itr)
final = [0] * maxlen
errs = []
errs = set()

for s in structs:
if isinstance(s, str):
s = _need_bytes(s)
for s in itr:
for i, e in enumerate(s):
if e > 0:
if final[i]:
errs.append(i)
errs.add(i)
final[i] = e

if errs:
Expand Down

0 comments on commit b5a1ca8

Please sign in to comment.