Skip to content

Commit d762fe0

Browse files
authored
Fix corefile module after pyelftools update (Gallopsled#2261)
* Fix corefile module after pyelftools update The `bytes2str` internal method was moved while Python 2 support was removed in pyelftools 0.3.0. Just use our own python aware decoder wrapper. Fixes Gallopsled#2260 * Pin pyelftools < 0.3 for Python 2 * Fix pyelftools version number scheme * Update CHANGELOG
1 parent 6ff7425 commit d762fe0

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ The table below shows which release corresponds to each branch, and what date th
8888

8989
- [#2214][2214] Fix bug at ssh.py:`download` and `download_file` with relative paths
9090
- [#2241][2241] Fix ssh.process not setting ssh_process.cwd attribute
91+
- [#2261][2261] Fix corefile module after pyelftools update
9192

9293
[2214]: https://github.com/Gallopsled/pwntools/pull/2214
9394
[2241]: https://github.com/Gallopsled/pwntools/pull/2241
95+
[2261]: https://github.com/Gallopsled/pwntools/pull/2261
9496

9597
## 4.10.0
9698

pwnlib/elf/corefile.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
from io import BytesIO, StringIO
7777

7878
import elftools
79-
from elftools.common.py3compat import bytes2str
8079
from elftools.common.utils import roundup
8180
from elftools.common.utils import struct_parse
8281
from elftools.construct import CString
@@ -94,6 +93,7 @@
9493
from pwnlib.util.fiddling import unhex
9594
from pwnlib.util.misc import read
9695
from pwnlib.util.misc import write
96+
from pwnlib.util.packing import _decode
9797
from pwnlib.util.packing import pack
9898
from pwnlib.util.packing import unpack_many
9999

@@ -134,12 +134,13 @@ def iter_notes(self):
134134
self.stream.seek(offset)
135135
# n_namesz is 4-byte aligned.
136136
disk_namesz = roundup(note['n_namesz'], 2)
137-
note['n_name'] = bytes2str(
138-
CString('').parse(self.stream.read(disk_namesz)))
139-
offset += disk_namesz
137+
with context.local(encoding='latin-1'):
138+
note['n_name'] = _decode(
139+
CString('').parse(self.stream.read(disk_namesz)))
140+
offset += disk_namesz
140141

141-
desc_data = bytes2str(self.stream.read(note['n_descsz']))
142-
note['n_desc'] = desc_data
142+
desc_data = _decode(self.stream.read(note['n_descsz']))
143+
note['n_desc'] = desc_data
143144
offset += roundup(note['n_descsz'], 2)
144145
note['n_size'] = offset - note['n_offset']
145146
yield note

pwnlib/util/packing.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,9 @@ def _encode(s):
10651065
return s.encode(context.encoding)
10661066

10671067
def _decode(b):
1068+
if isinstance(b, (str, six.text_type)):
1069+
return b # already text
1070+
10681071
if context.encoding == 'auto':
10691072
try:
10701073
return b.decode('utf-8')

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747

4848
install_requires = ['paramiko>=1.15.2',
4949
'mako>=1.0.0',
50-
'pyelftools>=0.2.4',
50+
'pyelftools>=0.24, <0.30; python_version < "3"',
51+
'pyelftools>=0.24; python_version >= "3"',
5152
'capstone>=3.0.5rc2', # See Gallopsled/pwntools#971, Gallopsled/pwntools#1160
5253
'ropgadget>=5.3',
5354
'pyserial>=2.7',

0 commit comments

Comments
 (0)