-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedd_rnn.py
47 lines (36 loc) · 1.15 KB
/
embedd_rnn.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri May 24 00:10:01 2019
@author: uranusq
"""
import json
from keras.models import model_from_json
from keras_preprocessing.text import tokenizer_from_json
import numpy as np
from keras.preprocessing import sequence
mode = 100
with open("model_100api_600_genLegal.json", "r") as f:
model = model_from_json(f.read())
model.load_weights('model_100api_600_check.h5')
with open("tokenizer.json", "rb") as f:
data = json.load(f)
tokenizer = tokenizer_from_json(data)
def predict_rnn(api_calls):
if len(api_calls) > mode:
api_calls = api_calls[-mode:]
tokens = tokenizer.texts_to_sequences(api_calls)
if len(tokens) < mode:
for i in range(mode-len(tokens)):
tokens.append([0])
tokens = sequence.pad_sequences(tokens)
tokens = np.reshape(tokens, (1, mode))
#tokens = np.fliplr(tokens)
return model.predict(tokens)[0]
if __name__ == "__main__":
api_calls = ["NtClose", "LdrLoadDll", "cryptacquirecontexta"]
res = predict_rnn(api_calls)
if res > 0.5:
print("Malicious: ", res)
else:
print("Ok: ", res)