|
| 1 | +import { z } from 'zod'; |
| 2 | +import { |
| 3 | + componentRegistry, |
| 4 | + ComponentDefinition, |
| 5 | + port, |
| 6 | + ValidationError, |
| 7 | + ConfigurationError, |
| 8 | + fromHttpResponse, |
| 9 | + ComponentRetryPolicy, |
| 10 | +} from '@shipsec/component-sdk'; |
| 11 | + |
| 12 | +const inputSchema = z.object({ |
| 13 | + ipAddress: z.string().describe('The IPv4 or IPv6 address you want to check.'), |
| 14 | + maxAgeInDays: z.number().default(90).describe('Max age in days for reports to be included (default: 90).'), |
| 15 | + verbose: z.boolean().default(false).describe('Include verbose information.'), |
| 16 | + apiKey: z.string().describe('Your AbuseIPDB API Key.'), |
| 17 | +}); |
| 18 | + |
| 19 | +const outputSchema = z.object({ |
| 20 | + ipAddress: z.string().describe('The IP address that was checked.'), |
| 21 | + isPublic: z.boolean().optional(), |
| 22 | + ipVersion: z.number().optional(), |
| 23 | + isWhitelisted: z.boolean().optional(), |
| 24 | + abuseConfidenceScore: z.number().describe('The confidence score (0-100).'), |
| 25 | + countryCode: z.string().optional(), |
| 26 | + usageType: z.string().optional(), |
| 27 | + isp: z.string().optional(), |
| 28 | + domain: z.string().optional(), |
| 29 | + hostnames: z.array(z.string()).optional(), |
| 30 | + totalReports: z.number().optional(), |
| 31 | + numDistinctUsers: z.number().optional(), |
| 32 | + lastReportedAt: z.string().optional(), |
| 33 | + reports: z.array(z.record(z.string(), z.any())).optional(), |
| 34 | + full_report: z.record(z.string(), z.any()).describe('The full raw JSON response.'), |
| 35 | +}); |
| 36 | + |
| 37 | +type Input = z.infer<typeof inputSchema>; |
| 38 | +type Output = z.infer<typeof outputSchema>; |
| 39 | + |
| 40 | +const abuseIPDBRetryPolicy: ComponentRetryPolicy = { |
| 41 | + maxAttempts: 4, |
| 42 | + initialIntervalSeconds: 2, |
| 43 | + maximumIntervalSeconds: 120, |
| 44 | + backoffCoefficient: 2.0, |
| 45 | + nonRetryableErrorTypes: [ |
| 46 | + 'AuthenticationError', |
| 47 | + 'ValidationError', |
| 48 | + 'ConfigurationError', |
| 49 | + ], |
| 50 | +}; |
| 51 | + |
| 52 | +const definition: ComponentDefinition<Input, Output> = { |
| 53 | + id: 'security.abuseipdb.check', |
| 54 | + label: 'AbuseIPDB Check', |
| 55 | + category: 'security', |
| 56 | + runner: { kind: 'inline' }, |
| 57 | + retryPolicy: abuseIPDBRetryPolicy, |
| 58 | + inputSchema, |
| 59 | + outputSchema, |
| 60 | + docs: 'Check the reputation of an IP address using the AbuseIPDB API.', |
| 61 | + metadata: { |
| 62 | + slug: 'abuseipdb-check', |
| 63 | + version: '1.0.0', |
| 64 | + type: 'scan', |
| 65 | + category: 'security', |
| 66 | + description: 'Get threat intelligence reports for an IP from AbuseIPDB.', |
| 67 | + icon: 'Shield', |
| 68 | + author: { name: 'ShipSecAI', type: 'shipsecai' }, |
| 69 | + isLatest: true, |
| 70 | + deprecated: false, |
| 71 | + inputs: [ |
| 72 | + { id: 'ipAddress', label: 'IP Address', dataType: port.text(), required: true }, |
| 73 | + { id: 'apiKey', label: 'API Key', dataType: port.secret(), required: true }, |
| 74 | + { id: 'maxAgeInDays', label: 'Max Age (Days)', dataType: port.number(), required: false }, |
| 75 | + { id: 'verbose', label: 'Verbose', dataType: port.boolean(), required: false }, |
| 76 | + ], |
| 77 | + outputs: [ |
| 78 | + { id: 'abuseConfidenceScore', label: 'Confidence Score', dataType: port.number() }, |
| 79 | + { id: 'isWhitelisted', label: 'Whitelisted', dataType: port.boolean() }, |
| 80 | + { id: 'countryCode', label: 'Country', dataType: port.text() }, |
| 81 | + { id: 'isp', label: 'ISP', dataType: port.text() }, |
| 82 | + { id: 'totalReports', label: 'Total Reports', dataType: port.number() }, |
| 83 | + { id: 'full_report', label: 'Full Report', dataType: port.json() }, |
| 84 | + ], |
| 85 | + }, |
| 86 | + resolvePorts(params) { |
| 87 | + return { |
| 88 | + inputs: [ |
| 89 | + { id: 'ipAddress', label: 'IP Address', dataType: port.text(), required: true }, |
| 90 | + { id: 'apiKey', label: 'API Key', dataType: port.secret(), required: true }, |
| 91 | + { id: 'maxAgeInDays', label: 'Max Age (Days)', dataType: port.number(), required: false }, |
| 92 | + { id: 'verbose', label: 'Verbose', dataType: port.boolean(), required: false }, |
| 93 | + ], |
| 94 | + outputs: [ |
| 95 | + { id: 'abuseConfidenceScore', label: 'Confidence Score', dataType: port.number() }, |
| 96 | + { id: 'isWhitelisted', label: 'Whitelisted', dataType: port.boolean() }, |
| 97 | + { id: 'countryCode', label: 'Country', dataType: port.text() }, |
| 98 | + { id: 'isp', label: 'ISP', dataType: port.text() }, |
| 99 | + { id: 'totalReports', label: 'Total Reports', dataType: port.number() }, |
| 100 | + { id: 'full_report', label: 'Full Report', dataType: port.json() }, |
| 101 | + ] |
| 102 | + }; |
| 103 | + }, |
| 104 | + async execute(params, context) { |
| 105 | + const { ipAddress, apiKey, maxAgeInDays, verbose } = params; |
| 106 | + |
| 107 | + if (!ipAddress) { |
| 108 | + throw new ValidationError('IP Address is required', { |
| 109 | + fieldErrors: { ipAddress: ['IP Address is required'] }, |
| 110 | + }); |
| 111 | + } |
| 112 | + if (!apiKey) { |
| 113 | + throw new ConfigurationError('AbuseIPDB API Key is required', { |
| 114 | + configKey: 'apiKey', |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + const endpoint = 'https://api.abuseipdb.com/api/v2/check'; |
| 119 | + const queryParams = new URLSearchParams({ |
| 120 | + ipAddress, |
| 121 | + maxAgeInDays: String(maxAgeInDays), |
| 122 | + }); |
| 123 | + if (verbose) { |
| 124 | + queryParams.append('verbose', 'true'); |
| 125 | + } |
| 126 | + |
| 127 | + const url = `${endpoint}?${queryParams.toString()}`; |
| 128 | + |
| 129 | + context.logger.info(`[AbuseIPDB] Checking IP: ${ipAddress}`); |
| 130 | + |
| 131 | + const response = await fetch(url, { |
| 132 | + method: 'GET', |
| 133 | + headers: { |
| 134 | + 'Key': apiKey, |
| 135 | + 'Accept': 'application/json' |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + if (response.status === 404) { |
| 140 | + context.logger.warn(`[AbuseIPDB] IP not found: ${ipAddress}`); |
| 141 | + return { |
| 142 | + ipAddress, |
| 143 | + abuseConfidenceScore: 0, |
| 144 | + full_report: { error: 'Not Found' } |
| 145 | + }; |
| 146 | + } |
| 147 | + |
| 148 | + if (!response.ok) { |
| 149 | + const text = await response.text(); |
| 150 | + throw fromHttpResponse(response, text); |
| 151 | + } |
| 152 | + |
| 153 | + const data = await response.json() as any; |
| 154 | + const info = data.data || {}; |
| 155 | + |
| 156 | + context.logger.info(`[AbuseIPDB] Score for ${ipAddress}: ${info.abuseConfidenceScore}`); |
| 157 | + |
| 158 | + return { |
| 159 | + ipAddress: info.ipAddress, |
| 160 | + isPublic: info.isPublic, |
| 161 | + ipVersion: info.ipVersion, |
| 162 | + isWhitelisted: info.isWhitelisted, |
| 163 | + abuseConfidenceScore: info.abuseConfidenceScore, |
| 164 | + countryCode: info.countryCode, |
| 165 | + usageType: info.usageType, |
| 166 | + isp: info.isp, |
| 167 | + domain: info.domain, |
| 168 | + hostnames: info.hostnames, |
| 169 | + totalReports: info.totalReports, |
| 170 | + numDistinctUsers: info.numDistinctUsers, |
| 171 | + lastReportedAt: info.lastReportedAt, |
| 172 | + reports: info.reports, // Only present if verbose is true |
| 173 | + full_report: data, |
| 174 | + }; |
| 175 | + }, |
| 176 | +}; |
| 177 | + |
| 178 | +componentRegistry.register(definition); |
| 179 | + |
| 180 | +export { definition }; |
0 commit comments