Форум сайта python.su
0
Добрый день.
Вирус порезал все файлы, если посмотреть в HEX редакторе получается таблица в плохом файле
08 0f 08 0f 47 70 6c 69 61 6c 6b 62 25 66 6a
в хорошем
0d 0a 0d 0a 42 75 69 6c 64 69 6e 67 20 63 6f
т.е. вирус тупо по таблице
оригинал=вирус
0=5
1=4
2=7
3=6
4=1
5=0
6=3
7=2
8=d
9=c
a=f
b=e
c=9
d=8
e=b
f=a
преобразовал вторую цифру в каждой ячейке (именно вторую, первая неизменна как видите)
Как можно на питоне зная это соответствие (оно во всех файлах одинаково) перегнать обратно?
Получается пока вот что:
# -*- coding: utf-8 -*- import stat, sys, os, string, commands import fnmatch import os kollichestvo=0 dir = 'c:\\111\\' for root, dirs, files in os.walk(dir): # пройти по директории рекурсивно for name in files: if fnmatch.fnmatch(name, '*.police'): fullname = os.path.join(root, name) # получаем полное имя файла print fullname # делаем что-нибудь с ним bfile = open(fullname, 'rb') filedata = bfile.read() bytelist = [] for bfile in filedata: bytelist.append(bfile) #bfile.close() print filedata print '\n' print bytelist print '\n' kollichestvo+=1 print kollichestvo print "Press any key"
Отредактировано ffldove (Сен. 20, 2012 16:38:08)
Офлайн
173
# -*- coding: utf-8 -*- _virus_table = { 0x0: 0x5, 0x1: 0x4, 0x2: 0x7, 0x3: 0x6, 0x4: 0x1, 0x5: 0x0, 0x6: 0x3, 0x7: 0x2, 0x8: 0xd, 0x9: 0xc, 0xa: 0xf, 0xb: 0xe, 0xc: 0x9, 0xd: 0x8, 0xe: 0xb, 0xf: 0xa, } def fix_data(data): result = [] for c in data: byte = ord(c) lb = byte & 0x0F fixed = (byte & 0xF0) | _virus_table.get(lb, lb) result.append(fixed) return ''.join(map(chr, result)) # test it print repr(fix_data("\x08\x0f\x08\x0f\x47\x70\x6c\x69\x61\x6c\x6b\x62\x25\x66\x6a")) # '\r\n\r\nBuilding co'
Отредактировано reclosedev (Сен. 20, 2012 17:37:00)
Офлайн
14
^ 5
Офлайн
173
odnochlen^ 5
точно.import string def generate_tables(): orig = range(0xff) new = [(byte ^ 0x05) for byte in orig] return ''.join(map(chr, orig)), ''.join(map(chr, new)) _trans_table = string.maketrans(*generate_tables()) def fix_data(data): return data.translate(_trans_table)
Отредактировано reclosedev (Сен. 20, 2012 18:01:00)
Офлайн
14
КриптоанализЕще в лицее ломал шифрование паролей на шары windows 95, там был ксор. Только я его так и не нашел, т.к. не учел кодировки. В результате просто сделал таблицы для символов в каждой позиции.
Офлайн
857
#!/usr/bin/env python3 with open('file.txt', 'rb') as fin, \ open('output.txt', 'wb') as fout: fout.write(bytes(b ^ 5 for b in fin.read()))
[guest@localhost py]$ .hex file.txt
00000000 08 0f 08 0f 47 70 6c 69 61 6c 6b 62 25 66 6a |....Gplialkb%fj|
0000000f
[guest@localhost py]$ ./t.py
[guest@localhost py]$ .hex output.txt
00000000 0d 0a 0d 0a 42 75 69 6c 64 69 6e 67 20 63 6f |....Building co|
0000000f
[guest@localhost py]$
Отредактировано py.user.next (Сен. 21, 2012 04:17:54)
Офлайн
0
py.user.nextЧто то ругается на запятую.#!/usr/bin/env python3 with open('file.txt', 'rb') as fin, \ open('output.txt', 'wb') as fout: fout.write(bytes(b ^ 5 for b in fin.read()))[guest@localhost py]$ .hex file.txt
00000000 08 0f 08 0f 47 70 6c 69 61 6c 6b 62 25 66 6a |....Gplialkb%fj|
0000000f
[guest@localhost py]$ ./t.py
[guest@localhost py]$ .hex output.txt
00000000 0d 0a 0d 0a 42 75 69 6c 64 69 6e 67 20 63 6f |....Building co|
0000000f
[guest@localhost py]$
File "antipolice_1.py", line 13 with open(fullname, 'rb') as fin, open(fullname+'.tmp', 'wb') as fout: ^ SyntaxError: invalid syntax
Отредактировано ffldove (Сен. 21, 2012 08:36:35)
Офлайн
0
reclosedevПереоценил свои знания в питон, а как считать и потом записать эти строчки в файл в 16ом виде?odnochlen^ 5точно.
Тогда вот это побыстрее должно работать:import string def generate_tables(): orig = range(0xff) new = [(byte ^ 0x05) for byte in orig] return ''.join(map(chr, orig)), ''.join(map(chr, new)) _trans_table = string.maketrans(*generate_tables()) def fix_data(data): return data.translate(_trans_table)
Отредактировано ffldove (Сен. 21, 2012 09:06:51)
Офлайн
857
ffldoveэто для третьего питона код
Что то ругается на запятую.
Офлайн
173
ffldoveЕсли файлы небольшие и их можно прочитать в память, то
Переоценил свои знания в питон, а как считать и потом записать эти строчки в файл в 16ом виде?
with open('file.txt', 'rb') as fin: with open('output.txt', 'wb') as fout: fout.write(fix_data(fin.read()))
Офлайн