Pranking Mac Salespeople (and wasting your time)

This is really stupid, but also a good excuse to get to know your terminal.

Most computer stores sell Macs these days. Most salespeople are good at their pitches and bad at computers. Most stores have their Macs running so you can try them out, tempting you to buy one. All Macs have python and netcat installed. I wonder if that can be hoisted into a good old geeky prank?
Spoiler: It can. That is exactly what this post is about. It is also a major waste of time for everyone involved. Please read on. If you’re more into visuals, here’s a youtube clip of me wasting time.

Generating nonsense

Imagine you are a salesperson. You can say your tech buzz words with a buzz and your idea of hacking is strictly pop culture with everything that entails, i.e. not hacking. Your visual idea of being hacked is something along the lines of the raining green text in The Matrix. Suddenly, one of the expensive Macs in your store starts spewing out a garbled mess of text instead of the beautiful desktop you get paid to sell. Panic!

Let’s make that scenario happen, so we can laugh in the corner like the geeks we are. (Yes, I have done this on multiple occasions and yes, I do need to get a life and a proper sense of humour).

With python, we can quickly bash together some code that does a lot. Note that we could do everything in this post without python, but python is easy, so python it is.

Let us start off by generating some text with python.
We want to use all letters a-z, A-Z and the digits 0-9.
We can pull up an ascii table (in the store of course, so you look like hackerman) using the terminal:

man ascii

The characters we want to insert are located from 48-57 (digits), 65-90 (capital letters) and 97-122 (lowercase letters).
We can generate the ranges as lists and append them to each other, then use a list comprehension to turn that into a list of characters. Pythons ranges are not inclusive, so we have to add one to the end of our ranges.

It turns out that the range 32,128 will give us all that, including all the punctuation characters. It’d be nice to have a newline character in there as well, and maybe a tab and a bell to make some random length lines and some random noise every once in a while. We add 7 to our range for the bell, 9 for the tab and 10 for the newline.
Cleaning up the character generation, we now have:

in python2, the range function produces a list, and we can append two lists with list1 + list2. The append of the two lists is evaluated before the list comprehension. Python is pretty forgiving with its operations.

We could of course achieve the same list of characters by typing them in, but this is a blog about stupid problems and programming. The hard way is the fun way and we’re already doing something really stupid.

Let’s produce a loop that picks a random character and prints it. the print function in python2 automatically appends a newline, which we don’t want. It can be avoided by ending the statement with a comma as print "somestring",, but unfortunately that inserts a space. Python might be forgiving, but it is also very assuming about what you want to do. Lucky for us, the print function is just a wrapper around sys.stdout.write, so we can avoid that mess entirely. The following code produces around a screen of garbled characters.

Cool cool. We can print useless junk. This is why I study computer science.
We need to get the hackerman feeling in there. How about some color?

ANSI Escape Codes Will Color Your Black And White Life

Most basic terminals, including the standard terminal in MacOS, can handle colors through ANSI escape codes.

  • Black : \x1b[30m
  • Red : \x1b[31m
  • Green : \x1b[32m
  • Yellow : \x1b[33m
  • Blue : \x1b[34m
  • Magenta : \x1b[35m
  • Cyan : \x1b[36m
  • White : \x1b[37m

Printing one of these escape codes will not print anything, but change the color of the next character(s) printed. There are many more codes, including ones for moving the cursor around. There, no more excuses for boring CLI applications. Now go color up your .bash_profile.

We add another list to our python program. We can make every other character we print one of the color changing ones.

Pretty funky stuff.

Sock-et Salespeople!

Now we can quickly write some easily memorizable python that prints a colored garbled mess of nonsense. It would be pretty cool to hide our program away.
We can use sockets and netcat for that!

First, we need to open a new terminal, then create a (local) network connection between the two. We will use two sockets, one socket in python to send text from and one socket in netcat to receive our messages.

Opening a listening socket with netcat is easy. nc -l 1337 will open a listener on localhost, port 1337.

Python takes two lines more, and we have to replace our print statement with a line that prints the character in the netcat terminal.

You of course have to open the listener before you can run your program that sends stuff to the listener. If you did everything I did, you should have two terminals, one of which will print a lot of colored characters and ring the bell like crazy.

Now all that’s left to do is
go and overwrite the normal keyboard shortcuts for closing the terminal and fullscreen the netcat listener.

This is a bad prank, but at least you now know about ascii, ansi escape codes, opening sockets in python, using netcat as a listener, generating strings through list comprehensions and you have a starting point for a horrible text-based chat program.

Leave a Reply