const synFloodTracker = {};
const MAX_SYN_REQUESTS = 10; // Max allowed per minute
const SYN_BLOCK_TIME_MS = 60000; // Block time if SYN flood detected
// Rate Limiting Configurations
const rateLimit = {};
const MAX_REQUESTS_PER_MINUTE = 100; // Set your limit here (100)
const BLOCK_TIME_MS = 60000; // Time to block the IP (1 minute) (60000)
// DNS attack prevention configurations
const dnsWhitelist = ['8.8.8.8', '1.1.1.1']; // Trusted DNS resolvers (Google DNS and Cloudflare DNS)
const MAX_DNS_REQUESTS_PER_MINUTE = 50; // Limit DNS-related requests
const dnsRateLimit = {};
// Function to normalize IP addresses
const normalizeIp = (ip) => {
if (ip.startsWith("::ffff:")) {
return ip.substring(7);
}
if (ip === "::1") {
return "127.0.0.1";
}
return ip;
};
// Function to check if IP is from cloud/server ranges
const isCloudOrServerIp = (ip) => {
return cloudServerIpRanges.some((range) => ip.startsWith(range));
};
// Helper function to check GeoIP blocking
const isBlockedGeoIp = (ip) => {
const geo = geoip.lookup(ip);
if (geo && !allowedCountries.includes(geo.country)) {
return true;
}
return false;
};
const isSynFlood = (ip) => {
const now = Date.now();
if (!synFloodTracker[ip]) {
synFloodTracker[ip] = { count: 1, timestamp: now };
return false;
}
const timePassed = now - synFloodTracker[ip].timestamp;
if (timePassed > SYN_BLOCK_TIME_MS) {
synFloodTracker[ip] = { count: 1, timestamp: now };
return false;
} else {
synFloodTracker[ip].count++;
if (synFloodTracker[ip].count > MAX_SYN_REQUESTS) {
console.log(chalk.red(SYN flood detected from IP: ${ip}));
logBlockedIP(ip, 'SYN Flood');
return true;
}
}
return false;
};
// Start UI Server
configApp.listen(CONFIG_PORT, () => {
console.log(chalk.blue(Admin dashboard is running on http://localhost:${CONFIG_PORT}/config));
});
// Start Server Function
const startServer = async () => {
const proxy = httpProxy.createProxyServer({});
const pk = fs.readFileSync(path.join(__dirname, 'server.key'));
const pc = fs.readFileSync(path.join(__dirname, 'server.crt'));
const optss = { key: pk, cert: pc };
const trustedIPs = ['127.0.0.1'];
const port = process.argv[2] || 443;
EventEmitter.defaultMaxListeners = 100;
const packet = server|${config.ip}\nport|${config.port}\ntype|1\n#maint|Server is currently initializing or re-syncing with sub servers. Please try again in a minute.\n\n\nloginurl|${config.loginurl}\nbeta_server|127.0.0.1\nbeta_port|17091\nbeta_type|1\nbeta2_server|127.0.0.1\nbeta2_port|17099\nbeta2_type|1\nmeta|${Math.floor(Date.now() / 1000)}\nRTENDMARKERBS1001;
const server = https.createServer(optss, function (req, res) {
let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const normalizedIp = normalizeIp(ip);
const hostHeader = req.headers.host;
// Set keepAliveTimeout, which sets how long a connection should remain open in milliseconds.
server.keepAliveTimeout = 60 * 1000; // Keep alive for 60 seconds
// Set the headers timeout slightly longer than keepAliveTimeout to accommodate requests that arrive just before the keep alive timeout
server.headersTimeout = 65 * 1000; // 65 seconds
code
// Handle /growtopia/server_data.php - Only POST and allowed User-Agents
if (req.url === "/growtopia/server_data.php") {
if (req.method !== 'POST' || hostHeader !== 'www.growtopia1.com') {
console.log(chalk.red(Mata mata : ${req.url}));
res.statusCode = 403;
res.end(`
server|normalizedIpport∣{Math.floor(Date.now() / 69)}
type|1
#maint|Server is currently initializing or re-syncing with sub servers. Please try again in a minute.
server.listen(port, () => {
console.log(chalk.green(Server is running on port ${port}));
});
// -------------------- UDP Server Setup --------------------
const udpServer = dgram.createSocket('udp4');
const UDP_PORT = 53; // DNS typically uses port 53
udpServer.on('error', (err) => {
console.error(UDP server error:\n${err.stack});
udpServer.close();
});
udpServer.on('message', (msg, rinfo) => {
const ip = rinfo.address;
const normalizedIp = normalizeIp(ip);
console.log(chalk.yellow(UDP request from IP: ${normalizedIp}, Port: ${rinfo.port}));
code
// Rate Limiting
if (isUdpRateLimited(normalizedIp)) {
console.log(chalk.red(`UDP Rate limit exceeded for IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'UDP');
return; // Drop the packet silently or send a response indicating rate limiting
}
// GeoIP Blocking
if (isBlockedGeoIp(normalizedIp)) {
console.log(chalk.red(`Blocking UDP access from IP: ${normalizedIp} (GeoIP Block)`));
logBlockedIP(normalizedIp, 'UDP-GeoIP');
return; // Drop the packet silently or send a forbidden response
}
// Network Usage Limiting for UDP
const messageBytes = msg.length;
if (isNetworkUsageLimited(normalizedIp, messageBytes)) {
console.log(chalk.red(`Network usage limit exceeded for IP: ${normalizedIp} (UDP)`));
logBlockedIP(normalizedIp, 'UDP-NETWORK');
return; // Drop the packet
}
// Implement your DNS handling logic here
// For example, respond with a simple DNS response or forward the request to a legitimate DNS server
// Example: Forward the DNS request to an external DNS server (e.g., Google DNS)
const externalDns = '8.8.8.8';
const externalDnsPort = 53;
const externalClient = dgram.createSocket('udp4');
externalClient.send(msg, 0, msg.length, externalDnsPort, externalDns, (error) => {
if (error) {
console.error(`Error forwarding UDP request: ${error}`);
externalClient.close();
}
});
externalClient.on('message', (response) => {
udpServer.send(response, 0, response.length, rinfo.port, rinfo.address, (err) => {
if (err) {
console.error(`Error sending UDP response: ${err}`);
}
externalClient.close();
});
});
// Optional: Implement timeouts to prevent hanging
externalClient.on('error', (err) => {
console.error(`External DNS client error: ${err}`);
externalClient.close();
});
// Set a timeout for the external DNS response
externalClient.setTimeout(5000, () => { // 5 seconds timeout
console.error(`External DNS request timed out for IP: ${normalizedIp}`);
externalClient.close();
});
});
udpServer.on('listening', () => {
const address = udpServer.address();
console.log(chalk.green(UDP Server listening on ${address.address}:${address.port}));
});
udpServer.bind(UDP_PORT);
const shutdown = () => {
console.log('Shutting down the server...');
code
// Stop the server from accepting new connections
server.close(() => {
console.log('HTTP server closed.');
// Close the UDP server
udpServer.close(() => {
console.log('UDP server closed.');
// Exit the process
process.exit(0);
});
});
// Ensure shutdown if connections remain
setTimeout(() => {
console.error('Forced shutdown due to open connections.');
process.exit(1); // exit with error code if connections remain
}, 5000).unref();
is there any other way to protect the POST /growtopia/server_data.php from program like curl or any, don't use challenge / token / api method cuz it will not work for the game
Assistant
To protect the POST /growtopia/server_data.php endpoint from unauthorized access or abuse by tools like curl, while not relying on challenges or token-based methods, you can employ a combination of alternative strategies:
IP Whitelisting:
Maintain a list of allowed IP addresses from which legitimate requests can originate. Ensure that only requests from these IPs are processed.
Consider dynamic IP ranges if the game clients use varying but predictable IP ranges.
User-Agent Verification:
While you mentioned you already check the User-Agent, ensure it’s a comprehensive list and check for common discrepancies known to be used by non-legitimate clients.
Be cautious, as User-Agent strings can be easily spoofed.
Rate Limiting:
Implement finer-grained rate limiting specifically for the server_data.php endpoint. This can help limit the potential impact of automated tools while allowing normal game traffic.
Request Size and Format Validation:
Validate that incoming requests to server_data.php conform to the expected size and format. Reject requests that deviate from normal parameters as they might not be from the game client.
TLS Client Certificates:
If feasible, require mutual TLS where the client presents a valid certificate. This is more complex and requires distributing certificates to legitimate clients, but it can significantly improve security.
Real-time Monitoring and Anomaly Detection:
Use real-time monitoring and anomaly detection to identify unusual patterns or request behaviors indicative of abuse. Once identified, take corrective actions such as blocking the offending IPs.
Network Security Measures:
Use firewalls and DDoS protection services to filter out known malicious activities or traffic patterns before they reach the application layer.
Content Delivery Network (CDN):
Utilize a CDN with in-built security features to absorb and analyze traffic, detecting anomalies at the edge.
No single method is foolproof, and often the best approach is a combination of several strategies tailored to the specific needs and constraints of your application and its clients.
User
cuz when i tried using curl i can get the data by doing this : curl -X POST "https://54.165.29.40/growtopia/server_data.php" --insecure -H "Host: www.growtopia1.com" -H "Accept: /" -H "Content-Type: application/x-www-form-urlencoded" --user-agent "UbiServices_SDK_2022.Release.9_PC64_ansi_static" -d "version=4.62&platform=0&protocol=209"
Assistant
The challenge you're facing is a common one, especially when trying to protect an endpoint from being accessed via tools like curl while still allowing legitimate clients. Given that you've highlighted certain constraints (e.g., not using JWTs or tokens), I’ll focus on strategies that might help in your situation:
Enhanced User-Agent Verification:
While the User-Agent you are checking is specific to your app, curl allows for easy spoofing. Implement server-side logic that not only checks User-Agent strings but also verifies additional headers or a pattern of headers that your legitimate client would present.
TLS/SSL Certificate Pinning:
Encourage or enforce that your client application uses certificate pinning. This prevents curl and other command-line tools from easily simulating the legitimate client, as the SSL/TLS connection will not establish correctly if the certificate doesn't match the pinned one.
Request Header Depth Inspection:
Examine all headers in incoming requests. Legitimate client requests might include specific HTTP headers added by the operating system or unique to how it handles HTTP requests, something that curl users might not replicate.
Traffic Signature Analysis:
Autonomous tools often fail to mimic the exact essence of legitimate application traffic. Analyzing patterns such as timing of requests, packet sizes, and sequences can help differentiate between a legitimate client and automated access.
Dynamic Content:
Consider involving dynamic parameters or minor variations in your endpoint’s expected requests. While you can’t involve tokens, you might involve semi-randomized elements that vary with logic the client understands but aren’t straightforward for an automated tool.
IP Reputation and Geo-blocking:
Utilize existing IP reputation data and geo-blocking strategies to restrict access from regions where your game isn’t intended to be used or from known malicious IP addresses or hosting services commonly exploited.
Fuzzing and Anomaly Detection:
Implement server-side checks that expect strict compliance with request formatting and data. Any deviation from the anticipated behavior could be flagged or blocked.
To enhance your server's security with Network Protocol Execution, Enhanced User-Agent Verification, and Request Header Depth Inspection, you can implement the following changes in your Node.js application:
1. Network Protocol Execution
This involves adding some custom logic to your request processing, incorporating encoding or obfuscation mechanisms. This could mean using a simple cipher to encode certain parameters or defining special rule-based transformations understood solely by the client and server.
Example Implementation:
javascript
// Function to encode request parameters
function encodeParameters(params) {
// Simple example: Base64 encoding or similar transformations.
return Buffer.from(params).toString('base64');
}
// Decode the parameters when processing the request
function decodeParameters(encodedParams) {
return Buffer.from(encodedParams, 'base64').toString('utf8');
}
// For incoming requests on /growtopia/server_data.php
if (req.url === "/growtopia/server_data.php" && req.method === 'POST') {
// Decode incoming data
let encodedData = req.body; // Assume request body is obtained
let decodedData = decodeParameters(encodedData);
// Validate and process the decodedData
if (decodedData.version && decodedData.platform) {
// Normal business logic here
res.write(packet);
res.end();
} else {
res.statusCode = 400;
res.end('Invalid request');
}
}
2. Enhanced User-Agent Verification
Apart from just string checking, we can also explore additional attributes, though caution is required due to easy spoofing. Thus, ensure consistent signature checks.
Implementation:
javascript
User
can you give me the full code without touching it's original,
here is my code :
code
Assistant
To enhance your existing code with Network Protocol Execution, Enhanced User-Agent Verification, and Request Header Depth Inspection, I'll provide you the additional code snippets required for these enhancements without altering the original functions of your code.
Full Enhanced Code
Below are the code snippets that you need to append to your existing code at appropriate places.
1. Network Protocol Execution
Add encoding and decoding functions for request parameters:
javascript
// Function to encode request parameters
function encodeParameters(params) {
return Buffer.from(params).toString('base64'); // Using Base64 for illustration
}
// Function to decode request parameters
function decodeParameters(encodedParams) {
return Buffer.from(encodedParams, 'base64').toString('utf8');
}
Apply these functions within the /growtopia/server_data.php route:
javascript
if (req.url === "/growtopia/server_data.php") {
if (req.method !== 'POST' || hostHeader !== 'www.growtopia1.com') {
// Additional checks here
console.log(chalk.red(`Mata mata : ${req.url}`));
res.statusCode = 403;
res.end('<BLOCKED REASON>');
return;
}
// Example protocol execution: Decode incoming data
let decodedData;
try {
decodedData = decodeParameters(req.body); // Assuming req.body contains encoded data
} catch (error) {
console.log(chalk.red('Decoding error:', error));
res.statusCode = 400;
res.end('Invalid request format');
return;
}
// Process decodedData as needed...
if (validatedLogicForGrowtopia(decodedData)) { // Pseudo function to validate expected data fields
res.write(packet);
res.end();
} else {
res.statusCode = 400;
res.end('Invalid request content');
}
}
User
give me the full code.
Assistant
Certainly! Below is your full code with the added enhancements for Network Protocol Execution, Enhanced User-Agent Verification, and Request Header Depth Inspection. I've annotated the added sections for clarity.
javascript
User
if (/* validatedLogicForGrowtopia(decodedData) */) { // Pseudo function to validate expected data fields
^
SyntaxError: Unexpected token ')'
Assistant
The error you're encountering comes from the fact that in the code if (/* validatedLogicForGrowtopia(decodedData) */), the function validatedLogicForGrowtopia(decodedData) is commented out and thus doesn't do anything, resulting in a syntax error because the if statement's condition is incomplete.
To fix this syntax error, you need to replace the commented function call with an actual condition. Here is a corrected version:
javascript
// A pseudo function to validate expected data fields within the decoded parameters
function validatedLogicForGrowtopia(decodedData) {
// Implement your validation logic here
// Example: return true only if 'version' and 'platform' fields are present
return decodedData && decodedData.includes('version') && decodedData.includes('platform');
}
if (validatedLogicForGrowtopia(decodedData)) {
res.write(packet);
res.end();
} else {
res.statusCode = 400;
res.end('Invalid request content');
}
Explanation:
validatedLogicForGrowtopia(decodedData): This function is a placeholder. You need to implement the logic that checks whether the decoded request data is valid. The function should return true if the decoded data is as expected, and false otherwise.
Integration in the if clause: The if statement now calls this function to decide whether to execute the packet writing logic or reject the request with a status code and error message.
Replace the logic inside validatedLogicForGrowtopia with whatever checks are appropriate for your specific application. This could involve checking for specific fields, ensuring values are within expected ranges, etc.
User
give me the full code
Assistant
Certainly! Here's your full code with the corrections to ensure that the logic for validating request data is implemented, along with enhancements for security:
javascript
Not yet included in a published release.
console.log(chalk.green(`[DEVELOPER MODE]`, req.headers['user-agent']));
console.log(chalk.green(`Request received from IP: ${normalizedIp}`));
if (isSynFlood(ip)) {
console.log(chalk.red(`SYN flood detected from IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'SYN Flood');
// Use the underlying socket to destroy the connection
req.socket.end(); // Gracefully end the connection
req.socket.destroy(); // Forcefully close the connection, ignoring any pending data or errors
return;
}
// Rate Limiting Logic
if (!rateLimit[normalizedIp]) {
rateLimit[normalizedIp] = { count: 1, timestamp: Date.now() };
} else {
const currentTime = Date.now();
const timePassed = currentTime - rateLimit[normalizedIp].timestamp;
// Reset count if more than 1 minute has passed
if (timePassed > BLOCK_TIME_MS) {
rateLimit[normalizedIp] = { count: 1, timestamp: currentTime };
} else {
rateLimit[normalizedIp].count++;
}
// Check if the limit is reached
if (rateLimit[normalizedIp].count > MAX_REQUESTS_PER_MINUTE) {
console.log(chalk.red(`Rate limit exceeded for IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP');
res.statusCode = 429; // Too Many Requests
res.end('Too many requests. Your connection is being throttled.');
return;
}
}
// DNS attack protection
if (isSuspiciousDNSRequest(normalizedIp, req.url)) {
console.log(chalk.red(`DNS DDoS detected from IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP-DNS');
res.statusCode = 403; // Forbidden
res.end('Forbidden: DNS DDoS attack detected.');
return;
}
// Network Usage Limiting for TCP
const requestBytes = Buffer.byteLength(req.url); // Approximate
if (isNetworkUsageLimited(normalizedIp, requestBytes)) {
console.log(chalk.red(`Network usage limit exceeded for IP: ${normalizedIp} (TCP)`));
logBlockedIP(normalizedIp, 'TCP-NETWORK');
res.statusCode = 429; // Too Many Requests
res.end('Too many requests. Your connection is being throttled.');
return;
}
// Check for cloud or server IP range
if (!trustedIPs.includes(normalizedIp) && isCloudOrServerIp(normalizedIp)) {
console.log(chalk.red(`Blocking cloud/server IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP-Cloud');
res.statusCode = 403;
res.end('Forbidden: Access from cloud/server IP is blocked.');
return;
}
// Check for GeoIP block
if (isBlockedGeoIp(normalizedIp)) {
console.log(chalk.red(`Blocking access from IP: ${normalizedIp} (GeoIP Block)`));
logBlockedIP(normalizedIp, 'TCP-GeoIP');
res.statusCode = 403;
res.end('Forbidden: Access from your location is blocked.');
return;
}
const parsedUrl = url.parse(req.url);
const pathname = `.${parsedUrl.pathname}`;
const ext = path.parse(pathname).ext;
console.log(chalk.blue(`Connection Request from: ${normalizedIp} URL: ${req.url} Method: ${req.method}`));
const map = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.doc': 'application/msword'
};
const allowedUserAgents = [
'UbiServices_SDK_2022.Release.9_PC64_ansi_static',
'UbiServices_SDK_2022.Release.9_PC32_ansi_static',
'UbiServices_SDK_2022.Release.9_ANDROID64_static',
'UbiServices_SDK_2022.Release.9_ANDROID32_static',
'UbiServices_SDK_2022.Release.9_IOS64',
'UbiServices_SDK_2022.Release.9_IOS32',
'UbiServices_SDK_2022.Release.9_MAC64',
'UbiServices_SDK_2022.Release.9_MAC32',
];
const userAgent = req.headers['user-agent'];
// Handle /growtopia/server_data.php - Only POST and allowed User-Agents
normalizedIpbeta2port∣
Deploy a WAF that can inspect incoming requests and apply rules based on expected behaviors. Modern WAFs often include deep knowledge of common command-line tool signatures.
Network Protocol Execution:
Instead of relying solely on HTTP POSTs with typical x-www-form-urlencoded data, consider encoding or encrypting parts of your request dynamically on the client. Even simple forms of obfuscation add complexity for reverse-engineering.
Monitoring and Alerts:
Set up extensive logging and monitoring so that any unusual access patterns trigger alerts for your security team to investigate in real-time.
These strategies offer ways to strengthen your endpoint's posture against unauthorized access using tools like curl. It's vital to regularly analyze and iterate on your security measures, as attackers often change tactics to bypass defenses.
const ALLOWED_USER_AGENTS = [
'UbiServices_SDK_2022.Release.9_PC64_ansi_static',
// Add other allowed agents
];
// Refactor the agent verification with additional checks
function isUserAgentValid(userAgent) {
// Perform additional checks beyond simple string equality
return ALLOWED_USER_AGENTS.some(allowedAgent => {
return userAgent.includes(allowedAgent) && // Base check against known user agents
userAgent.split(' ').length > 3; // Hypothetical condition for "depth"
});
}
// Validation within your request handling logic
if (!isUserAgentValid(req.headers['user-agent'])) {
res.statusCode = 403;
res.end('Forbidden: Invalid User-Agent.');
return;
}
3. Request Header Depth Inspection
Here, inspect additional headers that common tools don't typically replicate, such as custom expected headers or specific header count.
Implementation:
javascript
function analyzeRequestHeaders(headers) {
// Example scenario: Ensure a specific header structure or additional headers
return headers['x-custom-header'] && Object.keys(headers).length > 10;
}
// Within your request handling
if (!analyzeRequestHeaders(req.headers)) {
res.statusCode = 403;
res.end('Forbidden: Unexpected header structure.');
return;
}
Implementation Strategy
Deploy Protocol and Encoding Logic: Introduce encoding/decoding logic to your communication, using something trivial to start but which can deter basic scripted attacks.
User-Agent Logic Enhancement: Beyond agent name, perform validation on other properties such as typical segment patterns in the User-Agent strings to increase integrity checks.
Header Analysis Logic: Implement functions to inspect request headers comprehensively, including potential dummy headers that legitimate clients always attach.
These enhancements aim to obfuscate the process for ill-intentioned users without dramatically altering client-server communication, thus increasing resistance to unauthorized scripted attacks while considering overhead and complexity.
const https = require('https');
const httpProxy = require('http-proxy');
const url = require('url');
const path = require('path');
const fs = require('fs');
const os = require('os');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const chalk = require('chalk');
const { EventEmitter } = require('events');
const axios = require('axios');
const geoip = require('geoip-lite'); // Importing geoip-lite for GeoIP blocking
const exec = require('child_process').exec; // For executing shell commands
const { spawn } = require('child_process');
const dgram = require('dgram'); // Importing dgram for UDP handling
const basicAuth = require('express-basic-auth');
const compression = require('compression');
const osu = require('os-utils');
const { netstat } = require('node-os-utils'); // Import netstat from node-os-utils
const mem = require('node-os-utils').mem;
// Create an Agent instance configured for keep-alive
const keepAliveAgent = new https.Agent({
keepAlive: true,
maxSockets: 100, // Control the number of sockets to handle concurrent connections
maxFreeSockets: 10, // Control the number of free sockets to keep open
});
axios.defaults.httpsAgent = keepAliveAgent;
let logMessages = [];
const originalLog = console.log;
console.log = (...args) => {
originalLog(...args);
logMessages.push(args.join(' '));
};
const synFloodTracker = {};
const MAX_SYN_REQUESTS = 10; // Max allowed per minute
const SYN_BLOCK_TIME_MS = 60000; // Block time if SYN flood detected
// Rate Limiting Configurations
const rateLimit = {};
const MAX_REQUESTS_PER_MINUTE = 100; // Set your limit here (100)
const BLOCK_TIME_MS = 60000; // Time to block the IP (1 minute) (60000)
// DNS attack prevention configurations
const dnsWhitelist = ['8.8.8.8', '1.1.1.1']; // Trusted DNS resolvers (Google DNS and Cloudflare DNS)
const MAX_DNS_REQUESTS_PER_MINUTE = 50; // Limit DNS-related requests
const dnsRateLimit = {};
// UDP Rate Limiting Configurations
const udpRateLimit = {};
const MAX_UDP_REQUESTS_PER_MINUTE = 100; // Adjust as needed
const UDP_BLOCK_TIME_MS = 60000; // 1 minute
// Network Usage Limiting Configurations
const networkUsage = {};
const MAX_BYTES_PER_MINUTE = 1024 * 1024; // 1 MB per minute per IP (adjust as needed)
const NETWORK_BLOCK_TIME_MS = 60000; // 1 minute
// Cloud Server IP Ranges
const cloudServerIpRanges = ['35.', '52.', '169.', '198.', '199.', '200.', '216.', '47.'];
// Allowed Countries for GeoIP
const allowedCountries = ['ID', 'SG', 'PH']; // Add allowed countries by their ISO code
const configApp = express();
const CONFIG_PORT = 6969;
// Middleware
configApp.use(compression());
configApp.set('view engine', 'ejs');
configApp.use(bodyParser.urlencoded({ extended: true }));
configApp.use(express.static('public'));
// Function to check and update DNS rate limits
const isSuspiciousDNSRequest = (ip, requestUrl) => {
if (!requestUrl.toLowerCase().includes('dns')) return false; // Only check DNS-related requests
if (!dnsWhitelist.includes(ip)) {
const now = Date.now();
if (!dnsRateLimit[ip]) {
dnsRateLimit[ip] = { count: 1, timestamp: now };
} else {
const timePassed = now - dnsRateLimit[ip].timestamp;
if (timePassed > BLOCK_TIME_MS) {
dnsRateLimit[ip] = { count: 1, timestamp: now };
} else {
dnsRateLimit[ip].count++;
}
if (dnsRateLimit[ip].count > MAX_DNS_REQUESTS_PER_MINUTE) {
return true; // DNS request limit exceeded
}
}
}
return false;
};
// Function to check and update UDP rate limits
const isUdpRateLimited = (ip) => {
const now = Date.now();
if (!udpRateLimit[ip]) {
udpRateLimit[ip] = { count: 1, timestamp: now };
return false;
} else {
const timePassed = now - udpRateLimit[ip].timestamp;
if (timePassed > UDP_BLOCK_TIME_MS) {
udpRateLimit[ip] = { count: 1, timestamp: now };
return false;
} else {
udpRateLimit[ip].count++;
if (udpRateLimit[ip].count > MAX_UDP_REQUESTS_PER_MINUTE) {
return true;
}
}
}
return false;
};
// Function to check and update network usage
const isNetworkUsageLimited = (ip, bytes) => {
const now = Date.now();
if (!networkUsage[ip]) {
networkUsage[ip] = { sent: bytes, received: bytes, timestamp: now };
return false;
} else {
const timePassed = now - networkUsage[ip].timestamp;
if (timePassed > NETWORK_BLOCK_TIME_MS) {
networkUsage[ip] = { sent: bytes, received: bytes, timestamp: now };
return false;
} else {
networkUsage[ip].sent += bytes;
networkUsage[ip].received += bytes;
if (networkUsage[ip].sent > MAX_BYTES_PER_MINUTE || networkUsage[ip].received > MAX_BYTES_PER_MINUTE) {
return true;
}
}
}
return false;
};
// Function to log blocked IPs
const logBlockedIP = (ip, protocol = 'TCP') => {
fs.appendFile('blocked_ips.log', `${new Date().toISOString()} - Blocked IP: ${ip} [${protocol}]\n`, (err) => {
if (err) console.error(chalk.red('Error logging blocked IP:'), err);
});
};
// Function to normalize IP addresses
const normalizeIp = (ip) => {
if (ip.startsWith("::ffff:")) {
return ip.substring(7);
}
if (ip === "::1") {
return "127.0.0.1";
}
return ip;
};
// Function to check if IP is from cloud/server ranges
const isCloudOrServerIp = (ip) => {
return cloudServerIpRanges.some((range) => ip.startsWith(range));
};
// Helper function to check GeoIP blocking
const isBlockedGeoIp = (ip) => {
const geo = geoip.lookup(ip);
if (geo && !allowedCountries.includes(geo.country)) {
return true;
}
return false;
};
const configPath = path.resolve(__dirname, './config/main.json');
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
// Fungsi untuk membaca konfigurasi
const readConfig = () => {
try {
const data = fs.readFileSync(configPath, 'utf8');
return JSON.parse(data);
} catch (err) {
console.error(chalk.red('Error membaca main.json:'), err);
return {};
}
};
// Fungsi untuk menulis konfigurasi
const writeConfig = (config) => {
try {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');
console.log(chalk.green('Configuration updated.'));
console.clear();
} catch (err) {
console.error(chalk.red('Error writing to Config:'), err);
}
};
// Middleware Autentikasi
configApp.use('/config', basicAuth({
users: { [config.username]: config.password }, // Use credentials from config
challenge: true,
realm: 'Config Area'
}));
// Route untuk menampilkan form konfigurasi
configApp.get('/config', (req, res) => {
const config = readConfig();
res.render('config', { config });
});
// Route untuk memproses perubahan konfigurasi
configApp.post('/config', (req, res) => {
const { ip, port, loginurl, cdn, username, password } = req.body;
const newConfig = { ip, port: parseInt(port), loginurl, cdn, username, password };
writeConfig(newConfig);
res.redirect('/config');
});
configApp.get('/logs', (req, res) => {
res.json({
logs: logMessages
});
});
// Endpoint to gather system stats
configApp.get('/stats', async (req, res) => {
try {
// Use os-utils or os for retrieving CPU and memory information
const freeMemory = os.freemem();
const totalMemory = os.totalmem();
const usedMemory = totalMemory - freeMemory;
const memoryUsagePercent = ((usedMemory) / totalMemory) * 100;
osu.cpuUsage(async (cpuPercentage) => {
const network = await netstat.inOut();
res.json({
memoryUsage: {
free: freeMemory,
total: totalMemory,
usedPercent: memoryUsagePercent.toFixed(2),
},
cpuUsage: (cpuPercentage * 100).toFixed(2),
network: {
inputMb: network.inputMb,
outputMb: network.outputMb
}
});
});
} catch (error) {
console.error('Error fetching system statistics:', error);
res.json({ error: 'Unable to fetch system statistics' });
}
});
configApp.post('/restart', (req, res) => {
console.log('Restarting server...');
res.send('Server restarting...');
process.exit(1);
});
const isSynFlood = (ip) => {
const now = Date.now();
if (!synFloodTracker[ip]) {
synFloodTracker[ip] = { count: 1, timestamp: now };
return false;
}
const timePassed = now - synFloodTracker[ip].timestamp;
if (timePassed > SYN_BLOCK_TIME_MS) {
synFloodTracker[ip] = { count: 1, timestamp: now };
return false;
} else {
synFloodTracker[ip].count++;
if (synFloodTracker[ip].count > MAX_SYN_REQUESTS) {
console.log(chalk.red(`SYN flood detected from IP: ${ip}`));
logBlockedIP(ip, 'SYN Flood');
return true;
}
}
return false;
};
// Start UI Server
configApp.listen(CONFIG_PORT, () => {
console.log(chalk.blue(`Admin dashboard is running on http://localhost:${CONFIG_PORT}/config`));
});
// Start Server Function
const startServer = async () => {
const proxy = httpProxy.createProxyServer({});
const pk = fs.readFileSync(path.join(__dirname, 'server.key'));
const pc = fs.readFileSync(path.join(__dirname, 'server.crt'));
const optss = { key: pk, cert: pc };
const trustedIPs = ['127.0.0.1'];
const port = process.argv[2] || 443;
EventEmitter.defaultMaxListeners = 100;
const packet = `server|${config.ip}\nport|${config.port}\ntype|1\n#maint|Server is currently initializing or re-syncing with sub servers. Please try again in a minute.\n\n\nloginurl|${config.loginurl}\nbeta_server|127.0.0.1\nbeta_port|17091\nbeta_type|1\nbeta2_server|127.0.0.1\nbeta2_port|17099\nbeta2_type|1\nmeta|${Math.floor(Date.now() / 1000)}\nRTENDMARKERBS1001`;
const server = https.createServer(optss, function (req, res) {
let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const normalizedIp = normalizeIp(ip);
const hostHeader = req.headers.host;
// Set keepAliveTimeout, which sets how long a connection should remain open in milliseconds.
server.keepAliveTimeout = 60 * 1000; // Keep alive for 60 seconds
// Set the headers timeout slightly longer than keepAliveTimeout to accommodate requests that arrive just before the keep alive timeout
server.headersTimeout = 65 * 1000; // 65 seconds
console.log(chalk.green(`[DEVELOPER MODE]`, req.headers['user-agent']));
console.log(chalk.green(`Request received from IP: ${normalizedIp}`));
if (isSynFlood(ip)) {
console.log(chalk.red(`SYN flood detected from IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'SYN Flood');
// Use the underlying socket to destroy the connection
req.socket.end(); // Gracefully end the connection
req.socket.destroy(); // Forcefully close the connection, ignoring any pending data or errors
return;
}
// Rate Limiting Logic
if (!rateLimit[normalizedIp]) {
rateLimit[normalizedIp] = { count: 1, timestamp: Date.now() };
} else {
const currentTime = Date.now();
const timePassed = currentTime - rateLimit[normalizedIp].timestamp;
// Reset count if more than 1 minute has passed
if (timePassed > BLOCK_TIME_MS) {
rateLimit[normalizedIp] = { count: 1, timestamp: currentTime };
} else {
rateLimit[normalizedIp].count++;
}
// Check if the limit is reached
if (rateLimit[normalizedIp].count > MAX_REQUESTS_PER_MINUTE) {
console.log(chalk.red(`Rate limit exceeded for IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP');
res.statusCode = 429; // Too Many Requests
res.end('Too many requests. Your connection is being throttled.');
return;
}
}
// DNS attack protection
if (isSuspiciousDNSRequest(normalizedIp, req.url)) {
console.log(chalk.red(`DNS DDoS detected from IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP-DNS');
res.statusCode = 403; // Forbidden
res.end('Forbidden: DNS DDoS attack detected.');
return;
}
// Network Usage Limiting for TCP
const requestBytes = Buffer.byteLength(req.url); // Approximate
if (isNetworkUsageLimited(normalizedIp, requestBytes)) {
console.log(chalk.red(`Network usage limit exceeded for IP: ${normalizedIp} (TCP)`));
logBlockedIP(normalizedIp, 'TCP-NETWORK');
res.statusCode = 429; // Too Many Requests
res.end('Too many requests. Your connection is being throttled.');
return;
}
// Check for cloud or server IP range
if (!trustedIPs.includes(normalizedIp) && isCloudOrServerIp(normalizedIp)) {
console.log(chalk.red(`Blocking cloud/server IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP-Cloud');
res.statusCode = 403;
res.end('Forbidden: Access from cloud/server IP is blocked.');
return;
}
// Check for GeoIP block
if (isBlockedGeoIp(normalizedIp)) {
console.log(chalk.red(`Blocking access from IP: ${normalizedIp} (GeoIP Block)`));
logBlockedIP(normalizedIp, 'TCP-GeoIP');
res.statusCode = 403;
res.end('Forbidden: Access from your location is blocked.');
return;
}
const parsedUrl = url.parse(req.url);
const pathname = `.${parsedUrl.pathname}`;
const ext = path.parse(pathname).ext;
console.log(chalk.blue(`Connection Request from: ${normalizedIp} URL: ${req.url} Method: ${req.method}`));
const map = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.doc': 'application/msword'
};
const allowedUserAgents = [
'UbiServices_SDK_2022.Release.9_PC64_ansi_static',
'UbiServices_SDK_2022.Release.9_PC32_ansi_static',
'UbiServices_SDK_2022.Release.9_ANDROID64_static',
'UbiServices_SDK_2022.Release.9_ANDROID32_static',
'UbiServices_SDK_2022.Release.9_IOS64',
'UbiServices_SDK_2022.Release.9_IOS32',
'UbiServices_SDK_2022.Release.9_MAC64',
'UbiServices_SDK_2022.Release.9_MAC32',
];
const userAgent = req.headers['user-agent'];
// Handle /growtopia/server_data.php - Only POST and allowed User-Agents
// Handle /growtopia/server_data.php - Only POST and allowed User-Agents
if (req.url === "/growtopia/server_data.php") {
if (req.method !== 'POST' || hostHeader !== 'www.growtopia1.com') {
console.log(chalk.red(`Mata mata : ${req.url}`));
res.statusCode = 403;
res.end(`
server|${normalizedIp}
port|${Math.floor(Date.now() / 69)}
type|1
#maint|Server is currently initializing or re-syncing with sub servers. Please try again in a minute.
loginurl|http-king.com
beta_server|${normalizedIp}
beta_port|${Math.floor(Date.now() / 69)}
beta_type|1
beta2_server|${normalizedIp}
beta2_port|${Math.floor(Date.now() / 69)}
beta2_type|1
meta|69-SCP-PROTECTION
RTENDMARKERBS1001
`);
return;
}
res.write(packet, function (err) {
if (err) console.log(chalk.red(err));
});
res.end();
}
// Handle /cache and /0098 - Only GET and allowed User-Agents
else if (req.url.indexOf("/cache") !== -1 || req.url.indexOf("/0098") !== -1) {
if (req.method !== 'GET' || !allowedUserAgents.includes(userAgent)) {
console.log(chalk.red(`Blocked: Invalid method or User-Agent for ${req.url}`));
res.statusCode = 403;
res.end('Hello world');
return;
}
console.log(chalk.blue(`[CACHE-SPEED] Connection from: ${normalizedIp}\n Downloading assets: ${req.url}`));
fs.exists(pathname, function (exist) {
if (!exist) {
res.statusCode = 301;
res.writeHead(301, {
Location: `https://ubistatic-a.akamaihd.net/${config.cdn}${req.url}`
}).end();
return;
}
fs.readFile(pathname, function (err, data) {
if (err) {
res.statusCode = 404;
res.end(`error`);
} else {
res.setHeader('Content-type', map[ext] || 'text/plain');
res.end(data);
}
});
});
}
// Block all other requests
else {
console.log(chalk.red(`🛡 [PROTECTION] Blocked request: ${req.method} ${req.url} from IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP-Other');
res.statusCode = 403;
res.end('Hello world');
}
});
server.listen(port, () => {
console.log(chalk.green(`Server is running on port ${port}`));
});
// -------------------- UDP Server Setup --------------------
const udpServer = dgram.createSocket('udp4');
const UDP_PORT = 53; // DNS typically uses port 53
udpServer.on('error', (err) => {
console.error(`UDP server error:\n${err.stack}`);
udpServer.close();
});
udpServer.on('message', (msg, rinfo) => {
const ip = rinfo.address;
const normalizedIp = normalizeIp(ip);
console.log(chalk.yellow(`UDP request from IP: ${normalizedIp}, Port: ${rinfo.port}`));
// Rate Limiting
if (isUdpRateLimited(normalizedIp)) {
console.log(chalk.red(`UDP Rate limit exceeded for IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'UDP');
return; // Drop the packet silently or send a response indicating rate limiting
}
// GeoIP Blocking
if (isBlockedGeoIp(normalizedIp)) {
console.log(chalk.red(`Blocking UDP access from IP: ${normalizedIp} (GeoIP Block)`));
logBlockedIP(normalizedIp, 'UDP-GeoIP');
return; // Drop the packet silently or send a forbidden response
}
// Network Usage Limiting for UDP
const messageBytes = msg.length;
if (isNetworkUsageLimited(normalizedIp, messageBytes)) {
console.log(chalk.red(`Network usage limit exceeded for IP: ${normalizedIp} (UDP)`));
logBlockedIP(normalizedIp, 'UDP-NETWORK');
return; // Drop the packet
}
// Implement your DNS handling logic here
// For example, respond with a simple DNS response or forward the request to a legitimate DNS server
// Example: Forward the DNS request to an external DNS server (e.g., Google DNS)
const externalDns = '8.8.8.8';
const externalDnsPort = 53;
const externalClient = dgram.createSocket('udp4');
externalClient.send(msg, 0, msg.length, externalDnsPort, externalDns, (error) => {
if (error) {
console.error(`Error forwarding UDP request: ${error}`);
externalClient.close();
}
});
externalClient.on('message', (response) => {
udpServer.send(response, 0, response.length, rinfo.port, rinfo.address, (err) => {
if (err) {
console.error(`Error sending UDP response: ${err}`);
}
externalClient.close();
});
});
// Optional: Implement timeouts to prevent hanging
externalClient.on('error', (err) => {
console.error(`External DNS client error: ${err}`);
externalClient.close();
});
// Set a timeout for the external DNS response
externalClient.setTimeout(5000, () => { // 5 seconds timeout
console.error(`External DNS request timed out for IP: ${normalizedIp}`);
externalClient.close();
});
});
udpServer.on('listening', () => {
const address = udpServer.address();
console.log(chalk.green(`UDP Server listening on ${address.address}:${address.port}`));
});
udpServer.bind(UDP_PORT);
const shutdown = () => {
console.log('Shutting down the server...');
// Stop the server from accepting new connections
server.close(() => {
console.log('HTTP server closed.');
// Close the UDP server
udpServer.close(() => {
console.log('UDP server closed.');
// Exit the process
process.exit(0);
});
});
// Ensure shutdown if connections remain
setTimeout(() => {
console.error('Forced shutdown due to open connections.');
process.exit(1); // exit with error code if connections remain
}, 5000).unref();
};
configApp.post('/shutdown', (req, res) => {
res.send('Server is shutting down...');
shutdown();
});
// Capture abort signals
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
console.log('')
console.log(chalk.magenta('========================================================'));
console.log(chalk.magenta(`- CONFIGURATION LOADS : `));
console.log(chalk.magenta(`[@] Login URL : ${config.loginurl}`));
console.log(chalk.magenta(`[@] HTTP Port ${port}`));
console.log(chalk.magenta(`[@] ANTI-HOSTCHECK ${port}`));
console.log(chalk.magenta(`[@] ANTI-SCAN SERVER DATA PHP STATUS : ENABLED [PENTEST-SECURITY]`));
console.log(chalk.magenta(`[@] OPERATION SYSTEM : WINDOWS`));
console.log(chalk.magenta(`[@] HTTP KING VERSION : 3.3.8`));
console.log(chalk.greenBright(`[@] SUBSCRIPTION : VIP-SUPER`));
console.log(chalk.greenBright(`[@] LAST HTTP UPDATE : 6/10/2024`));
console.log(chalk.magenta('========================================================'));
console.log('')
console.log(chalk.green(`
::: ::: ::::::::::: :::: ::: :::::::: ::: ::: :::::::: ::::::::
:+: :+: :+: :+:+: :+: :+: :+: :+: :+: :+: :+: :+: :+:
+:+ +:+ +:+ :+:+:+ +:+ +:+ +:+ +:+ +:+ +:+
+#++:++ +#+ +#+ +:+ +#+ :#: +#+ +:+ +#++: +#++:
+#+ +#+ +#+ +#+ +#+#+# +#+ +#+# +#+ +#+ +#+ +#+
#+# #+# #+# #+# #+#+# #+# #+# #+#+# #+# #+# #+# #+#
### ### ########### ### #### ######## ### ######## ########
`));
}
startServer();
2. Enhanced User-Agent Verification
Modify your user-agent verification to perform additional checks:
javascript
const isUserAgentValid = (userAgent) => {
// Enhanced check, e.g., allowing specific version numbers only
return allowedUserAgents.some(allowed =>
userAgent.includes(allowed) && /[0-9]{4}/.test(userAgent)
);
};
// Validate User-Agent within your existing conditional checks
if (!isUserAgentValid(userAgent)) {
console.log(chalk.red(`Blocked: Invalid User-Agent for ${req.url}`));
res.statusCode = 403;
res.end('Forbidden: Invalid User-Agent.');
return;
}
3. Request Header Depth Inspection
Implement deeper validation for request headers:
javascript
const isHeaderStructureValid = (headers) => {
// Check the presence of certain headers or count to identify automated scripts
return headers['x-special-header'] && Object.keys(headers).length >= 10;
};
// Apply header inspection as part of your blocking logic
if (!isHeaderStructureValid(req.headers)) {
console.log(chalk.red(`Blocked: Invalid header structure for ${req.url}`));
res.statusCode = 403;
res.end('Forbidden: Invalid header structure.');
return;
}
Integration
Place the Network Protocol Execution functions at the beginning of your server logic, just where you define helper functions.
Incorporate Enhanced User-Agent Verification alongside your existing checks where you validate the User-Agent.
Integrate Request Header Depth Inspection inside any conditional blocks where headers are being checked or validated.
Integrating these functionalities will provide an extra layer of security without affecting the original operations of your server code. This design maintains your original functionality while adding intelligence to detect potentially malicious requests.
const https = require('https');
const httpProxy = require('http-proxy');
const url = require('url');
const path = require('path');
const fs = require('fs');
const os = require('os');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const chalk = require('chalk');
const { EventEmitter } = require('events');
const axios = require('axios');
const geoip = require('geoip-lite'); // Importing geoip-lite for GeoIP blocking
const exec = require('child_process').exec; // For executing shell commands
const { spawn } = require('child_process');
const dgram = require('dgram'); // Importing dgram for UDP handling
const basicAuth = require('express-basic-auth');
const compression = require('compression');
const osu = require('os-utils');
const { netstat } = require('node-os-utils'); // Import netstat from node-os-utils
const mem = require('node-os-utils').mem;
// Create an Agent instance configured for keep-alive
const keepAliveAgent = new https.Agent({
keepAlive: true,
maxSockets: 100, // Control the number of sockets to handle concurrent connections
maxFreeSockets: 10, // Control the number of free sockets to keep open
});
axios.defaults.httpsAgent = keepAliveAgent;
let logMessages = [];
const originalLog = console.log;
console.log = (...args) => {
originalLog(...args);
logMessages.push(args.join(' '));
};
const synFloodTracker = {};
const MAX_SYN_REQUESTS = 10; // Max allowed per minute
const SYN_BLOCK_TIME_MS = 60000; // Block time if SYN flood detected
// Rate Limiting Configurations
const rateLimit = {};
const MAX_REQUESTS_PER_MINUTE = 100; // Set your limit here (100)
const BLOCK_TIME_MS = 60000; // Time to block the IP (1 minute) (60000)
// DNS attack prevention configurations
const dnsWhitelist = ['8.8.8.8', '1.1.1.1']; // Trusted DNS resolvers (Google DNS and Cloudflare DNS)
const MAX_DNS_REQUESTS_PER_MINUTE = 50; // Limit DNS-related requests
const dnsRateLimit = {};
// UDP Rate Limiting Configurations
const udpRateLimit = {};
const MAX_UDP_REQUESTS_PER_MINUTE = 100; // Adjust as needed
const UDP_BLOCK_TIME_MS = 60000; // 1 minute
// Network Usage Limiting Configurations
const networkUsage = {};
const MAX_BYTES_PER_MINUTE = 1024 * 1024; // 1 MB per minute per IP (adjust as needed)
const NETWORK_BLOCK_TIME_MS = 60000; // 1 minute
// Cloud Server IP Ranges
const cloudServerIpRanges = ['35.', '52.', '169.', '198.', '199.', '200.', '216.', '47.'];
// Allowed Countries for GeoIP
const allowedCountries = ['ID', 'SG', 'PH']; // Add allowed countries by their ISO code
const configApp = express();
const CONFIG_PORT = 6969;
// Middleware
configApp.use(compression());
configApp.set('view engine', 'ejs');
configApp.use(bodyParser.urlencoded({ extended: true }));
configApp.use(express.static('public'));
// Function to check and update DNS rate limits
const isSuspiciousDNSRequest = (ip, requestUrl) => {
if (!requestUrl.toLowerCase().includes('dns')) return false; // Only check DNS-related requests
if (!dnsWhitelist.includes(ip)) {
const now = Date.now();
if (!dnsRateLimit[ip]) {
dnsRateLimit[ip] = { count: 1, timestamp: now };
} else {
const timePassed = now - dnsRateLimit[ip].timestamp;
if (timePassed > BLOCK_TIME_MS) {
dnsRateLimit[ip] = { count: 1, timestamp: now };
} else {
dnsRateLimit[ip].count++;
}
if (dnsRateLimit[ip].count > MAX_DNS_REQUESTS_PER_MINUTE) {
return true; // DNS request limit exceeded
}
}
}
return false;
};
// Function to check and update UDP rate limits
const isUdpRateLimited = (ip) => {
const now = Date.now();
if (!udpRateLimit[ip]) {
udpRateLimit[ip] = { count: 1, timestamp: now };
return false;
} else {
const timePassed = now - udpRateLimit[ip].timestamp;
if (timePassed > UDP_BLOCK_TIME_MS) {
udpRateLimit[ip] = { count: 1, timestamp: now };
return false;
} else {
udpRateLimit[ip].count++;
if (udpRateLimit[ip].count > MAX_UDP_REQUESTS_PER_MINUTE) {
return true;
}
}
}
return false;
};
// Function to check and update network usage
const isNetworkUsageLimited = (ip, bytes) => {
const now = Date.now();
if (!networkUsage[ip]) {
networkUsage[ip] = { sent: bytes, received: bytes, timestamp: now };
return false;
} else {
const timePassed = now - networkUsage[ip].timestamp;
if (timePassed > NETWORK_BLOCK_TIME_MS) {
networkUsage[ip] = { sent: bytes, received: bytes, timestamp: now };
return false;
} else {
networkUsage[ip].sent += bytes;
networkUsage[ip].received += bytes;
if (networkUsage[ip].sent > MAX_BYTES_PER_MINUTE || networkUsage[ip].received > MAX_BYTES_PER_MINUTE) {
return true;
}
}
}
return false;
};
// Function to log blocked IPs
const logBlockedIP = (ip, protocol = 'TCP') => {
fs.appendFile('blocked_ips.log', `${new Date().toISOString()} - Blocked IP: ${ip} [${protocol}]\n`, (err) => {
if (err) console.error(chalk.red('Error logging blocked IP:'), err);
});
};
// Function to normalize IP addresses
const normalizeIp = (ip) => {
if (ip.startsWith("::ffff:")) {
return ip.substring(7);
}
if (ip === "::1") {
return "127.0.0.1";
}
return ip;
};
// Function to check if IP is from cloud/server ranges
const isCloudOrServerIp = (ip) => {
return cloudServerIpRanges.some((range) => ip.startsWith(range));
};
// Helper function to check GeoIP blocking
const isBlockedGeoIp = (ip) => {
const geo = geoip.lookup(ip);
if (geo && !allowedCountries.includes(geo.country)) {
return true;
}
return false;
};
const configPath = path.resolve(__dirname, './config/main.json');
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
// Fungsi untuk membaca konfigurasi
const readConfig = () => {
try {
const data = fs.readFileSync(configPath, 'utf8');
return JSON.parse(data);
} catch (err) {
console.error(chalk.red('Error membaca main.json:'), err);
return {};
}
};
// Fungsi untuk menulis konfigurasi
const writeConfig = (config) => {
try {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');
console.log(chalk.green('Configuration updated.'));
console.clear();
} catch (err) {
console.error(chalk.red('Error writing to Config:'), err);
}
};
// Middleware Autentikasi
configApp.use('/config', basicAuth({
users: { [config.username]: config.password }, // Use credentials from config
challenge: true,
realm: 'Config Area'
}));
// Route untuk menampilkan form konfigurasi
configApp.get('/config', (req, res) => {
const config = readConfig();
res.render('config', { config });
});
// Route untuk memproses perubahan konfigurasi
configApp.post('/config', (req, res) => {
const { ip, port, loginurl, cdn, username, password } = req.body;
const newConfig = { ip, port: parseInt(port), loginurl, cdn, username, password };
writeConfig(newConfig);
res.redirect('/config');
});
configApp.get('/logs', (req, res) => {
res.json({
logs: logMessages
});
});
// Endpoint to gather system stats
configApp.get('/stats', async (req, res) => {
try {
// Use os-utils or os for retrieving CPU and memory information
const freeMemory = os.freemem();
const totalMemory = os.totalmem();
const usedMemory = totalMemory - freeMemory;
const memoryUsagePercent = ((usedMemory) / totalMemory) * 100;
osu.cpuUsage(async (cpuPercentage) => {
const network = await netstat.inOut();
res.json({
memoryUsage: {
free: freeMemory,
total: totalMemory,
usedPercent: memoryUsagePercent.toFixed(2),
},
cpuUsage: (cpuPercentage * 100).toFixed(2),
network: {
inputMb: network.inputMb,
outputMb: network.outputMb
}
});
});
} catch (error) {
console.error('Error fetching system statistics:', error);
res.json({ error: 'Unable to fetch system statistics' });
}
});
configApp.post('/restart', (req, res) => {
console.log('Restarting server...');
res.send('Server restarting...');
process.exit(1);
});
const isSynFlood = (ip) => {
const now = Date.now();
if (!synFloodTracker[ip]) {
synFloodTracker[ip] = { count: 1, timestamp: now };
return false;
}
const timePassed = now - synFloodTracker[ip].timestamp;
if (timePassed > SYN_BLOCK_TIME_MS) {
synFloodTracker[ip] = { count: 1, timestamp: now };
return false;
} else {
synFloodTracker[ip].count++;
if (synFloodTracker[ip].count > MAX_SYN_REQUESTS) {
console.log(chalk.red(`SYN flood detected from IP: ${ip}`));
logBlockedIP(ip, 'SYN Flood');
return true;
}
}
return false;
};
// Start UI Server
configApp.listen(CONFIG_PORT, () => {
console.log(chalk.blue(`Admin dashboard is running on http://localhost:${CONFIG_PORT}/config`));
});
// New functions for enhanced security
// Function to encode request parameters
function encodeParameters(params) {
return Buffer.from(params).toString('base64'); // Using Base64 for illustration
}
// Function to decode request parameters
function decodeParameters(encodedParams) {
return Buffer.from(encodedParams, 'base64').toString('utf8');
}
// Enhanced User-Agent Verification
const isUserAgentValid = (userAgent) => {
// Enhanced check allowing specific version numbers only
const allowedUserAgents = [
'UbiServices_SDK_2022.Release.9_PC64_ansi_static',
'UbiServices_SDK_2022.Release.9_PC32_ansi_static',
'UbiServices_SDK_2022.Release.9_ANDROID64_static',
'UbiServices_SDK_2022.Release.9_ANDROID32_static',
'UbiServices_SDK_2022.Release.9_IOS64',
'UbiServices_SDK_2022.Release.9_IOS32',
'UbiServices_SDK_2022.Release.9_MAC64',
'UbiServices_SDK_2022.Release.9_MAC32',
];
return allowedUserAgents.some(allowed =>
userAgent.includes(allowed) && /[0-9]{4}/.test(userAgent)
);
};
// Function for Request Header Depth Inspection
const isHeaderStructureValid = (headers) => {
// Check for additional headers or a minimum count
return headers['x-special-header'] && Object.keys(headers).length >= 10;
};
// Start Server Function
const startServer = async () => {
const proxy = httpProxy.createProxyServer({});
const pk = fs.readFileSync(path.join(__dirname, 'server.key'));
const pc = fs.readFileSync(path.join(__dirname, 'server.crt'));
const optss = { key: pk, cert: pc };
const trustedIPs = ['127.0.0.1'];
const port = process.argv[2] || 443;
EventEmitter.defaultMaxListeners = 100;
const packet = `server|${config.ip}\nport|${config.port}\ntype|1\n#maint|Server is currently initializing or re-syncing with sub servers. Please try again in a minute.\n\n\nloginurl|${config.loginurl}\nbeta_server|127.0.0.1\nbeta_port|17091\nbeta_type|1\nbeta2_server|127.0.0.1\nbeta2_port|17099\nbeta2_type|1\nmeta|${Math.floor(Date.now() / 1000)}\nRTENDMARKERBS1001`;
const server = https.createServer(optss, function (req, res) {
let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const normalizedIp = normalizeIp(ip);
const hostHeader = req.headers.host;
// Set keepAliveTimeout, which sets how long a connection should remain open in milliseconds.
server.keepAliveTimeout = 60 * 1000; // Keep alive for 60 seconds
// Set the headers timeout slightly longer than keepAliveTimeout to accommodate requests that arrive just before the keep alive timeout
server.headersTimeout = 65 * 1000; // 65 seconds
console.log(chalk.green(`[DEVELOPER MODE]`, req.headers['user-agent']));
console.log(chalk.green(`Request received from IP: ${normalizedIp}`));
if (isSynFlood(ip)) {
console.log(chalk.red(`SYN flood detected from IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'SYN Flood');
// Use the underlying socket to destroy the connection
req.socket.end(); // Gracefully end the connection
req.socket.destroy(); // Forcefully close the connection, ignoring any pending data or errors
return;
}
// Rate Limiting Logic
if (!rateLimit[normalizedIp]) {
rateLimit[normalizedIp] = { count: 1, timestamp: Date.now() };
} else {
const currentTime = Date.now();
const timePassed = currentTime - rateLimit[normalizedIp].timestamp;
// Reset count if more than 1 minute has passed
if (timePassed > BLOCK_TIME_MS) {
rateLimit[normalizedIp] = { count: 1, timestamp: currentTime };
} else {
rateLimit[normalizedIp].count++;
}
// Check if the limit is reached
if (rateLimit[normalizedIp].count > MAX_REQUESTS_PER_MINUTE) {
console.log(chalk.red(`Rate limit exceeded for IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP');
res.statusCode = 429; // Too Many Requests
res.end('Too many requests. Your connection is being throttled.');
return;
}
}
// DNS attack protection
if (isSuspiciousDNSRequest(normalizedIp, req.url)) {
console.log(chalk.red(`DNS DDoS detected from IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP-DNS');
res.statusCode = 403; // Forbidden
res.end('Forbidden: DNS DDoS attack detected.');
return;
}
// Network Usage Limiting for TCP
const requestBytes = Buffer.byteLength(req.url); // Approximate
if (isNetworkUsageLimited(normalizedIp, requestBytes)) {
console.log(chalk.red(`Network usage limit exceeded for IP: ${normalizedIp} (TCP)`));
logBlockedIP(normalizedIp, 'TCP-NETWORK');
res.statusCode = 429; // Too Many Requests
res.end('Too many requests. Your connection is being throttled.');
return;
}
// Check for cloud or server IP range
if (!trustedIPs.includes(normalizedIp) && isCloudOrServerIp(normalizedIp)) {
console.log(chalk.red(`Blocking cloud/server IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP-Cloud');
res.statusCode = 403;
res.end('Forbidden: Access from cloud/server IP is blocked.');
return;
}
// Check for GeoIP block
if (isBlockedGeoIp(normalizedIp)) {
console.log(chalk.red(`Blocking access from IP: ${normalizedIp} (GeoIP Block)`));
logBlockedIP(normalizedIp, 'TCP-GeoIP');
res.statusCode = 403;
res.end('Forbidden: Access from your location is blocked.');
return;
}
const parsedUrl = url.parse(req.url);
const pathname = `.${parsedUrl.pathname}`;
const ext = path.parse(pathname).ext;
console.log(chalk.blue(`Connection Request from: ${normalizedIp} URL: ${req.url} Method: ${req.method}`));
const map = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.doc': 'application/msword'
};
const userAgent = req.headers['user-agent'];
// Apply enhanced User-Agent checks
if (!isUserAgentValid(userAgent)) {
console.log(chalk.red(`Blocked: Invalid User-Agent for ${req.url}`));
res.statusCode = 403;
res.end('Forbidden: Invalid User-Agent.');
return;
}
// Apply header inspection
if (!isHeaderStructureValid(req.headers)) {
console.log(chalk.red(`Blocked: Invalid header structure for ${req.url}`));
res.statusCode = 403;
res.end('Forbidden: Invalid header structure.');
return;
}
// Handle /growtopia/server_data.php - Only POST and allowed User-Agents
if (req.url === "/growtopia/server_data.php") {
if (req.method !== 'POST' || hostHeader !== 'www.growtopia1.com') {
console.log(chalk.red(`Mata mata : ${req.url}`));
res.statusCode = 403;
res.end(`
server|${normalizedIp}
port|${Math.floor(Date.now() / 69)}
type|1
#maint|Server is currently initializing or re-syncing with sub servers. Please try again in a minute.
loginurl|http-king.com
beta_server|${normalizedIp}
beta_port|${Math.floor(Date.now() / 69)}
beta_type|1
beta2_server|${normalizedIp}
beta2_port|${Math.floor(Date.now() / 69)}
beta2_type|1
meta|69-SCP-PROTECTION
RTENDMARKERBS1001
`);
return;
}
// Example protocol execution: Decode incoming data
let decodedData;
try {
decodedData = decodeParameters(req.body); // Assuming req.body contains encoded data
} catch (error) {
console.log(chalk.red('Decoding error:', error));
res.statusCode = 400;
res.end('Invalid request format');
return;
}
// Process decodedData as needed...
if (/* validatedLogicForGrowtopia(decodedData) */) { // Pseudo function to validate expected data fields
res.write(packet);
res.end();
} else {
res.statusCode = 400;
res.end('Invalid request content');
}
}
// Handle /cache and /0098 - Only GET and allowed User-Agents
else if (req.url.indexOf("/cache") !== -1 || req.url.indexOf("/0098") !== -1) {
if (req.method !== 'GET') {
console.log(chalk.red(`Blocked: Invalid method for ${req.url}`));
res.statusCode = 403;
res.end('Hello world');
return;
}
console.log(chalk.blue(`[CACHE-SPEED] Connection from: ${normalizedIp}\n Downloading assets: ${req.url}`));
fs.exists(pathname, function (exist) {
if (!exist) {
res.statusCode = 301;
res.writeHead(301, {
Location: `https://ubistatic-a.akamaihd.net/${config.cdn}${req.url}`
}).end();
return;
}
fs.readFile(pathname, function (err, data) {
if (err) {
res.statusCode = 404;
res.end(`error`);
} else {
res.setHeader('Content-type', map[ext] || 'text/plain');
res.end(data);
}
});
});
}
// Block all other requests
else {
console.log(chalk.red(`🛡 [PROTECTION] Blocked request: ${req.method} ${req.url} from IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP-Other');
res.statusCode = 403;
res.end('Hello world');
}
});
server.listen(port, () => {
console.log(chalk.green(`Server is running on port ${port}`));
});
// -------------------- UDP Server Setup --------------------
const udpServer = dgram.createSocket('udp4');
const UDP_PORT = 53; // DNS typically uses port 53
udpServer.on('error', (err) => {
console.error(`UDP server error:\n${err.stack}`);
udpServer.close();
});
udpServer.on('message', (msg, rinfo) => {
const ip = rinfo.address;
const normalizedIp = normalizeIp(ip);
console.log(chalk.yellow(`UDP request from IP: ${normalizedIp}, Port: ${rinfo.port}`));
// Rate Limiting
if (isUdpRateLimited(normalizedIp)) {
console.log(chalk.red(`UDP Rate limit exceeded for IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'UDP');
return; // Drop the packet silently or send a response indicating rate limiting
}
// GeoIP Blocking
if (isBlockedGeoIp(normalizedIp)) {
console.log(chalk.red(`Blocking UDP access from IP: ${normalizedIp} (GeoIP Block)`));
logBlockedIP(normalizedIp, 'UDP-GeoIP');
return; // Drop the packet silently or send a forbidden response
}
// Network Usage Limiting for UDP
const messageBytes = msg.length;
if (isNetworkUsageLimited(normalizedIp, messageBytes)) {
console.log(chalk.red(`Network usage limit exceeded for IP: ${normalizedIp} (UDP)`));
logBlockedIP(normalizedIp, 'UDP-NETWORK');
return; // Drop the packet
}
// Implement your DNS handling logic here
// For example, respond with a simple DNS response or forward the request to a legitimate DNS server
// Example: Forward the DNS request to an external DNS server (e.g., Google DNS)
const externalDns = '8.8.8.8';
const externalDnsPort = 53;
const externalClient = dgram.createSocket('udp4');
externalClient.send(msg, 0, msg.length, externalDnsPort, externalDns, (error) => {
if (error) {
console.error(`Error forwarding UDP request: ${error}`);
externalClient.close();
}
});
externalClient.on('message', (response) => {
udpServer.send(response, 0, response.length, rinfo.port, rinfo.address, (err) => {
if (err) {
console.error(`Error sending UDP response: ${err}`);
}
externalClient.close();
});
});
// Optional: Implement timeouts to prevent hanging
externalClient.on('error', (err) => {
console.error(`External DNS client error: ${err}`);
externalClient.close();
});
// Set a timeout for the external DNS response
externalClient.setTimeout(5000, () => { // 5 seconds timeout
console.error(`External DNS request timed out for IP: ${normalizedIp}`);
externalClient.close();
});
});
udpServer.on('listening', () => {
const address = udpServer.address();
console.log(chalk.green(`UDP Server listening on ${address.address}:${address.port}`));
});
udpServer.bind(UDP_PORT);
const shutdown = () => {
console.log('Shutting down the server...');
// Stop the server from accepting new connections
server.close(() => {
console.log('HTTP server closed.');
// Close the UDP server
udpServer.close(() => {
console.log('UDP server closed.');
// Exit the process
process.exit(0);
});
});
// Ensure shutdown if connections remain
setTimeout(() => {
console.error('Forced shutdown due to open connections.');
process.exit(1); // exit with error code if connections remain
}, 5000).unref();
};
configApp.post('/shutdown', (req, res) => {
res.send('Server is shutting down...');
shutdown();
});
// Capture abort signals
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
console.log('')
console.log(chalk.magenta('========================================================'));
console.log(chalk.magenta(`- CONFIGURATION LOADS : `));
console.log(chalk.magenta(`[@] Login URL : ${config.loginurl}`));
console.log(chalk.magenta(`[@] HTTP Port ${port}`));
console.log(chalk.magenta(`[@] ANTI-HOSTCHECK ${port}`));
console.log(chalk.magenta(`[@] ANTI-SCAN SERVER DATA PHP STATUS : ENABLED [PENTEST-SECURITY]`));
console.log(chalk.magenta(`[@] OPERATION SYSTEM : WINDOWS`));
console.log(chalk.magenta(`[@] HTTP KING VERSION : 3.3.8`));
console.log(chalk.greenBright(`[@] SUBSCRIPTION : VIP-SUPER`));
console.log(chalk.greenBright(`[@] LAST HTTP UPDATE : 6/10/2024`));
console.log(chalk.magenta('========================================================'));
console.log('')
console.log(chalk.green(`
::: ::: ::::::::::: :::: ::: :::::::: ::: ::: :::::::: ::::::::
:+: :+: :+: :+:+: :+: :+: :+: :+: :+: :+: :+: :+: :+:
+:+ +:+ +:+ :+:+:+ +:+ +:+ +:+ +:+ +:+ +:+
+#++:++ +#+ +#+ +:+ +#+ :#: +#+ +:+ +#++: +#++:
+#+ +#+ +#+ +#+ +#+#+# +#+ +#+# +#+ +#+ +#+ +#+
#+# #+# #+# #+# #+#+# #+# #+# #+#+# #+# #+# #+# #+#
### ### ########### ### #### ######## ### ######## ########
`));
}
startServer();
Key Additions Explained:
Network Protocol Execution:
Added encodeParameters and decodeParameters functions using Base64 for obfuscation.
Demonstrated how to decode incoming requests and apply simple logic checks.
Enhanced User-Agent Verification:
Function isUserAgentValid checks for expected patterns in User-Agent strings.
Request Header Depth Inspection:
Function isHeaderStructureValid checks for specific headers and validates the count of headers to differentiate requests from automated tools.
These additions provide additional security layers by inspecting and analyzing incoming requests more deeply, adding protocol-level encoding/decoding, and verifying the authenticity of header structures. Be sure to adjust these checks to better fit the unique patterns of legitimate client traffic to minimize false positives.
const https = require('https');
const httpProxy = require('http-proxy');
const url = require('url');
const path = require('path');
const fs = require('fs');
const os = require('os');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const chalk = require('chalk');
const { EventEmitter } = require('events');
const axios = require('axios');
const geoip = require('geoip-lite'); // Importing geoip-lite for GeoIP blocking
const exec = require('child_process').exec; // For executing shell commands
const { spawn } = require('child_process');
const dgram = require('dgram'); // Importing dgram for UDP handling
const basicAuth = require('express-basic-auth');
const compression = require('compression');
const osu = require('os-utils');
const { netstat } = require('node-os-utils'); // Import netstat from node-os-utils
const mem = require('node-os-utils').mem;
// Create an Agent instance configured for keep-alive
const keepAliveAgent = new https.Agent({
keepAlive: true,
maxSockets: 100, // Control the number of sockets to handle concurrent connections
maxFreeSockets: 10, // Control the number of free sockets to keep open
});
axios.defaults.httpsAgent = keepAliveAgent;
let logMessages = [];
const originalLog = console.log;
console.log = (...args) => {
originalLog(...args);
logMessages.push(args.join(' '));
};
const synFloodTracker = {};
const MAX_SYN_REQUESTS = 10; // Max allowed per minute
const SYN_BLOCK_TIME_MS = 60000; // Block time if SYN flood detected
// Rate Limiting Configurations
const rateLimit = {};
const MAX_REQUESTS_PER_MINUTE = 100; // Set your limit here (100)
const BLOCK_TIME_MS = 60000; // Time to block the IP (1 minute) (60000)
// DNS attack prevention configurations
const dnsWhitelist = ['8.8.8.8', '1.1.1.1']; // Trusted DNS resolvers (Google DNS and Cloudflare DNS)
const MAX_DNS_REQUESTS_PER_MINUTE = 50; // Limit DNS-related requests
const dnsRateLimit = {};
// UDP Rate Limiting Configurations
const udpRateLimit = {};
const MAX_UDP_REQUESTS_PER_MINUTE = 100; // Adjust as needed
const UDP_BLOCK_TIME_MS = 60000; // 1 minute
// Network Usage Limiting Configurations
const networkUsage = {};
const MAX_BYTES_PER_MINUTE = 1024 * 1024; // 1 MB per minute per IP (adjust as needed)
const NETWORK_BLOCK_TIME_MS = 60000; // 1 minute
// Cloud Server IP Ranges
const cloudServerIpRanges = ['35.', '52.', '169.', '198.', '199.', '200.', '216.', '47.'];
// Allowed Countries for GeoIP
const allowedCountries = ['ID', 'SG', 'PH']; // Add allowed countries by their ISO code
const configApp = express();
const CONFIG_PORT = 6969;
// Middleware
configApp.use(compression());
configApp.set('view engine', 'ejs');
configApp.use(bodyParser.urlencoded({ extended: true }));
configApp.use(express.static('public'));
// Function to check and update DNS rate limits
const isSuspiciousDNSRequest = (ip, requestUrl) => {
if (!requestUrl.toLowerCase().includes('dns')) return false; // Only check DNS-related requests
if (!dnsWhitelist.includes(ip)) {
const now = Date.now();
if (!dnsRateLimit[ip]) {
dnsRateLimit[ip] = { count: 1, timestamp: now };
} else {
const timePassed = now - dnsRateLimit[ip].timestamp;
if (timePassed > BLOCK_TIME_MS) {
dnsRateLimit[ip] = { count: 1, timestamp: now };
} else {
dnsRateLimit[ip].count++;
}
if (dnsRateLimit[ip].count > MAX_DNS_REQUESTS_PER_MINUTE) {
return true; // DNS request limit exceeded
}
}
}
return false;
};
// Function to check and update UDP rate limits
const isUdpRateLimited = (ip) => {
const now = Date.now();
if (!udpRateLimit[ip]) {
udpRateLimit[ip] = { count: 1, timestamp: now };
return false;
} else {
const timePassed = now - udpRateLimit[ip].timestamp;
if (timePassed > UDP_BLOCK_TIME_MS) {
udpRateLimit[ip] = { count: 1, timestamp: now };
return false;
} else {
udpRateLimit[ip].count++;
if (udpRateLimit[ip].count > MAX_UDP_REQUESTS_PER_MINUTE) {
return true;
}
}
}
return false;
};
// Function to check and update network usage
const isNetworkUsageLimited = (ip, bytes) => {
const now = Date.now();
if (!networkUsage[ip]) {
networkUsage[ip] = { sent: bytes, received: bytes, timestamp: now };
return false;
} else {
const timePassed = now - networkUsage[ip].timestamp;
if (timePassed > NETWORK_BLOCK_TIME_MS) {
networkUsage[ip] = { sent: bytes, received: bytes, timestamp: now };
return false;
} else {
networkUsage[ip].sent += bytes;
networkUsage[ip].received += bytes;
if (networkUsage[ip].sent > MAX_BYTES_PER_MINUTE || networkUsage[ip].received > MAX_BYTES_PER_MINUTE) {
return true;
}
}
}
return false;
};
// Function to log blocked IPs
const logBlockedIP = (ip, protocol = 'TCP') => {
fs.appendFile('blocked_ips.log', `${new Date().toISOString()} - Blocked IP: ${ip} [${protocol}]\n`, (err) => {
if (err) console.error(chalk.red('Error logging blocked IP:'), err);
});
};
// Function to normalize IP addresses
const normalizeIp = (ip) => {
if (ip.startsWith("::ffff:")) {
return ip.substring(7);
}
if (ip === "::1") {
return "127.0.0.1";
}
return ip;
};
// Function to check if IP is from cloud/server ranges
const isCloudOrServerIp = (ip) => {
return cloudServerIpRanges.some((range) => ip.startsWith(range));
};
// Helper function to check GeoIP blocking
const isBlockedGeoIp = (ip) => {
const geo = geoip.lookup(ip);
if (geo && !allowedCountries.includes(geo.country)) {
return true;
}
return false;
};
const configPath = path.resolve(__dirname, './config/main.json');
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
// Fungsi untuk membaca konfigurasi
const readConfig = () => {
try {
const data = fs.readFileSync(configPath, 'utf8');
return JSON.parse(data);
} catch (err) {
console.error(chalk.red('Error membaca main.json:'), err);
return {};
}
};
// Fungsi untuk menulis konfigurasi
const writeConfig = (config) => {
try {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');
console.log(chalk.green('Configuration updated.'));
console.clear();
} catch (err) {
console.error(chalk.red('Error writing to Config:'), err);
}
};
// Middleware Autentikasi
configApp.use('/config', basicAuth({
users: { [config.username]: config.password }, // Use credentials from config
challenge: true,
realm: 'Config Area'
}));
// Route untuk menampilkan form konfigurasi
configApp.get('/config', (req, res) => {
const config = readConfig();
res.render('config', { config });
});
// Route untuk memproses perubahan konfigurasi
configApp.post('/config', (req, res) => {
const { ip, port, loginurl, cdn, username, password } = req.body;
const newConfig = { ip, port: parseInt(port), loginurl, cdn, username, password };
writeConfig(newConfig);
res.redirect('/config');
});
configApp.get('/logs', (req, res) => {
res.json({
logs: logMessages
});
});
// Endpoint to gather system stats
configApp.get('/stats', async (req, res) => {
try {
// Use os-utils or os for retrieving CPU and memory information
const freeMemory = os.freemem();
const totalMemory = os.totalmem();
const usedMemory = totalMemory - freeMemory;
const memoryUsagePercent = ((usedMemory) / totalMemory) * 100;
osu.cpuUsage(async (cpuPercentage) => {
const network = await netstat.inOut();
res.json({
memoryUsage: {
free: freeMemory,
total: totalMemory,
usedPercent: memoryUsagePercent.toFixed(2),
},
cpuUsage: (cpuPercentage * 100).toFixed(2),
network: {
inputMb: network.inputMb,
outputMb: network.outputMb
}
});
});
} catch (error) {
console.error('Error fetching system statistics:', error);
res.json({ error: 'Unable to fetch system statistics' });
}
});
configApp.post('/restart', (req, res) => {
console.log('Restarting server...');
res.send('Server restarting...');
process.exit(1);
});
const isSynFlood = (ip) => {
const now = Date.now();
if (!synFloodTracker[ip]) {
synFloodTracker[ip] = { count: 1, timestamp: now };
return false;
}
const timePassed = now - synFloodTracker[ip].timestamp;
if (timePassed > SYN_BLOCK_TIME_MS) {
synFloodTracker[ip] = { count: 1, timestamp: now };
return false;
} else {
synFloodTracker[ip].count++;
if (synFloodTracker[ip].count > MAX_SYN_REQUESTS) {
console.log(chalk.red(`SYN flood detected from IP: ${ip}`));
logBlockedIP(ip, 'SYN Flood');
return true;
}
}
return false;
};
// Start UI Server
configApp.listen(CONFIG_PORT, () => {
console.log(chalk.blue(`Admin dashboard is running on http://localhost:${CONFIG_PORT}/config`));
});
// New functions for enhanced security
// Function to encode request parameters
function encodeParameters(params) {
return Buffer.from(params).toString('base64'); // Using Base64 for illustration
}
// Function to decode request parameters
function decodeParameters(encodedParams) {
return Buffer.from(encodedParams, 'base64').toString('utf8');
}
// A pseudo function to validate expected data fields
function validatedLogicForGrowtopia(decodedData) {
// Implement validation logic here, e.g., check if needed fields exist
return decodedData && decodedData.includes('version') && decodedData.includes('platform');
}
// Enhanced User-Agent Verification
const isUserAgentValid = (userAgent) => {
// Enhanced check allowing specific version numbers only
const allowedUserAgents = [
'UbiServices_SDK_2022.Release.9_PC64_ansi_static',
'UbiServices_SDK_2022.Release.9_PC32_ansi_static',
'UbiServices_SDK_2022.Release.9_ANDROID64_static',
'UbiServices_SDK_2022.Release.9_ANDROID32_static',
'UbiServices_SDK_2022.Release.9_IOS64',
'UbiServices_SDK_2022.Release.9_IOS32',
'UbiServices_SDK_2022.Release.9_MAC64',
'UbiServices_SDK_2022.Release.9_MAC32',
];
return allowedUserAgents.some(allowed =>
userAgent.includes(allowed) && /[0-9]{4}/.test(userAgent)
);
};
// Function for Request Header Depth Inspection
const isHeaderStructureValid = (headers) => {
// Check for additional headers or a minimum count
return headers['x-special-header'] && Object.keys(headers).length >= 10;
};
// Start Server Function
const startServer = async () => {
const proxy = httpProxy.createProxyServer({});
const pk = fs.readFileSync(path.join(__dirname, 'server.key'));
const pc = fs.readFileSync(path.join(__dirname, 'server.crt'));
const optss = { key: pk, cert: pc };
const trustedIPs = ['127.0.0.1'];
const port = process.argv[2] || 443;
EventEmitter.defaultMaxListeners = 100;
const packet = `server|${config.ip}\nport|${config.port}\ntype|1\n#maint|Server is currently initializing or re-syncing with sub servers. Please try again in a minute.\n\n\nloginurl|${config.loginurl}\nbeta_server|127.0.0.1\nbeta_port|17091\nbeta_type|1\nbeta2_server|127.0.0.1\nbeta2_port|17099\nbeta2_type|1\nmeta|${Math.floor(Date.now() / 1000)}\nRTENDMARKERBS1001`;
const server = https.createServer(optss, function (req, res) {
let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const normalizedIp = normalizeIp(ip);
const hostHeader = req.headers.host;
// Set keepAliveTimeout, which sets how long a connection should remain open in milliseconds.
server.keepAliveTimeout = 60 * 1000; // Keep alive for 60 seconds
// Set the headers timeout slightly longer than keepAliveTimeout to accommodate requests that arrive just before the keep alive timeout
server.headersTimeout = 65 * 1000; // 65 seconds
console.log(chalk.green(`[DEVELOPER MODE]`, req.headers['user-agent']));
console.log(chalk.green(`Request received from IP: ${normalizedIp}`));
if (isSynFlood(ip)) {
console.log(chalk.red(`SYN flood detected from IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'SYN Flood');
// Use the underlying socket to destroy the connection
req.socket.end(); // Gracefully end the connection
req.socket.destroy(); // Forcefully close the connection, ignoring any pending data or errors
return;
}
// Rate Limiting Logic
if (!rateLimit[normalizedIp]) {
rateLimit[normalizedIp] = { count: 1, timestamp: Date.now() };
} else {
const currentTime = Date.now();
const timePassed = currentTime - rateLimit[normalizedIp].timestamp;
// Reset count if more than 1 minute has passed
if (timePassed > BLOCK_TIME_MS) {
rateLimit[normalizedIp] = { count: 1, timestamp: currentTime };
} else {
rateLimit[normalizedIp].count++;
}
// Check if the limit is reached
if (rateLimit[normalizedIp].count > MAX_REQUESTS_PER_MINUTE) {
console.log(chalk.red(`Rate limit exceeded for IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP');
res.statusCode = 429; // Too Many Requests
res.end('Too many requests. Your connection is being throttled.');
return;
}
}
// DNS attack protection
if (isSuspiciousDNSRequest(normalizedIp, req.url)) {
console.log(chalk.red(`DNS DDoS detected from IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP-DNS');
res.statusCode = 403; // Forbidden
res.end('Forbidden: DNS DDoS attack detected.');
return;
}
// Network Usage Limiting for TCP
const requestBytes = Buffer.byteLength(req.url); // Approximate
if (isNetworkUsageLimited(normalizedIp, requestBytes)) {
console.log(chalk.red(`Network usage limit exceeded for IP: ${normalizedIp} (TCP)`));
logBlockedIP(normalizedIp, 'TCP-NETWORK');
res.statusCode = 429; // Too Many Requests
res.end('Too many requests. Your connection is being throttled.');
return;
}
// Check for cloud or server IP range
if (!trustedIPs.includes(normalizedIp) && isCloudOrServerIp(normalizedIp)) {
console.log(chalk.red(`Blocking cloud/server IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP-Cloud');
res.statusCode = 403;
res.end('Forbidden: Access from cloud/server IP is blocked.');
return;
}
// Check for GeoIP block
if (isBlockedGeoIp(normalizedIp)) {
console.log(chalk.red(`Blocking access from IP: ${normalizedIp} (GeoIP Block)`));
logBlockedIP(normalizedIp, 'TCP-GeoIP');
res.statusCode = 403;
res.end('Forbidden: Access from your location is blocked.');
return;
}
const parsedUrl = url.parse(req.url);
const pathname = `.${parsedUrl.pathname}`;
const ext = path.parse(pathname).ext;
console.log(chalk.blue(`Connection Request from: ${normalizedIp} URL: ${req.url} Method: ${req.method}`));
const map = {
'.ico': 'image/x-icon',
'.html': 'text/html',
'.js': 'text/javascript',
'.json': 'application/json',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.wav': 'audio/wav',
'.mp3': 'audio/mpeg',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
'.doc': 'application/msword'
};
const userAgent = req.headers['user-agent'];
// Apply enhanced User-Agent checks
if (!isUserAgentValid(userAgent)) {
console.log(chalk.red(`Blocked: Invalid User-Agent for ${req.url}`));
res.statusCode = 403;
res.end('Forbidden: Invalid User-Agent.');
return;
}
// Apply header inspection
if (!isHeaderStructureValid(req.headers)) {
console.log(chalk.red(`Blocked: Invalid header structure for ${req.url}`));
res.statusCode = 403;
res.end('Forbidden: Invalid header structure.');
return;
}
// Handle /growtopia/server_data.php - Only POST and allowed User-Agents
if (req.url === "/growtopia/server_data.php") {
if (req.method !== 'POST' || hostHeader !== 'www.growtopia1.com') {
console.log(chalk.red(`Mata mata : ${req.url}`));
res.statusCode = 403;
res.end(`
server|${normalizedIp}
port|${Math.floor(Date.now() / 69)}
type|1
#maint|Server is currently initializing or re-syncing with sub servers. Please try again in a minute.
loginurl|http-king.com
beta_server|${normalizedIp}
beta_port|${Math.floor(Date.now() / 69)}
beta_type|1
beta2_server|${normalizedIp}
beta2_port|${Math.floor(Date.now() / 69)}
beta2_type|1
meta|69-SCP-PROTECTION
RTENDMARKERBS1001
`);
return;
}
// Example protocol execution: Decode incoming data
let decodedData;
try {
decodedData = decodeParameters(req.body); // Assuming req.body contains encoded data
} catch (error) {
console.log(chalk.red('Decoding error:', error));
res.statusCode = 400;
res.end('Invalid request format');
return;
}
// Process decodedData as needed...
if (validatedLogicForGrowtopia(decodedData)) {
res.write(packet);
res.end();
} else {
res.statusCode = 400;
res.end('Invalid request content');
}
}
// Handle /cache and /0098 - Only GET and allowed User-Agents
else if (req.url.indexOf("/cache") !== -1 || req.url.indexOf("/0098") !== -1) {
if (req.method !== 'GET') {
console.log(chalk.red(`Blocked: Invalid method for ${req.url}`));
res.statusCode = 403;
res.end('Hello world');
return;
}
console.log(chalk.blue(`[CACHE-SPEED] Connection from: ${normalizedIp}\n Downloading assets: ${req.url}`));
fs.exists(pathname, function (exist) {
if (!exist) {
res.statusCode = 301;
res.writeHead(301, {
Location: `https://ubistatic-a.akamaihd.net/${config.cdn}${req.url}`
}).end();
return;
}
fs.readFile(pathname, function (err, data) {
if (err) {
res.statusCode = 404;
res.end(`error`);
} else {
res.setHeader('Content-type', map[ext] || 'text/plain');
res.end(data);
}
});
});
}
// Block all other requests
else {
console.log(chalk.red(`🛡 [PROTECTION] Blocked request: ${req.method} ${req.url} from IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'TCP-Other');
res.statusCode = 403;
res.end('Hello world');
}
});
server.listen(port, () => {
console.log(chalk.green(`Server is running on port ${port}`));
});
// -------------------- UDP Server Setup --------------------
const udpServer = dgram.createSocket('udp4');
const UDP_PORT = 53; // DNS typically uses port 53
udpServer.on('error', (err) => {
console.error(`UDP server error:\n${err.stack}`);
udpServer.close();
});
udpServer.on('message', (msg, rinfo) => {
const ip = rinfo.address;
const normalizedIp = normalizeIp(ip);
console.log(chalk.yellow(`UDP request from IP: ${normalizedIp}, Port: ${rinfo.port}`));
// Rate Limiting
if (isUdpRateLimited(normalizedIp)) {
console.log(chalk.red(`UDP Rate limit exceeded for IP: ${normalizedIp}`));
logBlockedIP(normalizedIp, 'UDP');
return; // Drop the packet silently or send a response indicating rate limiting
}
// GeoIP Blocking
if (isBlockedGeoIp(normalizedIp)) {
console.log(chalk.red(`Blocking UDP access from IP: ${normalizedIp} (GeoIP Block)`));
logBlockedIP(normalizedIp, 'UDP-GeoIP');
return; // Drop the packet silently or send a forbidden response
}
// Network Usage Limiting for UDP
const messageBytes = msg.length;
if (isNetworkUsageLimited(normalizedIp, messageBytes)) {
console.log(chalk.red(`Network usage limit exceeded for IP: ${normalizedIp} (UDP)`));
logBlockedIP(normalizedIp, 'UDP-NETWORK');
return; // Drop the packet
}
// Implement your DNS handling logic here
// For example, respond with a simple DNS response or forward the request to a legitimate DNS server
// Example: Forward the DNS request to an external DNS server (e.g., Google DNS)
const externalDns = '8.8.8.8';
const externalDnsPort = 53;
const externalClient = dgram.createSocket('udp4');
externalClient.send(msg, 0, msg.length, externalDnsPort, externalDns, (error) => {
if (error) {
console.error(`Error forwarding UDP request: ${error}`);
externalClient.close();
}
});
externalClient.on('message', (response) => {
udpServer.send(response, 0, response.length, rinfo.port, rinfo.address, (err) => {
if (err) {
console.error(`Error sending UDP response: ${err}`);
}
externalClient.close();
});
});
// Optional: Implement timeouts to prevent hanging
externalClient.on('error', (err) => {
console.error(`External DNS client error: ${err}`);
externalClient.close();
});
// Set a timeout for the external DNS response
externalClient.setTimeout(5000, () => { // 5 seconds timeout
console.error(`External DNS request timed out for IP: ${normalizedIp}`);
externalClient.close();
});
});
udpServer.on('listening', () => {
const address = udpServer.address();
console.log(chalk.green(`UDP Server listening on ${address.address}:${address.port}`));
});
udpServer.bind(UDP_PORT);
const shutdown = () => {
console.log('Shutting down the server...');
// Stop the server from accepting new connections
server.close(() => {
console.log('HTTP server closed.');
// Close the UDP server
udpServer.close(() => {
console.log('UDP server closed.');
// Exit the process
process.exit(0);
});
});
// Ensure shutdown if connections remain
setTimeout(() => {
console.error('Forced shutdown due to open connections.');
process.exit(1); // exit with error code if connections remain
}, 5000).unref();
};
configApp.post('/shutdown', (req, res) => {
res.send('Server is shutting down...');
shutdown();
});
// Capture abort signals
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
console.log('')
console.log(chalk.magenta('========================================================'));
console.log(chalk.magenta(`- CONFIGURATION LOADS : `));
console.log(chalk.magenta(`[@] Login URL : ${config.loginurl}`));
console.log(chalk.magenta(`[@] HTTP Port ${port}`));
console.log(chalk.magenta(`[@] ANTI-HOSTCHECK ${port}`));
console.log(chalk.magenta(`[@] ANTI-SCAN SERVER DATA PHP STATUS : ENABLED [PENTEST-SECURITY]`));
console.log(chalk.magenta(`[@] OPERATION SYSTEM : WINDOWS`));
console.log(chalk.magenta(`[@] HTTP KING VERSION : 3.3.8`));
console.log(chalk.greenBright(`[@] SUBSCRIPTION : VIP-SUPER`));
console.log(chalk.greenBright(`[@] LAST HTTP UPDATE : 6/10/2024`));
console.log(chalk.magenta('========================================================'));
console.log('')
console.log(chalk.green(`
::: ::: ::::::::::: :::: ::: :::::::: ::: ::: :::::::: ::::::::
:+: :+: :+: :+:+: :+: :+: :+: :+: :+: :+: :+: :+: :+:
+:+ +:+ +:+ :+:+:+ +:+ +:+ +:+ +:+ +:+ +:+
+#++:++ +#+ +#+ +:+ +#+ :#: +#+ +:+ +#++: +#++:
+#+ +#+ +#+ +#+ +#+#+# +#+ +#+# +#+ +#+ +#+ +#+
#+# #+# #+# #+# #+#+# #+# #+# #+#+# #+# #+# #+# #+#
### ### ########### ### #### ######## ### ######## ########
`));
}
startServer();
Key Enhancements Implemented:
Network Protocol Execution: Added encodeParameters and decodeParameters functions to handle obfuscation of request data.
Enhanced User-Agent Verification: Function isUserAgentValid checks for expected patterns in User-Agent strings.
Request Header Depth Inspection: Function isHeaderStructureValid checks for specific headers and validates the structure.
This full code integrates the new security measures and maintains the server's core functionalities. The validatedLogicForGrowtopia function is a placeholder for where you can implement your specific validation logic for incoming request data. Adjust the logic to suit the specifics of your application's expected data fields.