admin管理员组

文章数量:1335861

I am creating a nestjs app that uses node-nmap-hosts npm package to scan a network for connected devices but keeps getting an error of root privileges when I start my server.

Here is the code that runs the scan

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { runNmapScan } from 'node-nmap-hosts';

runNmapScan('192.168.1.1/24', [])
  .then((data) => {
    console.log('Nmap Scan Data:', data);
  })
  .catch((error) => {
    console.error('Error during Nmap Scan:', error);
  });

async function bootstrap() {
  const app: INestApplication = await NestFactory.create(AppModule);

  const config = new DocumentBuilder()
    .setTitle('AccionMFB Asset Register API')
    .setDescription('The AccionMFB Asset Register API description')
    .setVersion('0.1')
    .build();

  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('', app, document);

  app.useGlobalPipes(new ValidationPipe({ whitelist: true }));

  const configService = app.get(ConfigService);
  await app.listen(configService.get<string>('PORT'));
  console.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();

When I run npm run start:dev this error message is thrown

Execution Error: Error: Command failed: nmap -sS -T2 -O --osscan-guess -p  -sV -oX - 192.168.1.1/24
You requested a scan type which requires root privileges.
QUITTING!

    at genericNodeError (node:internal/errors:983:15)
    at wrappedFn (node:internal/errors:537:14)
    at ChildProcess.exithandler (node:child_process:419:12)
    at ChildProcess.emit (node:events:518:28)
    at maybeClose (node:internal/child_process:1104:16)
    at Socket.<anonymous> (node:internal/child_process:456:11)
    at Socket.emit (node:events:518:28)
    at Pipe.<anonymous> (node:net:343:12) {
  code: 1,
  killed: false,
  signal: null,
  cmd: 'nmap -sS -T2 -O --osscan-guess -p  -sV -oX - 192.168.1.1/24'
}
Error during Nmap Scan: Error: Command failed: nmap -sS -T2 -O --osscan-guess -p  -sV -oX - 192.168.1.1/24
You requested a scan type which requires root privileges.
QUITTING!

    at genericNodeError (node:internal/errors:983:15)
    at wrappedFn (node:internal/errors:537:14)
    at ChildProcess.exithandler (node:child_process:419:12)
    at ChildProcess.emit (node:events:518:28)
    at maybeClose (node:internal/child_process:1104:16)
    at Socket.<anonymous> (node:internal/child_process:456:11)
    at Socket.emit (node:events:518:28)
    at Pipe.<anonymous> (node:net:343:12) {
  code: 1,
  killed: false,
  signal: null,
  cmd: 'nmap -sS -T2 -O --osscan-guess -p  -sV -oX - 192.168.1.1/24'
}

npm run start dev is a script to execute nest start --watch

The runNmapScan is internally calling nmap -sS -T2 -O --osscan-guess -p -sV -oX - 192.168.1.1/24 which require the sudo access.

I have tried running sudo npm run start:dev after inputing the password it returned sudo: npm: command not found, tried modifying my sudoers file to grant access to any program calling nmap internally but the error persist.

I need assistance on how i can run the program with sudo or preventing the prompt for sudo access.

本文标签: nodejsRunning a nestjs app using nmap with a sudo accessStack Overflow