Elffuss BraidHub

kikocis/osiris public

master · 4.0 KiBRawPermalink
1import { NextResponse } from 'next/server';
2import { isRateLimited, getClientIp } from '@/lib/ssrf-guard';
3
4// Threat Intelligence — AlienVault OTX public pulse feed + Tor exit nodes
5export async function GET(req: Request) {
6 const { searchParams } = new URL(req.url);
7 const query = searchParams.get('query'); // Optional: IP or domain to check
8
9 const clientIp = getClientIp(req);
10 if (isRateLimited(clientIp, 20, 60_000)) {
11 return NextResponse.json({ error: 'Rate limit exceeded' }, { status: 429 });
12 }
13
14 try {
15 const results: any = { timestamp: new Date().toISOString() };
16
17 // 1. AlienVault OTX — public pulse feed (no key needed for public data)
18 try {
19 const res = await fetch('https://otx.alienvault.com/api/v1/pulses/subscribed?limit=10&page=1', {
20 signal: AbortSignal.timeout(8000),
21 headers: { 'Accept': 'application/json' },
22 });
23 // Public endpoint may require auth, fall back to activity feed
24 if (!res.ok) {
25 const actRes = await fetch('https://otx.alienvault.com/api/v1/pulses/activity?limit=10', {
26 signal: AbortSignal.timeout(8000),
27 });
28 if (actRes.ok) {
29 const data = await actRes.json();
30 results.pulses = (data.results || []).slice(0, 10).map((p: any) => ({
31 name: p.name,
32 description: p.description?.slice(0, 200),
33 created: p.created,
34 modified: p.modified,
35 tags: p.tags?.slice(0, 5),
36 adversary: p.adversary,
37 targeted_countries: p.targeted_countries,
38 indicators_count: p.indicator_count,
39 }));
40 }
41 }
42 } catch (e) { console.warn('[OSIRIS] Suppressed error:', e instanceof Error ? e.message : e); }
43
44 // 2. Check specific IP/domain if provided
45 if (query) {
46 const isIP = /^(\d{1,3}\.){3}\d{1,3}$/.test(query);
47
48 if (isIP) {
49 // Check against Tor exit node list
50 try {
51 const torRes = await fetch('https://check.torproject.org/torbulkexitlist', {
52 signal: AbortSignal.timeout(5000),
53 });
54 if (torRes.ok) {
55 const torList = await torRes.text();
56 results.tor_exit_node = torList.includes(query);
57 }
58 } catch {
59 results.tor_exit_node = null;
60 }
61
62 // AlienVault OTX IP reputation (public)
63 try {
64 const res = await fetch(`https://otx.alienvault.com/api/v1/indicators/IPv4/${query}/general`, {
65 signal: AbortSignal.timeout(5000),
66 });
67 if (res.ok) {
68 const data = await res.json();
69 results.otx = {
70 reputation: data.reputation,
71 pulse_count: data.pulse_info?.count || 0,
72 country: data.country_name,
73 asn: data.asn,
74 };
75 }
76 } catch (e) { console.warn('[OSIRIS] Suppressed error:', e instanceof Error ? e.message : e); }
77 } else {
78 // Domain check
79 try {
80 const res = await fetch(`https://otx.alienvault.com/api/v1/indicators/domain/${encodeURIComponent(query)}/general`, {
81 signal: AbortSignal.timeout(5000),
82 });
83 if (res.ok) {
84 const data = await res.json();
85 results.otx = {
86 pulse_count: data.pulse_info?.count || 0,
87 whois: data.whois ? {
88 registrar: data.whois.registrar,
89 creation_date: data.whois.creation_date,
90 expiration_date: data.whois.expiration_date,
91 } : null,
92 };
93 }
94 } catch (e) { console.warn('[OSIRIS] Suppressed error:', e instanceof Error ? e.message : e); }
95 }
96 }
97
98 results.threat_level = (results.otx?.pulse_count || 0) > 5 ? 'HIGH' :
99 (results.otx?.pulse_count || 0) > 0 ? 'MEDIUM' : 'LOW';
100
101 return NextResponse.json(results);
102 } catch {
103 return NextResponse.json({ error: 'Threat lookup failed' }, { status: 500 });
104 }
105}