-
Notifications
You must be signed in to change notification settings - Fork 12
/
knight.py
executable file
·132 lines (109 loc) · 3.49 KB
/
knight.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
#!/usr/bin/env python
'''
CryptoKnight
@gregorydhill
'''
import os, sys, csv, datetime, shutil, subprocess, signal, threading, itertools, time, argparse
from src.setup import alive, setup
from src.title import title
BASE = os.path.dirname(os.path.realpath(__file__))
PIN = os.path.join(BASE, "pin_tool")
SAMPLE = os.path.join(BASE, "data", "sample")
def signal_handler(signal, frame):
print('\n\n[!] Killing process and exiting.')
os.system('stty echo; setterm -cursor on;')
sys.exit(0)
def main(argv):
parser = argparse.ArgumentParser(description='CryptoKnight - Assessment')
parser.add_argument('-p', '--predict', type=str, metavar='args', \
help='specify arguments as string literal')
parser.add_argument('-e', '--evaluate', type=str, metavar='distribution', \
help='specify set to evaluate')
parser.add_argument('--setup', action='store_true', help='install dependencies')
args = parser.parse_args()
signal.signal(signal.SIGINT, signal_handler)
title()
if (not args.setup and not alive()):
print("[!] Environment not setup.")
parser.print_help()
sys.exit(0)
elif (args.setup):
if (not setup()):
print("[!] Setup could not complete.")
sys.exit(0)
print("[+] Environment ready.")
if (not args.predict and not args.evaluate):
parser.print_help()
sys.exit(0)
arguments = args.evaluate if args.evaluate else args.predict.split(" ")[0]
if not os.path.isfile(arguments):
print("[!] " + str(arguments) + " is not a file.")
sys.exit(0)
# prevent user input during analysis
os.system('stty -echo; setterm -cursor off;')
# evaluate model on specified set
if args.evaluate:
print("[!] Evaluating: " + str(args.evaluate) + "\n")
print("[+] x = predicted, y = actual")
os.system("python ./src/Model/dcnn.py --evaluate " + args.evaluate)
os.system('stty echo; setterm -cursor on;')
sys.exit(0)
arguments = args.predict.split(" ") # collect exe specific args
open(SAMPLE, 'w').close() # clean previous sample
begin = False
def animate():
for c in itertools.cycle(['|', '/', '-', '\\']):
if begin: break
sys.stdout.write('\r[*] Tracing ' + c)
sys.stdout.flush()
time.sleep(0.2)
# start timing
start = datetime.datetime.now()
print("[+] Start Time: " + str(start) + "\n")
try:
# trace animation
t = threading.Thread(target=animate)
t.daemon = True
t.start()
except (KeyboardInterrupt, SystemExit):
begin = True
print("\n[!] Exiting.")
sys.exit(0)
# run pintool in killable subprocess
cmd = [PIN + "/pin", "-t", PIN + \
"/source/tools/CryptoKnight/obj-intel64/CryptoTrace.so", \
"-v", "3", "-o", SAMPLE, "--"]
cmd.extend(arguments)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
try:
while True:
# read from stdout
line = p.stdout.readline()
if line:
if not begin:
print("\r[+] Execution finished.\n")
print("[=====]\n")
begin = True
else: break
print(line),
except KeyboardInterrupt:
try:
# user defined closure, terminate subprocess
p.terminate()
except OSError:
pass
p.wait()
# collect timing information
end = datetime.datetime.now()
print("\n[+] End Time: " + str(end))
mins, secs = divmod(((end-start).total_seconds()), 60)
hours, mins = divmod(mins, 60)
total = '%02d:%02d:%02d' % (hours, mins, secs)
print("[+] Analysis Time: " + total)
# evaluate model with custom sample
os.system("python ./src/Model/dcnn.py --predict " + SAMPLE)
# cleanup
os.system('stty echo; setterm -cursor on;')
if os.path.isdir("tmp"): shutil.rmtree("tmp")
if __name__ == "__main__":
main(sys.argv)