Bash Socket Programming
by Domenico Raffaele on Jul.01, 2010, under Hacking, Linux, Networking
Sotto bash è possibile aprire un socket e far passare dei dati attraverso di esso. Non è necessario usare i comandi curl o lynx per ottenere i dati da un server remoto.
Bash dispone di due files device speciali che possono essere utilizzati per aprire network sockets.
Dalla man page della bash:
- /dev/tcp/host/port - Se host è un hostname o un indirizzo Internet valido, e port è un intero nel range dei port number o un nome di servizio, la bash tenta di aprire una connessione TCP verso il corrispondente socket.
- /dev/udp/host/port - Se host è un hostname o un indirizzo Internet valido, e port è un intero nel range dei port number o un nome di servizio, la bash tenta di aprire una connessione UDP verso il corrispondente socket.
È possibile usare questa tecnica per determinare se una porta è in stato open oppure closed su un server locale o remoto senza usare nmap o un altro port scanner:
Questo esempio rileva se la porta 80 è open oppure no:
# (echo >/dev/tcp/localhost/80) &>/dev/null && echo "TCP port 80 open" || echo "TCP port 80 closed"
È possibile a questo punto usare un loop bash e trovare le porte “open”:
echo "Scanning TCP ports..."
for p in {1..1023}
do
(echo >/dev/tcp/localhost/$p) >/dev/null 2>&1 && echo "$p open"
done
output dell’ esempio:
Scanning TCP ports...
22 open
25 open
53 open
80 open
139 open
445 open
631 open
Nell’ esempio successivo, lo script bash si comporta come un client HTTP :
# cat prova.sh
#!/bin/bash
exec 3<> /dev/tcp/www.voipandhack.it/80
printf "GET / HTTP/1.0rn" >&3
printf "Accept: text/html, text/plainrn" >&3
printf "Accept-Language: enrn" >&3
printf "User-Agent: NetCraft_BashScript v.%srn" "${BASH_VERSION}" >&3
printf "rn" >&3
while read LINE <&3
do
echo $LINE
done
# ./prova.sh
HTTP/1.1 200 OK
Date: Wed, 30 Jun 2010 21:22:22 GMT
Server: Apache
Last-Modified: Tue, 08 Jan 2008 10:08:40 GMT
ETag: "4b8679-2c-44333292c3a00"
Accept-Ranges: bytes
Content-Length: 44
Connection: close
Content-Type: text/html; charset=ISO-8859-1
X-Pad: avoid browser bug
Apache is functioning normally
Nell’esempio successivo si comporta come un client HTTP più credibile:
# cat prova.sh #!/bin/bash exec 2<>/dev/tcp/www.voipandhack.it/80 printf "GET / HTTP/1.0rn" >&2 printf "Host: www.voipandhack.itrn" >&2 printf "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8rn" >&2 printf "Accept-Language: en-us,en;q=0.5rn" >&2 printf "Accept-encoding: gzip,deflatern" >&2 printf "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.8) Gecko/20100214 Ubuntu/9.10 (karmic) Firefox/3.5.9rn" >&2 printf "Connection: keep-alivern" >&2 printf "rn" >&2 while read LINE <&2 do echo $LINE done # ./prova.sh HTTP/1.1 200 OK Date: Wed, 30 Jun 2010 21:35:36 GMT Server: Apache X-Powered-By: PHP/5.2.12 X-Pingback: http://www.voipandhack.it/xmlrpc.php Connection: close Content-Type: text/html; charset=UTF-8 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> VoIP and Hacking [...]


July 3rd, 2010 on 8:08 pm
[...] Un articolo dell’ottimo voipandhack.it mi ha fatto conoscere una feature di bash che permette di aprire socket verso host remoti. In rete [...]