Skip to content

Instantly share code, notes, and snippets.

@anibalardid
Created May 18, 2021 01:02
Show Gist options
  • Save anibalardid/5e05b6472feb3d31116729dc24e6d3e2 to your computer and use it in GitHub Desktop.
Save anibalardid/5e05b6472feb3d31116729dc24e6d3e2 to your computer and use it in GitHub Desktop.
Extract ports from nmap output #hack #security #s4vitar
# Used:
# nmap -p- --open -T5 -v -n ip -oG allPorts
# Extract nmap information
# Run as:
# extractPorts allPorts
function extractPorts(){
ports="$(cat $1 | grep -oP '\d{1,5}/open' | awk '{print $1}' FS='/' | xargs | tr ' ' ',')"
ip_address="$(cat $1 | grep -oP '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | sort -u | head -n 1)"
echo -e "\n[*] Extracting information...\n" > extractPorts.tmp
echo -e "\t[*] IP Address: $ip_address" >> extractPorts.tmp
echo -e "\t[*] Open ports: $ports\n" >> extractPorts.tmp
echo $ports | tr -d '\n' | xclip -sel clip
echo -e "[*] Ports copied to clipboard\n" >> extractPorts.tmp
cat extractPorts.tmp; rm extractPorts.tmp
}
@dstnat
Copy link

dstnat commented Feb 17, 2024

./extraeports.sh allPorts
./extraeports.sh: 7: Syntax error: "(" unexpected

@krootca
Copy link

krootca commented Mar 9, 2024

./extraeports.sh allPorts ./extraeports.sh: 7: Syntax error: "(" unexpected

Simplemente agrega al principio de la linea:

#!/bin/bash

Ya que no reconoce en que lenguaje va ejecutar el script, si es que lo pusiste sin su extensión, yo por ejemplo lo tengo así

/bin/extractPorts

Le di privilegios para ejecución:

chmod +x /bin/extractPorts

Y modifique el programa tal que así:

#!/bin/bash
# Used:
# nmap -p- --open -T5 -v -n ip -oG allPorts

# Extract nmap information
# Run as:
# extractPorts allPorts

function extractPorts(){
	# say how to usage
	if [ -z "$1" ]; then
		echo "Usage: extractPorts <filename>"
		return 1
	fi

	# Say file not found
	if [ ! -f "$1" ]; then
		echo "File $1 not found"
		return 1
	fi

	#if this not found correctly, you can delete it, from "if" to "fi".
	if ! grep -qE '^[^#].*/open/' "$1"; then
		echo "Format Invalid: Use -oG <file>, in nmap for a correct format."
		return 1
	fi

	ports="$(cat $1 | grep -oP '\d{1,5}/open' | awk '{print $1}' FS='/' | xargs | tr ' ' ',')";
	ip_address="$(cat $1 | grep -oP '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' | sort -u | head -n 1)"
	echo -e "\n[*] Extracting information...\n" > extractPorts.tmp
	echo -e "\t[*] IP Address: $ip_address"  >> extractPorts.tmp
	echo -e "\t[*] Open ports: $ports\n"  >> extractPorts.tmp
	echo $ports | tr -d '\n' | xclip -selection clipboard
	echo -e "[*] Ports copied to clipboard\n"  >> extractPorts.tmp
	cat extractPorts.tmp; rm extractPorts.tmp
}
extractPorts "$1"

Siguiendo lo mismo, ahora funciona mejor el script.

@dstnat
Copy link

dstnat commented Mar 10, 2024

agradecido, muy muy agradecido, jamas pense que fuese eso, ya no da el error ahora lo que no se es porque no me pega lo que deberia copiar con xclip que lo tengo instalado.

@dstnat
Copy link

dstnat commented Mar 10, 2024

captura = cat allPorts

Nmap 7.94SVN scan initiated Sat Feb 17 17:15:17 2024 as: nmap -p- --open -T5 -v -n -oG allPorts 192.168.0.1

Ports scanned: TCP(65535;1-65535) UDP(0;) SCTP(0;) PROTOCOLS(0;)

Host: 192.168.0.1 () Status: Up
Host: 192.168.0.1 () Ports: 22/open/tcp//ssh///, 53/open/tcp//domain///, 80/open/tcp//http///, 1900/open/tcp//upnp///

Nmap done at Sat Feb 17 17:15:29 2024 -- 1 IP address (1 host up) scanned in 12.52 seconds

@dstnat
Copy link

dstnat commented Mar 10, 2024

de hecho le acabo de dar ./extractPorts.sh allPorts y me pega lo que acabo de pegar aca

@dstnat
Copy link

dstnat commented Mar 10, 2024

hecho amigo, lo copie del suyo, muchisimas gracias!!!!

@HA356AH
Copy link

HA356AH commented Jul 18, 2024

ty Krootca

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment