-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.js
More file actions
188 lines (141 loc) Β· 4.09 KB
/
Copy pathagent.js
File metadata and controls
188 lines (141 loc) Β· 4.09 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const axios = require("axios");
const { runTool } = require("./tools/runTool");
const { parseFfufOutput } = require("./tools/parsers/ffufParser");
const { detectLogin } = require("./tools/parsers/loginDetector");
const { buildExploitChain } = require("./tools/exploitPlanner");
const { saveMemory, searchMemory } = require("./memory/ragStore");
// ========================
// π§ OLLAMA CALL
// ========================
async function queryOllama(model, prompt) {
const res = await axios.post("http://localhost:11434/api/generate", {
model,
prompt,
stream: false
});
return res.data.response;
}
// ========================
// π§ MODEL ROUTER
// ========================
function chooseModel(taskType, inputLength) {
if (taskType === "analysis") return "llama3";
return inputLength > 1500 ? "llama3" : "mistral";
}
// ========================
// π§ RECON PLANNER (AI + RAG)
// ========================
async function planRecon(target) {
const past = searchMemory(target) || [];
const prompt = `
You are GhostShell Recon AI.
Return ONLY a valid JSON array.
Allowed tools:
whois, dig, headers, whatweb, ffuf
IMPORTANT RULES:
- Output ONLY JSON
- No markdown
- No explanation
Target: ${target}
PAST KNOWLEDGE:
${JSON.stringify(past.slice(-3), null, 2)}
`;
const res = await queryOllama("mistral", prompt);
try {
// π§ STEP 1: clean response
let cleaned = res
.replace(/```json/g, "")
.replace(/```/g, "")
.trim();
// π§ STEP 2: extract JSON array safely
const match = cleaned.match(/\[[\s\S]*\]/);
if (!match) throw new Error("No JSON array found");
const parsed = JSON.parse(match[0]);
return Array.isArray(parsed)
? parsed
: ["whois", "dig", "headers", "whatweb"];
} catch (e) {
console.log("[!] JSON parse failed, using fallback tools");
return ["whois", "dig", "headers", "whatweb"];
}
}
// ========================
// βοΈ TOOL EXECUTOR
// ========================
async function executeTools(tools, target) {
let results = {};
let intel = {
login: [],
ffuf: []
};
for (const tool of tools) {
console.log(`[+] Running ${tool}...`);
const output = await runTool(tool, target);
results[tool] = output;
// π§ LOGIN DETECTION (FIXED β append instead of overwrite)
if (tool === "whatweb" || tool === "headers") {
const loginSignals = detectLogin(output);
intel.login.push(...loginSignals);
}
// π§ FFUF INTELLIGENCE
if (tool === "ffuf") {
intel.ffuf = parseFfufOutput(output);
}
}
// π§ Deduplicate login signals
intel.login = [...new Set(intel.login)];
return { results, intel };
}
// ========================
// π§ FINAL ANALYSIS (AI)
// ========================
async function analyzeResults(resultsObj, target) {
const model = "llama3";
const { results, intel } = resultsObj;
const exploitChain = await buildExploitChain(intel, results);
const prompt = `
You are GhostShell, elite penetration tester.
Analyze recon data.
TARGET:
${target}
RECON:
${JSON.stringify(results, null, 2)}
INTEL:
${JSON.stringify(intel, null, 2)}
EXPLOIT CHAIN:
${JSON.stringify(exploitChain, null, 2)}
Output:
1. Key findings
2. Attack priority
3. Step-by-step exploitation plan
`;
const response = await queryOllama(model, prompt);
return response;
}
// ========================
// π MAIN AGENT
// ========================
async function runAgent(taskType, input, target) {
const model = chooseModel(taskType, input.length);
console.log("\n[+] Planning recon...");
const tools = await planRecon(target);
console.log("[+] Tools selected:", tools);
console.log("\n[+] Executing tools...");
const resultsObj = await executeTools(tools, target);
console.log("\n[+] Analyzing results...");
const final = await analyzeResults(resultsObj, target);
// π§ MEMORY SAVE (FIXED POSITION)
saveMemory({
timestamp: new Date().toISOString(),
target,
toolsUsed: tools,
intel: resultsObj.intel,
output: final
});
return {
modelUsed: model,
toolsUsed: tools,
output: final
};
}
module.exports = { runAgent };