-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_provision.py
165 lines (135 loc) · 5.12 KB
/
graph_provision.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import time
import json
import pydgraph
from pydgraph import DgraphClient, DgraphClientStub
def set_process_schema(client: DgraphClient, engagement: bool = False):
schema = """node_key: string @upsert @index(hash) .
process_id: int @index(int) .
created_timestamp: int @index(int) .
asset_id: string @index(hash) .
terminate_time: int @index(int) .
image_name: string @index(exact, hash, trigram, fulltext) .
process_name: string @index(exact, hash, trigram, fulltext) .
arguments: string @index(fulltext) @index(trigram) .
bin_file: uid @reverse .
children: [uid] @reverse .
created_files: [uid] @reverse .
deleted_files: [uid] @reverse .
read_files: [uid] @reverse .
wrote_files: [uid] @reverse .
created_connections: [uid] @reverse .
bound_connections: [uid] @reverse .
"""
if engagement:
schema += "\n"
schema += "risks: [uid] @reverse ."
# unstable
schema += """
process_guid: string @index(exact, hash, trigram, fulltext) .
"""
op = pydgraph.Operation(schema=schema)
client.alter(op)
def set_file_schema(client: DgraphClient, engagement: bool=False) -> None:
schema = """
node_key: string @upsert @index(hash) .
file_name: string @index(exact, hash, trigram, fulltext) .
asset_id: string @index(exact, hash, trigram, fulltext) .
file_path: string @index(exact, hash, trigram, fulltext) .
file_extension: string @index(exact, hash, trigram, fulltext) .
file_mime_type: string @index(exact, hash, trigram, fulltext) .
file_size: int @index(int) .
file_version: string @index(exact, hash, trigram, fulltext) .
file_description: string @index(exact, hash, trigram, fulltext) .
file_product: string @index(exact, hash, trigram, fulltext) .
file_company: string @index(exact, hash, trigram, fulltext) .
file_directory: string @index(exact, hash, trigram, fulltext) .
file_inode: int @index(int) .
file_hard_links: string @index(exact, hash, trigram, fulltext) .
signed: bool @index(bool) .
signed_status: string @index(exact, hash, trigram, fulltext) .
md5_hash: string @index(exact, hash, trigram, fulltext) .
sha1_hash: string @index(exact, hash, trigram, fulltext) .
sha256_hash: string @index(exact, hash, trigram, fulltext) .
"""
if engagement:
schema += "\n"
schema += "risks: uid @reverse ."
op = pydgraph.Operation(schema=schema)
client.alter(op)
def set_outbound_connection_schema(client, engagement=False):
schema = """
create_time: int @index(int) .
terminate_time: int @index(int) .
last_seen_time: int @index(int) .
ip: string @index(exact, trigram, hash) .
port: string @index(exact, trigram, hash) .
"""
if engagement:
schema += "\n"
schema += "risks: [uid] @reverse ."
op = pydgraph.Operation(schema=schema)
client.alter(op)
def set_inbound_connection_schema(client, engagement=False):
schema = """
node_key: string @upsert @index(hash) .
asset_id: string @index(exact, hash, trigram, fulltext) .
port: string @index(exact, trigram, hash) .
"""
if engagement:
schema += "\n"
schema += "risks: [uid] @reverse ."
op = pydgraph.Operation(schema=schema)
client.alter(op)
def set_external_ip_schema(client, engagement=False):
schema = """
node_key: string @upsert @index(hash) .
external_ip: string @index(exact, trigram, hash) .
"""
if engagement:
schema += "\n"
schema += "risks: [uid] @reverse ."
op = pydgraph.Operation(schema=schema)
client.alter(op)
def set_risk_schema(client, engagement=False):
schema = """
analyzer_name: string @index(exact, trigram, hash) .
risk_score: int @index(int) .
"""
op = pydgraph.Operation(schema=schema)
client.alter(op)
def set_lens_schema(client, engagement=False):
schema = """
scope: [uid] @reverse .
lens: string @upsert @index(exact, trigram, hash) .
score: int @index(int) .
"""
op = pydgraph.Operation(schema=schema)
client.alter(op)
def set_ipc_schema(client, engagement=False):
schema = """
node_key: string @upsert @index(hash) .
node_type: string @index(hash) .
key: string @index(hash) .
node_type: string @index(hash) .
ipc_type: string @index(hash) .
src_pid: int @index(int) .
dst_pid: int @index(int) .
ipc_creator: uid @reverse .
ipc_recipient: uid @reverse .
"""
if engagement:
schema += "\n"
schema += "risks: [uid] @reverse ."
op = pydgraph.Operation(schema=schema)
client.alter(op)
def drop_all(client):
op = pydgraph.Operation(drop_all=True)
client.alter(op)
client = DgraphClient(DgraphClientStub('localhost:9080'))
drop_all(client)
set_process_schema(client)
set_file_schema(client)
set_outbound_connection_schema(client)
set_inbound_connection_schema(client)
set_external_ip_schema(client)
set_ipc_schema(client)