Simple Python SMTP Enumeration script
Question: I am trying to write a simple Python SMTP enumeration script, which reads
usernames from a text file (filename supplied as the second argument -
`sys.argv[2]`), and checks them against an SMTP server (hostname or ip
supplied as the first argument - `sys.argv[1]`. I found something that is kind
of close, and tweaked it a bit, like so:

    
    
    #!/usr/bin/python
    
    import socket
    import sys
    
    users = sys.argv[2]
    for line in users:
        line = line.strip()
        if line!='':
            users.append(line)
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    s.connect((sys.argv[1], 25))
    fn = s.makefile('rwb')
    
    fn.readline()                
    fn.write('HELO testing.com \r\n')
    fn.flush()
    fn.readline()
    
    for user in users:
        fn.write('VRFY %s\r\n' % user)
        fn.flush()
        print '%s: %s' % (user, fn.readline().strip())
    
    fn.write('QUIT\r\n')
    fn.flush()
    s.close()
    

However, when I run the script (for example):

    
    
    ./smtp-vrfy.py 192.168.1.9 users.txt 
    

It results in an error:

**File "./smtp-vrfy.py", line 10, in users.append(line) AttributeError: 'str'
object has no attribute 'append'**

What am I doing wrong? How can I fix it? Perhaps there is an easier way to
accomplish what I'm trying to do?

Any help would be greatly appreciated.

Thanks!


Answer: `users` is a file name, but you're not reading it. Instead, see what happens:

    
    
    >>> users = "users.txt"
    >>> for line in users:
    ...     print(line)
    ...
    u
    s
    e
    r
    s
    .
    t
    x
    t
    

You probably want:

    
    
    with open(users) as f:
        for line in f:
            # ...
    

Even better:

    
    
    filename = sys.argv[2]
    with open(filename) as f:
        users = [line.strip() for line in f.readlines() if line]
    

Hope it helps!

