Skip to main content

A framework for designing, orchestrating and executing backdoor systems, with short and long term memory, dynamic execution, data delivery, multithreading live communication and more.

Project description

backdoor

A framework for designing, orchestrating and executing backdoor systems, with short and long term memory, dynamic execution, data delivery, multithreading live communication and more.

Installation

pip install backdoor-io

example

Simple commands to execute one by one.

from backdoor import (
    Executor, Command, Data, Actions, Action
)

commands = [
    Command(request=Data(payload='text', name='value', write=True)),
    Command(request=Data(name='value', read=True)),
    Command(
        action=Action(type=Actions.EXECUTION.TYPE, name=Actions.EXECUTION.PYTHON),
        request=Data(payload='print(value); result = value * 2;')
    ),
    Command(
        action=Action(type=Actions.DATA.TYPE, name=Actions.DATA.DELETE),
        request=Data(name='value')
    ),
    Command(
        action=Action(type=Actions.DATA.TYPE, name=Actions.DATA.SEARCH),
        request=Data(name='value')
    ),
    Command(
        action=Action(type=Actions.DATA.TYPE, name=Actions.DATA.READ),
        request=Data(name='result')
    ),
    Command(
        action=Action(type=Actions.EXECUTION.TYPE, name=Actions.EXECUTION.CMD),
        request=Data(payload='dir')
    )
]

executor = Executor()

for command in commands:
    print(executor.execute(command), "\n")

output

Command(
    id='793f1d0d-641e-45b4-ba2f-dad801c35a3f',
    action=Action(type='data', name='write', repetitions=1, timeout=None, thread=False, wait=True),  
    request=Data(payload='text', format='text', name='value', read=False, write=True, timestamp=1710051829.9969397), 
    response=Data(payload="'value' was written to memory in memory.", format='text', name=None, read=False, write=False, timestamp=1710051829.9969397), 
    memory=None, complete=True, running=False, error=None
)

Command(
    id='b5985414-1d9f-4a0e-966b-445b2be92a40',
    action=Action(type='data', name='read', repetitions=1, timeout=None, thread=False, wait=True), 
    request=Data(payload='text', format='text', name='value', read=True, write=False, timestamp=1710051829.9969397), 
    response=Data(payload='text', format='text', name=None, read=False, write=False, timestamp=1710051829.9969397), 
    memory=None, complete=True, running=False, error=None
)

Command(
    id='958f80d4-e74e-45e6-bf08-0ab6c89ab771',
    action=Action(type='execution', name='python', repetitions=1, timeout=None, thread=False, wait=True), 
    request=Data(payload='print(value); result = value * 2;', format='text', name=None, read=False, write=False, timestamp=1710051829.9969397), 
    response=Data(payload={'stdout': '', 'stderr': ''}, format='json', name=None, read=False, write=False, timestamp=1710051829.9969397), 
    memory=None, complete=True, running=False, error=None
)

Command(
    action=Action(type='data', name='delete', repetitions=1, timeout=None, thread=False, wait=True), 
    id='b91c39ca-d19f-4322-a209-b5ca4c06dfe5', 
    request=Data(payload=None, format=None, name='value', read=False, write=False, timestamp=1710051829.9969397), 
    response=Data(payload="'value' was deleted from memory.", format='text', name=None, read=False, write=False, timestamp=1710051829.99794), 
    memory=None, complete=True, running=False, error=None
)

Command(
    id='a9d47fcf-5266-4a47-b102-ed267b9bbba4',
    action=Action(type='data', name='search', repetitions=1, timeout=None, thread=False, wait=True), 
    request=Data(payload=None, format=None, name='value', read=False, write=False, timestamp=1710051829.9969397), 
    response=Data(payload=False, format='json', name=None, read=False, write=False, timestamp=1710051829.99794), 
    memory=None, complete=True, running=False, error=None)

Command(
    id='d9ec5d45-e214-4a36-8d72-f52adb605da3',
    action=Action(type='data', name='read', repetitions=1, timeout=None, thread=False, wait=True), 
    request=Data(payload=None, format=None, name='result', read=False, write=False, timestamp=1710051829.9969397), 
    response=Data(payload='texttext', format='text', name=None, read=False, write=False, timestamp=1710051829.99794), 
    memory=None, complete=True, running=False, error=None)

Command(
    id='d5093ce2-0cbf-4d5e-8195-850c27d46e54',
    action=Action(type='execution', name='cmd', repetitions=1, timeout=None, thread=False, wait=True), 
    request=Data(payload='dir', format='text', name=None, read=False, write=False, timestamp=1710051829.9969397), 
    response=Data(payload={'stdout': ' Volume in drive C is Windows-SSD\n Volume Serial Number is 0C65-337C\n\n Directory of ...', 'stderr': ''}, format='json', name=None, read=False, write=False, timestamp=1710051830.0080411), 
    memory=None, complete=True, running=False, error=None
)

