-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (55 loc) · 1.91 KB
/
app.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
from flask import Flask, request, send_from_directory
from flask_restful import Resource, Api
from flask_cors import CORS
from Extractor import extract_one
import yaml
import os
from time import time
from random import randint
app = Flask(__name__, static_url_path='')
api = Api(app)
# Allow all origin to access the API
CORS(app)
IMAGES_DIR = "images"
def save_image(name, image, format):
"""
Take a Pillow image and save it using the format and name provided.
"""
ts = int(time())
rand = str(randint(0, 4096)).zfill(5)
file_name = '{name}{time}{rand}.{format}'
file_name = file_name.format(name=name, time=ts, rand=rand, format=format)
image.save(os.path.join(IMAGES_DIR, file_name))
url = 'http://extractor/{image_dir}/{file_name}'
return url.format(image_dir=IMAGES_DIR, file_name=file_name)
def prepare_response(features, images):
image_urls = {}
for image_name in images.keys():
image_value = images[image_name]
image = image_value['image']
image_format = image_value['image_format']
url = save_image(image_name, image, image_format)
image_urls[image_name] = url
features['image_urls'] = image_urls
return features
class Extraction(Resource):
def post(self):
"""
Extract features from an executable file.
"""
executable = request.files['exec']
extractor_conf = request.files['extractor_conf']
exec = executable.read()
conf = yaml.load(extractor_conf)
features, images = extract_one(exec, conf)
resp = prepare_response(features, images)
return resp
api.add_resource(Extraction, '/extract')
@app.route('/images/<path:path>')
def send_js(path):
return send_from_directory(IMAGES_DIR, path)
if __name__ == '__main__':
# Prepate folder to store images
if not os.path.exists(IMAGES_DIR):
os.makedirs(IMAGES_DIR)
app.run('0.0.0.0', 80)