Foiling google.com's pay-per-click advertising program

Alec Jacobson

July 30, 2009

weblog/

For educational purposes only: Here is a sketch for a script that works with Safari and shell to "click" on all the ads you see while surfing the web. These visits to the advertisers pages (after first being rerouted by the pay-per-click counter, in this case google.com) happen behind the scene. So to the advertiser and the host of the ads it seems as though you clicked on the ad you saw, but to you your surfing went undisturbed.
Save the following in a file called clickads.sh:
#!/bin/bash
SOURCE=`cat -`
ADS=`echo $SOURCE | grep -o "/aclk?[^\"]*" | \
sed s/amp\;//g | sed s/^/http:\\\/\\\/google.com/g`
for x in $ADS
do
  `/sw/bin/wget -q -erobots=off -t2 -O - -U "Mozilla/5.0 \
(Macintosh; U; PPC Mac OS X 10_4_11; en) AppleWebKit/530.17 \
(KHTML, like Gecko) Version/4.0 Safari/530.17" "$x" 2>&1 | \
cat > log.clickads`
done
Note: change your absolute path to wget accordingly (find what it is by issuing which wget in a terminal).
clickads.sh will accept the html source of a google search, strip out all of the side bar ads and "click" each one. Remove the log and change your -U User agent accordingly (remember this is not for use and for educational purposes only).
Now, so that the background clicker only "clicks" on ads you actually see, save and run this as an Applescript:
repeat
	try
		tell application "Safari"
			set old_current_source to ""
			set current_source to ""
			repeat until old_current_source is equal to current_source and current_source is not equal to ""
				set old_current_source to source of front document as string
				delay 1.0
				set current_source to source of front document as string
			end repeat
			set current_url to URL of front document as string
		end tell
		if current_url starts with "http://www.google.com" then
			set newFileName to ".behindthescenesadclick.temp"
			set newFile to open for access newFileName with write permission
			set eof of newFile to 0
			write current_source to newFile
			close access newFile
			do shell script "cat /.behindthescenesadclick.temp | bash ~/Bash/clickads.sh"
		else
			delay 1
		end if
	end try
end repeat
The path to the dump temp file ".behindthescenesadclick.temp" has to be in Applescript format not UNIX. And remember to change the path to ~/Bash/clickads.sh to point to your file.
The above code could be easily modified to target pay-per-click hosts other than google.com and there was no reason other than availability that I chose that site here.