Skip to content

Commit 0add811

Browse files
committed
change checksec=False on ELF class
There is no need to print checksec details every time. Most usecases only need to view checksec once in a while. Here is the `checksec=False` set before this patch: ``` > rg '\sELF\(' | rg checksec pwnlib/libcdb.py: local_libc = ELF(shell_path, checksec=False).libc pwnlib/libcdb.py: libc = ELF(filename, checksec=False) pwnlib/libcdb.py: >>> libc_path = ELF(which('ls'), checksec=False).libc.path pwnlib/libcdb.py: libc = ELF(libc_path, checksec=False) pwnlib/elf/elf.py: return ELF(lib, self._print_checksec) pwnlib/elf/elf.py: return ELF(exepath, checksec=False) pwnlib/elf/elf.py: return ELF(exepath, checksec=False) pwnlib/commandline/libcdb.py: exe = ELF(path, checksec=False) pwnlib/commandline/checksec.py: e = ELF(f) pwnlib/data/templates/pwnup.mako: ctx.binary = ELF(binary, checksec=False) ```
1 parent 636b3b2 commit 0add811

File tree

11 files changed

+37
-39
lines changed

11 files changed

+37
-39
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ The table below shows which release corresponds to each branch, and what date th
7474

7575
## 5.0.0 (`dev`)
7676

