-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmasscan_to_nmap.sh
More file actions
executable file
·245 lines (194 loc) · 7.08 KB
/
Copy pathmasscan_to_nmap.sh
File metadata and controls
executable file
·245 lines (194 loc) · 7.08 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/bin/bash
################################################################################
# masscan_to_nmap.sh - Workflow Híbrido Masscan → Nmap
#
# Descripción:
# Script que automatiza el workflow profesional de reconocimiento:
# 1. Masscan: Escaneo RÁPIDO de todos los puertos (6-10 segundos)
# 2. Nmap: Análisis DETALLADO solo de puertos abiertos (versiones + scripts)
#
# Uso:
# ./masscan_to_nmap.sh <target_ip> [output_dir]
#
# Ejemplo:
# ./masscan_to_nmap.sh 10.10.10.8
# ./masscan_to_nmap.sh 192.168.1.100 scans/target1
#
# Requisitos:
# - masscan (sudo apt install masscan)
# - nmap
# - sudo privileges
#
# Autor: @3diklab
# Inspirado en luvliscan (Mi script privado)
################################################################################
set -euo pipefail
# Colores
readonly RED='\033[1;31m'
readonly GREEN='\033[1;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[1;34m'
readonly CYAN='\033[1;36m'
readonly NC='\033[0m'
# Configuración
readonly MASSCAN_RATE="${MASSCAN_RATE:-10000}"
readonly NMAP_TIMING="${NMAP_TIMING:-4}"
################################################################################
# Funciones auxiliares
################################################################################
log_info() {
echo -e "${BLUE}[INFO]${NC} $*"
}
log_success() {
echo -e "${GREEN}[✓]${NC} $*"
}
log_error() {
echo -e "${RED}[✗]${NC} $*" >&2
}
log_warning() {
echo -e "${YELLOW}[!]${NC} $*"
}
show_banner() {
echo ""
echo -e "${CYAN}╔═══════════════════════════════════════════════════════════╗${NC}"
echo -e "${CYAN}║${NC} ${CYAN}║${NC}"
echo -e "${CYAN}║${NC} ${GREEN}MASSCAN → NMAP WORKFLOW${NC} ${CYAN}║${NC}"
echo -e "${CYAN}║${NC} ${YELLOW}Reconocimiento Híbrido Profesional${NC} ${CYAN}║${NC}"
echo -e "${CYAN}║${NC} ${CYAN}║${NC}"
echo -e "${CYAN}║${NC} ${BLUE}@3diklab${NC} ${CYAN}║${NC}"
echo -e "${CYAN}║${NC} ${CYAN}║${NC}"
echo -e "${CYAN}╚═══════════════════════════════════════════════════════════╝${NC}"
echo ""
}
validate_requirements() {
local missing=()
if ! command -v masscan &> /dev/null; then
missing+=("masscan")
fi
if ! command -v nmap &> /dev/null; then
missing+=("nmap")
fi
if [ ${#missing[@]} -gt 0 ]; then
log_error "Missing required tools: ${missing[*]}"
log_info "Install with: sudo apt install ${missing[*]}"
return 1
fi
if [ "$EUID" -ne 0 ]; then
log_warning "This script requires sudo for masscan"
log_info "Re-running with sudo..."
exec sudo bash "$0" "$@"
fi
return 0
}
validate_ip() {
local ip=$1
if [[ ! $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
log_error "Invalid IP address format: $ip"
return 1
fi
return 0
}
################################################################################
# Funciones principales
################################################################################
run_masscan() {
local target=$1
local output_file=$2
log_info "Starting MASSCAN on $target (rate: $MASSCAN_RATE pps)"
echo ""
local start_time=$(date +%s)
if masscan "$target" -p1-65535 --rate="$MASSCAN_RATE" -oG "$output_file" 2>/dev/null; then
local end_time=$(date +%s)
local duration=$((end_time - start_time))
log_success "Masscan completed in ${duration}s"
return 0
else
log_error "Masscan failed"
return 1
fi
}
parse_ports() {
local masscan_file=$1
# Extraer puertos abiertos del formato grepable de masscan
# Formato: Timestamp: 1762109848 Host: 45.33.32.156 () Ports: 31337/open/tcp//unknown//
grep "Ports:" "$masscan_file" 2>/dev/null | \
awk '{print $7}' | \
cut -d'/' -f1 | \
sort -n | \
uniq | \
tr '\n' ',' | \
sed 's/,$//'
}
run_nmap() {
local target=$1
local ports=$2
local output_dir=$3
log_info "Starting NMAP service detection on ports: $ports"
echo ""
local start_time=$(date +%s)
nmap -p"$ports" -sCV -T"$NMAP_TIMING" -oA "$output_dir/nmap_service_scan" "$target"
local end_time=$(date +%s)
local duration=$((end_time - start_time))
log_success "Nmap completed in ${duration}s"
}
display_results() {
local nmap_file=$1
echo ""
echo -e "${GREEN}╔═══════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║${NC} ${YELLOW}SCAN RESULTS${NC} ${GREEN}║${NC}"
echo -e "${GREEN}╚═══════════════════════════════════════════════════════════╝${NC}"
echo ""
if [ -f "$nmap_file.nmap" ]; then
grep -E "^[0-9]+/tcp|^[0-9]+/udp" "$nmap_file.nmap" || log_warning "No open ports found"
fi
echo ""
}
################################################################################
# Main
################################################################################
main() {
show_banner
# Validar argumentos
if [ $# -lt 1 ]; then
log_error "Usage: $0 <target_ip> [output_dir]"
echo ""
echo "Example:"
echo " $0 10.10.10.8"
echo " $0 192.168.1.100 scans/target1"
echo ""
exit 1
fi
local target=$1
local output_dir="${2:-.}"
# Validaciones
validate_ip "$target" || exit 1
validate_requirements || exit 1
# Crear directorio de salida
mkdir -p "$output_dir"
local masscan_file="$output_dir/masscan_results.gnmap"
# Fase 1: Masscan
echo -e "${CYAN}═══ PHASE 1: FAST PORT DISCOVERY (MASSCAN) ═══${NC}"
echo ""
run_masscan "$target" "$masscan_file" || exit 1
# Parsear puertos
local ports=$(parse_ports "$masscan_file")
if [ -z "$ports" ]; then
log_error "No open ports found or parsing failed"
log_info "Check: $masscan_file"
exit 1
fi
local port_count=$(echo "$ports" | tr ',' '\n' | wc -l)
log_success "Found $port_count open port(s): $ports"
echo ""
echo -e "${CYAN}═══ PHASE 2: SERVICE DETECTION (NMAP) ═══${NC}"
echo ""
# Fase 2: Nmap
run_nmap "$target" "$ports" "$output_dir"
# Mostrar resultados
display_results "$output_dir/nmap_service_scan"
log_success "Scan completed!"
log_info "Results saved in: $output_dir/"
echo ""
}
# Ejecutar
main "$@"