Variables, Loops, and Port Scanner

In the previous post, we went over how to create a Bash script and run it on our terminal. In this one, we will be taking a look into creating variables, loops, passing system arguments, and finally - building a portscanner.

Variables

Variables are names that hold a value. If we have a fruit, the fruit will have seeds in it, in programming we might say fruit="Seeds", it's a great way to store things which we want to reference it later in the program.

#!/bin/bash

hello="Hello World"
echo $hello

We first assigned a string "Hello World" to our variable called hello, then we used the $ operator to reference the variable hello, and used the echo command to print the string out to the user.

We can use a number of data types to store into our variables, namely:

string:         Alphanumerical Character  "Apple"
float:          Decimal Value             3.14
integer:        Numerical Value           5

Loops

Loops are methods in which we can iterate over a given code a specific number of times or, an infinite number of times (infinite loop). Generally, we want to avoid infinite loops as it can crash your program or computer.

There are a couple of loops that we can choose from; While loops, Until loops, and For Loops.

For Loop

The for loop is usually used to iterate through a range of items or a sequence of integers. Let's take a look at a simple example using the same code as before:

#!/bin/bash

hello="Hello World"
for i in range {1..5}
do
	echo $hello
done

We first gave an instruction to our loop to iterate on a range from 1-5: for i in range {1..5}, then we said do to tell the program what to run, which in this case was our echo statement, then we closed the loop using done

The output looks like this:

Hello World
Hello World
Hello World
Hello World
Hello World

While Loops

While loops are used when the limit is unknown, like loop $function until $function > 10, we don't know when it will be more than 10, but when it reaches that the program stops. (When it reaches the specified limit, it changes True to False)

#!/bin/bash

count=0
while [ $count -le 5 ]
do
	echo "Count = " $count
	((count++))
done

We first assigned an integer variable called count with the value 0 so we can iterate up to a number limit. Then, in our while loop, we specified that count should be Less Than (-le) 5. The command in the loop will print out the current number that count is assigned to then increment count by 1.

The output looks like this:

Count =  0
Count =  1
Count =  2
Count =  3
Count =  4
Count =  5

Until Loops

Until loops are similar to While loops, but instead of the statement being True, it's first False then changed to True once the condition is met.

count=0
until [ $count -gt 5 ]
do
	echo "Count = " $count
	((count++))
done

Our condition is that until our count variable is Greater Than (-gt) 5, repeat the command.

The output for this is the same as While.

A more in-depth look into loops can be found in this link

Port Scanner

A port scanner is a program which identifies what network ports a given address has. For example, if we have a network 127.0.0.1 with an http server, we might identify port 80. This is useful if we want to attack networks as it gives us a path of what we want to enumerate or use as our attack vector.

#!/bin/bash

# Variables
ip=$1

# Scanner
for port in range {1..65535}
do
        2>/dev/null echo > /dev/tcp/$ip/$port
        if [ $? == 0 ]; then
                {
                        echo "[+] Port: $port is open"
                }
        fi
done

The variable ip takes in the first command line argument from the user ./portscanner.sh <IP>, which is then run through /dev/tcp with the port from range 1..65535 (the max number of ports on a network). If the port is open, then the port will be printed to the user. If not, then it will be sent to /dev/null to hide the output.

$> ./portscanner.sh 127.0.0.1
[+] Port: 8080 is open

Conclusion

We now have a basic understanding of how Bash scripts are written and how we can interact with linux applications using it. Your task is to experiment with different things that you might need in a CTF, daily automation, etc. Good luck!

Last updated