-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_crc32_forcer.py
More file actions
141 lines (106 loc) · 3.15 KB
/
python_crc32_forcer.py
File metadata and controls
141 lines (106 loc) · 3.15 KB
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
import sys, zlib, struct, os
programName = "Python CRC-32 Forcer"
version = "1.0"
author = "dreamer2908"
def crc32v2(fileName):
fd = open(fileName,"rb")
crc = 0
while True:
buffer = fd.read(1024 * 1024)
if len(buffer) == 0:
fd.close()
if sys.version_info[0] < 3 and crc < 0:
crc += 2 ** 32
return crc
crc = zlib.crc32(buffer, crc)
def crc32_i(fileName):
iHash = crc32v2(fileName)
if sys.version_info[0] < 3 and iHash < 0:
iHash += 2 ** 32
return iHash
def crc32_s(fileName):
iHash = crc32v2(fileName)
if sys.version_info[0] < 3 and iHash < 0:
iHash += 2 ** 32
sHash = '%08X' % iHash
return sHash
def itos(iHash):
return '%08X' % iHash
def stoi(sHash):
return int(sHash, base=16)
def calcNewContents(targetCRC, originalCRC):
CRCPOLY = 0xEDB88320
CRCINV = 0x5B358FD3
INITXOR = 0xFFFFFFFF
FINALXOR = 0xFFFFFFFF
targetCRC ^= FINALXOR
originalCRC ^= FINALXOR
newContents = 0x00000000
for i in range(0, 32):
# reduce modulo CRCPOLY
if (newContents & 1) != 0:
newContents = (newContents >> 1) ^ CRCPOLY
else:
newContents >>= 1
# add CRCINV if corresponding bit of operand is set
if (targetCRC & 1) != 0:
newContents ^= CRCINV
targetCRC >>= 1
# finally add old crc
newContents ^= originalCRC
return newContents
def appendContents(fileName, number):
fd = open(fileName, 'ab')
# http://stackoverflow.com/questions/20621829/appending-data-bytes-to-binary-file-with-python
#ar = number.to_bytes(4, byteorder='little', signed=False)
ar = struct.pack("<I", number)
fd.write(ar)
fd.close()
def changeCRC(fileName, targetCRC):
originalCRC = crc32_i(fileName)
newContents = calcNewContents(targetCRC, originalCRC)
appendContents(fileName, newContents)
def printReadme():
# Print user manual
print("%s v%s by %s\n" % (programName, version, author))
print("Syntax: python python_crc32_forcer.py <filename> <target CRC-32>")
print("Examples:")
print(' python python_crc32_forcer.py \"D:\\Code\\FakeCRC32\\Video.mp4\" 1234ABCD')
def main():
if len(sys.argv) < 3:
printReadme()
sys.exit(-1)
fileName = sys.argv[1]
targetCRCs = sys.argv[2]
validTargetCRC = True
crc_max = int("FFFFFFFF", base=16)
crc_min = int("00000000", base=16)
try:
targetCRC = int(targetCRCs, base=16)
if targetCRC > crc_max or targetCRC < crc_min:
validTargetCRC = False
except:
validTargetCRC = False
if validTargetCRC:
if os.path.isfile(fileName):
originalCRC = crc32_i(fileName)
targetCRC = int(targetCRCs, base=16)
print('Original CRC-32: %s' % itos(originalCRC))
print('Target CRC-32: %s' % itos(targetCRC))
newContents = calcNewContents(targetCRC, originalCRC)
print('Four bytes to append (hex): %s' % itos(newContents))
appendContents(fileName, newContents)
finalCRC = crc32_i(fileName)
print('Final CRC-32: %s' % itos(finalCRC))
if finalCRC == targetCRC:
print('Done.')
else:
print('Failed.')
else:
print("Input file inaccessible.")
else:
# print("Invalid target CRC-32.")
printReadme()
sys.exit(-1)
if __name__== "__main__":
main()