|
11 | 11 | import importlib |
12 | 12 | import select |
13 | 13 | import socket |
| 14 | +import errno |
14 | 15 | from functools import wraps |
15 | 16 | from distutils.util import strtobool |
16 | 17 | from abc import ABCMeta, abstractmethod |
|
34 | 35 | # Disable certificate verification warnings |
35 | 36 | requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) |
36 | 37 |
|
| 38 | +Resource = collections.namedtuple("Resource", ["name", "template_path", "context"]) |
| 39 | + |
37 | 40 |
|
38 | 41 | def index_modules(modules_directory=MODULES_DIR): |
39 | 42 | """ Return list of all exploits modules """ |
@@ -528,3 +531,76 @@ def tokenize(token_specification, text): |
528 | 531 | else: |
529 | 532 | column = mo.start() - line_start |
530 | 533 | 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 |
0 commit comments