Exemplo n.º 1
0
def test_elf_parse_file_open():
    b = six.BytesIO()

    header_fmt = '4sBBBBB7sHHIIIIIHHHHHH'
    header_fmt_size = pwny.pack_size(header_fmt)

    b.write(pwny.pack(
        header_fmt,
        b'\x7fELF',
        1,
        1,
        1,
        pwny.ELF.OSABI.linux.value,
        0,
        b'\x00' * 7,
        pwny.ELF.Type.executable.value,
        pwny.ELF.Machine.i386.value,
        1,
        0,
        0,
        header_fmt_size,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
    ))

    b.seek(0)

    with mock.patch('pwnypack.elf.open', create=True) as mock_open:
        mock_open.return_value = b
        pwny.ELF('test.elf')
Exemplo n.º 2
0
def test_elf_parse_file_open():
    b = six.BytesIO()

    header_fmt = '4sBBBBB7sHHIIIIIHHHHHH'
    header_fmt_size = pwny.pack_size(header_fmt)

    b.write(pwny.pack(
        header_fmt,
        b'\x7fELF',
        1,
        1,
        1,
        pwny.ELF.OSABI.linux.value,
        0,
        b'\x00' * 7,
        pwny.ELF.Type.executable.value,
        pwny.ELF.Machine.i386.value,
        1,
        0,
        0,
        header_fmt_size,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
    ))

    b.seek(0)

    with mock.patch('pwnypack.elf.open', create=True) as mock_open:
        mock_open.return_value = b
        pwny.ELF('test.elf')
Exemplo n.º 3
0
def test_elf_parse_section_invalid_type():
    section_fmt = 'IIIIIIIIII'
    section_fmt_size = pwny.pack_size(section_fmt)
    section = pwny.pack(
        section_fmt,
        1,
        0x5fffffff,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        section_fmt_size,
    )
    elf = pwny.ELF()
    elf.bits = 32
    section = elf.SectionHeader(elf, section)
    assert section.type == pwny.ELF.SectionHeader.Type.unknown
Exemplo n.º 4
0
def test_elf_parse_section_invalid_type():
    section_fmt = 'IIIIIIIIII'
    section_fmt_size = pwny.pack_size(section_fmt)
    section = pwny.pack(
        section_fmt,
        1,
        0x5fffffff,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        section_fmt_size,
    )
    elf = pwny.ELF()
    elf.bits = 32
    section = elf.SectionHeader(elf, section)
    assert section.type == pwny.ELF.SectionHeader.Type.unknown
Exemplo n.º 5
0
def test_pack():
    assert pwny.pack('I', 0x41424344) == b'DCBA'
Exemplo n.º 6
0
def test_pack_invalid_endian():
    pwny.pack('I', 1, endian='invalid')
Exemplo n.º 7
0
def test_pack_explicit_target():
    assert pwny.pack('I', 0x41424344, target=target_big_endian) == b'ABCD'
Exemplo n.º 8
0
def test_pack_explicit_endian():
    assert pwny.pack('I', 0x41424344, endian=pwny.Target.Endian.big) == b'ABCD'
Exemplo n.º 9
0
def test_pack_format_with_endian():
    assert pwny.pack('>I', 0x41424344) == b'ABCD'
