-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathweb.py
More file actions
executable file
·116 lines (94 loc) · 3.24 KB
/
web.py
File metadata and controls
executable file
·116 lines (94 loc) · 3.24 KB
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
#!/usr/bin/env python
"""
@about: Web service wrapper for hash identification.
@author: moloch
Usage: ./web.py or see --help for more options
--------------------------------------------------------------------------
JavaScript Simple Example
--------------------------------------------------------------------------
var hash = "3da541559918a808c2402bba5012f6c60b27661c";
cors_request = new XMLHttpRequest();
cors_request.onreadystatechange = function() {
if (cors_request.readyState == 4) {
console.log(cors_request.responseText);
}
}
cors_request.open("GET", "http://hashid.badwith.computer/" + hash);
cors_request.send();
--------------------------------------------------------------------------
Python Simple Example
--------------------------------------------------------------------------
import requests
hsh = "3da541559918a808c2402bba5012f6c60b27661c"
resp = requests.get("http://hashid.badwith.computer/%s" % hsh)
print resp.text
--------------------------------------------------------------------------
Python Multiple Hash Identification Example
--------------------------------------------------------------------------
import json
import requests
hashes = {'hashes': [
"3da541559918a808c2402bba5012f6c60b27661c",
"912ec803b2ce49e4a541068d495ab570"
]}
resp = requests.post("http://hashid.badwith.computer/", data=json.dumps(hashes))
print resp.text
"""
import json
import tornado.ioloop
import tornado.web
import tornado.log
from tornado.gen import coroutine, Return
from tornado.options import define, options
from hashidentifier.HashIdentifier import identify_hashes
class MainHandler(tornado.web.RequestHandler):
"""
Creates a simple JSON API
"""
def data_received(self, chunk):
pass
def initialize(self):
"""
Add headers.
"""
self.set_header("Server", "HashIdentifier")
self.set_header("X-Content-Type-Options", "nosniff")
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Methods", "GET, POST")
@coroutine
def identify_hashes(self, hsh):
"""
Just a small async wrapper function
"""
raise Return(identify_hashes(hsh))
@coroutine
def get(self, *args):
"""
GET identifies a single hash
"""
results = yield self.identify_hashes(args[0])
self.write({args[0]: results})
@coroutine
def post(self, *args):
"""
POST can accept multiple hashes
"""
response = {}
try:
hashes = json.loads(self.request.body)['hashes']
for hsh in set(hashes):
response[hsh] = yield self.identify_hashes(hsh)
except ValueError:
response = {'error': 'could not parse request'}
self.write(response)
application = tornado.web.Application([(r"/(.*)", MainHandler), ])
define("port",
default="8888",
type=int,
help="the listen port for the web server"
)
if __name__ == "__main__":
tornado.options.parse_command_line()
application.listen(options.port)
print("Starting HAI Server on localhost on port number 8888 ...")
tornado.ioloop.IOLoop.instance().start()