-
Notifications
You must be signed in to change notification settings - Fork 1
/
coelioxys_vaccine.py
executable file
·214 lines (176 loc) · 6.32 KB
/
coelioxys_vaccine.py
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
#!/usr/bin/env python
# -*- coding: latin-1 -*-
################################### Coelioxys #####################################
#### Copyright (C) 2016 Fernando Vañó García #
#### #
#### This program is free software: you can redistribute it and/or modify #
#### it under the terms of the GNU Affero General Public License as #
#### published by the Free Software Foundation, either version 3 of the #
#### License, or (at your option) any later version. #
#### #
#### This program is distributed in the hope that it will be useful, #
#### but WITHOUT ANY WARRANTY; without even the implied warranty of #
#### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
#### GNU Affero General Public License for more details. #
#### #
#### You should have received a copy of the GNU Affero General Public License #
#### along with this program. If not, see <http://www.gnu.org/licenses/>. #
#### #
#### Fernando Vañó García <fernando@fervagar.com> #
#### #
###################################################################################
from __future__ import print_function;
import traceback;
from argparse import ArgumentParser;
from sys import argv, stdout, stderr;
from os.path import isfile;
import struct;
def inform(*msg):
print("[+]", *msg, file=stdout);
def error(*error):
print("[-]", *error, file=stderr);
def checkELF(data):
magic = struct.unpack('<L', data[0x00:0x04])[0];
if magic != 0x464c457f:
return False;
arch = struct.unpack('<H', data[0x12:0x14])[0];
if arch != 0x03:
return False;
return True;
def askUser():
reply = str(raw_input('Are you sure you want to continue...? [y/N]: ')).lower().strip();
if len(reply) > 0 and reply[0] == 'y':
return True;
else:
return False;
def readContents(filename):
with open(filename, 'rb') as f:
data = f.read();
return data;
def writeContents(filename, data):
with open(filename, 'wb') as f:
f.write(data);
def getLoadSegments(pht, esize, nsegm):
hasNoteSegment = False;
segmentsList = [];
sgmOffset = 0;
PT_LOAD = 1;
PT_NOTE = 4;
for i in range(0, nsegm):
idx = (i * esize);
stype = struct.unpack('<L', pht[idx : idx+4])[0];
if stype == PT_LOAD:
## Look if the segment has RWE attributes
flags = struct.unpack('<L', pht[idx+4*6 : idx+4*7])[0]
if flags == 0x7:
segmentsList.append(pht[idx : idx+4*8]);
sgmOffset = idx;
elif stype == PT_NOTE:
hasNoteSegment = True;
return segmentsList, sgmOffset, hasNoteSegment;
def fixup(segment, elf, dataPtr, offset):
coelioxys_size = 0x29c;
restored = [];
for i in range(0, 0x18):
restored.append(dataPtr[i]);
## Retrieve the original Entry Point ##
ep_offset = 0x182;
##orig_ep = hex(struct.unpack('<L', segment[ep_offset:(ep_offset+4)])[0])[2:];
try:
j = 0;
for i in range(0x18, 0x1c):
restored.append(segment[ep_offset+j]);
j += 1;
for i in range(0x1c, offset):
restored.append(dataPtr[i]);
except:
return False;
## Clear the segment header
for i in range(0, 4*8):
restored.append('\x00');
for i in range((offset + 4*8), len(dataPtr)-coelioxys_size):
restored.append(dataPtr[i]);
## Delete the padding
item = restored[-1];
while item == '\x00':
item = restored.pop();
restored.append(item);
restored.append('\00');
data = ''.join(chr(struct.unpack('<B', x)[0]) for x in restored);
writeContents(elf, data);
return True;
def _treat(elf, usermode):
coelioxys_EP = 0x70000000;
ret = False;
if not isfile(elf):
return -1;
contents = readContents(elf);
if len(contents) < 0x34 or not checkELF(contents):
return -2;
entryPoint = struct.unpack('<L', contents[0x18:0x1c])[0];
## Search the second LOAD Segment ##
phoff = struct.unpack('<L', contents[0x1c:0x20])[0];
phentsize = struct.unpack('<H', contents[0x2a:0x2c])[0];
phnum = struct.unpack('<H', contents[0x2c:0x2e])[0];
phtsize = phnum * phentsize;
## Get the Program Header Table ##
pht = contents[phoff:(phoff + phtsize)]; ## Program Header Table
try:
segmentsList, sgmOffset, hasNoteSegment = getLoadSegments(pht, phentsize, phnum);
length = len(segmentsList);
if length == 0:
return 1;
elif length > 1:
return -3;
else:
segment = segmentsList[0];
segm_offset = struct.unpack('<L', segment[4*1 : 4*2])[0];
segm_size = struct.unpack('<L', segment[4*4 : 4*5])[0];
segment = contents[segm_offset : (segm_offset+segm_size)];
if hasNoteSegment and (entryPoint != coelioxys_EP):
return 2;
elif hasNoteSegment:
if usermode:
inform("This ELF has a NOTE segment...");
## Ask the user to continue...
if askUser():
ret = fixup(segment, elf, contents, (phoff+sgmOffset));
else:
ret = fixup(segment, elf, contents, (phoff+sgmOffset));
else:
if usermode and (entryPoint != coelioxys_EP):
inform("Seems like this ELF is not infected by Coelioxys...");
if askUser():
ret = fixup(segment, elf, contents, (phoff+sgmOffset));
else:
ret = fixup(segment, elf, contents, (phoff+sgmOffset));
if ret:
return 0;
else:
return 2;
except:
None;
##traceback.print_exc();
## For external imports ##
def ApplyCoelioxysVaccine(filename):
_treat(filename, False);
def main():
parser = ArgumentParser(description='This is the Vaccine for the Coelioxys virus.\nIt can be used directly or as a imported library.\nIn the second case, use the function ApplyCoelioxysVaccine(filename). Fernando Vanyo <fernando@fervagar.com>');
parser.add_argument('<infected file>', type=str, help='ELF binary file infected by Coelioxys')
args = vars(parser.parse_args());
filename = args['<infected file>'];
ret = _treat(filename, True);
if ret == -3:
error("This tool cannot fix this ELF... Sorry.");
elif ret == -2:
error("Bad file: %s" % filename);
elif ret == -1:
error("'%s' is not a file!" % filename);
elif ret == 0:
inform("The recovery of the file '%s' has been successful" % filename);
elif ret == 1:
inform("The ELF is free of Coelioxys");
else:
inform("The provided binary is not infected");
if __name__ == '__main__':
main();