nmmapper / python3-nmap

A python 3 library which helps in using nmap port scanner. This is done by converting each nmap command into a callable python3 method or function. System administrators can now automatic nmap scans using python
https://www.nmmapper.com/sys/networkmapper/nmap/online-port-scanning/
GNU General Public License v3.0
277 stars 73 forks source link

Removed possibility to elevate privileges in code (commit: 3c2246b7dfdd6164dd5b7a0a74cfdc6585ea5a32) #80

Closed tnyblom closed 2 years ago

tnyblom commented 2 years ago

In commit: 3c2246b7dfdd6164dd5b7a0a74cfdc6585ea5a32 the possibility to use this tool as a normal user and only allow the usage of sudo for scanning was removed as part of an IPv6 fix. Any particular reason why this change was included there?

nmmapper commented 2 years ago

The latest changes released days ago don't include that part of the code, it appears you are using an older version of the release which was released on Jan 14.

tnyblom commented 2 years ago

Hi, I'm confused, in the latest code: https://github.com/nmmapper/python3-nmap/blob/6784da6c9d4831c1e3e30e0b778ac218ebdf4b65/nmap3/nmap3.py#L91 The "sudo" part is missing so the possibility to use with running my script as sudo is removed.

nmmapper commented 2 years ago

You, just ensure that you are calling the appropriate code with super user access. Where necessary.

tnyblom commented 2 years ago

My issue is that there has been an backward incompatible change that removes functionality and renders exposed API calls no-ops. I now need to run any script that includes this library fully as root where as before I could simply add a line in sudoers that would allow the script user to run nmap (with any options if needed) as root without having to fully open up root access. Running script as root when not needed seems like an unnecessary risk to me.

Might I ask why this change was done?

mcornella commented 1 month ago

For people checking this in the future, it's possible to patch the nmap object to prepend "sudo" to the default command:

import nmap3
from unittest.mock import patch

nmap = nmap3.Nmap()
default_command = "sudo " + nmap.default_command()
with patch.object(nmap, "default_command", return_value=default_command):
    nmap_results = nmap.scan_top_ports("host")