Programming

Following Chapter 3 of Georgia Weidman's Penetration Testing book.


Bash Shell Scripting

Command Line Arguments

#!/bin/bash

echo "Dollars0 = $0"
echo "Dollars1 = $1"

Making the Script Executable

chmod a+x scriptname

Ping Scan

#!/bin/bash
if [ "$1" == "" ]
then
echo "Usage: ./pingscript.sh [network]"
echo "example: ./pingscript.sh 192.168.10"
else
for x in `seq 1 254`; do
ping -c 1 $1.$x
done
fi
Note: the back-tick character ` is the key below Esc, near the top left of the keyboard.

Cleaner Ping Scan

#!/bin/bash
if [ "$1" == "" ]
then
echo "Usage: ./pingscript.sh [network]"
echo "example: ./pingscript.sh 192.168.10"
else
for x in `seq 1 254`; do
ping -c 1 $1.$x | grep "64 bytes" | cut -d " " -f 4 | sed 's/.$//'
done
fi


Python Scripting

Port-Scanning

NOTE: the textbook example script has an error: the int() function is required to convert the port number from a string to an integer. The script below is correct.
#!/usr/bin/python
import socket
ip = raw_input("Enter the ip: ")
port = int(raw_input("Enter the port: "))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if s.connect_ex((ip, port)):
	print "Port", port, "is closed"
else:
	print "Port", port, "is open"


Writing and Compiling C Programs

Hello

#include <stdio.h>
int main(int argc, char *argv[])
{
	if (argc < 2)
	{
		printf("%s\n", "Pass your name as an argument");
		return(0);
	}
	else
	{
		printf("Hello %s\n", argv[1]);
		return(0);
	}
}
Compile and run
gcc hello.c -o hello

./hello