Python Classes

Introduction

Classes are the cornerstone of Object Oriented Programming. They are the blueprints used to create objects. And, as the name suggests, all of Object Oriented Programming centers around the use of objects to build programs.

You don’t write objects, not really. They are created, or instantiated, in a program using a class as their basis. So, you design objects by writing classes. That means that the most important part of understanding Object Oriented Programming is understanding what classes are and how they work.

Read more

Python String Basics

Introduction

Strings are called what they are because they are strings of characters. It doesn’t matter if those characters are letters, numbers, symbols or spaces. They are all taken literally and not processed within a string. That’s why strings are sometimes referred to as string literals.

String Basics

If you’ve been following along with the previous guides, you’ve already experimented with some strings. You’ve typed in some, and you’ve printed them back out. What about getting user input when the Python script runs? Python has built in functionality to take in user input and assign it to a variable. Try it out.

user_input = input("Please enter some text: ")
print(user_input)

Read more

Python Constructors

Introduction

By now, you should be familiar with the way basic classes work in Python. If classes were just what you’ve seen, they’d be fairly rigid and not all that useful.

Thankfully, classes are much more than just that. They are designed to be much more adaptable and can take in information to shape the way they look initially. Not every car starts off exactly the same, and neither should classes. After all, how awful would it be if every car was an orange 71′ Ford Pinto? That’s not a good situation.

Writing A Class

Start off by setting up a class like the one in the last guide. This class will evolve over the course of this guide. It will move from being a rigid, photocopy-like, situation to a template that can generate multiple unique objects within the outline of the class.

Write the first line of the class, defining it as a class and naming it. This guide is going to stick with the car analogy from before. Don’t forget to pass your class object so that it extends the base object class.

Read more

Advanced Python Strings

Introduction

In the previous guide, you learned the basics of handling strings in Python. In this guide, you will explore some of the more complex things that strings are capable of. There are tools built into Python, called string methods, that help you to handle strings and do some very powerful things. Through the use of string methods, you can masterfully manipulate text and use it to its fullest potential without writing a ton of code.

Navigating a String

Strings aren’t words. They aren’t sentences, phrases, and believe it or not, they aren’t even a collection of text. Strings are just a lists of characters. Those characters can be letter, numbers, symbols, spaces, and escape characters. Python sees strings by their parts(the characters) and uses those parts to manipulate strings. This is actually true of almost any programming language. So, that means that you can select individual characters out of a string. Try this:

phrase_string = "This phrase is a string!"
print(phrase_string[0])

Read more

Python Comments

Introduction

This guide isn’t about programming. In fact, there is no new code involved at all. However, it does have everything to do with making sure that the code that you write is understandable to both yourself and anyone else that might look at it down the line.

If you’ve looked at any open source projects, you’ve probably noticed notes placed in by the programmers. Those notes are just plain text. The programming language doesn’t compile or interpret them in any way. It just ignores them. It knows that those comments are for humans, not computers.

Read more

Python Lists

Introduction

Lists are a big deal. It really can’t be overemphasized what a big deal they are. Lists are not only used for iterating through data, but they are also a popular data storage and
categorization method used for handling data as a program is running. For anyone who’s programmed in another language, lists are often known as arrays.

Lists may be either very simple or very complex, but they all follow the same rules. Lists may contain different types of data, but be careful when working with it. If you create a list that mixes, say, strings and floats, be sure not to try to call a string method on a list item containing a float.

Remember when the string guides said that strings were just a list of characters. Well, now’s the time to be glad that you paid attention to strings. You can do many of the same things that you did
with strings with lists, and you can do them in the same way. So, some of this is going to feel like a repeat of the string guide, but don’t break focus. There are differences, and there are list
specific methods, so assuming that strings and lists are the exact same this will get you into trouble.

Creating a List

Creating a list is a bit different than the other variables that you’ve learned about so far. You can create a list with absolutely no values in it at all. This is useful for situations where you
don’t exactly know what will be added to the list because the data isn’t in the program yet. There will also be plenty of situations where you won’t be sure how many entries will be in a list, so
again, creating an empty one and adding data later is the right move.

