A spectre is haunting Twitter — the spectre of bots.

As well as doing some online learning, I want to keep myself busy with little practical projects that have some kind of purpose, even if they aren’t useful. My mischievous side liked the idea of making a twitter bot and after a little bit of googling I found that this was something that wasn’t too hard to achieve, even for a novice like myself.

I used this tutorial which is incredibly thorough. The first few steps are fairly obvious, make a twitter account and because the tutorial uses the python language, install that too. The next step is to give developer access to your account, to do this you make a new twitter app. Then you obtain the keys and access tokens for that app and save them in some way because we will need them later. These are what allows your script to log in remotely to the account and post to it’s heart’s content.

After this, we need to download and install a python wrapper for the Twitter API, this allows python to access the Twitter API and execute the script we are about to write. This tutorial recommends using Python Twitter Tools by @sixohsix. After running a few setup programs its almost time to start writing the script. Before that, we need to install the MarkovBot library as this is the basis for the Twitter bot we are building. The libraries in Python are similar to those in C, in that they contain pre-made routines. After the Markov library has been installed the script needs feeding a lot of data so it can create a Markov chain N.B. I don’t really understand what a Markov chain is, something to do with the previous state of something determining the probability of future events, probability mathematics not being one of my strong points.

To get a lot of data into the bot, you tell it to read a book or two, literally. I chose Karl Marx’s and Friedrich Engel’s ‘The Communist Manifesto’. Once you have a plain text version of your book in the folder that contains all the bits for the bot we can finally start writing the script.

The script isn’t too complicated and Python seems to be a fairly easy language to get to grips with and not too dissimilar to C. In fact it seems at first glance to be a bit easier to write with than C, I’m not seeing many semi-colons or curly braces. I wont go into the specifics of the script because it is explained so well in the tutorial. I’m also going to upload the code to my GitHub so I can show the options I chose and the subtle changes I made.

The first problem I faced with the code is that the script would simply terminate straight away and the bot wouldn’t actually tweet anything. After searching through the comments on the tutorial I found that you have to use the sleep command to make the bot run in the background. This can be seen in lines 31 and 32 of my code.

The next problem I faced wasn’t really to do with the code, it was to do with Twitter. Unfortunately I was a little bit over zealous with the bot and because its keyword was so general, it was sending out far too many tweets, Twitter considered it spam and so took away the app’s write permission.


“Behind the scenes at Twitter HQ”

I suppose this is good in a way because it stops people from turning your account into a spambot but in this instance, I WANT TO BE A BOT! To hopefully remedy this I made a simple change to my code. Instead of searching for the target string ‘Communism’, the bot now looks for ‘#KarlMarx’. This hashtag turns up in twitter far less than the word ‘Communism’ so I should have very little risk of the bot spamming the heck out of people.

At the time of writing the new version of the bot has been live for about 20 minutes and so far has found two tweets that contained the new target string. Hopefully twitter won’t kill it off this time. At some point in the future I will probably end up getting a Raspberry Pi or similar and run the bot from that so I can have it running 24/7 and so I can actually turn off my laptop.


To ‘C’ or not to ‘C’?

This post is going to be all about the week 1/pset1 of the CS50 course.

Week 1 follows on from week 0 by building on the principles taught with Scratch, however it now moves these principles into the text based programming language ‘C’.

The lecture goes over basic commands and syntax as well as implementing loops. The tasks posed for this week are:

  • Say hello to the world with hello.c.
  • Calculate your water consumption with water.c.
  • Recreate one of Mario’s pyramids, in mario.c.
  • Provide a user with either cash or credit in greedy.c or credit.c.


Hello.c is a simple program that prints the phrase “Hello, world!”. The code is given in the instructions and this task serves to get the user used to using the CS50 IDE interface and the syntax highlighting. It also introduces the user to the clang compiler.


Water.c asks the user to create a program that will work out the equivalent bottles of water used when showering based on how long the shower takes. It introduces simple maths into a program and works along the lines of n * 12 to determine the number of bottles.

The program prompts the user for an input and then uses a do while loop to do the calculation only when the user inputs a valid integer.


This task instructs the user to create a program that will take an integer from the user and then print a right angled pyramid on the screen that is as many lines tall as the user specifies. The pyramid is made from ‘#’ and ‘ ‘, and the top level has two ‘#’.

Once the user has entered a valid value (between 0 and 23) the main bulk of this code is contained within 3 for loops. These loops simply take it in turns to print the spaces and then print the #’s. The program uses simple formulas to determine the number of characters to print based on initial height entered. The code for this project can be seen here.


The final task in this week’s problem set requires the user to figure out the smallest amount of coins that can be given to make up a customers change, the coins you have to choose from are $0.25, $0.10, $0.05 and $0.01.

