Today I've learned: Bash Scripting

Intro

Bash scripting is a really important skill. Without this skill you can’t do much stuff.

You have to know how to write some hacking tools. How to automate some actions and commands.

To become an elite hacker you need to know how to build a script in one of the widely used scripting languages, such as Ruby, Python or Perl. (Perl is the best text manipulation scripting language.)

I’m planning to dive deeper into the Perl in the future. At the moment I have some knowledge in Python.

In this article we’ll build some basic scripts with Bash.

What is Bash?

Bash is - Bourne-again shell. It’s an interface between the user and the operating system.

It enables you to manipulate files and run commands, utilities, programs, etc…

Main advantage of a shell is that you perform these tasks immediately from the computer and not through an abstraction, like GUI.

Tools that we need

For Bash scripting we need text manipulation tool. It's similar to Notepad on Windows or TextEdit on macOS..

I’ll be using Nano terminal editor.

Also we’ll use commands echo and read (it reads the data and stores it somewhere else).

First script

We’ll write a simple script that echos Hello, my friend.

In the terminal write:

nano

When the editor window opens, write the following:

#! /bin/bash

# This is my first bash script

echo "Hello, my friend"
  • #! - we start with shebang (combination of hash mark and exclamation mark) to tell the system that we want to use the bash interpreter.

  • hash mark (#) - We add a comment, which is usually used to explain what the script does. Comments are visible only to humans.

Now save this file as hello by pressing CTRL+S and exit the editor with CTRL+X.

Permission for execution

By default a newly created bash script is not executable. We need to change permissions using chmod.

You can see current permission by long listing files:

ls -l
-rw-r--r-- 1 root root   30 Dec 27 06:36 hello

You can see that owner can only read and write (UGO syntax shows us this information).

I’ve already learned about files permissions in the previous chapters of Linux Basics for Hackers, so I share the article about this topic. Long story short (r (read), w(write), x(execute). UGO stands for User(owner), Group, Others)

To change permission we can do this:

chmod 755 hello
ls -l
-rwxr-xr-x 1 root root   30 Dec 27 06:36 hello

Now, as you can see, the owner can read, write, and execute. Group and other can read and execute.

Running the script

At this point you can run this script by doing so:

./hello
Hello, my friend

The ./ before the filename tells the system that we want to execute this script.

Script with user input

Now, let’s interact with user inputs.

Now variables will come into play.

Variable is an area of storage that can hold something in memory. That something can be text (string), number or a true/false value (boolean).

We can change the value of our variables and it is very useful.

Let’s create another script with a name variable.

#! /bin/bash
echo "What is your name?"
read name
echo "Welcome to our journey, $name."

By using read, we store the value of user input into the variable, which we later use with echo.

To use the variable we need to use $ to retrieve it.

Important - don’t forget to change the permissions as we did before with chmod.

Then we can call the script:

./hello
What is your name?
User
Welcome to our journey, User.

We took an input from the user and we interacted with variable. For the hacking tools and some serious scripts, variables and inputs are really important. These concepts are crucial.

Advanced script for network analysis using nmap

#! /bin/bash

# This script is designed to analyse networks
# You can add the range of IP addresses
# Also you can use custom ports
# For example:
# 3306 is MySQL
# 1433 is Microsoft SQL server

echo "Enter the starting IP address: "
read FirstIP

echo "Enter the last octet of the last IP address : "
read LastOctetIP

echo "Enter the port number you want to scan for : "
read port

nmap -sT $FirstIP-$LastOctetIP -p $port >/dev/null -oG MySQLscan

cat MySQLscan | grep open > MySQLscan2

cat MySQLscan2

Credits

I’m learning using this book:

Linux Basics For Hackers by OCCUPYTHEWEB (MASTER OTP)

10
Subscribe to my newsletter

Read articles from Jonas Satkauskas directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jonas Satkauskas
Jonas Satkauskas