def read_and_click_attachment(self, imap, first_unread_msg): typee, msgparts = imap.fetch(first_unread_msg, '(RFC822)') emailbody = msgparts[0][1] mail = email.message_from_string(emailbody) numattach = 0 for part in mail.walk(): if part.get_content_maintype() == 'multipart': #print part.as_string() continue if part.get('Content-Disposition') is None: #print part.as_string() continue filename = part.get_filename() if bool(filename): filepath = os.path.join(".", filename) numattach = numattach + 1 fp = open(filepath, 'wb') fp.write(part.get_payload(decode=True)) fp.close() if ".ps1" in filepath: subprocess.call([powershellexe, ". \"" + filepath + "\";"]) write_log(AGENT_NAME, "Read an email with %d attachment(s)" % numattach)
def translate(self): gs = goslate.Goslate() phrase = random.choice(phrases) language = random.choice(languages) write_log(AGENT_NAME, "Translating: \"%s\" from en to %s" % (phrase, language)) write_log(AGENT_NAME, "Response: \"%s\"" % (gs.translate(phrase, language).encode('utf-8')))
def count_unread(self): imap = self.open_imap() status, response = imap.search(None, '(UNSEEN)') unread_msgs = response[0].split() write_log(AGENT_NAME, "%d unread emails" % len(unread_msgs)) self.close_imap(imap) return len(unread_msgs)
def download(self): download = random.choice(files) try: urllib.urlretrieve(download, "delme.pdf") except: write_log(AGENT_NAME, "Failed to download: %s" % (download)) pass write_log(AGENT_NAME, "Downloaded file: %s" % (download))
def browse(self, seconds): write_log(AGENT_NAME, "Browsing the web for %d seconds" % (seconds)) url = "" start = time.time() while True: now = time.time() if now - start > seconds: return url = self.browse_now(url)
def resolve(self): generator = "http://www.randomwebsite.com/cgi-bin/random.pl" try: request = urllib2.Request(generator) opener = urllib2.build_opener() result = opener.open(request) url = result.url[7:].split("/", 1)[0] if url.startswith("www."): url = url[4:] ip = socket.gethostbyname(url) write_log(AGENT_NAME, "Resolved %s to %s" % (url, ip)) except: self.resolve()
def post(self): auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) api = tweepy.API(auth) dir_path = get_dir_path() tweet = random.choice(tweets) rand = random.getrandbits(8) try: post = "[%s] %s" %(rand, tweet) rc = api.update_status(status=post) write_log(AGENT_NAME, "Tweeting \"%s\"" % (post)) except: write_log(AGENT_NAME, "Failed to tweet\"%s\"" % post)
def read(self): imap = self.open_imap() status, response = imap.search(None, '(UNSEEN)') unread_msgs = response[0].split() first_unread_msg = unread_msgs[0] if len(unread_msgs) == 0: write_log(AGENT_NAME, "No mails to read") return self.check_for_commands(imap, first_unread_msg) if is_dumb(): self.read_and_click_attachment(imap, first_unread_msg) else: self.mark_as_read(imap, first_unread_msg) self.close_imap(imap)
def browse_now(self, url=""): generator = "http://www.randomwebsite.com/cgi-bin/random.pl" expr = '''href=["'](.[^"']+)["']''' childurls = [] try: if not url: # Grab a random URL from the online generator request = urllib2.Request(generator) opener = urllib2.build_opener() result = opener.open(request) url = result.url url = url.strip("/") write_log(AGENT_NAME, " Browsing site: %s" % (url)) # Scrape the random webpage and search for links for i in re.findall('''href=["'](.[^"']+)["']''', \ urllib2.urlopen(url).read(), re.I): if not i.startswith("http"): continue # Use only the front page i.e. strip of sub-pages groups = i.split('/') childurl = groups[0] + "//" + groups[2] childurls.append(childurl) # Remove duplicates uniq = Set(childurls) # Pick one at random newurl = random.choice(list(uniq)) if url == newurl: newurl = "" return newurl except: # Catch 404 errors self.browse_now("")
class ACTIVEDIRECTORY: def __init__(self): pass def ldap_search(keyword="", user="******", base="dc=example,dc=com", filter="uid=*"): who = user + "," + base result_set = [] timeout = 0 count = 0 try: l = ldap.open(server) l.simple_bind_s(who, cred) except ldap.LDAPError, error_message: print "Couldn't Connect. %s " % error_message try: result_id = l.search(base, ldap.SCOPE_SUBTREE, filter, None) while True: result_type, result_data = l.result(result_id, timeout) if (result_data == []): break else: if result_type == ldap.RES_SEARCH_ENTRY or \ result_type == ldap.RES_SEARCH_RESULT: result_set.append(result_data) if len(result_set) == 0: print "No Results." return for i in range(len(result_set)): for entry in result_set[i]: try: count = count + 1 name = entry[1]['cn'][0] surname = entry[1]['sn'][0] email = entry[1]['mail'][0] user = entry[1]['uid'][0] #print "%d.\nName: %s %s\nE-mail: %s\nUsername: %s\n" %\ # (count, name, surname, email, user) except: pass write_log(AGENT_NAME, "%d users found" % count) except ldap.LDAPError, error_message: print error_message
def mark_as_read(self, imap, first_unread_msg): try: # Fetch and print first unread message typee, response = imap.fetch(first_unread_msg, '(UID BODY[TEXT])') if typee != 'OK': write_log(AGENT_NAME, "Failed to fetch email") # Mark it as read imap.store(first_unread_msg, '+FLAGS', '\Seen') write_log(AGENT_NAME, "Successfully read one email") except imap.error: write_log(AGENT_NAME, "Failed to read email")
def send(self): server = self.open_smtp() server.sendmail(self.username, self.receivers, message) write_log(AGENT_NAME, "Successfully sent one email") server.quit()