Exemplo n.º 10
0
def test_elf_parse_file_64():
    b = six.BytesIO()

    header_fmt = '4sBBBBB7sHHIQQQIHHHHHH'
    header_fmt_size = pwny.pack_size(header_fmt)

    section_fmt = 'IIQQQQIIQQ'
    section_fmt_size = pwny.pack_size(section_fmt)

    strings_section = b'\x00strings\x00'

    b.write(pwny.pack(
        header_fmt,
        b'\x7fELF',
        2,
        1,
        1,
        pwny.ELF.OSABI.linux.value,
        0,
        b'\x00' * 7,
        pwny.ELF.Type.executable.value,
        pwny.ELF.Machine.x86_64.value,
        1,
        0,
        0,
        header_fmt_size,
        0,
        header_fmt_size,
        0,
        0,
        section_fmt_size,
        1,
        0,
    ))

    b.write(pwny.pack(
        section_fmt,
        1,
        pwny.ELF.SectionHeader.Type.null.value,
        0,
        0,
        header_fmt_size + section_fmt_size,
        len(strings_section),
        0,
        0,
        0,
        section_fmt_size,
    ))

    b.write(strings_section)
    b.seek(0)

    elf = pwny.ELF(b)

    for key, value in {
        'arch': pwny.Target.Arch.x86,
        'bits': 64,
        'endian': pwny.Target.Endian.little,
        'abi_version': 0,
        'entry': 0,
        'flags': 0,
        'hsize': header_fmt_size,
        'osabi': pwny.ELF.OSABI.linux,
        'phentsize': 0,
        'phnum': 0,
        'phoff': 0,
        'shentsize': section_fmt_size,
        'shnum': 1,
        'shoff': header_fmt_size,
        'shstrndx': 0,
        'type': pwny.ELF.Type.executable,
    }.items():
        assert getattr(elf, key) == value, '%s != %r' % (key, value)

    assert len(elf.section_headers) == 1
Exemplo n.º 11
0
def test_elf_parse_file_64():
    b = six.BytesIO()

    header_fmt = '4sBBBBB7sHHIQQQIHHHHHH'
    header_fmt_size = pwny.pack_size(header_fmt)

    section_fmt = 'IIQQQQIIQQ'
    section_fmt_size = pwny.pack_size(section_fmt)

    strings_section = b'\x00strings\x00'

    b.write(pwny.pack(
        header_fmt,
        b'\x7fELF',
        2,
        1,
        1,
        pwny.ELF.OSABI.linux.value,
        0,
        b'\x00' * 7,
        pwny.ELF.Type.executable.value,
        pwny.ELF.Machine.x86_64.value,
        1,
        0,
        0,
        header_fmt_size,
        0,
        header_fmt_size,
        0,
        0,
        section_fmt_size,
        1,
        0,
    ))

    b.write(pwny.pack(
        section_fmt,
        1,
        pwny.ELF.SectionHeader.Type.null.value,
        0,
        0,
        header_fmt_size + section_fmt_size,
        len(strings_section),
        0,
        0,
        0,
        section_fmt_size,
    ))

    b.write(strings_section)
    b.seek(0)

    elf = pwny.ELF(b)

    for key, value in {
        'arch': pwny.Target.Arch.x86,
        'bits': 64,
        'endian': pwny.Target.Endian.little,
        'abi_version': 0,
        'entry': 0,
        'flags': 0,
        'hsize': header_fmt_size,
        'osabi': pwny.ELF.OSABI.linux,
        'phentsize': 0,
        'phnum': 0,
        'phoff': 0,
        'shentsize': section_fmt_size,
        'shnum': 1,
        'shoff': header_fmt_size,
        'shstrndx': 0,
        'type': pwny.ELF.Type.executable,
    }.items():
        assert getattr(elf, key) == value, '%s != %r' % (key, value)

    assert len(elf.section_headers) == 1
Exemplo n.º 12
0
def test_pack():
    assert pwny.pack('I', 0x41424344) == b'DCBA'
Exemplo n.º 13
0
def test_pack_invalid_endian():
    pwny.pack('I', 1, endian='invalid')
Exemplo n.º 14
0
def test_pack_explicit_target():
    assert pwny.pack('I', 0x41424344, target=target_big_endian) == b'ABCD'
Exemplo n.º 15
0
def test_pack_explicit_endian():
    assert pwny.pack('I', 0x41424344, endian=pwny.Target.Endian.big) == b'ABCD'
Exemplo n.º 16
0
def test_pack_format_with_endian():
    assert pwny.pack('>I', 0x41424344) == b'ABCD'