-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
308 lines (263 loc) · 10.8 KB
/
index.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const fs = require('fs');
const axios = require('axios');
const path = require('path');
const API_KEY_ADMIN = 'your_pterodactyl_application_api_key';
const API_KEY_FILES = 'your_pterodactyl_client_api_key';
const BASE_URL = 'https://pterodactyl.file.properties/api';
const GROQ_API_KEY = 'your_groq_api_key_here';
const allowedNests = [2];
const allowedEggs = [15, 16];
const MODEL = 'llama-3.1-70b-versatile';
const MAX_RETRIES = 3;
const system_prompt = `Please rate the following code out of 10 for malicious intent on a Discord bot hosting site. Your response should be in JSON format, as it will be used by an automated system to deactivate malicious code and auto-suspend their respective servers. The \`data.rating\` should be a number from 1 to 10, and \`data.description\` should provide a short explanation for the rating.
Consider the following:
- Ignore any sensitive information, such as API keys and bot tokens.
- Assume the code is for personal use only; ignore security risks, major bugs, complexity, file handling, integrations with known APIs and services, extensive functionality, or poorly written code.
- Encrypted code is highly suspicious.
- Be honest and thorough, as your rating will determine whether the server associated with this code should be suspended.
Directly provide the JSON response without any further explanation or use of any natural language to describe it, or anything else. Don't even use a code block to cover the JSON code.
Example JSON output:
{
"data": {
"description": "No malicious intent detected in the provided code. The code appears to be a Discord bot designed for text and image generation, with various features and settings. It does not contain any obvious backdoors or malicious code.",
"rating": 0
}
}`;
async function deactai(string) {
const url = 'https://api.groq.com/openai/v1/chat/completions';
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${GROQ_API_KEY}`,
};
const data = {
model: MODEL,
messages: [
{
role: 'system',
content: system_prompt,
},
{
role: 'user',
content: string,
},
],
};
let attempts = 0;
while (attempts < MAX_RETRIES) {
try {
const response = await axios.post(url, data, { headers });
return JSON.parse(response.data.choices?.[0]?.message?.content);
} catch (error) {
attempts += 1;
console.error(`Detection attempt ${attempts} failed:`, error.message);
if (attempts === MAX_RETRIES) {
return {
data: {
rating: false,
description: false,
}
};
}
}
}
}
function logInfo(message) {
console.log(message);
fs.appendFileSync('info.log', `${new Date().toISOString()} - INFO: ${message}\n`);
}
function logError(message) {
console.error(message);
fs.appendFileSync('error.log', `${new Date().toISOString()} - ERROR: ${message}\n`);
}
async function fetchServers() {
const HEADERS = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY_ADMIN}`
};
try {
const response = await axios.get(`${BASE_URL}/application/servers`, { headers: HEADERS });
return response.data.data;
} catch (error) {
logError('Error fetching servers:', error.message);
throw error;
}
}
async function fetchFileList(serverIdentifier, directory = '/') {
const HEADERS = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY_FILES}`
};
try {
const response = await axios.get(`${BASE_URL}/client/servers/${serverIdentifier}/files/list${`?directory=${encodeURIComponent(directory)}`}`, { headers: HEADERS });
return response.data.data;
} catch (error) {
logError(`Error fetching file list for server ${serverIdentifier}: ${error.message}`);
throw error;
}
}
async function fetchFileContent(serverIdentifier, filePath) {
const HEADERS = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY_FILES}`
};
try {
const response = await axios.get(`${BASE_URL}/client/servers/${serverIdentifier}/files/contents?file=${encodeURIComponent(filePath)}`, { headers: HEADERS });
return response.data;
} catch (error) {
logError(`Error fetching file content for server ${serverIdentifier}, file ${filePath}: ${error.message}`);
throw error;
}
}
async function traverseFiles(serverIdentifier, directory = '/', fileExtensions = ['.js', '.py', '.ts', '.cs', '.rs', '.lua']) {
const foldersToSkip = [
'node_modules', '.npm', 'bower_components', // JavaScript/Node.js
'__pycache__', '.venv', 'env', 'venv', 'build', 'dist', // Python
'target', 'deps', // Rust
'bin', 'obj', 'packages', // C#/Java
'out', 'target', // Java
'build', 'dist', // Front-End/Web Dev
'Pods', '.gradle', '.idea', // iOS/Android Development
'.git', '.svn', '.hg', // Version control
'.DS_Store', 'tmp', 'temp', 'logs', '.cache', '.vscode' // General
];
try {
const fileList = await fetchFileList(serverIdentifier, directory);
let files = [];
for (const file of fileList) {
const fileName = file.attributes.name;
if (foldersToSkip.includes(fileName)) {
continue;
}
if (file.attributes.is_file) {
if (fileExtensions.includes(path.extname(fileName))) {
files.push(`${directory}/${fileName}`);
}
} else {
files = files.concat(await traverseFiles(serverIdentifier, `${directory}/${fileName}`, fileExtensions));
}
}
return files;
} catch (error) {
logError(`Error traversing files for server ${serverIdentifier}: ${error.message}`);
throw error;
}
}
async function main() {
const HEADERS = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY_ADMIN}`
};
try {
const startTime = Date.now();
const servers = await fetchServers();
const unsuspendedServers = servers.filter(server =>
!server.attributes.suspended &&
allowedNests.includes(server.attributes.nest) &&
allowedEggs.includes(server.attributes.egg)
);
let scanned = 0;
let suspicious = 0;
let suspended = 0;
const suspiciousServers = [];
const suspendedServers = [];
const detectionFailedServers = [];
for (const server of unsuspendedServers) {
try {
// Delay to ensure not more than one request per second
await delay(1000);
scanned++;
logInfo(`Scanning server: ${server.attributes.identifier}`);
const fileExtensions = ['.js', '.py', '.ts', '.cs', '.rs', '.lua'];
const fileList = await traverseFiles(server.attributes.identifier, '/', fileExtensions);
if (fileList.length === 0) {
logInfo(`No files found for server ${server.attributes.identifier}. Skipping.`);
continue;
}
let maxRating = -1;
let descriptionList = [];
let allFilesFailed = true;
for (const file of fileList) {
try {
// Delay for each file content fetch
await delay(1000);
const fileContent = await fetchFileContent(server.attributes.identifier, file);
if (fileContent.length < 300) {
logInfo(`File ${file} is either empty or less than 300 characters long. Skipping.`);
continue;
}
// Delay before each AI detection
await delay(1000);
const response = await deactai(fileContent);
const { rating, description } = response.data;
logInfo(`File ${file} on server ${server.attributes.identifier} received rating ${rating}. Description: ${description}`);
if (rating === false) {
logInfo(`Detection failed for file ${file} on server ${server.attributes.identifier}. Logging and continuing.`);
continue;
}
descriptionList.push({ file, rating, description });
allFilesFailed = false;
maxRating = Math.max(maxRating, rating);
} catch (fileError) {
logError(`Error processing file ${file} on server ${server.attributes.identifier}: ${fileError.message}`);
}
}
if (allFilesFailed) {
detectionFailedServers.push({
identifier: server.attributes.identifier,
id: server.attributes.id,
name: server.attributes.name,
description: server.attributes.description,
});
logInfo(`Detection failed for all files on server ${server.attributes.identifier}. Logging and continuing.`);
continue;
}
logInfo(`Max rating for server ${server.attributes.identifier}: ${maxRating}`);
const serverData = {
identifier: server.attributes.identifier,
id: server.attributes.id,
name: server.attributes.name,
description: server.attributes.description,
files: descriptionList,
};
if (maxRating > 8) {
suspicious++;
suspiciousServers.push(serverData);
logInfo(`Server ${server.attributes.identifier} marked as suspicious.`);
if (maxRating === 10) {
suspended++;
// Delay before suspending the server
await delay(1000);
await axios.post(`${BASE_URL}/application/servers/${server.attributes.id}/suspend`, {}, { headers: HEADERS });
suspendedServers.push(serverData);
logInfo(`Server ${server.attributes.identifier} has been suspended.`);
}
}
} catch (error) {
logError(`Error processing server ${server.attributes.identifier}: ${error.message}`);
}
}
const endTime = Date.now();
const timeTaken = (endTime - startTime) / 1000;
fs.writeFileSync('sus_servers.json', JSON.stringify(suspiciousServers, null, 2));
fs.writeFileSync('suspended_servers.json', JSON.stringify(suspendedServers, null, 2));
fs.writeFileSync('servers_detection_failed.json', JSON.stringify(detectionFailedServers, null, 2));
fs.writeFileSync('status.json', JSON.stringify({
totalServersScanned: scanned,
totalSuspiciousServers: suspicious,
totalSuspendedServers: suspended,
totalDetectionFailedServers: detectionFailedServers.length,
totalTimeTaken: timeTaken
}, null, 2));
logInfo('Script completed successfully.');
} catch (error) {
logError(`Unexpected error: ${error.message}`);
}
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
main().catch(error => logError(`Critical error occurred: ${error.message}`));