Unverified Commit dfae76c1 by Marcin Bury Committed by GitHub

Fixing options, adding encoder option (#515)

parent e6864eb0
...@@ -15,28 +15,22 @@ class Option(object): ...@@ -15,28 +15,22 @@ class Option(object):
self.label = None self.label = None
self.description = description self.description = description
self.default = default if default:
self.display_value = default self.__set__("", default)
self.value = default else:
self.display_value = ""
self.value = ""
def __get__(self, instance, owner): def __get__(self, instance, owner):
return self.value return self.value
def __set__(self, instance, value):
if self._apply_widget(value):
self.display_value = value
self.value = value
def set_label(self, label):
self.label = label
class OptIP(Option): class OptIP(Option):
""" Option IP attribute """ """ Option IP attribute """
def _apply_widget(self, value): def __set__(self, instance, value):
if is_ipv4(value) or is_ipv6(value): if not value or is_ipv4(value) or is_ipv6(value):
return value self.value = self.display_value = value
else: else:
raise OptionValidationError("Invalid address. Provided address is not valid IPv4 or IPv6 address.") raise OptionValidationError("Invalid address. Provided address is not valid IPv4 or IPv6 address.")
...@@ -44,26 +38,23 @@ class OptIP(Option): ...@@ -44,26 +38,23 @@ class OptIP(Option):
class OptPort(Option): class OptPort(Option):
""" Option Port attribute """ """ Option Port attribute """
def _apply_widget(self, value): def __set__(self, instance, value):
try: try:
value = int(value) value = int(value)
if 0 < value <= 65535: # max port number is 65535 if 0 < value <= 65535: # max port number is 65535
return value self.display_value = str(value)
self.value = value
else: else:
raise OptionValidationError("Invalid option. Port value should be between 0 and 65536.") raise OptionValidationError("Invalid option. Port value should be between 0 and 65536.")
except ValueError: except ValueError:
raise OptionValidationError("Invalid option. Cannot cast '{}' to integer.".format(value)) raise OptionValidationError("Invalid option. Cannot cast '{}' to integer.".format(value))
def __get__(self, instance, owner):
return int(self.value)
class OptBool(Option): class OptBool(Option):
""" Option Bool attribute """ """ Option Bool attribute """
def __init__(self, default, description=""): def __init__(self, default, description=""):
self.label = None
self.description = description self.description = description
if default: if default:
...@@ -73,25 +64,24 @@ class OptBool(Option): ...@@ -73,25 +64,24 @@ class OptBool(Option):
self.value = default self.value = default
def _apply_widget(self, value): def __set__(self, instance, value):
if value in ["true", "false"]: if value == "true":
return value self.value = True
self.display_value = value
elif value == "false":
self.value = False
self.display_value = value
else: else:
raise OptionValidationError("Invalid value. It should be true or false.") raise OptionValidationError("Invalid value. It should be true or false.")
def __get__(self, instance, owner):
if self.display_value == "true":
return True
return False
class OptInteger(Option): class OptInteger(Option):
""" Option Integer attribute """ """ Option Integer attribute """
def _apply_widget(self, value): def __set__(self, instance, value):
try: try:
return int(value) self.display_value = str(value)
self.value = int(value)
except ValueError: except ValueError:
raise OptionValidationError("Invalid option. Cannot cast '{}' to integer.".format(value)) raise OptionValidationError("Invalid option. Cannot cast '{}' to integer.".format(value))
...@@ -99,9 +89,10 @@ class OptInteger(Option): ...@@ -99,9 +89,10 @@ class OptInteger(Option):
class OptFloat(Option): class OptFloat(Option):
""" Option Float attribute """ """ Option Float attribute """
def _apply_widget(self, value): def __set__(self, instance, value):
try: try:
return float(value) self.display_value = str(value)
self.value = float(value)
except ValueError: except ValueError:
raise OptionValidationError("Invalid option. Cannot cast '{}' to float.".format(value)) raise OptionValidationError("Invalid option. Cannot cast '{}' to float.".format(value))
...@@ -109,9 +100,9 @@ class OptFloat(Option): ...@@ -109,9 +100,9 @@ class OptFloat(Option):
class OptString(Option): class OptString(Option):
""" Option String attribute """ """ Option String attribute """
def _apply_widget(self, value): def __set__(self, instance, value):
try: try:
return str(value) self.value = self.display_value = str(value)
except ValueError: except ValueError:
raise OptionValidationError("Invalid option. Cannot cast '{}' to string.".format(value)) raise OptionValidationError("Invalid option. Cannot cast '{}' to string.".format(value))
...@@ -119,25 +110,16 @@ class OptString(Option): ...@@ -119,25 +110,16 @@ class OptString(Option):
class OptMAC(Option): class OptMAC(Option):
""" Option MAC attribute """ """ Option MAC attribute """
def _apply_widget(self, value): def __set__(self, instance, value):
regexp = r"^[a-f\d]{1,2}:[a-f\d]{1,2}:[a-f\d]{1,2}:[a-f\d]{1,2}:[a-f\d]{1,2}:[a-f\d]{1,2}$" regexp = r"^[a-f\d]{1,2}:[a-f\d]{1,2}:[a-f\d]{1,2}:[a-f\d]{1,2}:[a-f\d]{1,2}:[a-f\d]{1,2}$"
if re.match(regexp, value.lower()): if re.match(regexp, value.lower()):
return value self.value = self.display_value = value
else: else:
raise OptionValidationError("Invalid option. '{}' is not a valid MAC address".format(value)) raise OptionValidationError("Invalid option. '{}' is not a valid MAC address".format(value))
class OptWordlist(Option): class OptWordlist(Option):
""" Option Wordlist attribtue """ """ Option Wordlist attribute """
def _apply_widget(self, value):
if value.startswith("file://"):
path = value.replace("file://", "")
if not os.path.exists(path):
raise OptionValidationError("File '{}' does not exist.".format(path))
return value
return value
def __get__(self, instance, owner): def __get__(self, instance, owner):
if self.display_value.startswith("file://"): if self.display_value.startswith("file://"):
...@@ -149,4 +131,32 @@ class OptWordlist(Option): ...@@ -149,4 +131,32 @@ class OptWordlist(Option):
return self.display_value.split(",") return self.display_value.split(",")
def __set__(self, instance, value): def __set__(self, instance, value):
self.display_value = value if value.startswith("file://"):
path = value.replace("file://", "")
if not os.path.exists(path):
raise OptionValidationError("File '{}' does not exist.".format(path))
self.value = self.display_value = value
class OptEncoder(Option):
""" Option Encoder attribute """
def __init__(self, default, description=""):
self.description = description
if default:
self.display_value = default
self.value = default
else:
self.display_value = ""
self.value = None
def __set__(self, instance, value):
encoder = instance.get_encoder(value)
if encoder:
self.value = encoder
self.display_value = value
else:
raise OptionValidationError("Encoder not available. Check available encoders with `show encoders`.")
...@@ -345,13 +345,6 @@ class RoutersploitInterpreter(BaseInterpreter): ...@@ -345,13 +345,6 @@ class RoutersploitInterpreter(BaseInterpreter):
def command_set(self, *args, **kwargs): def command_set(self, *args, **kwargs):
key, _, value = args[0].partition(" ") key, _, value = args[0].partition(" ")
if key in self.current_module.options: if key in self.current_module.options:
if key == "encoder":
value = self.current_module.get_encoder(value)
if not value:
print_error("Encoder not available. Check available encoders with `show encoders`.")
return
setattr(self.current_module, key, value) setattr(self.current_module, key, value)
self.current_module.exploit_attributes[key][0] = value self.current_module.exploit_attributes[key][0] = value
......
from routersploit.core.exploit.option import OptString from routersploit.core.exploit.option import OptEncoder
from routersploit.core.exploit.payloads import ( from routersploit.core.exploit.payloads import (
GenericPayload, GenericPayload,
Architectures, Architectures,
...@@ -17,7 +17,7 @@ class Payload(BindTCPPayloadMixin, GenericPayload): ...@@ -17,7 +17,7 @@ class Payload(BindTCPPayloadMixin, GenericPayload):
} }
architecture = Architectures.PERL architecture = Architectures.PERL
encoder = OptString(Encoder(), "Encoder") encoder = OptEncoder(Encoder(), "Encoder")
def generate(self): def generate(self):
return ( return (
......
from routersploit.core.exploit.option import OptString from routersploit.core.exploit.option import OptEncoder
from routersploit.core.exploit.payloads import ( from routersploit.core.exploit.payloads import (
GenericPayload, GenericPayload,
Architectures, Architectures,
...@@ -17,7 +17,7 @@ class Payload(ReverseTCPPayloadMixin, GenericPayload): ...@@ -17,7 +17,7 @@ class Payload(ReverseTCPPayloadMixin, GenericPayload):
} }
architecture = Architectures.PERL architecture = Architectures.PERL
encoder = OptString(Encoder(), "Encoder") encoder = OptEncoder(Encoder(), "Encoder")
def generate(self): def generate(self):
return ( return (
......
from routersploit.core.exploit.option import OptString from routersploit.core.exploit.option import OptEncoder
from routersploit.core.exploit.payloads import ( from routersploit.core.exploit.payloads import (
GenericPayload, GenericPayload,
Architectures, Architectures,
...@@ -18,7 +18,7 @@ class Payload(BindTCPPayloadMixin, GenericPayload): ...@@ -18,7 +18,7 @@ class Payload(BindTCPPayloadMixin, GenericPayload):
} }
architecture = Architectures.PHP architecture = Architectures.PHP
encoder = OptString(Encoder(), "Encoder") encoder = OptEncoder(Encoder(), "Encoder")
def generate(self): def generate(self):
return ( return (
......
from routersploit.core.exploit.option import OptString from routersploit.core.exploit.option import OptEncoder
from routersploit.core.exploit.payloads import ( from routersploit.core.exploit.payloads import (
GenericPayload, GenericPayload,
Architectures, Architectures,
...@@ -17,7 +17,7 @@ class Payload(ReverseTCPPayloadMixin, GenericPayload): ...@@ -17,7 +17,7 @@ class Payload(ReverseTCPPayloadMixin, GenericPayload):
} }
architecture = Architectures.PHP architecture = Architectures.PHP
encoder = OptString(Encoder(), "Encoder") encoder = OptEncoder(Encoder(), "Encoder")
def generate(self): def generate(self):
return ( return (
......
from routersploit.core.exploit.option import OptString from routersploit.core.exploit.option import OptEncoder
from routersploit.core.exploit.payloads import ( from routersploit.core.exploit.payloads import (
GenericPayload, GenericPayload,
Architectures, Architectures,
...@@ -17,7 +17,7 @@ class Payload(BindTCPPayloadMixin, GenericPayload): ...@@ -17,7 +17,7 @@ class Payload(BindTCPPayloadMixin, GenericPayload):
} }
architecture = Architectures.PYTHON architecture = Architectures.PYTHON
encoder = OptString(Encoder(), "Encoder") encoder = OptEncoder(Encoder(), "Encoder")
def generate(self): def generate(self):
return ( return (
......
from routersploit.core.exploit.option import OptString from routersploit.core.exploit.option import OptEncoder
from routersploit.core.exploit.payloads import ( from routersploit.core.exploit.payloads import (
GenericPayload, GenericPayload,
Architectures, Architectures,
...@@ -18,7 +18,7 @@ class Payload(BindTCPPayloadMixin, GenericPayload): ...@@ -18,7 +18,7 @@ class Payload(BindTCPPayloadMixin, GenericPayload):
} }
architecture = Architectures.PYTHON architecture = Architectures.PYTHON
encoder = OptString(Encoder(), "Encoder") encoder = OptEncoder(Encoder(), "Encoder")
def generate(self): def generate(self):
return ( return (
......
from routersploit.core.exploit.option import OptString from routersploit.core.exploit.option import OptEncoder
from routersploit.core.exploit.payloads import ( from routersploit.core.exploit.payloads import (
GenericPayload, GenericPayload,
Architectures, Architectures,
...@@ -17,7 +17,7 @@ class Payload(ReverseTCPPayloadMixin, GenericPayload): ...@@ -17,7 +17,7 @@ class Payload(ReverseTCPPayloadMixin, GenericPayload):
} }
architecture = Architectures.PYTHON architecture = Architectures.PYTHON
encoder = OptString(Encoder(), "Encoder") encoder = OptEncoder(Encoder(), "Encoder")
def generate(self): def generate(self):
return ( return (
......
from routersploit.core.exploit.option import OptString from routersploit.core.exploit.option import OptEncoder
from routersploit.core.exploit.payloads import ( from routersploit.core.exploit.payloads import (
GenericPayload, GenericPayload,
Architectures, Architectures,
...@@ -18,7 +18,7 @@ class Payload(ReverseTCPPayloadMixin, GenericPayload): ...@@ -18,7 +18,7 @@ class Payload(ReverseTCPPayloadMixin, GenericPayload):
} }
architecture = Architectures.PYTHON architecture = Architectures.PYTHON
encoder = OptString(Encoder(), "Encoder") encoder = OptEncoder(Encoder(), "Encoder")
def generate(self): def generate(self):
return ( return (
......
from routersploit.modules.encoders.php.hex import Encoder as PHPHexEncoder
from routersploit.core.exploit.exceptions import OptionValidationError
from routersploit.core.exploit.option import (
OptIP,
OptPort,
OptInteger,
OptFloat,
OptBool,
OptString,
OptMAC,
OptWordlist,
OptEncoder,
)
def test_opt_ip():
# Test OptIP creation
opt_ip = OptIP("", "Test IP Description")
assert opt_ip.description == "Test IP Description"
assert opt_ip.display_value == ""
assert opt_ip.value == ""
assert opt_ip.__get__(None, None) == ""
# Test OptIP setting to empty value
opt_ip.__set__(None, "")
assert opt_ip.value == ""
assert opt_ip.display_value == ""
assert opt_ip.__get__(None, None) == ""
# Test OptIP setting to 192.168.1.1
opt_ip.__set__(None, "192.168.1.1")
assert opt_ip.value == "192.168.1.1"
assert opt_ip.display_value == "192.168.1.1"
assert opt_ip.__get__(None, None) == "192.168.1.1"
# Test OptIP setting to InvalidIP value
try:
opt_ip.__set__(None, "InvalidIP")
assert False
except OptionValidationError:
assert True
def test_opt_port():
# Test OptPort creation
opt_port = OptPort(80, "Test Port Description")
assert opt_port.description == "Test Port Description"
assert opt_port.display_value == "80"
assert opt_port.value == 80
assert opt_port.__get__(None, None) == 80
# Test OptPort setting to 4444
opt_port.__set__(None, 4444)
assert opt_port.display_value == "4444"
assert opt_port.value == 4444
assert opt_port.__get__(None, None) == 4444
# Test OptPort setting to 0
try:
opt_port.__set__(None, 0)
assert False
except OptionValidationError:
assert True
# Test OptPort setting to 65536
try:
opt_port.__set__(None, 65536)
assert False
except OptionValidationError:
assert True
def test_opt_bool():
# Test OptBool creation
opt_bool = OptBool(True, "Test Bool Description")
assert opt_bool.description == "Test Bool Description"
assert opt_bool.display_value == "true"
assert opt_bool.value
assert opt_bool.__get__(None, None)
# Test OptBool setting to false
opt_bool.__set__(None, "false")
assert opt_bool.display_value == "false"
assert not opt_bool.value
assert not opt_bool.__get__(None, None)
# Test OptBool setting to true
opt_bool.__set__(None, "true")
assert opt_bool.display_value == "true"
assert opt_bool.value
assert opt_bool.__get__(None, None)
# Test OptBool setting to invalid value
try:
opt_bool.__set__(None, "Invalid Value")
assert False
except OptionValidationError:
assert True
def test_opt_integer():
# Test OptInteger creation
opt_integer = OptInteger(4444, "Test Integer Description")
assert opt_integer.description == "Test Integer Description"
assert opt_integer.display_value == "4444"
assert opt_integer.value == 4444
assert opt_integer.__get__(None, None) == 4444
# Test OptInteger setting to -1
opt_integer.__set__(None, -1)
assert opt_integer.display_value == "-1"
assert opt_integer.value == -1
assert opt_integer.__get__(None, None) == -1
# Test OptInteger setting to 9999999
opt_integer.__set__(None, 9999999)
assert opt_integer.display_value == "9999999"
assert opt_integer.value == 9999999
assert opt_integer.__get__(None, None) == 9999999
# Test OptInteget setting to invalid value
try:
opt_integer.__set__(None, "Invalid Value")
assert False
except OptionValidationError:
assert True
def test_opt_float():
# Test OptFloat creation
opt_float = OptFloat(3.14, "Test Float Description")
assert opt_float.description == "Test Float Description"
assert opt_float.display_value == "3.14"
assert opt_float.value == 3.14
assert opt_float.__get__(None, None) == 3.14
# Test OptFloat setting to -1
opt_float.__set__(None, -1)
assert opt_float.display_value == "-1"
assert opt_float.value == -1
assert opt_float.__get__(None, None) == -1
# Test OptFloat setting to 999.9999
opt_float.__set__(None, 999.9999)
assert opt_float.display_value == "999.9999"
assert opt_float.value == 999.9999
assert opt_float.__get__(None, None) == 999.9999
# Test OptFloat setting to invalid value
try:
opt_float.__set__(None, "Invalid Value")
assert False
except OptionValidationError:
assert True
def test_opt_string():
# Test OptString creation
opt_string = OptString("Test", "Test String Description")
assert opt_string.description == "Test String Description"
assert opt_string.display_value == "Test"
assert opt_string.value == "Test"
assert opt_string.__get__(None, None) == "Test"
# Test OptString setting to "AAAABBBBCCCCDDDD"
opt_string.__set__(None, "AAAABBBBCCCCDDDD")
assert opt_string.display_value == "AAAABBBBCCCCDDDD"
assert opt_string.value == "AAAABBBBCCCCDDDD"
assert opt_string.__get__(None, None) == "AAAABBBBCCCCDDDD"
def test_opt_mac():
# Test OptMAC creation
opt_mac = OptMAC("AA:BB:CC:DD:EE:FF", "Test MAC Description")
assert opt_mac.description == "Test MAC Description"
assert opt_mac.display_value == "AA:BB:CC:DD:EE:FF"
assert opt_mac.value == "AA:BB:CC:DD:EE:FF"
assert opt_mac.__get__(None, None) == "AA:BB:CC:DD:EE:FF"
# Test OptMAC setting to dd:ee:ff:dd:ee:ff
opt_mac.__set__(None, "dd:ee:ff:dd:ee:ff")
assert opt_mac.display_value == "dd:ee:ff:dd:ee:ff"
assert opt_mac.value == "dd:ee:ff:dd:ee:ff"
assert opt_mac.__get__(None, None) == "dd:ee:ff:dd:ee:ff"
# Test OptMAC setting to invalid value
try:
opt_mac.__set__(None, "Invalid Value")
assert False
except OptionValidationError:
assert True
def test_opt_wordlist():
# Test OptWordlist creation
opt_wordlist = OptWordlist("", "Test Wordlist Description")
assert opt_wordlist.description == "Test Wordlist Description"
assert opt_wordlist.display_value == ""
assert opt_wordlist.value == ""
assert opt_wordlist.__get__(None, None) == [""]
# Test OptWordlist setting to admin,test
opt_wordlist.__set__(None, "admin,test")
assert opt_wordlist.display_value == "admin,test"
assert opt_wordlist.value == "admin,test"
assert opt_wordlist.__get__(None, None) == ["admin", "test"]
def test_opt_encoder():
# Test OptEncoder creation
opt_encoder = OptEncoder(PHPHexEncoder(), "Test Encoder Description")
assert opt_encoder.description == "Test Encoder Description"
assert str(opt_encoder.display_value) == "php/hex"
assert type(opt_encoder.display_value) == PHPHexEncoder
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment