-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhichSystem.py
More file actions
76 lines (61 loc) · 2.24 KB
/
Copy pathwhichSystem.py
File metadata and controls
76 lines (61 loc) · 2.24 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pwn import text, log
import re
import subprocess
import sys
# ─────────────────────────────────────────
# Constantes
# ─────────────────────────────────────────
TTL_LINUX: int = 64
TTL_WINDOWS: int = 128
TTL_PATTERN: str = r"ttl=(\d+)"
def _resolve_os(ttl: int) -> str:
"""Devuelve el nombre del SO según el valor TTL."""
if ttl <= TTL_LINUX:
return "Linux"
elif ttl <= TTL_WINDOWS:
return "Windows"
return "Desconocido"
def system_detection(hostname: str) -> None:
"""
Lanza un ping al host y determina el SO basándose en el TTL.
Args:
hostname: IP o nombre de host objetivo.
Raises:
SystemExit: Si el host es inalcanzable o el ping falla.
"""
try:
output: str = subprocess.check_output(
["ping", "-c", "1", hostname],
text=True,
timeout=1,
stderr=subprocess.DEVNULL,
)
except subprocess.TimeoutExpired:
log.failure(text.yellow(f"Host {hostname} inalcanzable (timeout)"))
sys.exit(1)
except subprocess.CalledProcessError:
log.failure(text.yellow(f"Host {hostname} inalcanzable (ping fallido)"))
sys.exit(1)
except FileNotFoundError:
log.failure(text.yellow("Comando 'ping' no encontrado en el sistema"))
sys.exit(1)
match = re.search(TTL_PATTERN, output, re.IGNORECASE)
if not match:
log.failure(text.yellow(f"No se pudo extraer el TTL de la respuesta de {hostname}"))
sys.exit(1)
ttl_value: int = int(match.group(1))
os_name: str = _resolve_os(ttl_value)
print("")
log.info(text.yellow(f"Operative System: {hostname} → {os_name} (TTL={ttl_value})"))
def _parse_args() -> str:
"""Valida y devuelve el argumento IP/hostname desde la línea de comandos."""
if len(sys.argv) < 2:
print("")
log.info(text.yellow(f"Uso: python3 {sys.argv[0]} <Target Ip>"))
sys.exit(1)
return sys.argv[1]
if __name__ == "__main__":
target_ip: str = _parse_args()
system_detection(target_ip)