Complex commands to run in the background, with timeoutand threading.

import time

from backdoor import (
    Executor, Command, Data, Actions, Action
)

c1 = Command(
    action=Action(
        type=Actions.EXECUTION.TYPE,
        name=Actions.EXECUTION.PYTHON,
        wait=False,
        thread=True,
        # timeout=dt.timedelta(seconds=5)
    ),
    request=Data(payload='import time\nfor _ in range(10):\n\ttime.sleep(1)')
)
c2 = Command(
    action=Action(
        type=Actions.MANAGEMENT.TYPE,
        name=Actions.MANAGEMENT.COMMAND,
    ),
    request=Data(payload=c1.id)
)

c3 = Command(
    action=Action(
        type=Actions.MANAGEMENT.TYPE,
        name=Actions.MANAGEMENT.STOP,
    ),
    request=Data(payload=c1.id)
)

executor = Executor()

print(executor.execute(c1), "\n")
print("waiting for 3 seconds...")

time.sleep(3)

print(executor.execute(c2), "\n")
print("waiting for 3 seconds...")

time.sleep(3)

print(executor.execute(c3), "\n")
print(executor.execute(c2), "\n")

output

Command(
    id='3c9da903-028c-4dd0-b583-f15ccaed1280', 
    action=Action(type='execution', name='python', repetitions=1, timeout=None, thread=True, wait=False), 
    request=Data(payload='import time\nfor _ in range(10):\n\ttime.sleep(1)', format='text', name=None, read=False, write=False, timestamp=1710052480.8686686), 
    response=Data(payload={}, format='json', name=None, read=False, write=False, timestamp=1710052480.8705611), 
    memory=None, complete=False, running=True, error=None
)

waiting for 3 seconds...

Command(
    id='df96ff69-16d2-4c42-a5a9-f16c390f15ee', 
    action=Action(type='management', name='command', repetitions=1, timeout=None, thread=False, wait=True), 
    request=Data(payload='3c9da903-028c-4dd0-b583-f15ccaed1280', format='text', name=None, read=False, write=False, timestamp=1710052480.8686686), 
    response=Data(payload={'id': '3c9da903-028c-4dd0-b583-f15ccaed1280', 'action': {'type': 'execution', 'name': 'python', 'repetitions': 1, 'timeout': None, 'thread': True, 'wait': False}, 'request': {'payload': 'import time\nfor _ in range(10):\n\ttime.sleep(1)', 'format': 'text', 'name': None, 'read': False, 'write': False, 'timestamp': 1710052480.8686686}, 'response': {'payload': {}, 'format': 'json', 'name': None, 'read': False, 'write': False, 'timestamp': 1710052480.8705611}, 'memory': None, 'complete': False, 'running': True, 'error': None}, format='json', name=None, read=False, write=False, timestamp=1710052483.8717408), 
    memory=None, complete=True, running=False, error=None
)

waiting for 3 seconds...

Command(
    id='d6e7faea-6c1a-410e-89b7-3652ca914cc0', 
    action=Action(type='management', name='stop', repetitions=1, timeout=None, thread=False, wait=True), 
    request=Data(payload='3c9da903-028c-4dd0-b583-f15ccaed1280', format='text', name=None, read=False, write=False, timestamp=1710052480.8686686), 
    response=Data(payload='Command was stopped.', format='text', name=None, read=False, write=False, timestamp=1710052486.8754737), 
    memory=None, complete=True, running=False, error=None
)

Command(
    id='df96ff69-16d2-4c42-a5a9-f16c390f15ee', 
    action=Action(type='management', name='command', repetitions=1, timeout=None, thread=False, wait=True), 
    request=Data(payload='3c9da903-028c-4dd0-b583-f15ccaed1280', format='text', name=None, read=False, write=False, timestamp=1710052480.8686686), 
    response=Data(payload={'id': '3c9da903-028c-4dd0-b583-f15ccaed1280', 'action': {'type': 'execution', 'name': 'python', 'repetitions': 1, 'timeout': None, 'thread': True, 'wait': False}, 'request': {'payload': 'import time\nfor _ in range(10):\n\ttime.sleep(1)', 'format': 'text', 'name': None, 'read': False, 'write': False, 'timestamp': 1710052480.8686686}, 'response': {'payload': {}, 'format': 'json', 'name': None, 'read': False, 'write': False, 'timestamp': 1710052480.8705611}, 'memory': None, 'complete': False, 'running': False, 'error': None}, format='json', name=None, read=False, write=False, timestamp=1710052486.8764887),
    memory=None, complete=True, running=False, error=None
)

Constructing a Command object:

