Skip to content

Commit

Permalink
Update testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
ptr-yudai committed Aug 17, 2024
1 parent a36dc4e commit 34610f3
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions ptrlib/filestruct/elf/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def _offset_symbol(self, name: Union[str, bytes]) -> Optional[int]:
continue

if sym_name == name:
# TODO: Support searching for multiple identical symbols
return symtab['st_value']

return None
Expand Down
2 changes: 2 additions & 0 deletions tests/filestruct/elf/test_elf_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ def test_symbol(self):
self.assertEqual(self.elf.symbol('__libc_system'), 0x4f440)
self.assertEqual(self.elf.symbol('_IO_2_1_stdout_'), 0x3ec760)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), 0x3ed8c0)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), 0x3ed8c0) # test cache

self.elf.base = BASE
self.assertEqual(self.elf.symbol('system'), BASE + 0x4f440)
self.assertEqual(self.elf.symbol('system'), BASE + 0x4f440)
self.assertEqual(self.elf.symbol('getpwnam'), BASE + 0xe3260)
self.assertEqual(self.elf.symbol('_IO_2_1_stdout_'), BASE + 0x3ec760)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), BASE + 0x3ed8c0)

def test_search(self):
self.elf.base = 0
Expand Down
58 changes: 58 additions & 0 deletions tests/filestruct/elf/test_elf_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import unittest
import os
from ptrlib.filestruct.elf import ELF
from logging import getLogger, FATAL


PATH_ELF = "./tests/test.bin/libc-2.35.i386.so"
BASE = 0x56555000

class TestELF10(unittest.TestCase):
def setUp(self):
getLogger("ptrlib").setLevel(FATAL)
self.elf = ELF(PATH_ELF)

def test_symbol(self):
self.elf.base = 0
self.assertEqual(self.elf.symbol('system'), 0x50430)
self.assertEqual(self.elf.symbol('system'), 0x50430) # test cache
self.assertEqual(self.elf.symbol('__libc_system'), 0x50430)
self.assertEqual(self.elf.symbol('_IO_2_1_stdout_'), 0x231d40)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), 0x2328a0)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), 0x2328a0) # test cache

self.elf.base = BASE
self.assertEqual(self.elf.symbol('system'), BASE + 0x50430)
self.assertEqual(self.elf.symbol('system'), BASE + 0x50430)
self.assertEqual(self.elf.symbol('__libc_system'), BASE + 0x50430)
self.assertEqual(self.elf.symbol('_IO_2_1_stdout_'), BASE + 0x231d40)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), BASE + 0x2328a0)

def test_search(self):
self.elf.base = 0
it = self.elf.search('A')
self.assertEqual(next(it), 0x42c)
self.assertEqual(next(it), 0x80c)
self.assertEqual(next(it), 0xda0)
self.assertEqual(next(self.elf.search('A', writable=True)), 0x22fb8c)
self.assertEqual(next(self.elf.find(b'/bin/sh\0')), 0x1c4de8)

self.elf.base = BASE
it = self.elf.search('A')
self.assertEqual(next(it), BASE + 0x42c)
self.assertEqual(next(it), BASE + 0x80c)
self.assertEqual(next(it), BASE + 0xda0)
self.assertEqual(next(self.elf.search('A', writable=True)), BASE + 0x22fb8c)
self.assertEqual(next(self.elf.find(b'/bin/sh\0')), BASE + 0x1c4de8)

def test_main_arena(self):
self.elf.base = 0
self.assertEqual(self.elf.main_arena(), 0x231760)
self.elf.base = BASE
self.assertEqual(self.elf.main_arena(), BASE + 0x231760)

def test_security(self):
self.assertEqual(self.elf.relro(), 2)
self.assertEqual(self.elf.ssp(), True)
self.assertEqual(self.elf.nx(), True)
self.assertEqual(self.elf.pie(), True)
2 changes: 2 additions & 0 deletions tests/filestruct/elf/test_elf_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ def test_symbol(self):
self.assertEqual(self.elf.symbol('__libc_system'), 0x522c0)
self.assertEqual(self.elf.symbol('_IO_2_1_stdout_'), 0x1ed6a0)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), 0x1ee7e0)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), 0x1ee7e0) # test cache

self.elf.base = BASE
self.assertEqual(self.elf.symbol('system'), BASE + 0x522c0)
self.assertEqual(self.elf.symbol('system'), BASE + 0x522c0)
self.assertEqual(self.elf.symbol('__libc_system'), BASE + 0x522c0)
self.assertEqual(self.elf.symbol('_IO_2_1_stdout_'), BASE + 0x1ed6a0)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), BASE + 0x1ee7e0)

def test_search(self):
self.elf.base = 0
Expand Down
2 changes: 2 additions & 0 deletions tests/filestruct/elf/test_elf_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ def test_symbol(self):
self.assertEqual(self.elf.symbol('__libc_system'), 0x54ae0)
self.assertEqual(self.elf.symbol('_IO_2_1_stdout_'), 0x219760)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), 0x21b730)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), 0x21b730) # test cache

self.elf.base = BASE
self.assertEqual(self.elf.symbol('system'), BASE + 0x54ae0)
self.assertEqual(self.elf.symbol('system'), BASE + 0x54ae0)
self.assertEqual(self.elf.symbol('__libc_system'), BASE + 0x54ae0)
self.assertEqual(self.elf.symbol('_IO_2_1_stdout_'), BASE + 0x219760)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), BASE + 0x21b730)

def test_search(self):
self.elf.base = 0
Expand Down
2 changes: 2 additions & 0 deletions tests/filestruct/elf/test_elf_9.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ def test_symbol(self):
self.assertEqual(self.elf.symbol('__libc_system'), 0x50d70)
self.assertEqual(self.elf.symbol('_IO_2_1_stdout_'), 0x21b780)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), 0x21ca70)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), 0x21ca70) # test cache

self.elf.base = BASE
self.assertEqual(self.elf.symbol('system'), BASE + 0x50d70)
self.assertEqual(self.elf.symbol('system'), BASE + 0x50d70)
self.assertEqual(self.elf.symbol('__libc_system'), BASE + 0x50d70)
self.assertEqual(self.elf.symbol('_IO_2_1_stdout_'), BASE + 0x21b780)
self.assertEqual(self.elf.symbol('_IO_stdfile_1_lock'), BASE + 0x21ca70)

def test_search(self):
self.elf.base = 0
Expand Down
Binary file not shown.
Binary file added tests/test.bin/libc-2.35.i386.so
Binary file not shown.

0 comments on commit 34610f3

Please sign in to comment.