-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmtgxtocsv.py
51 lines (48 loc) · 1.21 KB
/
mtgxtocsv.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
#! /usr/bin/python
# This script can be used to generate a CSV file from the mtgx file. A mtgx file is a file created by saving the Maltego graph.
# It extracts the entities, their type and entity parent(s).
from BeautifulSoup import BeautifulSoup
import csv
import zipfile
import sys
file=sys.argv[1]
zp=zipfile.ZipFile(file)
path=str(zp.namelist()[0])
data=zp.read(path)
xmlbs=BeautifulSoup(data)
psxml=xmlbs.graph.findAll("node")
key=xmlbs.findAll("key")
edge=xmlbs.findAll("edge")
xkey=""
el=[]
edgelink=["Entity Parent(s)"]
entity=["Entity Type"]
entvalue=["Entity Value"]
link=["Entity ID"]
lnk=""
file=file.split('.')
opfilename=file[0]+".csv"
outcsv=open(opfilename, 'wb')
csvwriter = csv.writer(outcsv)
print ""
for k in key:
if (k["for"]=="edge"):
if (k["attr.name"]=="MaltegoLink"):
xkey=k["id"]
break
for node in psxml:
lnk=node["id"]
link.append(lnk)
for ed in edge:
if ed["target"]==lnk:
el.append(str(ed["source"]))
edgelink.append(el)
el=[]
ent=node.findAll("mtg:maltegoentity")[0]["type"]
entity.append(ent)
nod=node.findAll("mtg:value")[0].contents
for nd in nod:
entvalue.append(nd)
for val in (link, entity, entvalue, edgelink):
csvwriter.writerow(val)
outcsv.close()