# The id of the command, can also be a plaintext name.
id: str = str(uuid4())
# Describes the type and name of action to preform.
action: Action | None = None
# Describes the input (request) data and output (response) data,
# and what to do with them.
request: Data = Data()
response: Data = Data()
# a dedicated memory dictionary for the command, 
# rather than using the shared memory of the executor's.
memory: dict[str, JsonValue] = None

Additional attributes:

# Id the command is still running: complete: False, running: True.
complete: bool = False
running: bool = False
# When an error is raised, it is saved here.
error: str | None = None

Constructing an Action object:

# The type of action, from the Actions class.
type: str
# The name of the action from the type from the Actions class.
name: str
# The amount of times to commit the action in a row.
repetitions: int = 1
# A timeout to stop the action.
timeout: dt.timedelta = None
# A value to specify that the action is to be executed in a different thread.
# This means that the action will return back the command as usual, 
# but will specify that the command is not complete and still running.
thread: bool = False
# A value to make the executor wait to the action to finish.
wait: bool = True

Constructing a Data object:

# The bytes | json valid payload to contain.
payload: JsonValue = None
# The format of the data. Can be inferred automatically.
format: str | Literal['text', 'bytes', 'json'] | None = None
# The name of the data, with which the data can be 
# saved to or read from the memory of the command/executor.
name: str | None = None
# A value to specify that a value by the given name is to be read from memory.
read: bool | None = None
# A value to specify that a value by the given name is to be written to memory.
write: bool | None = None

Additional attributes:

# The timestamp of creation of the object.
timestamp: float = time.time()

Simple Json-valid I/O:

from backdoor import Command, Action, Data

c1 = Command(
    id='793f1d0d-641e-45b4-ba2f-dad801c35a3f',
    action=Action(type='data', name='write', repetitions=1, timeout=None, thread=False, wait=True),
    request=Data(payload='text', format='text', name='value', read=False, write=True, timestamp=1710051829.9969397),
    response=Data(payload="'value' was written to memory in memory.", format='text', name=None, read=False, write=False, timestamp=1710051829.9969397),
    memory=None, complete=True, running=False, error=None
)

print(c1.dump())
print("same data:", c1 == Command.load(c1.dump()))

output:

{
    'id': '793f1d0d-641e-45b4-ba2f-dad801c35a3f', 
    'action': {'type': 'data', 'name': 'write', 'repetitions': 1, 'timeout': None, 'thread': False, 'wait': True}, 
    'request': {'payload': 'text', 'format': 'text', 'name': 'value', 'read': False, 'write': True, 'timestamp': 1710051829.9969397}, 
    'response': {'payload': "'value' was written to memory in memory.", 'format': 'text', 'name': None, 'read': False, 'write': False, 'timestamp': 1710051829.9969397}, 
    'memory': None, 'complete': True, 'running': False, 'error': None
}
same data: True

Executor Construction:

# Saves the id of the commands executed.
history: list[str] = []
# Saves the id of the commands being executed.
running: list[str] = []
# Contains all command object.
commands: dict[str, Command] = {}
# Shared memory of data and variables from and for the execution of commands.
memory: dict[str, ...] = {}
# Contains custom command objects.
custom: dict[str, Command | None] = {}
# Specifies the initial and current locations of the system to execute in.
root_location: str = os.getcwd
current_location: str = os.getcwd
# Callables to save/load/delete command data/objects.
save: Callable[[Command], str] = None
load: Callable[[str], Command] = None
delete: Callable[[str], ...] = None

Executor Interface:

from backdoor import Executor, Command

executor = Executor()

# simple execution

c1 = Command(...)

c1_copy = c1.copy()

c1_return = executor.execute(c1)

assert c1 is c1_return
assert c1_copy != c1_return

# adding a custom command

custom_command = Command(...)

# method 1:
executor.add(custom_command)

# method 2:
from backdoor import Action, Data, Actions

custom_command_adder = Command(
    action=Action(type=Actions.MANAGEMENT.TYPE, name=Actions.MANAGEMENT.ADD),
    request=Data(payload=custom_command.dump())
)

executor.execute(custom_command_adder)

# running the custom command

custom_command_return = executor.execute(
    Command(
        action=Action(type=Actions.EXECUTION.TYPE, name=Actions.MANAGEMENT.CLEAN),
        request=Data(payload=custom_command.id)
    )
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

backdoor-io-0.0.1.tar.gz (17.3 kB view details)

Uploaded Source

File details

Details for the file backdoor-io-0.0.1.tar.gz.

File metadata

  • Download URL: backdoor-io-0.0.1.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.0

File hashes

Hashes for backdoor-io-0.0.1.tar.gz
Algorithm Hash digest
SHA256 069f28bfaadb13773c1a2a51fd34c3db20fffd5efdff2009b98fe477c060b540
MD5 26c411c4a48199a0a3a8a319d230f4d8
BLAKE2b-256 5c49a52353e34ba3dd21ddbe51cd66e2cc95c8995f2ac7013af72e462306ebce

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page