This challenge introduces someone of the problems that are inherent in the way that a computer counts and displays numbers, namely floating point imprecision. Because a number is represented by a series of 1’s and 0’s there is a finite amount of combinations of those that are used to represent an infinite amount of numbers. The example given in the lecture is that the the sum 1 / 10 displayed to a large number of decimal places should just be represented as 0.1000000000… but it is actually shown as 0.100000000000000000555111512312578… .

Because we are working in cents it is a good idea to multiply by 100 to move from decimal values to integer values we can then use the round command to round the number to the nearest integer. Once this calculation has been completed we can use a series of if loops inside a while loop to determine whether the change can be divided into each of the respective coins from biggest to smallest, all of this is going on inside a while loop which ensures that script runs whilst a valid integer is provided.

For example, if the value of $0.44 is given then the script:

  1. Multiplies by 100 = 44
  2. Can 44 be divided by 25? Yes, once
  3. Can the remainder be divided by 10? Yes, once
  4. Can the remainder be divided by 5? Yes, once
  5. Can the remainder be divided by 1? Yes, four times
  6. Print the amount of coins = 7

That explanation is just a rough approximation, in reality the script asks if the number can be divided by 25, if so it subtracts 25 from the number and adds one to the coin count, this then repeats, if it cannot be divided by 25 it moves on to the next denomination and so on.

The code for this script can be seen here.

This week was a lot of fun and it felt really good to start getting to grips with a programming language, I’ve heard good and bad things about ‘Learn C the Hard Way’ in so much as it’s a bit of a masochistic thing to undertake but it’s incredibly thorough. I think I will stick to the basics for now and see what happens in the future…

Bandit Level 1 -> Level 3

Level 1 -> Level 2

Level 1 -> Level 2 is much the same as Level 0 -> Level 1 but this time the file containing the password is simply named ‘-‘, if you try to cat the file now nothing happens…

Screen Shot 2017-04-21 at 11.38.23

This is because ‘-‘ is often used to refer to stdin, so cat – would simply display the standard input (in this case the keyboard). This is shown when text is entered into the command line and enter is pressed, whatever text was inputted is simply repeated back to you.

The way around this is to specify that ‘-‘ is actually a file and the easiest way to do this is to give it a path with the prefix ./ . Now if cat ./- is entered the password will be revealed

Screen Shot 2017-04-21 at 11.39.00

Level 2 -> Level 3

This level presents a problem as the file in which the password is stored is named ‘spaces in this filename’, if we were to simply enter cat spaces in this filename, then the system would try to output the files ‘spaces’, ‘in’, ‘this’ and ‘filename’ separately.

Screen Shot 2017-04-21 at 13.20.08

We can tell the system that we want it to see the text as one string by adding quotation marks around the filename. By entering cat “spaces in this filename” we obtain the password and can move on to the next level.

Screen Shot 2017-04-21 at 13.21.07

Bandit Level 0 -> Level 1

The first level of Bandit, Level 0, is incredibly simple. You use the ssh command to connect to the game server. ssh stands for ‘secure shell’ and allows a user to connect securely to an ssh server. The website gives you the username and password required to access the server. Do not get used to that…

Level 0 -> Level 1 introduces some simple commands; ls and cat. ls lists the files in your current directory and cat outputs the contents of a file. The ls command can be modified by adding an option as seen below.

Screen Shot 2017-04-21 at 10.26.40

ls is the most basic, ls -a includes entries that start with ‘.’ and ls -l uses a longer listing format. These options can be used in conjunction e.g. ls -al.

As mentioned above, cat simply outputs the contents of a file, we can see that the file readme contained the password for the next level.

Screen Shot 2017-04-21 at 10.32.16

The next step is to copy the password to the clipboard and then connect via ssh to the next level.

When I first went through these levels I ran into a problem at this point, after getting the password I tried to use ssh bandit1@labs.bandit.overthewire.org. This threw up an error stating that I couldn’t connect. After some digging and conversation in the Over The Wire IRC channel I was told that the machine that the game is hosted on doesn’t make outgoing connections for security reasons. So because I was already connected to the machine, it wouldn’t connect. What I had to do was to use the exit command to log out of the current ssh session and then log back in using the usual command. I also found that instead of logging out I could simply use ssh bandit1@localhost to connect because all the levels are hosted on the same machine and I technically wasn’t making an outbound connection.

For nearly all commands there is a manual page that can be accessed by adding man in front of the command e.g. man ls, man cat. The manual shows you the correct syntax for commands and gives a list of options that can be used and the effects that they have.

Over The Wire – Bandit

In my quest for computing knowledge I stumbled across a recommendation, probably on /r/howtohack, to check out Over the Wire’s Wargames. Wargames being a set of challenges that require you to complete various computing and security tasks to then progress to the next level.

The particular one I started with is Bandit and is completed entirely in the Unix command line using commands that you are provided with and some that you aren’t. The site provides some resources for reading about the different commands but it also suggests using Google to its full potential and I have to echo (haha) that suggestion.

I’ll be making a series of posts about either a group of levels, if they are easy enough to explain a few at a time, or a single level, if it takes a bit more effort.