Scripting 101 - Hello World!

Scripting 101 - Part 1

Prerequisites

  1. Basic Computer Operator Capability
  2. Python Installation Completed

Introduction

Scripting is the concept of using a computer language to automate a series of commands. Typically, these commands are simple and can be executed one-by-one manually; however, automation offers many benefits. Typically, scripts are able to be ran without having the code be compiled first. Note, we will not cover compiled code in this guide.

A script's scope can be either very simple to complex. Scripts can even be part of a bigger project. The project can be a complex compiled solution; which, then runs scripts similar to how a user would execute the scripts manually.

There are many pros & cons with the use of scripting:

Pros Cons
  • Easy to Learn & Use
  • Minimum Programming Knowledge or Experience Required
  • Time Efficiency for Menial Tasks
  • Less Error Prone & Aids in Error Avoidance
  • Executable Code can Inadvertently Be Remotely Uploaded & Executed
  • Poor Testing Can Quickly Lead to Devasting Effects

Before worrying too much about complexity, we're going to start off with simple concepts. Start by defining common used coding terminology, then move into conceptualization, and then finally making that concept into reality. By the end, you should be able to write simple scripts on your own.

Scripting Languages

Scripts can be written in many different languages. Each comes with their own pros & cons. Some of the most widely used scripting languages include:

  • Perl
  • PowerShell
  • Python
  • Bash
  • AWK
  • Javascript

In this tutorial we will cover how to take a concept for a script & turn it into reality using a few of these scripting examples.

Definitions

There are many common terms used within the coding/scripting world. This post will cover a few of the basic & commonly used terms.

Terminology Definition
  • Algorythm
  • A process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.
  • Class
  • With object-oriented programming, this term refers to a set of related objects that share common characteristics.
  • Back End
  • Refers to the code which controls the logical core computational logic of a website, software or information system. It is akin to "server-side" code. Though, back-end code can also refer to the connection between a data-base & the program.
  • Front End
  • Refers to the code which controls what the user sees.It is akin to the term "client-side" development.
  • Function
  • A function is a group of instructions, also known as a named procedure, used by programming languages to return a single result or a set of results.
  • Method
  • A function that is a member of a class/object (in technical terms, it is bound to that class/object). The method has access to the variables and properties defined in the class/object and can perform tasks on any instances of the class/object.
  • Variable
  • A location capable of storing temporary data within a program. This data can then be modified, stored, or displayed whenever needed.

Hello World!

Time to dive into the tried and true method of learning how to code; or, commonly known as the "Hello World!" solution. The idea of the "Hello World!" solution is introduce a user to the syntax & the basic steps to getting output with a selected language. It has been used time and time again to introduce the essentials to a beginning user.

This guide will include examples using two scripting languages, Python & Bash, to perform the same output.

NOTE: The Linux environment used in this guide is: Linux kali 4.15.0.

NOTE: The Windows environment used in this guide is: Microsoft Windows 10 Home

Bash

Bash is a command processor that typically runs in a text window where the user types commands that cause actions. Bash can also read and execute commands from a file, called a shell script. Like all Unix shells, it supports filename globbing (wildcard matching), piping, here documents, command substitution, variables, and control structures for condition-testing and iteration. The keywords, syntax and other basic features of the language are all copied from sh.

Linux Example

Step 1

Start by creating a new file in a desired location. Name the file "helloworld.sh", make sure that the ".sh" file extension is present.

Command Line Screen Shots
  • root@kali:[folder_path]# touch helloworld.sh

Step 2

Using your prefered method, edit the file. In this Linux example, I use the GNU Nano text editor.

Command Line Screen Shots
  • root@kali:[folder_path]# nano helloworld.sh
  • Within the file
  • #!/bin/bash
  • echo "Hello World!"
  • Within the file

Step 3

Finally, execute the script VIA command line.

Command Line Screen Shots
  • root@kali:[folder_path]# /bin/bash helloworld.sh

Windows Example

NOTE: The following steps require that you have Windows Subsystem for Linux installed. This guide will not cover how up install this service.

Step 1

Start by creating a new file in a desired location. Name the file "helloworld.sh", make sure that the ".sh" file extension is present.

Command Line Screen Shots
  • root@windows:[folder_path]# touch helloworld.sh

Step 2

Using your prefered method, edit the file. In this Windows example, I use the GNU Nano text editor.

Command Line Screen Shots
  • root@windows:[folder_path]# nano helloworld.sh
  • Within the file
  • #!/bin/bash
  • echo "Hello World!"
  • Within the file

Step 3

Finally, execute the script VIA command line.

Command Line Screen Shots
  • root@windows:[folder_path]# /bin/bash helloworld.sh

Python

Python is an open-source high-level programming language for general-purpose programming. Python supports multiple programming paradigms, including: object-oriented, imperative, functional & procdural. Additionally, there are many code libraries available.

Linux Example

Step 1

Start by created a new file in a desired location. Name the file "helloworld.py", make sure that the ".py" file extension is present.

Command Line Screen Shots
  • root@kali:[folder_path]# touch helloworld.py

Step 2

Using your prefered method, edit the file. In this Linux example, I use the GNU Nano text editor.

Command Line Screen Shots
  • root@kali:[folder_path]# nano helloworld.py
  • Within the file
  • #!/usr/bin/python
  • print("Hello World!")
  • Within the file

Step 3

Finally, execute the script VIA command line.

Command Line Screen Shots
  • root@windows:[folder_path]# /usr/bin/python helloworld.py

Windows Example

Step 1

Start by created a new file in a desired location. Name the file "helloworld.py", make sure that the ".py" file extension is present.

Command Line Screen Shots
  • C:\[folderPath]>type NUL > helloworld.py

Step 2

Using your prefered method, edit the file. In this Windows example, I echo the command input to the file.

Command Line Screen Shots
  • C:\[folderPath]>echo #! python > helloworld.py
  • C:\[folderPath]>echo print("Hello World!") >> helloworld.py
  • Display the contents of the file
  • C:\[folderPath]>type helloworld.py
  • Display the contents of the file

Step 3

Finally, execute the script VIA command line.

Command Line Screen Shots
  • C:\[folderPath]>python helloworld.py

Summary

As this guide comes to a close we'll cover what was taught. We covered how to create a script file, put correct syntax into the file, and execute the script. While these steps are rather simple, the basics covered are critical in any developer's world. Understanding how to use the command line to create & execute a script is very critical knowledge in a Penetration Tester's world.

Up Next

Up next is Scripting 101 - Variables. This topic covers the how to assign variables data & why this is important. It will discuss how to manipulate the data, pass variables as arguments to functions, and how variables can be used to keep clean read-able code.

Comments