-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgraph2prolog.py
executable file
·63 lines (43 loc) · 1.53 KB
/
graph2prolog.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
#!/usr/bin/env python3
from __future__ import print_function
import argparse
import sys
import os
import logging
import json
import networkx as nx
from android.sepolicy import SELinuxContext
from networkx.readwrite import json_graph
from config import *
logging.basicConfig(stream=sys.stdout, format="%(levelname)s: %(message)s", level=logging.INFO)
log = logging.getLogger(__name__)
def main():
print("Policy Graph to Prolog")
print("")
parser = argparse.ArgumentParser()
parser.add_argument('--debug', action='store_true')
parser.add_argument("graph_saved")
args = parser.parse_args()
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
if not os.access(args.graph_saved, os.R_OK):
log.error("Graph file does not exist or is not readable")
return 1
log.info("Loading policy graph %s", args.graph_saved)
with open(args.graph_saved, 'r') as fp:
graph_json = json.load(fp)
G = json_graph.node_link_graph(graph_json)
files = nx.get_node_attributes(G, 'files')
# Re-inflate SELinuxContext objects
for node, attr in files.items():
for fk, f in attr.items():
f["selinux"] = SELinuxContext.FromString(f["selinux"])
focus_types = ['init', 'mediaserver', 'untrusted_app', 'system_server']
G_subgraph = nx.MultiDiGraph(G.subgraph(focus_types))
for node in G_subgraph.nodes():
print(node, " ---- ", G_subgraph[node])
from IPython import embed
embed()
return 0
if __name__ == "__main__":
sys.exit(main())