Last active
November 16, 2015 11:05
-
-
Save LCBH/847686465955cf09f41e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""" Hides private data from Kresus logs. | |
Usage: $python HideLog.py log_input.json log_output.json. """ | |
from sys import argv | |
import json | |
from Crypto.Cipher import AES | |
# -- LABELS | |
BA = "bankAccount" | |
TL = "title" | |
RW = "raw" | |
AM = "amount" | |
AN = "accountNumber" | |
INA = "initialAmount" | |
LG = "login" | |
MAX_INT = 1000000000 # avoid possible overflow in Kresus | |
# -- CRYPTO | |
# this is BAD crypto (cst. IV, ECB instead of CBC, etc. | |
# but we need deterministic encryption) | |
# ================================================== | |
# MODIFY THIS | |
key = '123456789abcdefg' | |
# ================================================== | |
encryptor = AES.new(key, AES.MODE_ECB) | |
def hide(s): | |
""" Hides [s] deterministically.""" | |
# deal with integer/float | |
if type(s) == type(0) or type(s) == type(1.2): | |
s = str(s) | |
# padding | |
s = s.encode('ascii', 'ignore') | |
length = 16 - (len(s) % 16) | |
s += '0'*length | |
encS = hash(encryptor.encrypt(s)) | |
if type(s) == type(0): # deal with integer | |
encS = int(encS) | |
else: # deal with string | |
encS = str(abs(int(encS))) | |
return(encS) | |
def hideData(data): | |
""" Hides private data in any export of Kresus. """ | |
for op in data["operations"]: | |
op[BA] = hide(op[BA]) | |
op[TL] = hide(op[TL]) | |
op[RW] = hide(op[RW]) | |
op[AM] = int(hide(op[AM])) % MAX_INT | |
for acc in data["accounts"]: | |
acc[AN] = hide(acc[AN]) | |
acc[TL] = hide(acc[TL]) | |
acc[INA] = int(hide(acc[INA])) % MAX_INT | |
for access in data["accesses"]: | |
access[LG] = hide(access[LG]) | |
for cat in data["categories"]: | |
cat[TL] = hide(cat[TL]) | |
return(data) | |
def replace(in_name, out_name): | |
""" Reads in_name, hides privatde data and dump in out_name. """ | |
with open(in_name, 'r+') as f_in: | |
data = json.load(f_in) | |
data_out = hideData(data) | |
with open(out_name, 'w') as f_out: | |
json.dump(data_out, f_out, indent=4) | |
in_name = argv[1] | |
out_name = argv[2] | |
replace(in_name,out_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment