import {z} from 'zod' export type CreateApiSearchByIpArgs = {body: {ips: Array, sort_by?: ('date_compromised' | 'date_uploaded') | undefined, sort_direction?: ('asc' | 'desc') | undefined, types?: (Array<'employees' | 'users' | 'third_parties'>) | undefined, domains?: Array | undefined, keywords?: Array | undefined, keywords_match?: ('any' | 'all') | undefined, filter_credentials?: boolean | undefined, cursor?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, additional_fields?: (Array<'search_data' | 'installed_software' | 'employee_session_cookies' | 'password_strength' | 'sensitive_applications' | 'dir_tree'>) | undefined} | {cidr: string, sort_by?: ('date_compromised' | 'date_uploaded') | undefined, sort_direction?: ('asc' | 'desc') | undefined, types?: (Array<'employees' | 'users' | 'third_parties'>) | undefined, domains?: Array | undefined, keywords?: Array | undefined, keywords_match?: ('any' | 'all') | undefined, filter_credentials?: boolean | undefined, cursor?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, additional_fields?: (Array<'search_data' | 'installed_software' | 'employee_session_cookies' | 'password_strength' | 'sensitive_applications' | 'dir_tree'>) | undefined}}; export type CreateApiSearchByIpOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const stealerData = z.object({stealer: z.string().optional(), employeeAt: z.string().optional(), clientAt: z.string().optional(), date_compromised: z.string().optional(), date_uploaded: z.string().optional(), stealer_family: z.string().optional(), ip: z.string().optional(), computer_name: z.string().optional(), operating_system: z.string().optional(), malware_path: z.string().optional(), credentials: z.array(z.object({url: z.string().optional(), domain: z.string().optional(), username: z.string().optional(), password: z.string().optional(), type: z.enum(["employee", "user", "third_party"]).optional()})).optional()}); export const searchResponse = z.object({data: z.array(stealerData).optional(), nextCursor: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Search by IP/CIDR * * POST /search-by-ip */ export const createApiSearchByIp = async (args: CreateApiSearchByIpArgs, options?: CreateApiSearchByIpOptions) => { const {baseUrl = "https://api.hudsonrock.com/json/v3", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/search-by-ip${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(searchResponse, await res.json()) };