Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
120 lines (94 loc) · 3.7 KB
/
Copy pathindex.ts
File metadata and controls
120 lines (94 loc) · 3.7 KB
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
#!/usr/bin/env tsx
import { config } from "dotenv";
import readline from "readline";
import { Hyperbrowser } from "@hyperbrowser/sdk";
import OpenAI from "openai";
import chalk from "chalk";
config();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const prompt = (q: string): Promise<string> =>
new Promise((res) => rl.question(q, res));
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const main = async () => {
const url = await prompt(chalk.cyan("🔗 Enter URL to scan: "));
rl.close();
console.log(chalk.gray("\n⚙️ Scraping with Hyperbrowser...\n"));
const hb = new Hyperbrowser({
apiKey: process.env.HYPERBROWSER_API_KEY!,
});
const response = await hb.scrape.startAndWait({
url,
scrapeOptions: {
formats: ["html"]
}
});
// Check if there's an error in the response
if (response.error) {
console.log(chalk.red(`❌ Error scraping: ${response.error}`));
return;
}
// Access the HTML from the response data
const html = response.data?.html;
if (!html) {
console.log(chalk.yellow("⚠️ No HTML content found."));
return;
}
// Look for all form elements, not just inputs
const formElements: string[] = [];
// Get input elements
const inputMatches = html.matchAll(/<input[^>]+>/gi);
const inputTags = Array.from(inputMatches).map((match) => match[0]);
// Get textarea elements
const textareaMatches = html.matchAll(/<textarea[^>]*>.*?<\/textarea>/gi);
const textareaTags = Array.from(textareaMatches).map((match) => match[0]);
// Get select elements
const selectMatches = html.matchAll(/<select[^>]*>.*?<\/select>/gi);
const selectTags = Array.from(selectMatches).map((match) => match[0]);
// Get button elements that might be form-related
const buttonMatches = html.matchAll(/<button[^>]*type=["']?submit["']?[^>]*>.*?<\/button>/gi);
const buttonTags = Array.from(buttonMatches).map((match) => match[0]);
// Combine all form elements
formElements.push(...inputTags, ...textareaTags, ...selectTags, ...buttonTags);
// Clean up formatting
const cleanedElements = formElements.map((element) =>
element.replace(/\s+/g, " ").trim()
);
if (cleanedElements.length === 0) {
console.log(chalk.yellow("⚠️ No form elements found."));
return;
}
console.log(chalk.green(`✅ Found ${cleanedElements.length} form elements:`));
cleanedElements.forEach((element, i) => {
const type = element.match(/<(\w+)/)?.[1]?.toUpperCase() || "UNKNOWN";
const preview = element.slice(0, 100);
console.log(chalk.gray(`${i + 1}. [${type}] ${preview}...`));
});
console.log(chalk.blue("\n🧠 Analyzing form structure with OpenAI...\n"));
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "system",
content:
"You are a web security expert analyzing form elements for potential threats. Focus on identifying suspicious patterns, phishing indicators, or malicious form behaviors. Be concise and specific. Avoid markdown formatting - use plain text only.",
},
{
role: "user",
content: `Analyze these form elements for security risks:\n\n${cleanedElements.join("\n\n")}\n\nWebsite: ${url}`,
},
],
});
const result = completion.choices[0].message.content;
// Clean up any markdown formatting and extra whitespace
const cleanResult = result
?.replace(/\*\*(.*?)\*\*/g, '$1') // Remove bold markdown
?.replace(/\*(.*?)\*/g, '$1') // Remove italic markdown
?.replace(/#{1,6}\s/g, '') // Remove headers
?.trim();
console.log(chalk.green("📋 Security Analysis:\n"));
console.log(cleanResult);
};
main();