/
orm.py
327 lines (242 loc) · 11.4 KB
/
orm.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# -*- coding: utf-8 -*-
"""
Copyright (C) 2020 Soheil Khodayari, CISPA
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Description:
---------------
Neo4j database utility functions
Usage:
--------
python3 -m hpg_neo4j.orm
Further Documentation on NeoModel ORM:
--------
please visit:
https://neomodel.readthedocs.io/en/latest/module_documentation.html
https://github.com/neo4j-contrib/neomodel/tree/master/neomodel
"""
import neomodel
import constants as constantsModule
neomodel.config.DATABASE_URL = constantsModule.NEOMODEL_NEO4J_CONN_STRING
class RelationshipSchema(neomodel.StructuredRel):
RelationType= neomodel.StringProperty()
Arguments= neomodel.StringProperty()
class ASTNode(neomodel.StructuredNode):
# properties
Id= neomodel.UniqueIdProperty()
Type= neomodel.StringProperty()
Kind= neomodel.StringProperty()
Code= neomodel.StringProperty()
Range= neomodel.StringProperty()
Location= neomodel.StringProperty()
Value= neomodel.StringProperty()
Raw= neomodel.StringProperty()
Async= neomodel.StringProperty()
SemanticType= neomodel.StringProperty()
# Abstract Syntax Tree (AST) relationships
ASTConnectionsTo= neomodel.RelationshipTo('ASTNode', 'AST_parentOf', model=RelationshipSchema)
ASTConnectionsFrom= neomodel.RelationshipFrom('ASTNode', 'AST_parentOf', model=RelationshipSchema)
ASTConnections= neomodel.Relationship('ASTNode', 'AST_parentOf', model=RelationshipSchema)
# Control Flow Graph (CFG) relationships
CFGConnectionsTo= neomodel.RelationshipTo('ASTNode', 'CFG_parentOf', model=RelationshipSchema)
CFGConnectionsFrom= neomodel.RelationshipFrom('ASTNode', 'CFG_parentOf', model=RelationshipSchema)
CFGConnections= neomodel.Relationship('ASTNode', 'CFG_parentOf', model=RelationshipSchema)
# Program Dependence Graph (PDG) relationships
PDGConnectionsTo= neomodel.RelationshipTo('ASTNode', 'PDG_parentOf', model=RelationshipSchema)
PDGConnectionsFrom= neomodel.RelationshipFrom('ASTNode', 'PDG_parentOf', model=RelationshipSchema)
PDGConnections= neomodel.Relationship('ASTNode', 'PDG_parentOf', model=RelationshipSchema)
# Inter-Procedural Call Graph (IPCG) relationships
IPCGConnectionsTo= neomodel.RelationshipTo('ASTNode', 'CG_parentOf', model=RelationshipSchema)
IPCGConnectionsFrom= neomodel.RelationshipFrom('ASTNode', 'CG_parentOf', model=RelationshipSchema)
IPCGConnections= neomodel.Relationship('ASTNode', 'CG_parentOf', model=RelationshipSchema)
# Event Registration of ERDDG relationships
ERDDGRegistrationConnectionsTo= neomodel.RelationshipTo('ASTNode', 'ERDDG_Registration', model=RelationshipSchema)
ERDDGRegistrationConnectionsFrom= neomodel.RelationshipFrom('ASTNode', 'ERDDG_Registration', model=RelationshipSchema)
ERDDGRegistrationConnections= neomodel.Relationship('ASTNode', 'ERDDG_Registration', model=RelationshipSchema)
# Event Dispatch of ERDDG relationships
ERDDGDispatchConnectionsTo= neomodel.RelationshipTo('ASTNode', 'ERDDG_Dispatch', model=RelationshipSchema)
ERDDGDispatchConnectionsFrom= neomodel.RelationshipFrom('ASTNode', 'ERDDG_Dispatch', model=RelationshipSchema)
ERDDGRegistrationConnections= neomodel.Relationship('ASTNode', 'ERDDG_Registration', model=RelationshipSchema)
# Event Dependency of ERDDG relationships
ERDDGDependencyConnectionsTo= neomodel.RelationshipTo('ASTNode', 'ERDDG_Dependency', model=RelationshipSchema)
ERDDGDependencyConnectionsFrom= neomodel.RelationshipFrom('ASTNode', 'ERDDG_Dependency', model=RelationshipSchema)
ERDDGDependencyConnections= neomodel.Relationship('ASTNode', 'ERDDG_Dependency', model=RelationshipSchema)
# ------------------------------------------------------------------------------------------------ #
# General Methods
# ------------------------------------------------------------------------------------------------ #
def get_line(self):
"""
@return {list|None}: [(a, b)] where a, b are the start and end lines for the code of the AST node
"""
if self.location is not None:
loc= eval(self.location)
return [loc.start.line, loc.end.line]
return None
def is_cfg_node(self):
# TODO
pass
def has_cfg_edge(self):
# TODO
pass
def has_pdg_edge(self):
# TODO
pass
# ------------------------------------------------------------------------------------------------ #
# Edge Traversals
# ------------------------------------------------------------------------------------------------ #
def get_connections_by_ast(self, direction=neomodel.match.OUTGOING, **kwargs):
"""
@param {string} direction: neomodel.match.OUTGOING / neomodel.match.INCOMING / neomodel.match.EITHER
@param **kwargs: positional keyword arguments specifying the constraints on the edges for traversals
@return {list}: related AST nodes
"""
if direction == neomodel.match.OUTGOING:
return self.ASTConnectionsTo.match(**kwargs).all()
elif direction == neomodel.match.INCOMING:
return self.ASTConnectionsFrom.match(**kwargs).all()
else:
return self.ASTConnections.match(**kwargs).all()
def get_connections_by_cfg(self, direction=neomodel.match.OUTGOING, **kwargs):
"""
@param {string} direction: neomodel.match.OUTGOING / neomodel.match.INCOMING / neomodel.match.EITHER
@param **kwargs: positional keyword arguments specifying the constraints on the edges for traversals
@return {list} related CFG nodes
"""
if direction == neomodel.match.OUTGOING:
return self.CFGConnectionsTo.match(**kwargs).all()
elif direction == neomodel.match.INCOMING:
return self.CFGConnectionsFrom.match(**kwargs).all()
else:
return self.CFGConnections.match(**kwargs).all()
def get_connections_by_pdg(self, direction=neomodel.match.OUTGOING, **kwargs):
"""
@param {string} direction: neomodel.match.OUTGOING / neomodel.match.INCOMING / neomodel.match.EITHER
@param **kwargs: positional keyword arguments specifying the constraints on the edges for traversals
@return {list} related PDG nodes
"""
if direction == neomodel.match.OUTGOING:
return self.PDGConnectionsTo.match(**kwargs).all()
elif direction == neomodel.match.INCOMING:
return self.PDGConnectionsFrom.match(**kwargs).all()
else:
return self.PDGConnections.match(**kwargs).all()
def get_connections_by_ipcg(self, direction=neomodel.match.OUTGOING, **kwargs):
"""
@param {string} direction: neomodel.match.OUTGOING / neomodel.match.INCOMING / neomodel.match.EITHER
@param **kwargs: positional keyword arguments specifying the constraints on the edges for traversals
@return {list} related IPCG nodes
"""
if direction == neomodel.match.OUTGOING:
return self.IPCGConnectionsTo.match(**kwargs).all()
elif direction == neomodel.match.INCOMING:
return self.IPCGConnectionsFrom.match(**kwargs).all()
else:
return self.IPCGConnections.match(**kwargs).all()
def get_connections_by_erddg_dispatch(self, direction=neomodel.match.OUTGOING, **kwargs):
"""
@param {string} direction: neomodel.match.OUTGOING / neomodel.match.INCOMING / neomodel.match.EITHER
@param **kwargs: positional keyword arguments specifying the constraints on the edges for traversals
@return {list} related nodes via ERDDG Dispatch edges
"""
if direction == neomodel.match.OUTGOING:
return self.ERDDGDispatchConnectionsTo.match(**kwargs).all()
elif direction == neomodel.match.INCOMING:
return self.ERDDGDispatchConnectionsFrom.match(**kwargs).all()
else:
return self.ERDDGDispatchConnections.match(**kwargs).all()
def get_connections_by_erddg_registration(self, direction=neomodel.match.OUTGOING, **kwargs):
"""
@param {string} direction: neomodel.match.OUTGOING / neomodel.match.INCOMING / neomodel.match.EITHER
@param **kwargs: positional keyword arguments specifying the constraints on the edges for traversals
@return {list} related nodes via ERDDG Registration edges
"""
if direction == neomodel.match.OUTGOING:
return self.ERDDGRegistrationConnectionsTo.match(**kwargs).all()
elif direction == neomodel.match.INCOMING:
return self.ERDDGRegistrationConnectionsFrom.match(**kwargs).all()
else:
return self.ERDDGRegistrationConnections.match(**kwargs).all()
def get_connections_by_erddg_dependency(self, direction=neomodel.match.OUTGOING, **kwargs):
"""
@param {string} direction: neomodel.match.OUTGOING / neomodel.match.INCOMING / neomodel.match.EITHER
@param **kwargs: positional keyword arguments specifying the constraints on the edges for traversals
@return {list} related nodes via ERDDG Dependency edges
"""
if direction == neomodel.match.OUTGOING:
return self.ERDDGDependencyConnectionsTo.match(**kwargs).all()
elif direction == neomodel.match.INCOMING:
return self.ERDDGDependencyConnectionsFrom.match(**kwargs).all()
else:
return self.ERDDGDependencyConnections.match(**kwargs).all()
class RequestNode(neomodel.StructuredNode):
## request method --> Type field
## request kind (e.g., xmlhttprequest) --> Kind field
## request URL --> Value field
## request body --> Raw field
## request status code --> Code field
## other properties are currently empty
# properties
Id= neomodel.UniqueIdProperty()
Type= neomodel.StringProperty()
Kind= neomodel.StringProperty()
Code= neomodel.StringProperty()
Range= neomodel.StringProperty()
Location= neomodel.StringProperty()
Value= neomodel.StringProperty()
Raw= neomodel.StringProperty()
Async= neomodel.StringProperty()
SemanticType= neomodel.StringProperty()
class EventNode(neomodel.StructuredNode):
## event name --> Type field
## event target --> Value field
## other properties are currently empty
# properties
Id= neomodel.UniqueIdProperty()
Type= neomodel.StringProperty()
Kind= neomodel.StringProperty()
Code= neomodel.StringProperty()
Range= neomodel.StringProperty()
Location= neomodel.StringProperty()
Value= neomodel.StringProperty()
Raw= neomodel.StringProperty()
Async= neomodel.StringProperty()
SemanticType= neomodel.StringProperty()
class CookieNode(neomodel.StructuredNode):
## cookie name --> Raw field
## cookie value --> Value field
## cookie httpOnly --> Type field
## other properties are currently empty
# properties
Id= neomodel.UniqueIdProperty()
Type= neomodel.StringProperty()
Kind= neomodel.StringProperty()
Code= neomodel.StringProperty()
Range= neomodel.StringProperty()
Location= neomodel.StringProperty()
Value= neomodel.StringProperty()
Raw= neomodel.StringProperty()
Async= neomodel.StringProperty()
SemanticType= neomodel.StringProperty()
class DOMSnapshot(neomodel.StructuredNode):
## html path --> Location field
## html code --> Code field
## other properties are currently empty
# properties
Id= neomodel.UniqueIdProperty()
Type= neomodel.StringProperty()
Kind= neomodel.StringProperty()
Code= neomodel.StringProperty()
Range= neomodel.StringProperty()
Location= neomodel.StringProperty()
Value= neomodel.StringProperty()
Raw= neomodel.StringProperty()
Async= neomodel.StringProperty()
SemanticType= neomodel.StringProperty()