Skip to content

Commit

Permalink
Fix telnet timeout (#439)
Browse files Browse the repository at this point in the history
* Adding verbosity level to prints

* Checking if telnet connection was successful
  • Loading branch information
lucyoa authored May 28, 2018
1 parent a4e0390 commit 121f875
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions routersploit/core/ftp/ftp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def ftp_connect(self, retries=1):
try:
ftp_client.connect(self.target, self.port, timeout=FTP_TIMEOUT)
except (socket.error, socket.timeout):
print_error("Connection error")
print_error("Connection error", verbose=self.verbosity)
except Exception as err:
print_error(err)
print_error(err, verbose=self.verbosity)
else:
return ftp_client

Expand Down
6 changes: 3 additions & 3 deletions routersploit/core/ssh/ssh_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ def ssh_test_connect(self):
return True

except socket.error:
print_error("Connection error")
print_error("Connection error", verbose=self.verbosity)
ssh_client.close()
return False

except Exception as err:
print_error("Err: {}".format(err))
print_error("Err: {}".format(err), verbose=self.verbosity)

ssh_client.close()
return False
Expand Down Expand Up @@ -182,7 +182,7 @@ def writeall(sock):
chan.send(d)

except Exception as err:
print_error("Err: {}".format(err))
print_error("Err: {}".format(err), verbose=self.verbosity)

def ssh_close(self, ssh_client):
if ssh_client:
Expand Down
17 changes: 10 additions & 7 deletions routersploit/core/tcp/tcp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from routersploit.core.exploit.exploit import Exploit
from routersploit.core.exploit.exploit import Protocol
from routersploit.core.exploit.option import OptBool
from routersploit.core.exploit.printer import print_status
from routersploit.core.exploit.printer import print_error
from routersploit.core.exploit.utils import is_ipv4
Expand All @@ -16,13 +17,15 @@ class TCPClient(Exploit):

target_protocol = Protocol.TCP

verbosity = OptBool("true", "Enable verbose output: true/false")

def tcp_create(self):
if is_ipv4(self.target):
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
elif is_ipv6(self.target):
tcp_client = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
else:
print_error("Target address is not valid IPv4 nor IPv6 address")
print_error("Target address is not valid IPv4 nor IPv6 address", verbose=self.verbosity)
return None

tcp_client.settimeout(TCP_SOCKET_TIMEOUT)
Expand All @@ -33,12 +36,12 @@ def tcp_connect(self):
tcp_client = self.tcp_create()
tcp_client.connect((self.target, self.port))

print_status("Connection established")
print_status("Connection established", verbose=self.verbosity)
return tcp_client

except Exception as err:
print_error("Could not connect")
print_error(err)
print_error("Could not connect", verbose=self.verbosity)
print_error(err, verbose=self.verbosity)

return None

Expand All @@ -47,7 +50,7 @@ def tcp_send(self, tcp_client, data):
if type(data) is bytes:
return tcp_client.send(data)
else:
print_error("Data to send is not type of bytes")
print_error("Data to send is not type of bytes", verbose=self.verbosity)

return None

Expand All @@ -67,9 +70,9 @@ def tcp_recv(self, tcp_client, num):

return response
except socket.timeout:
print_error("Socket did timeout")
print_error("Socket did timeout", verbose=self.verbosity)
except socket.error:
print_error("Socket error")
print_error("Socket error", verbose=self.verbosity)

return None

Expand Down
7 changes: 5 additions & 2 deletions routersploit/core/telnet/telnet_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def telnet_login(self, username, password, target=None, port=None, retries=1):
for _ in range(retries):
try:
telnet_client = self.telnet_connect(target=target, port=port)
if not telnet_client:
continue

telnet_client.expect([b"Login: ", b"login: ", b"Username: ", b"username: "], 5)
telnet_client.write(bytes(username, "utf-8") + b"\r\n")
telnet_client.expect([b"Password: ", b"password: "], 5)
Expand All @@ -55,9 +58,9 @@ def telnet_login(self, username, password, target=None, port=None, retries=1):
print_error("Telnet Authentication Failed - Username: '{}' Password: '{}'".format(username, password), verbose=self.verbosity)
break
except EOFError:
print_error("Telnet connection error")
print_error("Telnet connection error", verbose=self.verbosity)
except Exception as err:
print_error(err)
print_error(err, verbose=self.verbosity)

return None

Expand Down
11 changes: 7 additions & 4 deletions routersploit/core/udp/udp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from routersploit.core.exploit.exploit import Exploit
from routersploit.core.exploit.exploit import Protocol
from routersploit.core.exploit.option import OptBool
from routersploit.core.exploit.printer import print_error
from routersploit.core.exploit.utils import is_ipv4
from routersploit.core.exploit.utils import is_ipv6
Expand All @@ -15,13 +16,15 @@ class UDPClient(Exploit):

target_protocol = Protocol.UDP

verbosity = OptBool("true", "Enable verbose output: true/false")

def udp_create(self):
if is_ipv4(self.target):
udp_client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
elif is_ipv6(self.target):
udp_client = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
else:
print_error("Target address is not valid IPv4 nor IPv6 address")
print_error("Target address is not valid IPv4 nor IPv6 address", verbose=self.verbosity)
return None

udp_client.settimeout(UDP_SOCKET_TIMEOUT)
Expand All @@ -34,7 +37,7 @@ def udp_send(self, udp_client, data):
elif type(data) is str:
return udp_client.sendto(bytes(data, "utf-8"), (self.target, self.port))
else:
print_error("Data to send is not type of bytes or string")
print_error("Data to send is not type of bytes or string", verbose=self.verbosity)

return None

Expand All @@ -44,9 +47,9 @@ def udp_recv(self, udp_client, num):
response = udp_client.recv(num)
return str(response, "utf-8")
except socket.timeout:
print_error("Socket did timeout")
print_error("Socket did timeout", verbose=self.verbosity)
except socket.error:
print_error("Socket err")
print_error("Socket err", verbose=self.verbosity)

return None

Expand Down
2 changes: 1 addition & 1 deletion rsf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env python3

from __future__ import print_function
import logging.handlers
import sys
if sys.version_info.major < 3:
print("RouterSploit supports only Python3. Rerun application in Python3 environment.")
exit(0)

import logging.handlers
from routersploit.interpreter import RoutersploitInterpreter

log_handler = logging.handlers.RotatingFileHandler(filename="routersploit.log", maxBytes=500000)
Expand Down

0 comments on commit 121f875

Please sign in to comment.