Fun with Bash

Special characters in file names

- is recognized as stdin, so use ./- for checking filenames beginning with -.

Use "hello world" when names contain spaces.

IO redirection

#!/bin/bash

exec 3<>/dev/tcp/localhost/30002
head -1 <&3

old_password="UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ"

for i in {0000..9999}
do
    answer="${old_password} ${i}"
    echo $answer >&3
    response=$(head -1 <&3)
    if ! [[ $response =~ "Wrong" ]]
    then
        echo $i
        break
    fi
done

exec 3>&-

exec 3<> makes file description 3 bidirectional; you can send or receive message from localhost:30002 through fd3. echo hello >&3 sends hello to fd3. head -1 <&3 prints out the first line of fd3; using head because this’s a stream so no eof.

exec 3>&- reset fd3 to normal.

Note: Basic fd for shell’s operation are 0 for stdin, 1 for stdout, 2 for stderr.

Variables

No spaces between =, ie. x=23. String interpolation uses ${}.

x="hello"
y="world"
z="${x} ${y}"

netcat

Use nc to create a server/client to send tcp.

# Server
nc -l 1234
# Client
nc localhost 1234

Opening ports to outside is as always dangerous.