Knowledge Bank
  • 🔭About Us
    • 👋Welcome to Securescape!
  • 👶Fundamentals
    • 🖥️Operating Systems (WIP)
      • Introduction to Operating Systems
      • Processes and Process Management
        • 📠Processes
        • 🧵Threads
        • 📅Scheduling
      • Memory Management
        • Virtual Memory Management
      • Storage Management
      • I/O Management
    • 🐧Linux (WIP)
      • Introduction to Linux
      • Linux System Management
    • ⚡Programming (WIP)
      • 🔗Assembly
      • 💪C(++/#)
      • 🐍Python
      • 👑Nim
      • 🔨Bash
        • Introduction to Bash Scripting
        • Variables, Loops, and Port Scanner
    • 🚩Networking (WIP)
      • 📶Networking 101
        • 🕸️Networking Basics
        • 🛑Protocols
        • 🧓IPv4
        • 🧒IPv6
      • 🪡Packet Tracer
        • Interface
        • Connections
        • Creating Networks
        • Virtual LANS & Trunks
      • 🕸️Subnetting
      • 🕵️Network Security
    • ✍️Report Writing (WIP)
      • 🔍Research Skills
      • 🏗️Structuring a Report
      • 🗃️Creating Templates
  • 🦂General Security
    • 🗒️Methodologies
      • 🇭🇰OSSTMM
      • 🐝OWASP
        • Web Security Testing Guide
        • Mobile Security Testing Guide
      • 🦅NIST
      • 🥢PTES
      • ⛓️Cyber Killchain
    • 🍔Binary Exploitation
      • ir0nstone's Binary Exploitation Notes
    • 🎩Cheat Sheets
      • Enumeration
        • Network Scan
        • Vulnerability Scan
        • Web Scan
      • Exploitation
        • Page 1
        • Payloads & Shells
      • Post Exploitation
        • Lay of The Land
        • Persistence
        • Data Exfiltration
        • Pivoting
      • Command & Control
      • Disassembly
        • ☢️Radare2
        • 🥜GDB
      • CEH Cheatsheet
  • ⚔️Offensive Security
    • 💡Hardware Exploitation
      • Intro to Hardware
    • 🥷Red Team
      • 🦠Malware Development
        • Crow Malware Development
        • 🪡C# Malware
      • 🏭Offensive Development
        • 🔧Offensive DevOps
          • 🏷️GitLab
            • GitLab Setup
            • Simple Calculator Project
            • Making our CI/CD Pipeline
            • Build Artifacts
          • 🌆TeamCity
            • TeamCity Setup
            • Creating TeamCity Projects
            • Obfuscating Payloads
          • 🍷Jenkins (WIP)
            • Jenkins Setup
            • Creating Pipelines
            • Managing Projects
            • API Interaction
        • 🏗️Infrastructure Development (WIP)
          • 🎮Command & Control Infrastructure
            • 🤖Command & Control Anatomy
              • Command & Control Frameworks
              • Ⓜ️Metasploit Framework
                • Installing Metasploit
                • Metasploit Basics
                • Advanced Features
                • Metasploit Documentation
              • 🐲Mythic Framework
                • Installing Mythic
                • Malleable Command & Control
                • All About Agents
                • Services
                • Mythic Documentation
            • 🚥Traffic Redirection
              • Nginx
              • Amazon Web Services
              • Microsoft Azure
              • Google Cloud Platform
              • Cloudflare Workers
            • 🥷Covert Infrastructure
              • Ensuring Resiliency
              • Traffic Masking
              • Network Rules
          • 🎣Phishing Infrastructure
            • 📧Email Anatomy
            • 🐟Phishing Infrastructure Setup
            • 🚚Payload Delivery
            • 🚩Removing Red Flags
          • 🪄Infrastructure as Code
            • 🏝️Terraform
              • Interacting with Docker
              • Going to the Cloud
              • Hybrid Deployment
            • 🧊Pulumi
            • 🎼Ansible
          • ⚙️Infrastructure Automation
            • 🦴Structuring our Project
            • 🏭Automating Server Setups
            • 🎼Orchestrating our Infrastructure
            • 🔧CI/CD Integration
      • 🏙️Active Directory (WIP)
        • Active Directory Overview
        • Authentication
        • AD Lab
      • Red Team Operations - Joas Santos
  • 🛡️Defensive Security
  • 📻Software Defined Radios
    • ⚠️Disclaimer
    • 📡Baofeng
      • Programming
  • 🧑‍🔬Home Lab
    • 💨Virtualisation
      • 🔸Proxmox
  • 🏁Capture The Flag
    • 🧊HackTheBox
      • 👾Cyber Apocalypse
        • Cyber Apocalypse 2023
    • 🐤TryHackMe
      • 🎄Advent of Code
      • 🚪Rooms
        • 🐥Basic Pentesting
        • 👨‍💻Blog
      • 👟Paths
    • 🏳️Competitions
      • Nahamcon
        • Nahamcon 2023
          • Binary Exploitation
            • Open Sesame
      • 👁️Iris CTF
  • 🦺DRAFTS
    • GS
      • 📱Mobile Application Security
      • 👨‍🔬Reverse Engineering
      • 🌐Web Application Security
      • 📌Information Security
      • 🔒Cryptography
      • 🤫Operational Security
    • DS
      • 🧠Threat Intelligence
        • 🦌ELK Stack
          • 🤸Elasticsearch
          • 🏕️Kibana
          • 🦤SELKS
        • 🚓Yara
      • 🏹Threat Hunting
      • 🧬Malware Analysis
        • Fundamentals
      • 🔬Forensics
        • 📶Network Forensics
          • 🦈Wireshark
          • 🥟TCP Dump
        • 💾Memory Forensics
          • ⚡Volatility
        • 💽Disk Forensics
          • 🐕Autopsy
        • 🪟Windows Forensics
        • 🐧Linux Forensics
      • 🌲Security Operations
        • Intrusion Detection & Prevention
          • 🐛Splunk
            • Splunk Basics
            • Integrating Suricata with Splunk
          • 🐗Suricata
            • Intro to Suricata
          • 🐽Snort
            • Snort Basics
        • Security Information and Event Management (SIEM)
        • Security Orchestration, Automation and Response (SOAR)
    • HL
      • 🖥️Hardware
    • OS
      • 📶Network Exploitation
      • 🌩️Cloud Exploitation
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Fundamentals
  2. Programming (WIP)
  3. Bash

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.

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!

PreviousIntroduction to Bash ScriptingNextNetworking (WIP)

Last updated 2 years ago

Was this helpful?

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

👶
⚡
🔨
this link