-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnmap-pentest.sh
More file actions
71 lines (57 loc) · 2.44 KB
/
nmap-pentest.sh
File metadata and controls
71 lines (57 loc) · 2.44 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
#!/usr/bin/env bash
# Install necessary tools
sudo apt-get update
sudo apt-get install -y nmap sqlite3
# Download and install nmap-formatter
VERSION=v2.1.0
curl -L "https://github.com/vdjagilev/nmap-formatter/releases/download/$VERSION/nmap-formatter-linux-amd64.tar.gz" -o nmap-formatter.tar.gz
tar -xzvf nmap-formatter.tar.gz
sudo mv nmap-formatter /usr/local/bin
# Create results directory
mkdir -p /home/nmap/result
cd /home/nmap
# File to store the session ID
SESSION_ID_FILE="/home/nmap/session_id.txt"
# Function to generate a new session ID (timestamp + UUID)
generate_session_id() {
echo "$(date +'%Y%m%d%H%M%S')-$(uuidgen)" # Using timestamp + UUID as session ID
}
# Check if session ID file exists, if not, create one
if [ ! -f "$SESSION_ID_FILE" ]; then
# Generate a new session ID and save it to the file
session_id=$(generate_session_id)
echo "$session_id" > "$SESSION_ID_FILE"
else
# Read the existing session ID from the file
session_id=$(cat "$SESSION_ID_FILE")
fi
echo "Using session ID: $session_id"
# Function to perform NMAP scan and save results to SQLite database
perform_nmap_scan() {
local hostname=$1
local ip=$2
local provider=$3
echo "Performing NMAP scan for $hostname ($ip) under $provider with session ID: $session_id"
local output_file="$hostname.xml"
nmap -p- -T4 --max-rtt-timeout 300ms "$ip" -oX "$output_file"
echo "Saving NMAP results to SQLite database"
# Save the Nmap scan results into the SQLite database
nmap-formatter sqlite "$output_file" --sqlite-dsn nmap.db
# Ensure the database and table exist with session_id column
sqlite3 nmap.db "CREATE TABLE IF NOT EXISTS boundary (session_id TEXT, host TEXT, ip TEXT, provider TEXT);"
sqlite3 nmap.db "INSERT INTO boundary (session_id, host, ip, provider) VALUES ('$session_id', '$hostname', '$ip', '$provider');"
}
# Read the ENDPOINTS environment variable, splitting on newlines
IFS=$'\n' read -rd '' -a ADDR <<< "$ENDPOINTS"
for entry in "${ADDR[@]}"; do
# Split each line into parts based on the delimiter '|'
IFS='|' read -ra PARTS <<< "$entry"
if [ "${#PARTS[@]}" -eq 3 ]; then
perform_nmap_scan "${PARTS[0]}" "${PARTS[1]}" "${PARTS[2]}"
else
echo "Skipping malformed entry: $entry"
fi
done
# Copy the database to the result directory
sudo cp nmap.db /home/nmap/result/nmap.db
echo "All NMAP scans completed and result saved to SQLite database file at /home/nmap/result/nmap.db"