-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservernmap.js
More file actions
88 lines (69 loc) · 2.47 KB
/
servernmap.js
File metadata and controls
88 lines (69 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express = require('express');
const app = express();
const port = 5000;
const cors = require('cors');
const bodyParser = require('body-parser');
const sudo = require('sudo-prompt');
const { exec } = require('child_process');
// Middleware
app.use(bodyParser.json());
app.use(cors());
app.use(express.json());
let sudoAuthenticated = false;
// how to commands line use interface created
app.post('/nmap', (req, res) => {
const { command } = req.body;
// Debugging: Log the command received
console.log(`Received command: ${command}`);
// Validate the command (basic example, improve for production use)
if (!command.startsWith('nmap ')) {
return res.status(400).send({ error: 'Invalid command' });
}
// Execute nmap command with sudo only if not authenticated yet
if (!sudoAuthenticated) {
// Prompt for sudo password
sudo.exec('echo "sudo password prompt"', { name: 'Nmap' }, (error, stdout, stderr) => {
if (error) {
console.error(`Error prompting for sudo password: ${stderr}`);
return res.status(500).send({ error: 'Failed to authenticate with sudo' });
}
sudoAuthenticated = true; // Set authentication flag to true
executeNmap(command, res); // Execute the actual nmap command
});
} else {
executeNmap(command, res); // Execute the actual nmap command if already authenticated
}
});
// Function to execute nmap command
function executeNmap(command, res) {
// Execute nmap command with sudo
exec(command, { name: 'Nmap' }, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${stderr}`);
return res.status(500).send({ error: stderr });
}
console.log(`Command output: ${stdout}`);
res.send({ output: stdout });
});
}
// ip info
app.get('/network-info', (req, res) => {
exec('ifconfig', (error, stdout, stderr) => {
if (error) {
res.status(500).json({ error: stderr });
return;
}
// Extract IP addresses from the ifconfig output
const ipRegex = /inet\s(\d+\.\d+\.\d+\.\d+)/g;
const ips = [];
let match;
while ((match = ipRegex.exec(stdout)) !== null) {
ips.push(match[1]);
}
res.json({ data: ips });
});
});
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});