forked from Th4nat0s/Chall_Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxor.py
executable file
·85 lines (71 loc) · 1.99 KB
/
xor.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
#!/usr/bin/python
import pefile
import sys, os
# Xor a file with byte key.
# v 0.1
# Need https://code.google.com/p/pefile et on lui doit TOUT
#
# Extract PEFile from "Dump"
#
# Copyleft Thanat0s
# http://Thanat0s.trollprod.org
#
# Licence GNU GPL
def getint(thestr):
try:
if "x" in thestr.lower(): # hex
value = int(thestr.lower().split('x')[1], 16)
if "-" in thestr.lower(): # Negatif
value = 0-value
else:
value = int (thestr)
except:
print "ERROR Key in invalid format"
sys.exit(1)
return (value)
# Needs two arg if not... help
if len(sys.argv) <= 2:
print 'Xor a file with Byte key'
print 'To Use: '+ sys.argv[0]+ ' filename xorkey [inc|dec|number] [start]'
print ' xorkey could be in hex(0x41) or decimal (101)'
print ' if inc|dec the xor key will be (inc|dec) on each byte, it could be also a 8bit signed value'
print ' if [start] start the xor key increment at this value (hex or decimal)'
sys.exit(1)
FILENAME = sys.argv[1]
# Test if file exists
if not os.path.isfile(FILENAME):
print 'ERROR: File not found'
sys.exit(1)
INC = 0
# Get arg 3
if len(sys.argv) >= 3+1:
if sys.argv[3] == "inc".lower():
INC = 1
elif sys.argv[3] == "dec".lower():
INC = -1
else:
INC = getint(sys.argv[3]) % 256
# Get xor key
KEY = getint(sys.argv[2])
BASE = 0
# Get increment value
if len(sys.argv) == 4+1:
BASE = getint(sys.argv[4])
print "data, key, inc, base, result"
LINE=0
with open(FILENAME, 'rb') as f:
filearray = bytearray(f.read())
for I in range (0, len(filearray)):
BCK = int( filearray[I])
filearray[I] = (filearray[I] ^ KEY) % 256
#KEY = (KEY + (INC + BASE)%256 ) % 256
LINE =LINE+1
if LINE < 10:
print (int(BCK), KEY, INC, BASE, int( filearray[I]) )
if LINE == 11:
print "... "
KEY = (KEY + INC + BASE ) % 256
BASE = 0
print ('writing output to %s.xor' % FILENAME)
with open(('%s.xor' % FILENAME), 'w') as outfile:
outfile.write(filearray)