77+
- [#XXX][XXX] `checksec` argument of `ELF` class now defaults to `False`.
7778
- [#2519][2519] Drop Python 2.7 support / Require Python 3.10
7879
- [#2507][2507] Add `+LINUX` and `+WINDOWS` doctest options and start proper testing on Windows
7980
- [#2522][2522] Support starting a kitty debugging window with the 'kitten' command
@@ -86,6 +87,7 @@ The table below shows which release corresponds to each branch, and what date th
8687
- [#2506][2506] ROP: fix `ROP(ELF(exe)).leave` is `None` in some ELF
8788
- [#2504][2504] doc: add example case for `tuple` (host, port pair) in `gdb.attach`
8889

90+
[XXX]: https://github.com/Gallopsled/pwntools/pull/XXX
8991
[2519]: https://github.com/Gallopsled/pwntools/pull/2519
9092
[2507]: https://github.com/Gallopsled/pwntools/pull/2507
9193
[2522]: https://github.com/Gallopsled/pwntools/pull/2522

pwnlib/commandline/checksec.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def main(args):
3434

3535
for f in files:
3636
try:
37-
e = ELF(f)
37+
e = ELF(f, checksec=True)
3838
except Exception as e:
3939
print("{name}: {error}".format(name=f, error=e))
4040

pwnlib/commandline/debug.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def main(args):
6161

6262
if args.executable:
6363
if os.path.exists(args.executable):
64-
context.binary = ELF(args.executable)
64+
context.binary = ELF(args.executable) # ???
6565
target = context.binary.path
6666

6767
# This path does nothing, but avoids the "print_usage()"

pwnlib/commandline/disablenx.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
def main(args):
2121
for f in args.elf:
22-
e = ELF(f.name)
22+
e = ELF(f.name, checksec=True)
2323
e.disable_nx()
24-
ELF(e.path)
24+
ELF(e.path, checksec=True)
2525

2626
if __name__ == '__main__':
2727
pwnlib.commandline.common.main(__file__, main)

pwnlib/commandline/elfpatch.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ def main(a):
2727
offset = int(a.offset, 16)
2828
bytes = unhex(a.bytes)
2929

30-
with context.silent:
31-
elf = ELF(a.elf)
32-
30+
elf = ELF(a.elf, checksec=False)
3331
elf.write(offset, bytes)
3432
getattr(sys.stdout, 'buffer', sys.stdout).write(elf.get_data())
3533

pwnlib/commandline/pwnstrip.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def main(args):
2727
sys.stderr.write(p.format_usage())
2828
sys.exit(0)
2929

30-
elf = ELF(args.file.name)
30+
elf = ELF(args.file.name, checksec=True)
3131
context.clear(arch=elf.arch)
3232

3333
if args.build_id:

pwnlib/data/templates/pwnup.mako

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ argv[0] = os.path.basename(argv[0])
1313
1414
try:
1515
if binary:
16-
ctx.binary = ELF(binary, checksec=False)
16+
ctx.binary = ELF(binary, checksec=False) # XXX
1717
except ELFError:
1818
pass
1919

pwnlib/dynelf.py

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ def __init__(self, leak, pointer=None, elf=None, libcdb=True):
173173
path = elf.path
174174

175175
# Load a fresh copy of the ELF
176+
# why suppress log ?
176177
with context.local(log_level='error'):
177178
w = self.waitfor("Loading from %r" % path)
178179
self.elf = ELF(path)
@@ -573,6 +574,7 @@ def lookup (self, symb = None, lib = None):
573574
log.info("Trying lookup based on Build ID: %s", build_id)
574575
path = libcdb.search_by_build_id(build_id)
575576
if path:
577+
# why suppress log ?
576578
with context.local(log_level='error'):
577579
e = ELF(path)
578580
e.address = dynlib.libbase

pwnlib/elf/elf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class ELF(ELFFile):
212212
_fill_gaps = True
213213

214214

215-
def __init__(self, path, checksec=True):
215+
def __init__(self, path, checksec=False):
216216
# elftools uses the backing file for all reads and writes
217217
# in order to permit writing without being able to write to disk,
218218
# mmap() the file.

pwnlib/tubes/process.py

+24-28
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,7 @@ def __on_enoexec(self, exception):
476476
binfmt helpers installed for QEMU.
477477
"""
478478
# Get the ELF binary for the target executable
479-
with context.quiet:
480-
# XXX: Cyclic imports :(
481-
from pwnlib.elf import ELF
482-
binary = ELF(self.executable)
479+
binary = pwnlib.elf.ELF(self.executable)
483480

484481
# If we're on macOS, this will never work. Bail now.
485482
# if platform.mac_ver()[0]:
@@ -892,15 +889,15 @@ def maps(self):
892889
"""maps() -> [mapping]
893890
894891
Returns a list of process mappings.
895-
892+
896893
A mapping object has the following fields:
897894
addr, address (addr alias), start (addr alias), end, size, perms, path, rss, pss, shared_clean, shared_dirty, private_clean, private_dirty, referenced, anonymous, swap
898895
899896
perms is a permissions object, with the following fields:
900897
read, write, execute, private, shared, string
901898
902899
Example:
903-
900+
904901
>>> p = process(['cat'])
905902
>>> p.sendline(b"meow")
906903
>>> p.recvline()
@@ -937,16 +934,16 @@ def maps(self):
937934
pmmap_ext = namedtuple(
938935
'pmmap_ext', 'addr perms ' + ' '.join(pmmap_grouped._fields))
939936
940-
941-
Here is an example of a pmmap_ext entry:
937+
938+
Here is an example of a pmmap_ext entry:
942939
943940
.. code-block:: python
944941
945942
pmmap_ext(addr='15555551c000-155555520000', perms='r--p', path='[vvar]', rss=0, size=16384, pss=0, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=0, referenced=0, anonymous=0, swap=0)
946943
"""
947944

948945
permissions = namedtuple("permissions", "read write execute private shared string")
949-
mapping = namedtuple("mapping",
946+
mapping = namedtuple("mapping",
950947
"addr address start end size perms path rss pss shared_clean shared_dirty private_clean private_dirty referenced anonymous swap")
951948
# addr = address (alias) = start (alias)
952949

@@ -976,11 +973,11 @@ def get_mapping(self, path_value, single=True):
976973
single(bool=True): Whether to only return the first
977974
mapping matched, or all of them.
978975
979-
Returns found mapping(s) in process memory according to
976+
Returns found mapping(s) in process memory according to
980977
path_value.
981978
982979
Example:
983-
980+
984981
>>> p = process(['cat'])
985982
>>> mapping = p.get_mapping('[stack]')
986983
>>> mapping.path == '[stack]'
@@ -1039,7 +1036,7 @@ def stack_mapping(self, single=True):
10391036
10401037
"""
10411038
return self.get_mapping('[stack]', single)
1042-
1039+
10431040
def heap_mapping(self, single=True):
10441041
"""heap_mapping(single=True) -> mapping
10451042
heap_mapping(False) -> [mapping]
@@ -1071,7 +1068,7 @@ def heap_mapping(self, single=True):
10711068
10721069
"""
10731070
return self.get_mapping('[heap]', single)
1074-
1071+
10751072
def vdso_mapping(self, single=True):
10761073
"""vdso_mapping(single=True) -> mapping
10771074
vdso_mapping(False) -> [mapping]
@@ -1100,7 +1097,7 @@ def vdso_mapping(self, single=True):
11001097
11011098
"""
11021099
return self.get_mapping('[vdso]', single)
1103-
1100+
11041101
def vvar_mapping(self, single=True):
11051102
"""vvar_mapping(single=True) -> mapping
11061103
vvar_mapping(False) -> [mapping]
@@ -1129,7 +1126,7 @@ def vvar_mapping(self, single=True):
11291126
11301127
"""
11311128
return self.get_mapping('[vvar]', single)
1132-
1129+
11331130
def libc_mapping(self, single=True):
11341131
"""libc_mapping(single=True) -> mapping
11351132
libc_mapping(False) -> [mapping]
@@ -1139,7 +1136,7 @@ def libc_mapping(self, single=True):
11391136
mapping matched, or all of them.
11401137
11411138
Returns either the first libc mapping found in process memory,
1142-
or all libc mappings, depending on "single".
1139+
or all libc mappings, depending on "single".
11431140
11441141
Example:
11451142
@@ -1183,7 +1180,7 @@ def libc_mapping(self, single=True):
11831180
if 'libc.so' in lib_basename or ('libc-' in lib_basename and '.so' in lib_basename):
11841181
l_mappings.append(mapping)
11851182
return l_mappings
1186-
1183+
11871184
def musl_mapping(self, single=True):
11881185
"""musl_mapping(single=True) -> mapping
11891186
musl_mapping(False) -> [mapping]
@@ -1193,7 +1190,7 @@ def musl_mapping(self, single=True):
11931190
mapping matched, or all of them.
11941191
11951192
Returns either the first musl mapping found in process memory,
1196-
or all musl mappings, depending on "single".
1193+
or all musl mappings, depending on "single".
11971194
"""
11981195
all_maps = self.maps()
11991196

@@ -1203,14 +1200,14 @@ def musl_mapping(self, single=True):
12031200
if 'musl.so' in lib_basename or ('musl-' in lib_basename and '.so' in lib_basename):
12041201
return mapping
12051202
return None
1206-
1203+
12071204
m_mappings = []
12081205
for mapping in all_maps:
12091206
lib_basename = os.path.basename(mapping.path)
12101207
if 'musl.so' in lib_basename or ('musl-' in lib_basename and '.so' in lib_basename):
12111208
m_mappings.append(mapping)
12121209
return m_mappings
1213-
1210+
12141211
def elf_mapping(self, single=True):
12151212
"""elf_mapping(single=True) -> mapping
12161213
elf_mapping(False) -> [mapping]
@@ -1274,10 +1271,10 @@ def lib_size(self, path_value):
12741271

12751272
# Expecting this to be sorted
12761273
lib_mappings = self.get_mapping(path_value, single=False)
1277-
1274+
12781275
if len(lib_mappings) == 0:
12791276
return 0
1280-
1277+
12811278
is_contiguous = True
12821279
total_size = lib_mappings[0].size
12831280
for i in range(1, len(lib_mappings)):
@@ -1293,7 +1290,7 @@ def lib_size(self, path_value):
12931290

12941291
def address_mapping(self, address):
12951292
"""address_mapping(address) -> mapping
1296-
1293+
12971294
Returns the mapping at the specified address.
12981295
12991296
Example:
@@ -1358,7 +1355,7 @@ def libs(self):
13581355
return maps
13591356

13601357
@property
1361-
def libc(self):
1358+
def libc(self, checksec=False):
13621359
"""libc() -> ELF
13631360
13641361
Returns an ELF for the libc for the current process.
@@ -1378,7 +1375,7 @@ def libc(self):
13781375
for lib, address in self.libs().items():
13791376
lib_basename = os.path.basename(lib)
13801377
if 'libc.so' in lib_basename or ('libc-' in lib_basename and '.so' in lib_basename):
1381-
e = ELF(lib)
1378+
e = ELF(lib, checksec)
13821379
e.address = address
13831380
return e
13841381

@@ -1388,8 +1385,7 @@ def elf(self):
13881385
13891386
Returns an ELF file for the executable that launched the process.
13901387
"""
1391-
import pwnlib.elf.elf
1392-
return pwnlib.elf.elf.ELF(self.executable)
1388+
return pwnlib.elf.ELF(self.executable)
13931389

13941390
@property
13951391
def corefile(self):
@@ -1479,7 +1475,7 @@ def writemem(self, address, data):
14791475
data(bytes): Data to write to the address
14801476
14811477
Example:
1482-
1478+
14831479
Let's write data to the beginning of the mapped memory of the ELF.
14841480
14851481
>>> context.clear(arch='i386')

pwnlib/tubes/ssh.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def libc(self):
350350

351351
for lib, address in self.libs().items():
352352
if 'libc.so' in lib:
353-
e = ELF(lib)
353+
e = ELF(lib, checksec=True)
354354
e.address = address
355355
return e
356356

0 commit comments

Comments
 (0)