Intro to bash scripting
Lee Baird
Lee works works as a malware analyst in the DC area. He has performed enterprise security assessments for Fortune 500 companies. He holds a bachelor's degree in accounting from Marshall University and is a Offensive Security Certified Professional.
Notes:
Bash 101 Course
When performing security assessments, it is very import that you have a repeatable process. As you become familiar with more tools, you will want to incorporate them into your process. Bash scripting is an easy way in which this can be accomplished. Learn how to read, edit and parse critical information that you need from the output of various tools. Take user's input and leverage multiple tools to perform OSINT. Starting with the very basics and taking you through functions and nested arrays, you will learn to build out your own pentest framework that works for you.
A very import thing to remember when scripting, is that there are many ways to solve a particular problem. There is no right way. Whatever works for you, is the best way.
Everything you do from the command line can be scripted.
Many of the examples in this class are the same types of commands, that when linked together in a script, can create very useful programs.
My editors of choice:CLI - nanoGUI - gedit
Create a text file:touch file Creates an empty file.pipe the output of a commandifconfig > tmpnano file
nanoctrl v Next page.ctrl y Previous page.ctrl w Where is (find).ctrl k Cut that line of test.ctrl x Exit editor.
Create a file and append text to it:ifconfig > tmpecho >> tmpping google.com -c3 >> tmp
How to view a file:cat file Show entire contents of file.more file Show one page at a time. Space bar for next page and (q) to exit.head file Show the first 10 lines.head -15 file Show the first 15 lines.tail file Show the last 10 lines.tail -15 file Show the last 15 lines.tail -f file Useful when viewing the output of a log file.
pipeFeeds the output of one process to the input of another process.cat tmp | grep Bcast
Processesps aux Show all running process for all users.kill -9 PID Nicely kill a PID.
Exampletab 1 ping google.comtab 2 ps aux | grep pingkill -9 (ping PID)tab 1 Verify that ping has stopped.
Exampletab 1 ping google.com > tmp2tab 2 tail -f tmp2tab 1 ctrl z (pauses the process), bg to background the process.tab 2 You can still see the log growing.tab 1 fg to foreground the process.ctrl c to kill the process.tab 2 Verify the log file has stopped growing.ctrl c to kill the process.
Count the number of lines in a file:wc -l tmp2
cut-d delimiter-f fields
sortsort -u file Sort by unique.
ProblemIsolate the IP addresscat tmp2 | cut -d '(' -f2 | cut -d ')' -f1 | sort -u
awkawk '{print $1}' file Show the 1st column.awk '{print $1,$5}' file Show the 1st and 5th columns.ProblemShow the 5 shortest and longest response times to a domain.grepgrep -v Remove a single string. grep -v 'red' fileegrep -v Remove multiple strings. egrep -v '(red|white|blue)' file
sedsed 's/FOO/BAR/g' file Replace FOO with BAR.sed 's/FOO//g' file Replace FOO with nothing.sed '/^FOO/d' file Remove lines that start with FOO.
color31=red 32=green 33=yellow 34=blue 35=magenta 36=cyanecho -e "\e[1;34mThis is a blue text.\e[0m"
Your first bash script.#!/bin/bashclearechoechoprint "Hello world."
Make a file executable.chmod +x filechmod 755 file
Variablesname=Leeecho $name
user=$(whoami)echo $user
echo 'Hello' $name. 'You are running as' $user.
ProblemHello <your name>. Your IP address is <your IP>.
ProblemName <your name>IP <your IP>Interface <your interface>User Inputread -p "Domain: " domain
Check For No User Inputif [ -z $domain ]; thenechoecho "#########################"echoecho "Invalid choice."echoexitfi
To see more of what you can do with bash scripting, please visit my collection of bash scripts and pentesting framework:
https://github.com/leebaird/backtrack-scripts
If you would like to republish one of the articles from this site on your webpage or print journal please contact IronGeek.
Copyright 2020, IronGeek