File: elfdiff.py

package info (click to toggle)
pwntools 4.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,508 kB
  • sloc: python: 59,870; ansic: 48,351; asm: 45,047; sh: 396; makefile: 256
file content (62 lines) | stat: -rw-r--r-- 1,373 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import absolute_import
from __future__ import division

import shutil
from argparse import ArgumentParser
from subprocess import CalledProcessError
from subprocess import check_output
from tempfile import NamedTemporaryFile

import pwnlib.args
pwnlib.args.free_form = False

from pwn import *
from pwnlib.commandline import common


def dump(objdump, path):
    n = NamedTemporaryFile(delete=False)
    o = check_output([objdump,'-d','-x','-s',path])
    n.write(o)
    n.flush()
    return n.name

def diff(a,b):
    try: return check_output(['diff',a,b], universal_newlines=True)
    except CalledProcessError as e:
        return e.output

p = common.parser_commands.add_parser(
    'elfdiff',
    help = 'Compare two ELF files',
    description = 'Compare two ELF files'
)

p.add_argument('a')
p.add_argument('b')

def main(a):
    with context.silent:
        x = ELF(a.a)
        y = ELF(a.b)

    if x.arch != y.arch:
        log.error("Architectures are not the same: %s vs %s" % (x.arch, y.arch))

    context.arch = x.arch

    objdump = pwnlib.asm.which_binutils('objdump')

    tmp = NamedTemporaryFile()
    name = tmp.name

    shutil.copy(x.path, name)
    x = dump(objdump, name)

    shutil.copy(y.path, name)
    y = dump(objdump, name)

    print(diff(x, y))

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