Read more

How to store all shell commands immediately after execution into .bash_history file

When using a bash shell all your entered commands are remembered by history library. The history library will keep track of every command you have entered. This is a default for most if not all Linux systems. However, the commands you enter are first temporarily stored into an internal memory and are written to your ~/.bash_history  only after you properly exit your shell session.

Depending on your shell usage this may cause some undesired results. For example, if your connection to a remote host gets disconnected, your history file will not get updated and thus you will lose all you previously entered commands. Furthermore, while your commands for one session are temporarily stored within system’s internal memory you would not be able to access it from another shell session.

Use the following linux command to force your shell to append every command entered during a current shell session into ~/.bash_history file:

shell 1: $ history -a

Read more

Bash Shellshock Bug Linux system vulnerability test

The Bash “Shellshock” bug is used to spread malware using botnets. To make sure that you can prevent your system against Shellshock exploit make sure that your system is up to date. Once you update your system use below vulnerability test to help you find out whether your system is vulnerable to Bash Shellshock attacks.

Simply open a terminal on your Linux system and execute the following linux command

$ env i='() { :;}; echo Your system is Shellshock vulnerable' bash -c "echo Shellshock Linux system vulnerability test"

If your system is vulnerable to the Bash “Shellshock” bug the above command will produce a following output:

Read more

How to Make a Basic Intrusion Detection System with Bash

Introduction

For most of us WEP encryption has become a joke. WPA is quickly going the same way thanks to many tools such as Aircrack-ng. On top of this, wired networks are no strangers to unwanted guests as well. Anyone serious about security should have a good Intrusion Detection system in their toolbox.

There are already some very good IDS’s (Intrusion Detection Systems) available. Why would anyone want to re-invent the wheel in Bash??? There are a couple of reasons for this. Obviously Bash scripts can be very light weight. Especially compared to some of the GUI programs that are out there. While programs like Etherape suck us in with pretty colors, they require constant monitoring to know when the network has changed. If you are like most of us, you only use the computer for two things, work and play. By using the system bell to alert for new clients online you can leave this script running and not have to have a constant watch. If you do decide you want to inspect what a suspicious client is doing more closely, you can always open up etherape, wireshark, or your tool of choice. But until you have a problem you can play or work on other things.

Another bonus to this program is that it will only show ip addresses on the networks connected to your computer. If you were hosting a busy server or perhaps downloading the latest Linux distro though a torrent client, an IDS may be flooded with connections. Looking for a new malicious client can be like looking for a needle in a hay stack. While this script may seem simple compared to other IDS’s, simplicity can have its perks too.

What you will need

Nmap is required for this script to work. We will not be doing any port scanning. However, to make this script fast we needed something better than a regular ping. Nmap’s -sP parameter will only use a ping scan to check if a clients up. There were some variations in how Nmap outputs information between versions. So far this script has only been tested using Nmap 5.00 (Debian Squeeze) and 5.21 (Debian Sid). You may have luck with other distros and versions of Nmap. However, with all the possibilities I could only support a couple at this time.

Read more

Linux Command Line & Bash Shell Shortcuts

Introduction

Although you may think that you have learned to master Linux command line with bash shell, there are always some new tricks to learn to make your command line skills more efficient. This article will teach you a few more basic tricks on how to make your life with the Linux command line & bash more bearable and even enjoyable.

Bash Command History Expansion

This section will mostly deal with bash shortcuts in combination with three bash history expansion characters “!”, “^” and “#”. Bash Command History Expansion character “!” indicates start of history expansion. The “^” is a substitution character to modify a previously run command. The last optional character is “#”, which denotes the reminder of the line as a comment.

Repeat last command

$ echo Bash Shortcuts
Bash Shortcuts
$ !!
echo Bash Shortcuts
Bash Shortcuts

!! is probably the easiest and most popular bash shortcut, which simply shows and executes your last entered command.

Read more