def copyRandomFile(fileNames, destination): # Pick a random file fileNameWithPath = fileNames[randint(0, len(fileNames) - 1)] if platform.system() == "Linux": separator = "/" elif platform.system() == "Windows": separator = "\\" # If the file is copied to the VM, the MAC address (as an integer) is placed before the file names # -> Avoid deadlocks when copying to the server if "localstorage" in destination: localFileName = str( getnode()) + "-" + fileNameWithPath.split(separator)[-1] else: localFileName = fileNameWithPath.split(separator)[-1] # Path to the appropriate drive and filename dstWithFileName = destination + localFileName # Copy file try: copyfile(fileNameWithPath, dstWithFileName) echoC(__name__, "Copied file " + localFileName + " to " + destination) except Exception as e: echoC( __name__, "copyfile() with destination " + destination + " error: " + str(e)) return -1 # copyfile() is not blocking time.sleep(random.randint(15, 120)) return 0
def createActivityList(browsing, mailing, printing, copyfiles, copysea, ssh, meeting, offline, private, breaks, attacking): l = [] for i in range(0, browsing): l.append("browsing") for i in range(0, mailing): l.append("mailing") for i in range(0, printing): l.append("printing") for i in range(0, copyfiles): l.append("copyFiles") for i in range(0, copysea): l.append("copySea") for i in range(0, ssh): l.append("ssh") for i in range(0, meeting): l.append("meeting") for i in range(0, offline): l.append("offline") for i in range(0, private): l.append("private") for i in range(0, breaks): l.append("breaks") for i in range(0, attacking): l.append("attacking") if not l: echoC(__name__, "Nothing to do. Maybe you should check some activities...") sys.exit(0) return l
def takeABreak(): # Determine whether it is time for lunch break, otherwise short coffee break # MinDuration and maxDuration represent the lower and upper limit of the pause duration in minutes breakDuration = 1 if hadLunchToday == False and isTimeForLunch(): minDuration = 15 maxDuration = 60 # Determine a random pause duration in seconds breakDuration = random.randint(minDuration * 60, maxDuration * 60) echoC(__name__, "Heading for lunch (" + str(breakDuration / 60) + " min)") # Set the lunch flag so that lunch is made only once a day global hadLunchToday hadLunchToday = True else: minDuration = 1 maxDuration = 5 # Determine a random pause duration in seconds breakDuration = random.randint(minDuration * 60, maxDuration * 60) echoC(__name__, "I need coffee (" + str(breakDuration / 60) + " min)") # Do break time.sleep(breakDuration) return
def run(self): while True: # Keep browsing until the end of the evening # Since the browsing itself gladly times hang up it is every 5 min restarted while datetime.datetime.now().hour < self.endOfWorkday: # Cancel if there are no more attacks if self.goOn == False: echoC(__name__, "Will not start another browsing process") return echoC(__name__, "Starting browsing process while attacking") self.p = Popen( ["python", "-m", "packages.browsing.browsing", self.mode]) time.sleep(300) # Regulatory exit of the virtual display and Firefox self.p.terminate() time.sleep(3) echoC(__name__, "Stopped browsing to check the clock") echoC(__name__, "End of workday. Taking a break from browsing") # If the next working day still the attack, the browsing is resumed while datetime.datetime.now().hour != self.startOfWorkday: time.sleep(300) echoC(__name__, "Start of workday. Continuing browsing")
def getAndSetSubnetHostAndHostname(parser): # Determine IP s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("google.com", 80)) ip = (s.getsockname()[0]) s.close() echoC(__name__, "My IP is " + ip) # Determine subnet using the IP Address subnet = ip.split('.')[2] # Determine the host part of the IP host = ip.split('.')[3] hostname = socket.gethostname() parser.set("network", "hostname", hostname) parser.set("network", "subnet", subnet) parser.set("network", "host", host) # "flush" with open("packages/system/config.ini", "w") as config: parser.write(config) return subnet, host, hostname
def close_popup(driver, var): try: driver.find_element_by_css_selector(var).click() time.sleep(5) except Exception as e: echoC(myName, "close_popup() error: " + str(e)) return -1 return 0
def restartClient(): if platform.system() == "Linux": os.system("shutdown -r now") if platform.system() == "Windows": # Works with Windows only sometimes (!?) # subprocess.call(["shutdown", "/r", "/f"]) echoC(__name__, "Windows does not reboot properly...") return False
def getIPRange(): try: with open(iprange) as f: content = [x.strip('\n') for x in f.readlines()] except Exception as e: echoC(__name__, "Error reading IP ranges: " + str(e)) content = -1 return content
def createAttackList(): try: with open(attackList) as f: content = [x.strip('\n') for x in f.readlines()] except Exception as e: echoC(__name__, "Error reading attackList: " + str(e)) return -1 return content
def getIPList(): try: with open(ipList) as f: content = [x.strip('\n') for x in f.readlines()] except Exception as e: echoC(__name__, "No IPs scanned yet, so no DoS attack possible") content = -1 return content
def readMails(mailuser, mailuserpw, mailserverIP): # Start SSL-Connection mail = imaplib.IMAP4_SSL(mailserverIP) # Login mail.login(mailuser, mailuserpw) # Selecting a folder - returns the number of mails (as a string) in [1] [0] no_of_msg = mail.select("inbox")[1][0] echoC(__name__, "Messages in inbox: " + no_of_msg) # Read existing e-mails if no_of_msg != '0': # Search all emails in the Inbox folder result, data = mail.uid('search', None, "ALL") # Search for the unique ID of the most recent e-mail latest_email_uid = data[0].split()[-1] # read last E-Mail result, data = mail.uid('fetch', latest_email_uid, "(RFC822)") # Random number of e-mail read (so to say as "new" e-mails not yet available on client) while random.randint(0, 3) != 0: # Read most recent E-Mail result, data = mail.uid('fetch', latest_email_uid, "(RFC822)") raw_email = data[0][1] #Create Email object email_message = email.message_from_string(raw_email) # Iterate over all E-Mails, search and store notes for part in email_message.walk(): if part.get_content_maintype() == 'multipart': continue if part.get('Content-Disposition') is None: continue data = part.get_payload(decode=True) if not data: continue if platform.system() == "Linux": attachment = "/home/debian/localstorage/attachment" else: attachment = "C:\\localstorage\\attachment" with open(attachment, "w") as f: f.write(data) # Output of some information from the last e-mail echoC( __name__, "Latest mail of " + email_message['Subject'] + " from '" + email_message['From'].split('@')[0] + "'") time.sleep(30) # If no e-mail is present, 0 is returned and an e-mail is sent return no_of_msg
def open_url(driver, url): try: # If a URL is not available, an exception is thrown after 60 seconds driver.set_page_load_timeout(60) driver.get(url) except Exception as e: echoC(myName, "open_url() error: " + str(e)) return -1 return 0
def create_driver(): try: if platform.system() == "Linux": # Geckodriver is required because FirefoxDriver is deprecated driver = webdriver.Firefox(executable_path="/opt/geckodriver") elif platform.system() == "Windows": # On Windows, you can work without a Geckodriver because the Windows VMs have an old version of Firefox driver = webdriver.Firefox() return driver except Exception as e: echoC(myName, "create_driver() error: " + str(e)) return None
def isTimeForLunch(): # Determine recent time try: currHour = int(datetime.now().strftime("%H")) except Exception as e: echoC(__name__, str(e)) currHour = 8 # Check whether the current time is in the period of the lunch break if 11 <= currHour < 13: # Determine whether lunch break is to be carried out if random.randint(1, 10) >= 9: return True return False
def click_on_stuff(driver, tagname): # Determine all elements for the tag name links = driver.find_elements_by_tag_name(tagname) # Check whether elements could be determined (empty list) if not links: echoC(myName, "click_on_stuff() error: No links found") return -1 # Filtering all links that are not active (eg generated by JS) try: validLinks = [link for link in links if link.is_displayed()] except Exception as e: echoC(myName, "click_on_stuff() error: is_displayed() exception: " + str(e)) return -1 # Otherwise randomly select an element try: l = validLinks[random.randint(0, len(validLinks) - 1)] except Exception as e: echoC(myName, "click_on_stuff() error: random validLinks: " + str(e)) return -1 # If it is a non-visible element, another element is selected # If no visible element is found 5 times, the method is aborted (cause, eg, connection termination) cntFehler = 0 while True: # Click on the determined element # Intercepting error messages for non-clickable elements time.sleep(5) try: l.click() echoC(myName, "Clicked on stuff") waitfor = random.randint(1, 40) time.sleep(waitfor) return 0 except Exception as e: # 5 Trial tests are tolerated cntFehler = cntFehler + 1 if cntFehler == 5: echoC(myName, "click_on_stuff() error: " + str(e)) return -1 # Determine a new link l = validLinks[random.randint(0, len(validLinks) - 1)]
def main(): # Return value for error detection error = 0 echoC(__name__, "Started mailing script...") # Init parser parser = SafeConfigParser() parser.read('packages/mailing/mail.ini') # Read e-mail user and password mailuser = parser.get('mailconfig', 'user') mailuserpw = parser.get('mailconfig', 'pw') mailserverIP = parser.get('mailconfig', 'smtp') # Connect to SMTP server smtpserver = getConnection(parser, mailuser, mailuserpw) if smtpserver == -1: return -1 # E-Mails check no_of_mails = readMails(mailuser, mailuserpw, mailserverIP) time.sleep(10) # Send multiple e-mails firstTime = True while random.randint(0, 2) != 0 or firstTime == True: firstTime = False # Determine receiver # If there are no e-mails in the inbox, an e-mail is sent to itself (sometimes just as well) if no_of_mails == '0' or random.randint(0, 1) == 1: to = mailuser else: # Identify random recipient to = getRecipient() # Generate E-Mail content msg = createMessage(mailuser, to) # Add note to the E-Mail addAttachments(msg) # Send E-Mail try: time.sleep(10) smtpserver.sendmail(mailuser, to, msg.as_string()) echoC(__name__, "Mail sent to '" + to.split('@')[0] + "'") except Exception as e: echoC(__name__, "sendmail() error: " + str(e)) error = -1 # Close connection to server smtpserver.close() echoC(__name__, "Done") return error
def generatePasswordList(amount): try: # Read big password file allPWs = open(pwSourceList).readlines() # Write passwords to test with open(pwList, "w") as f: for i in range(0, amount): f.write(random.choice(allPWs)) except Exception as e: echoC(__name__, "Error creating pwList: " + str(e)) return -1 return 0
def main(): # Set to -1 if errors occur # Used as return parameter error = 0 # Config file which contains all the IPs of the server parser = SafeConfigParser() parser.read('packages/system/serverconfig.ini') # Randomly select a subnet # Except the first, this is the backup server rand_subnet = random.choice(parser.sections()[1:]) # Randomly select an IP ip = parser.get(rand_subnet, random.choice(ipList)) echoC(__name__, "Opening SSH connection to " + str(ip)) try: # Start SSH connection s = pxssh.pxssh() s.login(ip, user, secret) # Execute several orders firstTime = True while random.randint(0, 4) != 0 or firstTime == True: firstTime = False # Determine the number of possible commands and randomly select one try: nb = file_len(ssh_cmdList) rand = random.randint(1, nb) cmd = linecache.getline(ssh_cmdList, rand) error = 0 except Exception as e: # If the file can not be read, simply specify a command echoC(__name__, "SSH: Error retrieving a command from the given file") cmd = "ls -lah" error = -1 # Send command s.sendline(cmd) s.prompt() echoC(__name__, "SSH output on " + str(ip) + ": " + s.before) # Wait a bit before it goes on time.sleep(random.randint(3, 60)) # Disconnect s.logout() except pxssh.ExceptionPxssh as e: echoC(__name__, "No Host found: " + str(e)) error = -1 return error
def dos(ip, port): # MEssage msg = "928ghs8mc9q84mvvnba5snn7q2t3vcgbcyfhb97nxgh673h" ddos = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: ddos.connect((ip, port)) ddos.send(msg) ddos.sendto(msg, (ip, port)) ddos.send(msg) except Exception as e: echoC(__name__, "Connection failed: " + str(e)) time.sleep(1) return echoC(__name__, "DDoS attack engaged") ddos.close()
def start_search(driver, keywords, var): try: # Determine the number of search terms and choose one randomly nb = file_len(keywords) rand = random.randint(1, nb) search = linecache.getline(keywords, rand) # Determine search field and enter search term elem = driver.find_element_by_name(var) elem.send_keys(search) elem.send_keys(Keys.RETURN) echoC(myName, "Searched: " + search) time.sleep(5) except Exception as e: echoC(myName, "start_search() error: " + str(e)) return -1 return 0
def isWorkingHours(parser): # Read working times from config clockIn = parser.getint("workinghours", "clock_in") clockOut = parser.getint("workinghours", "clock_out") # Determine cureent time try: currHour = int(datetime.now().strftime("%H")) except Exception as e: echoC(__name__, str(e)) currHour = 8 # Determine whether the working time has elapsed if clockIn <= currHour < clockOut: isTimeToWorkNew = 1 else: isTimeToWorkNew = 0 # If a status change takes place, a corresponding output is generated # as well as the global flag global isTimeToWork if isTimeToWork != isTimeToWorkNew: isTimeToWork = isTimeToWorkNew if isTimeToWork == 1: echoC(__name__, "Workday starts") # Status flag for showing whether already lunch break was made already today global hadLunchToday hadLunchToday = False else: echoC(__name__, "Workday ends") if platform.system() == "Linux": # Install updates echoC(__name__, "Updating system") update = Popen( "sudo apt-get update -qy && sudo apt-get upgrade -qy", shell=True) update.wait() # Restart client at the end of each working day echoC(__name__, "Restarting system") restartClient() return isTimeToWork
def getSubnetHostAndHostname(): # Determine IP of VM s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("google.com", 80)) ip = (s.getsockname()[0]) s.close() echoC(__name__, "My IP is " + ip) # Determine Subnet using IP subnet = ip.split('.')[2] # Determine host part of the IP host = ip.split('.')[3] # Determine host name hostname = socket.gethostname() return subnet, host, hostname
def init(browsing, mailing, printing, copyfiles, copysea, ssh, meeting, offline, private, breaks, attacking, t): activities = createActivityList(browsing, mailing, printing, copyfiles, copysea, ssh, meeting, offline, private, breaks, attacking) time.sleep(2) parser = SafeConfigParser() parser.read("packages/system/config.ini") subnet, host, hostname = getSubnetHostAndHostname() global myID if platform.system() == "Linux": myID = str(getnode()) else: # For Windows, something must be trickled, since getnode () returns an incorrect value hexMac = check_output(["getmac"])[162:180] hexMacNoDash = hexMac.replace("-", "") intMac = int(hexMacNoDash, 16) myID = str(intMac) global pathForLog if platform.system() == "Linux": pathForLog = "/home/debian/log/" else: pathForLog = "M:\\" # In endless loop perform different activities try: while True: if isWorkday(parser) and isWorkingHours(parser): doSomething(activities) else: # Should be just the end of his work, he must wait until he has to work again time.sleep(random.randint(1, 15) * 60 - random.randint(1, 55)) except KeyboardInterrupt: echoC(__name__, "SCRIPT STOPPED: KEYBOARDINTERRUPT") sys.exit(0)
def getConnection(parser, mailuser, mailuserpw): # Read server names from Mailconfig smtp_server = parser.get('mailconfig', 'smtp') # Connect to host / port with user and PW try: smtpserver = smtplib.SMTP(smtp_server, 587) smtpserver.ehlo() smtpserver.starttls() smtpserver.ehlo() smtpserver.login(mailuser, mailuserpw) except Exception as e: echoC(__name__, "Error connecting to the STMP-Server") return -1 echoC( __name__, "SMTP-Server: " + str(smtp_server) + " User: " + str(mailuser).split('@')[0]) return smtpserver
def doSomething(activities): activity = activities[random.randint(0, len(activities) - 1)] a_error = 0 a_start = datetime.now() if activity == "browsing": queue = Queue() # For browsing, a thread is started which ends after 10 minutes at the latest RunCmd("b", 600, queue).Run() a_error = queue.get() if activity == "copyFiles": a_error = copyFiles.main() if activity == "copySea": a_error = copySea.main() if activity == "mailing": a_error = mailing.main() if activity == "printing": a_error = printing.main() if activity == "ssh": a_error = sshConnections.main() if activity == "meeting": meetingDuration = random.randint(10 * 60, 120 * 60) echoC(__name__, "Attending a meeting for " + str(meetingDuration / 60) + " mins") time.sleep(meetingDuration) if activity == "offline": offlinew = random.randint(1 * 60, 50 * 60) echoC(__name__, "Doing some offline work for " + str(offlinew / 60) + " min") time.sleep(offlinew) if activity == "private": queue = Queue() # For browsing, a thread is started which ends after 10 minutes at the latest RunCmd("p", 600, queue).Run() a_error = queue.get() if activity == "breaks": takeABreak() if activity == "attacking": a_error = attacking.main() writeLog(a_start, datetime.now(), activity, a_error)
def Run(self): # call run method self.start() # Wait x seconds for thread timeout self.join(self.timeout) # If the thread is still active after x seconds, it is terminated if self.is_alive(): # Stop the Firefox driver so that they do not block the RAM as zombies # Only for linux # Update: Does not always work, therefore first kill() self.p.kill() self.queue.put(-1) self.join() echoC(__name__, "Browsing timeout: killed the Thread")
def deleteRandomFile(fileNames): # Select a random file fileNameWithPath = fileNames[randint(0, len(fileNames)-1)] if platform.system() == "Linux": separator = "/" elif platform.system() == "Windows": separator = "\\" # Copy file try: os.remove(fileNameWithPath) echoC(__name__, "File " + fileNameWithPath + " removed") except Exception as e: echoC(__name__, "File " + fileNameWithPath + " removed error: " + str(e)) return -1 # copyfile() is not blocking time.sleep(random.randint(15, 120)) return 0
def main(): echoC(__name__, "Starting a scan") # Determine subnets ipRangeList = getIPRange() if ipRangeList == -1: return -1 # Select a random subnet rand = random.randint(0, len(ipRangeList)-1) ipRange = ipRangeList[rand] # Define arguments scanOptions = ["-sF", "-sA", "-sU", "-sS", "-n -sP -PE"] myArguments = random.choice(scanOptions) + " -T " + str(random.randint(1, 3)) echoC(__name__, "Scanning " + str(ipRange) + " with arguments: " + myArguments) # Execute Scan nm = nmap.PortScanner() nm.scan(hosts=ipRangeList[rand], arguments=myArguments) # Store the found IPs # At first, delete old IPs open(ipList, 'w').close() for i in nm.all_hosts(): with open(ipList, 'a') as myfile: myfile.write(str(i) + '\n') echoC(__name__, "Done") returnval = "0,nmap args: " + myArguments return returnval
def isWorkday(parser): # Read all weekdays from config (list with tuples) workdays = parser.items("workdays") # Current weekday as int (0 = Monday, ...) currWeekday = date.today().weekday() # Determine whether it is a working day if workdays[currWeekday][1] == "1": isDayToWorkNew = 1 else: isDayToWorkNew = 0 # If a status change takes place, a corresponding output is generated # as well as the global flag global isDayToWork if isDayToWork != isDayToWorkNew: isDayToWork = isDayToWorkNew if isDayToWork == 1: echoC(__name__, "Today is a workday (" + workdays[currWeekday][0] + ")") echoC( __name__, "Working hours: " + parser.get("workinghours", "clock_in") + " - " + parser.get("workinghours", "clock_out") + " h") else: echoC(__name__, "Today is not a workday (" + workdays[currWeekday][0] + ")") return isDayToWork
def addAttachments(msg): attachments = [] lines = file_len("packages/mailing/attachments.txt") nb = random.randint(0, lines) for i in range(0, nb - 1): rand = random.randint(1, lines) att = linecache.getline("packages/mailing/attachments.txt", rand).replace("\n", "") attachments.append(att) if nb > 0: for f in attachments or []: with open(f, "rb") as fil: part = MIMEBase('application', "octet-stream") part.set_payload(fil.read()) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % f) msg.attach(part) echoC(__name__, "Attachments attached") else: echoC(__name__, "No attachments") return msg