-
Notifications
You must be signed in to change notification settings - Fork 10
/
path_discover3d.py
executable file
·212 lines (189 loc) · 5.42 KB
/
path_discover3d.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
#!/usr/bin/python3
from scapy.all import *
from geolite2 import geolite2 #pip3 install maxminddb-geolite2
from ipwhois import IPWhois #pip3 install ipwhois
import json
import sys
TIMEOUT = 5
geoip = geolite2.reader()
targets = []
if len(sys.argv) == 2:
with open( sys.argv[1] ) as f:
for target in f:
(host,port,proto) = target.split()
targets.append( (host, int(port), proto) )
else:
while True:
try:
line = input()
(host,port,proto) = line.split()[:3]
targets.append( (host, int(port), proto) )
except:
break
geoip_cache = {}
def geoiplookup(ip):
if ip in geoip_cache:
return geoip_cache[ip]
result = geoip.get(ip) or {}
country = result['country']['names']['en'] if "country" in result else "UNKN"
city = result["city"]["names"]["en"] if "city" in result else "UNKN"
geoip_cache[ip] = f"{country}, {city}"
return f"{country}, {city}"
whois_cache = {}
def whoislookup(ip):
if ip in whois_cache:
return whois_cache[ip]
result = IPWhois(ip).lookup_whois()
netname = result['nets'][0]['name']
descr = result['nets'][0]['description']
whois_cache[ip] = f"{netname}"
return f"{netname}"
def is_grey(ip):
parts = list(map(int, ip.split(".")))
if parts[0] == 10:
return True
elif parts[0] == 172 and 16 <= parts[1] <= 24:
return True
elif parts[0] == 192 and parts[1] == 168:
return True
return False
def get_color(ip):
if is_grey(ip):
return "#0000ff"
else:
return "#00ff00"
nodes = []
links = []
def add_node(ip):
global nodes
nodes.append({
"id": ip,
"ip": ip,
"size": 5,
"netname": whoislookup(ip) if not is_grey(ip) else "local",
"geo": geoiplookup(ip) if not is_grey(ip) else "local",
"color": get_color(ip)
})
def get_node(name):
global nodes
for node in nodes:
if node["id"] == name:
return node
def add_link(source, target):
global links
links.append({
"source": source,
"target": target,
"color": "#ff0000",
"label": "hop",
"distance": 100,
"width": 5,
"count": 1,
"speed": 0.01,
"particle": 2
})
def get_link(source, target):
global links
for link in links:
if link["source"] == source and link["target"] == target:
return link
paths = TracerouteResult()
add_node(conf.iface.ip)
for target in targets:
(host,port,proto) = target
if proto == 'tcp':
l4 = TCP(dport=port)
elif proto == 'udp':
l4 = UDP(dport=port)
elif proto == 'icmp':
l4 = ICMP()
node_prev = get_node(conf.iface.ip)
for hop in traceroute(host,l4=l4,timeout=TIMEOUT)[0]:
hop = hop[1]
if not get_node(hop[IP].src):
add_node(hop[IP].src)
node = get_node(hop[IP].src)
if node_prev:
add_link(node_prev["id"], node["id"])
node_prev = node
WWW = '''
<html>
<head>
<style> body { margin: 0; } </style>
<title>traceroute</title>
<script type="importmap">{ "imports": { "three": "https://unpkg.com/three/build/three.module.js" }}</script>
<script src="https://unpkg.com/three"></script>
<script src="https://unpkg.com/[email protected]/dist/three-spritetext.min.js"></script>
<script src="https://unpkg.com/3d-force-graph"></script>
</head>
<body>
<div id="3d-graph"></div>
<style>
body { margin: 0; }
.node-label {
font-size: 12px;
padding: 1px 4px;
border-radius: 4px;
background-color: rgba(0,0,0,0.5);
user-select: none;
}
</style>
<script type="module">
import { CSS2DRenderer, CSS2DObject } from 'https://unpkg.com/[email protected]/examples/jsm/renderers/CSS2DRenderer.js';
import { UnrealBloomPass } from 'https://unpkg.com/three/examples/jsm/postprocessing/UnrealBloomPass.js';
var dataset = %s
const elem = document.getElementById('3d-graph');
const Graph = ForceGraph3D({extraRenderers: [new CSS2DRenderer()]})(elem)
.graphData(dataset)
.backgroundColor('#101020')
.nodeAutoColorBy('group')
.linkAutoColorBy('color')
.nodeVal(node => node.size)
.nodeResolution(16)
.nodeThreeObject(node => {
const nodeEl = document.createElement('div');
nodeEl.textContent = `${node.ip} ${node.geo}`;
nodeEl.style.color = node.color;
nodeEl.className = 'node-label';
return new CSS2DObject(nodeEl);
})
.nodeThreeObjectExtend(true)
.linkWidth("width")
.linkDirectionalArrowLength(3.5)
.linkDirectionalArrowRelPos(1)
.linkDirectionalParticles("count")
.linkDirectionalParticleSpeed("speed")
.linkDirectionalParticleWidth("particle")
//.linkDirectionalParticleColor(() => 'white')
.nodeLabel(node => `${node.ip}`)
.linkLabel(link => link.label)
.onNodeDragEnd(node => {
node.fx = node.x;
node.fy = node.y;
node.fz = node.z;
})
.onNodeClick(function(node){
console.log(`selected ${node.ip}: ${node.netname}`)
zoom(node)
})
.onLinkClick(link => console.log(link))
Graph.d3Force('link').distance(link => link.distance)
function zoom(node)
{
const distance = 40;
const distRatio = 1 + distance/Math.hypot(node.x, node.y, node.z);
const newPos = node.x || node.y || node.z
? { x: node.x * distRatio, y: node.y * distRatio, z: node.z * distRatio }
: { x: 0, y: 0, z: distance }; // special case if node is in (0,0,0)
Graph.cameraPosition(
newPos, // new position
node, // lookAt ({ x, y, z })
3000 // ms transition duration
);
}
</script>
</body>
</html>
'''
with open("out.html", "w") as o:
o.write(WWW % json.dumps({"nodes": nodes, "links": links}))