@lowk3v/n8n-nodes-nmap
Version:
Nmap Scan
196 lines • 8.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NmapScan = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const ShellUtils_1 = require("./utils/ShellUtils");
const NmapUtils_1 = require("./utils/NmapUtils");
class NmapScan {
constructor() {
this.description = {
displayName: 'Nmap Scan',
name: 'nmapScan',
icon: 'file:NmapLogo.svg',
group: ['output'],
version: 1,
triggerPanel: false,
subtitle: '={{$parameter["operation"]}}',
description: 'Scan with nmap command',
defaults: {
name: 'Nmap Scan',
},
inputs: ["main"],
outputs: ["main"],
credentials: [
{
displayName: 'Local Sudo Password',
name: 'onlyPassword',
required: false,
},
],
properties: [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Quick Scan Network',
value: 'quick_scan_network',
description: 'Scan network with fast ping (-sn)',
action: 'Quick scan network',
},
{
name: 'Discovery Network',
value: 'discovery_network',
description: 'Discovery network with device and ports (-sS)',
action: 'Discovery network',
},
{
name: 'Ports Fast Scan',
value: 'ports_fast_scan',
description: 'Ports fast scan a host (-F)',
action: 'Ports fast scan',
},
{
name: 'All Ports Scan',
value: 'all_ports_scan',
description: 'All Ports scan a host (-p-)',
action: 'All ports scan',
},
],
default: 'discovery_network',
},
{
displayName: 'Target Network Range / Host / IP',
name: 'network_range',
type: 'string',
required: true,
default: '192.168.0.0/24',
description: 'Define the target to scan',
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add option',
default: {},
options: [
{
displayName: 'Aggressive Mode',
name: 'aggressive_mode',
type: 'number',
default: 5,
description: 'For faster execution (-T5)',
},
{
displayName: 'Check Top Ports',
name: 'top_ports',
type: 'number',
default: 1000,
description: 'Check usually top ports (--top-ports 1000)',
},
{
displayName: 'Host Discovery',
name: 'host_discovery',
type: 'boolean',
default: false,
description: 'Enable host discovery, faster if disable (-Pn)',
},
{
displayName: 'Put Result in Field',
name: 'ports_field',
type: 'string',
default: 'ports',
description: 'The name of the output field to put the data in',
},
],
},
],
};
this.methods = {};
}
async execute() {
const items = this.getInputData();
let item;
const returnItems = [];
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
item = { ...items[itemIndex] };
const newItem = {
json: item.json,
pairedItem: {
item: itemIndex,
},
};
const operation = this.getNodeParameter('operation', itemIndex);
const network_range = this.getNodeParameter('network_range', itemIndex);
const options = this.getNodeParameter('options', itemIndex);
const host_discovery = options.host_discovery ? '' : '-Pn';
const aggressive_mode = options.aggressive_mode ? '-T5' : '';
const top_ports = options.top_ports ? '--top-ports ' + options.top_ports : '';
const ports_field = options.ports_field || 'ports';
const handleOutput = (output) => {
console.log(`Nmap Scan done ${command}`);
if (operation === 'quick_scan_network') {
nmapUtils.parseNmapQuickScan(output).forEach((value) => {
returnItems.push({
json: value,
});
});
}
else if (operation === 'discovery_network') {
nmapUtils.parseNmapDiscovery(output, ports_field).forEach((value) => {
returnItems.push({
json: value,
});
});
}
else if (operation === 'ports_fast_scan' || operation === 'all_ports_scan') {
newItem.json[ports_field] = nmapUtils.parseNmapPorts(output);
returnItems.push(newItem);
}
};
let credentials = null;
try {
credentials = await this.getCredentials('onlyPassword');
}
catch (e) { }
let command = `nmap -help`;
if (operation === 'quick_scan_network') {
command = `nmap -sn ${aggressive_mode} ${network_range}`;
}
else if (operation === 'discovery_network') {
command = `nmap -sS ${host_discovery} ${aggressive_mode} ${top_ports} ${network_range}`;
}
else if (operation === 'ports_fast_scan') {
command = `nmap -F ${host_discovery} ${aggressive_mode} ${top_ports} ${network_range}`;
}
else if (operation === 'all_ports_scan') {
command = `nmap -p- ${host_discovery} ${aggressive_mode} ${top_ports} ${network_range}`;
}
console.log(`Nmap Scan starting ${command}`);
const nmapUtils = new NmapUtils_1.NmapUtils();
const shellUtils = new ShellUtils_1.ShellUtils();
const workingDirectory = await shellUtils.resolveHomeFolder('~/');
if (credentials && credentials.password !== undefined && credentials.password !== '') {
await shellUtils
.sudoCommand(command, workingDirectory, credentials.password)
.then(handleOutput)
.catch((e) => {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), e);
});
}
else {
await shellUtils
.command(command, workingDirectory)
.then(handleOutput)
.catch((e) => {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), e);
});
}
}
return [returnItems];
}
}
exports.NmapScan = NmapScan;
//# sourceMappingURL=NmapScan.node.js.map