-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathutils.py
102 lines (89 loc) · 3.08 KB
/
utils.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# nmap3.py
#
# Copyright 2019 Wangolo Joel <wangolo@ldap.testlumiotic.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import shlex
import subprocess
import sys
import os
import ctypes
import functools
from nmap3.exceptions import NmapNotInstalledError
__author__ = 'Wangolo Joel (inquiry@nmapper.com)'
__version__ = '1.6.0'
__last_modification__ = 'Sep/15/2024'
def get_nmap_path(path:str='') -> str:
"""
Accepts path, validate it. If not valide, search nmap path
Returns the location path where nmap is installed
by calling which nmap
If not found raises NmapNotInstalledError
"""
if path and (os.path.exists(path)):
return path
os_type = sys.platform
if os_type == 'win32':
cmd = "where nmap"
else:
cmd = "which nmap"
args = shlex.split(cmd)
sub_proc = subprocess.Popen(args, stdout=subprocess.PIPE)
output, e = sub_proc.communicate(timeout=15)
if e:
print(e)
if not output:
raise NmapNotInstalledError(path=path)
if os_type == 'win32':
return output.decode('utf8').strip().replace("\\", "/")
return output.decode('utf8').strip()
def get_nmap_version():
nmap = get_nmap_path()
cmd = nmap + " --version"
args = shlex.split(cmd)
sub_proc = subprocess.Popen(args, stdout=subprocess.PIPE)
try:
output, _ = sub_proc.communicate(timeout=15)
print(e)
sub_proc.kill()
else:
return output.decode('utf8').strip()
def user_is_root(func):
def wrapper(*args, **kwargs):
try:
is_root_or_admin = (os.getuid() == 0)
except AttributeError:
is_root_or_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
if(is_root_or_admin):
return func(*args, **kwargs)
else:
return {"error":True, "msg":"You must be root/administrator to continue!"}
return wrapper
def nmap_is_installed_async():
def wrapper(func):
@functools.wraps(func)
async def wrapped(*args, **kwargs):
nmap_path = get_nmap_path()
if(os.path.exists(nmap_path)):
return await func(*args, **kwargs)
else:
print({"error":True, "msg":"Nmap has not been install on this system yet!"})
return {"error":True, "msg":"Nmap has not been install on this system yet!"}
return wrapped
return wrapper