Skip to content

Commit 0783f8a

Browse files
committed
Merge branch 'add-test-from-template'
2 parents e09b224 + cea8978 commit 0783f8a

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

routersploit/templates/__init__.py

Whitespace-only changes.

routersploit/templates/exploit.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from routersploit import (
2+
exploits,
3+
mute,
4+
validators,
5+
)
6+
7+
8+
class Exploit(exploits.Exploit):
9+
""" Exploit template. """
10+
__info__ = {
11+
'name': '',
12+
'authors': [
13+
'', # vulnerability discovery
14+
'', # routersploit module
15+
],
16+
'description': '',
17+
'references': [
18+
'',
19+
],
20+
'devices': [
21+
'',
22+
],
23+
}
24+
25+
target = exploits.Option('', 'Target address e.g. http://192.168.1.1', validators=validators.url)
26+
port = exploits.Option(80, 'Target Port')
27+
28+
def run(self):
29+
pass
30+
31+
@mute
32+
def check(self):
33+
pass

routersploit/utils.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import importlib
1212
import select
1313
import socket
14+
import errno
1415
from functools import wraps
1516
from distutils.util import strtobool
1617
from abc import ABCMeta, abstractmethod
@@ -34,6 +35,8 @@
3435
# Disable certificate verification warnings
3536
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
3637

38+
Resource = collections.namedtuple("Resource", ["name", "template_path", "context"])
39+
3740

3841
def index_modules(modules_directory=MODULES_DIR):
3942
""" Return list of all exploits modules """
@@ -528,3 +531,76 @@ def tokenize(token_specification, text):
528531
else:
529532
column = mo.start() - line_start
530533
yield Token(kind, value, line_num, column, mo)
534+
535+
536+
def create_exploit(path): # TODO: cover with tests
537+
from .templates import exploit
538+
539+
parts = path.split(os.sep)
540+
module_type, name = parts[0], parts[-1]
541+
542+
if not name:
543+
print_error("Invalid exploit name. ;(")
544+
return
545+
546+
if module_type not in ['creds', 'exploits', 'scanners']:
547+
print_error("Invalid module type. ;(")
548+
return
549+
550+
create_resource(
551+
name=os.path.join(*parts[:-1]),
552+
content=(
553+
Resource(
554+
name="{}.py".format(name),
555+
template_path=os.path.abspath(exploit.__file__.rstrip("c")),
556+
context={}),
557+
),
558+
python_package=True
559+
)
560+
561+
562+
def create_resource(name, content=(), python_package=False): # TODO: cover with tests
563+
""" Creates resource directory in current working directory. """
564+
root_path = os.path.join(MODULES_DIR, name)
565+
mkdir_p(root_path)
566+
567+
if python_package:
568+
open(os.path.join(root_path, "__init__.py"), "a").close()
569+
570+
for name, template_path, context in content:
571+
if os.path.splitext(name)[-1] == "": # Checking if resource has extension if not it's directory
572+
mkdir_p(os.path.join(root_path, name))
573+
else:
574+
try:
575+
with open(template_path, "rb") as template_file:
576+
template = string.Template(template_file.read())
577+
except (IOError, TypeError):
578+
template = string.Template("")
579+
580+
try:
581+
file_handle = os.open(os.path.join(root_path, name), os.O_CREAT | os.O_EXCL | os.O_WRONLY)
582+
except OSError as e:
583+
if e.errno == errno.EEXIST:
584+
print_status("{} already exist.".format(name))
585+
else:
586+
raise
587+
else:
588+
with os.fdopen(file_handle, 'w') as target_file:
589+
target_file.write(template.substitute(**context))
590+
print_success("{} successfully created.".format(name))
591+
592+
593+
def mkdir_p(path): # TODO: cover with tests
594+
"""
595+
Simulate mkdir -p shell command. Creates directory with all needed parents.
596+
:param path: Directory path that may include non existing parent directories
597+
:return:
598+
"""
599+
try:
600+
os.makedirs(path)
601+
print_success("Directory {path} successfully created.".format(path=path))
602+
except OSError as exc:
603+
if exc.errno == errno.EEXIST and os.path.isdir(path):
604+
print_success("Directory {path}".format(path=path))
605+
else:
606+
raise

rsf.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
#!/usr/bin/env python2
22

3+
from __future__ import print_function
4+
5+
import argparse
6+
37
from routersploit.interpreter import RoutersploitInterpreter
8+
from routersploit.utils import create_exploit
9+
10+
11+
parser = argparse.ArgumentParser(description='RouterSploit - Router Exploitation Framework')
12+
parser.add_argument('-a',
13+
'--add-exploit',
14+
metavar='exploit_path',
15+
help='Add exploit using default template.')
416

517

618
def routersploit():
719
rsf = RoutersploitInterpreter()
820
rsf.start()
921

1022
if __name__ == "__main__":
11-
routersploit()
23+
args = parser.parse_args()
24+
25+
if args.add_exploit:
26+
create_exploit(args.add_exploit)
27+
else:
28+
routersploit()

0 commit comments

Comments
 (0)