Project 4x: Automating Keypresses in Mac OS X (25 pts. extra)

What You Need for This Project

Purpose

Learn how to control keystrokes sent to a browser with scripting. This is a very powerful technique to automate processes.

Learning about Applescript

Applescript is a very old language, still included in Mac OS X machines, that makes it easy to script keypresses.

In a browser, go to this URL:

http://computers.tutsplus.com/tutorials/the-ultimate-beginners-guide-to-applescript--mac-3436

Read through the tutorial quickly, without memorizing anything or doing the examples. The point is to find out what AppleScript can do, and get a general idea what it's good for. When you are actually writing scripts, you will need to review sections of this tutorial.

This is a skill you should develop--using a new language. There are many languages, but they are all pretty much the same, with variables, loops, etc.

Using the Login Form With a Mouse

In Safari, open this page:

http://attack.samsclass.info/p4xb-form.htm

First, use the mouse to click one radio button in each row, as shown below, and click Submit.

Unless you are very lucky, you will see an "Access Denied!" message. Click the Back button in your browser to return to the login page.

Notice that the buttons you clicked are still selected here. Click the Refresh button to clear them.

Using the Login Form With the Keyboard

In the "Project 4xb: Form (10 pts.)" window, click the Refresh button.

On your keyboard, press the Tab key.

If your Safari is set to the default preferences, the Tab key will highlight the address but not the buttons.

To get to the buttons, press Option+Tab".

In "Row 1", the "A" button is highlighted, as shown below.

On your keyboard, press the Option+Tab key several more times. Watch to see that these items are highlighted, in this order:

Click Refresh and then press these keys, to log in using buttons C and 2: You see the Access Denied message shown below:

Our goal here is to write a script that will try all login combinations, and save the results.

So after each attempt, we need to issue keypresses that will save the source code of the results page, record which login attempt we made, and close the source code window.

We'll use the filename to records the login attempt: "32" (button 3 on first row, button 2 on second row).

To do those actions, press these keys:

Setting the Safari Home Page

With Safari open, from the Menu bar, click Safari, Preferences.

On the General tab, in the "Homepage" field, enter:

http://attack.samsclass.info/p4xb-form.htm

as shown below:

Close the Preferences window.

Now the keypress Shift+Command+H goes Home.

Example Script

Here's the script I used. You may need to adjust the path save files in and the Sleep times for your system.
# Instructions for Use:
# 1. Open Safari, close all pages with Command+W
# 2. Set Safari home page to http://attack.samsclass.info/p4xb-form.htm
# 3. Create a folder to save files in named "p4x" in your home directory and make sure it is empty.
# 4. Run this script 

set m to 1
repeat while m <= 11
	set n to 1
	repeat while n <= 9
		tell application "Safari"
			activate
			tell application "System Events"
				keystroke "h" using {shift down, command down}
				delay 3
				
				keystroke tab using {option down}
				
				set num_arrows to m - 1
				set i to 1
				repeat while i <= num_arrows
					key code 124 -- right-arrow
					set i to i + 1
				end repeat
				
				keystroke space
				keystroke tab using {option down}
				
				set num_arrows to n - 1
				set i to 1
				repeat while i <= num_arrows
					key code 124 -- right-arrow
					set i to i + 1
				end repeat
				keystroke space
				
				delay 0.5
				
				keystroke return
				
				delay 3
				
				keystroke "u" using {option down, command down}
				delay 1
				
				keystroke "s" using {command down}
				delay 0.5
				
				set m_text to m as text
				set n_text to n as text
				set filename to "~/p4x/" & m_text & "-" & n_text & ".htm"
				keystroke filename
				
				delay 0.5
				
				keystroke return
				delay 0.5
				keystroke return
				delay 0.5
				keystroke "w" using {command down}
				delay 0.5
			end tell
		end tell
		set n to n + 1
	end repeat
	set m to m + 1
end repeat
When the script works, you should have a lot of files in a directory with names "1-1.htm", "1-2.htm", etc., as shown below.

Saving a Screen Image

Make sure your screen shows several of the files with numerical names, like 1-1.htm and 1-2.htm.

Press the PrintScrn key in the upper-right portion of the keyboard. That will copy the whole desktop to the clipboard.

YOU MUST SUBMIT A FULL-SCREEN IMAGE TO GET FULL CREDIT!

Open Paint and paste in the image.

Save the image with the filename "Your Name Proj 4xa". Use your real name, not the literal text "Your Name".

Finding the Winning Login

In this case, the "Congratulations!" page is larger than the "No Access" page, so all you need to do is sort the files by Size to find the winning page.

Double-click the largest pages one by one to find the winning page.

Note: When I did it, there was a file with an abnormally large filesizes, caused by my script running too fast and saving an error page instead of the results page. If you don't find a real "Congratulations" page, you may have to slow down the script and run it again. To slow it down, change the Sleep times to larger values.

When you find the winning page, save a full-desktop image as shown below:

Saving a Screen Image

Make sure your screen shows the "Congratulations!" page.

Press the PrintScrn key in the upper-right portion of the keyboard. That will copy the whole desktop to the clipboard.

YOU MUST SUBMIT A FULL-SCREEN IMAGE TO GET FULL CREDIT!

Open Paint and paste in the image.

Save the image with the filename "Your Name Proj 4xb". Use your real name, not the literal text "Your Name".

Challenge (+15 pts.)

Find the winning page for this form:

http://attack.samsclass.info/p4xc-form.htm

Save a full-screen image like this:

Turning in your Project

Email the images to cnit.124@gmail.com with the subject line: Proj 4x from YOUR NAME

Sources

Safari 6 (OS X Mountain Lion): Full keyboard access shortcuts


Last Modified: 4-21-14 4:47 pm