-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnmap_scan.py
More file actions
95 lines (77 loc) · 2.72 KB
/
nmap_scan.py
File metadata and controls
95 lines (77 loc) · 2.72 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
89
90
91
92
93
94
95
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Script Base para Escaneos con Nmap
Este script proporciona una interfaz base para diferentes tipos de escaneos con Nmap.
"""
import os
import sys
import subprocess
import logging
import argparse
from typing import List, Dict, Any
from datetime import datetime
class NmapScanner:
def __init__(self, target: str):
"""
Inicializa el escáner Nmap
Args:
target (str): IP o dominio a escanear
"""
self.target = target
self.results_file = f"nmap_results_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xml"
# Configurar logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def ejecutar_escaneo(self, argumentos: str) -> bool:
"""
Ejecuta un escaneo con Nmap
Args:
argumentos (str): Argumentos para el escaneo
Returns:
bool: True si el escaneo fue exitoso
"""
try:
cmd = ['nmap', *argumentos.split(), '-oX', self.results_file, self.target]
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
# Monitorear salida
while True:
output = process.stdout.readline()
if output == '' and process.poll() is not None:
break
if output:
logging.info(output.strip())
return True
except Exception as e:
logging.error(f"Error durante el escaneo: {str(e)}")
return False
def mostrar_resultados(self):
"""Muestra los resultados del escaneo"""
try:
with open(self.results_file, 'r') as f:
print("\nResultados del escaneo:")
print("-" * 50)
print(f.read())
except Exception as e:
logging.error(f"Error al mostrar resultados: {str(e)}")
def main():
parser = argparse.ArgumentParser(description='Script Base para Escaneos con Nmap')
parser.add_argument('target', help='IP o dominio a escanear')
parser.add_argument('--arguments', default='-sV -sS -T4',
help='Argumentos para Nmap')
args = parser.parse_args()
scanner = NmapScanner(args.target)
# Ejecutar escaneo
if scanner.ejecutar_escaneo(args.arguments):
scanner.mostrar_resultados()
else:
logging.error("El escaneo falló")
if __name__ == "__main__":
main()