This repository was archived by the owner on Dec 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
272 lines (213 loc) · 7.69 KB
/
test.py
File metadata and controls
272 lines (213 loc) · 7.69 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import json
from copy import deepcopy
from typing import *
from pydgraph import DgraphClient, DgraphClientStub
from grapl_analyzerlib.nodes.comparators import IntCmp, _str_cmps, StrCmp, _int_cmps
from grapl_analyzerlib.nodes.dynamic_node import DynamicNodeQuery, DynamicNodeView
from grapl_analyzerlib.nodes.external_ip_node import ExternalIpView
from grapl_analyzerlib.nodes.process_node import ProcessQuery, ProcessView, IProcessView
from grapl_analyzerlib.nodes.types import Property, PropertyT
from grapl_analyzerlib.nodes.viewable import Viewable, EdgeViewT, ForwardEdgeView
T = TypeVar('T')
def create_edge(client: DgraphClient, from_uid: str, edge_name: str, to_uid: str) -> None:
if edge_name[0] == '~':
mut = {
'uid': to_uid,
edge_name[1:]: {'uid': from_uid}
}
else:
mut = {
'uid': from_uid,
edge_name: {'uid': to_uid}
}
txn = client.txn(read_only=False)
try:
txn.mutate(set_obj=mut, commit_now=True)
finally:
txn.discard()
def _upsert(client: DgraphClient, node_dict: Dict[str, Property]) -> str:
if node_dict.get('uid'):
node_dict.pop('uid')
node_dict['uid'] = '_:blank-0'
node_key = node_dict['node_key']
query = f"""
{{
q0(func: eq(node_key, "{node_key}")) {{
uid,
expand(_forward_)
}}
}}
"""
txn = client.txn(read_only=False)
try:
res = json.loads(txn.query(query).json)['q0']
new_uid = None
if res:
node_dict['uid'] = res[0]['uid']
new_uid = res[0]['uid']
mutation = node_dict
m_res = txn.mutate(set_obj=mutation, commit_now=True)
uids = m_res.uids
if new_uid is None:
new_uid = uids['blank-0']
return str(new_uid)
finally:
txn.discard()
def upsert(
client: DgraphClient,
view_type: Type[Viewable],
node_key: str,
node_props: Dict[str, Property]
) -> Viewable:
node_props['node_key'] = node_key
uid = _upsert(client, node_props)
# print(f'uid: {uid}')
node_props['uid'] = uid
# print(node_props['node_key'])
return view_type.from_dict(client, node_props)
class IpcQuery(DynamicNodeQuery):
def __init__(self) -> None:
super(IpcQuery, self).__init__("Ipc", IpcView)
def with_src_pid(self, eq=IntCmp, gt=IntCmp, lt=IntCmp) -> "IpcQuery":
self.set_int_property_filter("src_pid", _int_cmps("src_pid", eq=eq, gt=gt, lt=lt))
return self
def with_dst_pid(self, eq=IntCmp, gt=IntCmp, lt=IntCmp) -> "IpcQuery":
self.set_int_property_filter("dst_pid", _int_cmps("dst_pid", eq=eq, gt=gt, lt=lt))
return self
def with_ipc_type(self, eq=StrCmp, contains=StrCmp, ends_with=StrCmp) -> "IpcQuery":
self.set_str_property_filter(
"ipc_type", _str_cmps("ipc_type", eq=eq, contains=contains, ends_with=ends_with)
)
return self
def with_ipc_creator(
self, ipc_creator_query: "Optional[ProcessQuery]" = None
) -> "IpcQuery":
if ipc_creator_query:
ipc_creator = deepcopy(ipc_creator_query)
else:
ipc_creator = ProcessQuery()
self.set_forward_edge_filter("ipc_creator", ipc_creator)
ipc_creator.set_reverse_edge_filter("~ipc_creator", self, "ipc_creator")
return self
def with_ipc_recipient(
self, ipc_recipient_query: "Optional[ProcessQuery]" = None
) -> "IpcQuery":
if ipc_recipient_query:
ipc_recipient = deepcopy(ipc_recipient_query)
else:
ipc_recipient = ProcessQuery()
self.set_forward_edge_filter("ipc_recipient", ipc_recipient)
ipc_recipient.set_reverse_edge_filter("~ipc_recipient", self, "ipc_recipient")
return self
class IpcView(DynamicNodeView):
def __init__(
self,
dgraph_client: DgraphClient,
node_key: str,
uid: str,
node_type: str,
src_pid: Optional[int] = None,
dst_pid: Optional[int] = None,
ipc_creator: Optional[IProcessView] = None,
ipc_recipient: Optional[IProcessView] = None,
**kwargs
) -> None:
super(IpcView, self).__init__(
dgraph_client=dgraph_client, node_key=node_key, uid=uid, node_type=node_type
)
self.node_type = node_type
self.src_pid = src_pid
self.dst_pid = dst_pid
self.ipc_creator = ipc_creator
self.ipc_recipient = ipc_recipient
@staticmethod
def _get_property_types() -> Mapping[str, "PropertyT"]:
return {"src_pid": int, "dst_pid": int}
@staticmethod
def _get_forward_edge_types() -> Mapping[str, "EdgeViewT"]:
f_edges = {"ipc_creator": ProcessView, "ipc_recipient": ProcessView}
return {fe[0]: fe[1] for fe in f_edges.items() if fe[1]}
def _get_forward_edges(self) -> "Mapping[str, ForwardEdgeView]":
f_edges = {"ipc_creator": self.ipc_creator, "ipc_recipient": self.ipc_recipient}
return {fe[0]: fe[1] for fe in f_edges.items() if fe[1]}
def _get_properties(self, fetch: bool = False) -> Mapping[str, Union[str, int]]:
props = {"src_pid": self.src_pid, "dst_pid": self.dst_pid}
return {p[0]: p[1] for p in props.items() if p[1] is not None}
def test_ipc(local_client: DgraphClient):
ipc = {
"dgraph.type": "Ipc",
"ipc_type": "UNIX_DOMAIN",
} # type: Dict[str, Property]
ipc_view = upsert(
local_client,
IpcView,
'6fadeb67-4b20-4870-b848-647e97bc5543',
ipc
)
qv = IpcQuery().with_ipc_type(eq="UNIX_DOMAIN")
# print(
# generate_query(
# query_name='qname',
# root=qv,
# binding_modifier='bm',
# )
# )
# print(qv.query_first(local_client))
# # print(qv)
# print(ipc)
# print(ipc_view)
def main() -> None:
local_client = DgraphClient(DgraphClientStub('localhost:9080'))
test_ipc(local_client)
parent = {
'process_id': 100,
'process_name': 'word.exe'
} # type: Dict[str, Property]
child = {
'process_id': 1234,
'process_name': 'cmd.exe'
} # type: Dict[str, Property]
external_ip = {
'external_ip': '56.123.14.24',
} # type: Dict[str, Property]
parent_view = upsert(
local_client,
ProcessView,
'ea75f056-61a1-479d-9ca2-f632d2c67205',
parent
)
child_view = upsert(
local_client,
ProcessView,
'10f585c2-cf31-41e2-8ca5-d477e78be3ac',
child
)
external_ip_view = upsert(
local_client,
ExternalIpView,
'8bc20354-e8c5-49fc-a984-2927b24c1a29',
external_ip
)
create_edge(local_client, parent_view.uid, 'children', child_view.uid)
create_edge(local_client, child_view.uid, 'created_connections', external_ip_view.uid)
queried_child_0 = ProcessQuery().with_process_id(eq=1234).query_first(local_client)
assert queried_child_0
assert queried_child_0.node_key == child_view.node_key
queried_child_1 = (
ProcessQuery()
.with_process_id(eq=1234)
.query_first(local_client, contains_node_key='10f585c2-cf31-41e2-8ca5-d477e78be3ac')
)
assert queried_child_1
assert queried_child_1.node_key == child_view.node_key
assert queried_child_1.node_key == queried_child_0.node_key
p = (
ProcessQuery()
.with_process_name(eq="cmd.exe")
.with_parent()
.with_created_connection()
.query_first(local_client)
) # type: Optional[ProcessView]
assert p
if __name__ == '__main__':
main()