Elffuss BraidHub

kikocis/osiris public

master · 6.0 KiBRawPermalink
1export interface SweepDevice {
2 ip: string;
3 ports: number[];
4 hostnames: string[];
5 cpes: string[];
6 vulns: string[];
7 tags: string[];
8 device_type: string;
9 device_icon: string;
10 device_color: string;
11 risk_level: 'CRITICAL' | 'HIGH' | 'MEDIUM' | 'LOW' | 'INFO';
12}
13
14export interface SweepResult {
15 center: {
16 lat: number;
17 lng: number;
18 city: string;
19 region: string;
20 country: string;
21 countryCode: string;
22 isp: string;
23 asn: string;
24 org: string;
25 };
26 subnet: string;
27 cidr: number;
28 target_ip: string;
29 devices: SweepDevice[];
30 summary: {
31 total_hosts: number;
32 total_responsive: number;
33 device_breakdown: Record<string, number>;
34 };
35 sweep_time_ms: number;
36}
37
38export interface ShodanInternetDBResponse {
39 cpes: string[];
40 hostnames: string[];
41 ip: string;
42 ports: number[];
43 tags: string[];
44 vulns: string[];
45}
46
47const IPV4_REGEX = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
48
49export function parseIPv4(ip: string): [number, number, number, number] | null {
50 const match = ip.match(IPV4_REGEX);
51 if (!match) return null;
52
53 const octets = [
54 parseInt(match[1], 10),
55 parseInt(match[2], 10),
56 parseInt(match[3], 10),
57 parseInt(match[4], 10),
58 ] as [number, number, number, number];
59
60 if (octets.some((o) => o < 0 || o > 255)) return null;
61 return octets;
62}
63
64export function isPrivateOrReserved(octets: [number, number, number, number]): boolean {
65 const [a, b] = octets;
66 if (a === 10) return true;
67 if (a === 172 && b >= 16 && b <= 31) return true;
68 if (a === 192 && b === 168) return true;
69 if (a === 127) return true;
70 if (a === 100 && b >= 64 && b <= 127) return true;
71 if (a === 169 && b === 254) return true;
72 if (a >= 224) return true;
73 if (a === 0) return true;
74 return false;
75}
76
77export function ipToNumber(octets: [number, number, number, number]): number {
78 return ((octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3]) >>> 0;
79}
80
81export function numberToIp(num: number): string {
82 return [
83 (num >>> 24) & 0xff,
84 (num >>> 16) & 0xff,
85 (num >>> 8) & 0xff,
86 num & 0xff,
87 ].join('.');
88}
89
90export function calculateSubnetStart(ipNum: number, cidr: number): number {
91 const mask = (0xffffffff << (32 - cidr)) >>> 0;
92 return (ipNum & mask) >>> 0;
93}
94
95export interface DeviceClassification {
96 device_type: string;
97 device_icon: string;
98 device_color: string;
99}
100
101export function classifyDevice(
102 ports: number[],
103 cpes: string[],
104 tags: string[],
105): DeviceClassification {
106 const portSet = new Set(ports || []);
107 const cpeLower = (cpes || []).map((c) => c.toLowerCase());
108 const tagLower = (tags || []).map((t) => t.toLowerCase());
109
110 if (portSet.has(554) || portSet.has(8554) || cpeLower.some((c) => /camera|dvr|hikvision|dahua|axis|ipcam/.test(c))) {
111 return { device_type: 'Camera/DVR', device_icon: 'Camera', device_color: '#FF3D3D' };
112 }
113 if (portSet.has(9100) || cpeLower.some((c) => /printer|hp.*laserjet|epson|brother/.test(c))) {
114 return { device_type: 'Printer', device_icon: 'Printer', device_color: '#F48FB1' };
115 }
116 if (portSet.has(1883) || portSet.has(8883) || tagLower.includes('iot')) {
117 return { device_type: 'IoT Device', device_icon: 'Cpu', device_color: '#39FF14' };
118 }
119 if (portSet.has(5060) || portSet.has(5061)) {
120 return { device_type: 'VoIP/SIP', device_icon: 'Phone', device_color: '#87CEEB' };
121 }
122 if (cpeLower.some((c) => /mikrotik|ubiquiti|cisco|juniper|fortinet/.test(c)) || portSet.has(161) || portSet.has(8291)) {
123 return { device_type: 'Router/Switch', device_icon: 'Router', device_color: '#00E5FF' };
124 }
125 if (portSet.has(3306) || portSet.has(5432) || portSet.has(27017) || portSet.has(6379) || portSet.has(9200) || portSet.has(5984)) {
126 return { device_type: 'Database', device_icon: 'Database', device_color: '#FF6B00' };
127 }
128 if (portSet.has(25) || portSet.has(587) || portSet.has(993) || portSet.has(995) || portSet.has(110) || portSet.has(143)) {
129 return { device_type: 'Mail Server', device_icon: 'Mail', device_color: '#FF9500' };
130 }
131 if (portSet.has(53)) {
132 return { device_type: 'DNS Server', device_icon: 'Server', device_color: '#00BCD4' };
133 }
134 if (portSet.has(21) || portSet.has(990)) {
135 return { device_type: 'FTP Server', device_icon: 'HardDrive', device_color: '#FFD700' };
136 }
137 if (portSet.has(1194) || portSet.has(1723) || portSet.has(500) || portSet.has(4500) || cpeLower.some((c) => /openvpn|wireguard/.test(c))) {
138 return { device_type: 'VPN Gateway', device_icon: 'ShieldCheck', device_color: '#D4AF37' };
139 }
140 if (portSet.has(3389)) {
141 return { device_type: 'Windows Workstation', device_icon: 'Monitor', device_color: '#E040FB' };
142 }
143 if (portSet.has(22) && !portSet.has(80) && !portSet.has(443)) {
144 return { device_type: 'Linux Server', device_icon: 'Terminal', device_color: '#76FF03' };
145 }
146 if (portSet.has(80) || portSet.has(443) || portSet.has(8080) || portSet.has(8443)) {
147 return { device_type: 'Web Server', device_icon: 'Globe', device_color: '#448AFF' };
148 }
149 return { device_type: 'Unknown Host', device_icon: 'CircleDot', device_color: '#666666' };
150}
151
152export function assessRisk(
153 device: Partial<SweepDevice>,
154): 'CRITICAL' | 'HIGH' | 'MEDIUM' | 'LOW' | 'INFO' {
155 const vulns = device.vulns ?? [];
156 const ports = device.ports ?? [];
157 const portSet = new Set(ports);
158
159 if (vulns.length > 5) return 'CRITICAL';
160 if (vulns.length > 0) return 'HIGH';
161 if (portSet.has(23) || portSet.has(21) || portSet.has(161)) return 'MEDIUM';
162 if (ports.length > 5) return 'LOW';
163 return 'INFO';
164}
165
166export async function batchFetch<T>(
167 urls: string[],
168 concurrency: number,
169 fn: (url: string) => Promise<T | null>,
170 onProgress?: (done: number) => void
171): Promise<(T | null)[]> {
172 const results: (T | null)[] = new Array(urls.length).fill(null);
173 let idx = 0;
174 let completed = 0;
175 const workers = Array.from({ length: concurrency }, async () => {
176 while (idx < urls.length) {
177 const i = idx++;
178 results[i] = await fn(urls[i]);
179 completed++;
180 if (onProgress) onProgress(completed);
181 }
182 });
183 await Promise.all(workers);
184 return results;
185}