April Fools’ Day Python Prank

This post was originally published here

“With great power comes great responsibility.
 Except on April Fools’ Day.”
              - Uncle Ben

Do you know someone who Karma’s been slacking on lately? Looking for a harmless April Fools’ Day prank that allows you to flex your Pythonic muscles? Well look no further. Here’s a simple little prank you can quickly code up in Python, customize for maximum effect, and maybe even learn about a couple useful Python packages along the way.

You will want Python installed to code the prank, but the victim probably won’t need to have Python on their machine. This prank makes use of the wonderfully easy-to-use pyautogui package, which allows you to automate mouse and keyboard actions, as well as make message boxes and take screenshots.

So here’s the idea: the victim gets on their computer (after you’ve had a minute or two with it) and sees a seemingly harmless message that they close, which triggers their cursor to start freaking out (after which, we hope, the victim starts freaking out…at least a little bit).

If you haven’t used pyautogui before, install it by entering the following command in Windows Command Prompt (or the Mac OS X or Linux equivalent):

pip install pyautogui

Looking over some pyautogui examples, you should start to get some ideas of what’s possible. Keep in mind that a fail-safe mechanism is built into pyautogui where if you move the cursor to the top-left corner of the screen (which you can usually do if you are quick with the mouse, even if the cursor’s moving quite fast on its own), an exception will be raised and the program will abort. This can be disabled, but exercise caution if you choose to do so.

Now let’s get to coding. Here’s that starter script I mentioned:

import time
import random
import pyautogui

def move_mouse(how_long_in_seconds):
    start_time = time.time()
    time_elapsed = time.time() - start_time
    xsize, ysize = pyautogui.size()
    while time_elapsed < how_long_in_seconds:
        x, y = random.randrange(xsize), random.randrange(ysize)
        pyautogui.moveTo(x, y, duration=0.2)
        time_elapsed = time.time() - start_time

if __name__ == "__main__":
    pyautogui.alert("Your Java update is now complete")

Once you’ve tailored this to your liking, we’ll want to be able to run your program on your victim’s machine even if Python isn’t installed on that machine. To accomplish this, we’ll use the PyInstaller package, which “freezes” Python programs into stand-alone executables that can then be run on any machine with the same operating system as the one that the program was frozen on. Install PyInstaller if you need to:

pip install pyinstaller

and once that’s finished, while still in your OS’s version of Command Prompt, navigate to the folder where your Python script is stored and enter the following command to initiate the creation of your executable:

pyinstaller your_prank_script.py

This will result in PyInstaller creating 3 things: a file with the same name as your prank script but with a “.spec” file extension, a folder named “build”, and a folder named “dist”. Open the “dist” folder and you will find the folder which constitutes your brand new app (and which should have the same name as your script). Going into that folder, you should see a file named “your_prank_script.exe”, and when you run it you will hopefully see your program execute like a charm without Python ever being needed.

All you need to do now is wait for an opportune moment to copy that first folder in the “dist” folder over to your victim’s machine and then run the executable. Once you run it, take a moment to position everything on their screen to your liking (a command prompt window may pop up which you’ll want to minimize) and then sit back and drink some Yoo-hoo as your prey falls helplessly for your ingenious ploy.

A few ideas on how to improve the above script:

  • Make the cursor movements spell something out, perhaps the victim’s name.
  • Make your program run at startup, so turning the computer off and back on doesn’t make it go away.
  • Incorporate mouse clicks, keyboard actions (hotkeys to change screen orientation?), or more messages.

Good luck, don’t get fired, and let me know how it goes!

Related Posts

You have two jobs Welcome to FictionalSoft! I hope your first week is going well? Great. As you start to find your feet, I want to make sure we’re have a sha...
psutil 5.4.0 with AIX support is out After a long time psutil finally adds support for a brand new exotic platform: AIX! Honestly I am not sure how many AIX Python users are out there (I ...
Gynvael’s Mission 11 (en): Python bytecode reverse-engineering Gynvael Coldwind is a security researcher at Google, who hosts weekly livestreams about security and programming in Polish and English). As part of th...
Leaving HPE For the past two years I have been employed by Hewlett Packard Enterprise to work on the various tools, libraries, and frameworks that make up the ope...