summaryrefslogtreecommitdiff
path: root/python/diffing_elf.py
blob: ccaaaa16f26659de2834a285086f1dcfc307d3e3 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import argparse
import sys
import pychrysalide
from pychrysalide.analysis.contents import FileContent
from pychrysalide.analysis import LoadedBinary
from pychrysalide.analysis.diffing import BinDiff, FingerprintList
from pychrysalide.format.elf import ElfFormat


def display_matches(matches, methods):
    """Show the matching routines."""

    routine_a_name_len = len('Routine A')
    routine_a_addr_len = len('Virt addr.')
    routine_b_name_len = len('Routine B')
    routine_b_addr_len = len('Virt addr.')

    # Length computing

    for match in matches:

        routine_name = match['routine'].label

        if len(routine_name) > routine_a_name_len:
            routine_a_name_len = len(routine_name)

        virt = '0x%x' % match['routine'].range.addr.virt

        if len(virt) > routine_a_addr_len:
            routine_a_addr_len = len(virt)

        matching = match['matching']

        for i in range(len(matching)):

            routine_name = matching[i]['other'].label

            if len(routine_name) > routine_b_name_len:
                routine_b_name_len = len(routine_name)

            virt = '0x%x' % matching[i]['other'].range.addr.virt

            if len(virt) > routine_b_addr_len:
                routine_b_addr_len = len(virt)

    line = '  {:<%u} | {:>%u} | {:<%u} | {:>%u} | {:^%u}' \
           % (routine_a_name_len, routine_a_addr_len, routine_b_name_len, routine_b_addr_len, len('Similarity'))

    for meth in methods:

        length = len(meth.name)

        line += ' | {:^%u}' % length

    # Columns display

    names = []

    for meth in methods:
        names.append(meth.name)

    header = line.format('Routine A', 'Virt addr.', 'Routine B', 'Virt addr.', 'Similarity', *names)

    print(header)

    print('-' * (len(header) + 2))

    # Matches display

    for match in matches:

        matching = match['matching']

        for i in range(len(matching)):

            routine_a_name = match['routine'].label if i == 0 else ''

            if i == 0:
                virt_a = '0x%x' % match['routine'].range.addr.virt
            else:
                virt_a = ''

            routine_b_name = matching[i]['other'].label

            if i == 0:
                virt_b = '0x%x' % matching[i]['other'].range.addr.virt
            else:
                virt_b = ''

            similarity = '%.2f' % matching[i]['similarity']

            values = [ 'X' if v else '' for v in list(matching[i]['values']) ]

            print(line.format(routine_a_name, virt_a, routine_b_name, virt_b, similarity, *values))


def display_unmatches(routines):
    """Show the unmatching routines."""

    name_len = len('Routine')
    addr_len = len('Virt addr.')

    # Length computing

    for r in routines:

        name = r.label

        if len(name) > name_len:
            name_len = len(name)

        virt = '0x%x' % r.range.addr.virt

        if len(virt) > addr_len:
            addr_len = len(virt)

    line = '  {:<%u} | {:>%u}' % (name_len, addr_len)

    # Columns display

    header = line.format('Routine', 'Virt addr.')

    print(header)

    print('-' * (len(header) + 2))

    # Matches display

    for r in routines:

        virt = '0x%x' % r.range.addr.virt

        print(line.format(r.label, virt))


if __name__ == '__main__':
    """Entry point."""

    parser = argparse.ArgumentParser(description='readelf - Displays information about ELF files.', add_help=False)

    parser.add_argument('-h', '--help', action='store_true', help='Display the command line options.')

    parser.add_argument('elffile1', type=str, help='The first object file to be examined')
    parser.add_argument('elffile2', type=str, help='The second object file to be examined')

    args = parser.parse_args()

    if args.help:
        parser.print_help()
        sys.exit(1)

    cnt = FileContent(args.elffile1)
    fmt = ElfFormat(cnt)
    binary1 = LoadedBinary(fmt)

    binary1.analyze_and_wait()

    cnt = FileContent(args.elffile2)
    fmt = ElfFormat(cnt)
    binary2 = LoadedBinary(fmt)

    binary2.analyze_and_wait()

    fingerlist1 = FingerprintList(None, binary1, True)
    fingerlist2 = FingerprintList(None, binary2, True)

    diff = BinDiff(fingerlist1, fingerlist2)

    diff.analyze_and_wait()

    print()

    matches = diff.single_matches

    if len(matches) > 0:

        print('Single match routines')
        print('=====================')
        print()

        display_matches(matches, fingerlist1.methods)

        print()

    matches = diff.multi_matches

    if len(matches) > 0:

        print('Multi match routines')
        print('====================')
        print()

        display_matches(matches, fingerlist1.methods)

        print()

    unmatched = diff.first_unmatches

    if len(unmatched) > 0:

        print('First binary unmatched routines')
        print('===============================')
        print()

        display_unmatches(unmatched)

        print()

    unmatched = diff.second_unmatches

    if len(unmatched) > 0:

        print('Second binary unmatched routines')
        print('================================')
        print()

        display_unmatches(unmatched)

        print()