|
1 | | -#!/usr/bin/python |
| 1 | +#!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | ############################################################################## |
4 | 4 | # Author: @harmj0y |
5 | 5 | # |
6 | 6 | # Based on: https://github.com/sixdub/DomainTrustExplorer by @sixdub |
7 | 7 | # |
8 | | -# Description: Usesnetworkx library to transform PowerView's updated |
| 8 | +# Description: Uses pyyed (yEd) library to transform PowerView's updated |
9 | 9 | # Get-DomainTrustMapping functionality output to graphml |
10 | 10 | # |
11 | 11 | # License: BSD 3-clause |
12 | 12 | ############################################################################## |
13 | 13 |
|
14 | | -import networkx as nx |
15 | | -import sys, csv |
| 14 | +import csv |
| 15 | +from os.path import basename, splitext |
| 16 | +from argparse import ArgumentParser |
16 | 17 |
|
17 | | -if __name__ == '__main__': |
18 | | - |
19 | | - if (len(sys.argv) != 2): |
20 | | - print "usage: ./TrustVisualizer.py <trust_file.csv>" |
21 | | - exit() |
22 | | - |
23 | | - graph = nx.DiGraph() |
24 | | - intputFile = sys.argv[1] |
25 | | - |
26 | | - with open(intputFile, 'rb') as csvfile: |
27 | | - |
28 | | - reader = csv.reader(csvfile, delimiter=',') |
29 | | - |
30 | | - for row in reader: |
31 | | - |
32 | | - # if we have the header row, skip |
33 | | - if row[0] == 'SourceName': |
34 | | - continue |
| 18 | +import pyyed |
35 | 19 |
|
36 | | - # csv format: |
37 | | - # SourceName,TargetName,TrustType,TrustAttributes,TrustDirection,WhenCreated,WhenChanged |
38 | | - ecolor = '' |
39 | | - sourceName = row[0].strip() |
40 | | - targetName = row[1].strip() |
41 | | - trustType = row[2].strip() |
42 | | - trustAttributes = row[3].strip() |
43 | | - trustDirection = row[4].strip() |
| 20 | +parser = ArgumentParser() |
| 21 | +parser.add_argument('trust_file', help='trust file in .csv format (generate with PowerView)') |
| 22 | +args = parser.parse_args() |
44 | 23 |
|
45 | | - # if the source and destination domains are the same, skip |
46 | | - if (sourceName == targetName): |
47 | | - continue |
48 | | - |
49 | | - if (trustType == 'MIT'): |
50 | | - # black label for MIT trusts |
51 | | - ecolor ='#000000' |
52 | | - |
53 | | - else: |
54 | | - if "WITHIN_FOREST" in trustAttributes: |
55 | | - # green label for intra-forest trusts |
56 | | - ecolor = '#009900' |
57 | | - elif (trustAttributes == "FOREST_TRANSITIVE"): |
58 | | - # blue label for inter-forest trusts |
59 | | - ecolor = '#0000CC' |
60 | | - elif ((trustAttributes == "") or (trustAttributes == "TREAT_AS_EXTERNAL") or (trustAttributes == "FILTER_SIDS")): |
61 | | - # red label for external trusts |
62 | | - ecolor = '#FF0000' |
63 | | - else: |
64 | | - # violet label for unknown |
65 | | - print "[-] Unrecognized trust attributes between %s and %s : %s" % (sourceName, targetName, trustAttributes) |
66 | | - ecolor = '#EE82EE' |
67 | | - |
68 | | - # add the domain nodes to the internal graph |
69 | | - graph.add_node(sourceName, label=sourceName) |
70 | | - graph.add_node(targetName, label=targetName) |
71 | | - |
72 | | - # add the edges to the graph |
73 | | - if "Bidirectional" in trustDirection: |
74 | | - graph.add_edge(sourceName, targetName, color=ecolor) |
75 | | - graph.add_edge(targetName, sourceName, color=ecolor) |
76 | | - elif "Outbound" in trustDirection: |
77 | | - graph.add_edge(targetName, sourceName, color=ecolor) |
78 | | - elif "Inbound" in trustDirection: |
79 | | - graph.add_edge(sourceName, targetName, color=ecolor) |
80 | | - else: |
81 | | - print "[-] Unrecognized relationship direction between %s and %s : %s" % (sourceName, targetName, trustDirection) |
82 | | - |
83 | | - outputFile = intputFile + ".graphml" |
84 | | - nx.write_graphml(graph, outputFile) |
85 | | - print "\n[+] Graphml writte to '%s'" % (outputFile) |
86 | | - print "\n[*] Note: green = within forest, red = external, blue = forest to forest, black = MIT, violet = unrecognized\n" |
| 24 | +if __name__ == '__main__': |
| 25 | + graph = pyyed.Graph() |
| 26 | + |
| 27 | + with open(args.trust_file, 'r', encoding='utf-8') as fd: |
| 28 | + reader = csv.reader(fd, delimiter=',') |
| 29 | + next(reader, None) |
| 30 | + |
| 31 | + for row in reader: |
| 32 | + # csv format: |
| 33 | + # "SourceName","TargetName","TrustType","TrustAttributes","TrustDirection","WhenCreated","WhenChanged" |
| 34 | + sourceName = row[0].strip().lower() |
| 35 | + targetName = row[1].strip().lower() |
| 36 | + trustType = row[2].strip() |
| 37 | + trustAttributes = row[3].strip() |
| 38 | + trustDirection = row[4].strip() |
| 39 | + |
| 40 | + # if the source and destination domains are the same, skip |
| 41 | + if sourceName == targetName: |
| 42 | + continue |
| 43 | + |
| 44 | + if trustType == 'MIT': |
| 45 | + # black label for MIT trusts |
| 46 | + ecolor = '#000000' |
| 47 | + else: |
| 48 | + if 'WITHIN_FOREST' in trustAttributes: |
| 49 | + # green label for intra-forest trusts |
| 50 | + ecolor = '#009900' |
| 51 | + elif 'FOREST_TRANSITIVE' in trustAttributes: |
| 52 | + # blue label for inter-forest trusts |
| 53 | + ecolor = '#0000CC' |
| 54 | + elif trustAttributes == '' or any(attr in trustAttributes for attr in ('TREAT_AS_EXTERNAL', 'FILTER_SIDS', 'CROSS_ORGANIZATION')): |
| 55 | + # red label for external trusts |
| 56 | + ecolor = '#FF0000' |
| 57 | + else: |
| 58 | + # violet label for unknown |
| 59 | + print(f'[-] Unrecognized trust attributes between {sourceName} and {targetName} : {trustAttributes}') |
| 60 | + ecolor = '#EE82EE' |
| 61 | + |
| 62 | + try: |
| 63 | + # add source node to the internal graph |
| 64 | + graph.add_node(sourceName, label=sourceName, shape_fill='#FFCC00') |
| 65 | + # node exists |
| 66 | + except RuntimeWarning: |
| 67 | + pass |
| 68 | + |
| 69 | + try: |
| 70 | + # add target node to the internal graph |
| 71 | + graph.add_node(targetName, label=targetName, shape_fill='#FFCC00') |
| 72 | + # node exists |
| 73 | + except RuntimeWarning: |
| 74 | + pass |
| 75 | + |
| 76 | + # add the edges to the graph |
| 77 | + if 'Bidirectional' in trustDirection: |
| 78 | + graph.add_edge(sourceName, targetName, color=ecolor) |
| 79 | + elif 'Outbound' in trustDirection: |
| 80 | + graph.add_edge(targetName, sourceName, color=ecolor) |
| 81 | + elif 'Inbound' in trustDirection: |
| 82 | + graph.add_edge(sourceName, targetName, color=ecolor) |
| 83 | + else: |
| 84 | + print(f'[-] Unrecognized relationship direction between {sourceName} and {targetName} : {trustDirection}' % (sourceName, targetName, trustDirection)) |
| 85 | + |
| 86 | + outputFile = splitext(basename(args.trust_file))[0] + '.graphml' |
| 87 | + graph.write_graph(outputFile) |
| 88 | + |
| 89 | + print(f'[+] Graphml writte to "{outputFile}"') |
| 90 | + print('[*] Note: green = within forest, red = external, blue = forest to forest, black = MIT, violet = unrecognized') |
0 commit comments