2024-07-30 12:15:37 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Terminal Processing\n",
"\n",
"© Crown-owned copyright 2024, Defence Science and Technology Laboratory UK"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook serves as a guide on the functionality and use of the new Terminal simulation component.\n",
"\n",
2024-07-30 15:24:37 +01:00
"By default, the Terminal will come pre-installed on any simulation component which inherits from `HostNode` (Computer, Server, Printer), and simulates the Secure Shell (SSH) protocol as the communication method."
2024-07-30 12:15:37 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from primaite.simulator.system.services.terminal.terminal import Terminal\n",
"from primaite.simulator.network.container import Network\n",
2024-08-05 09:29:17 +01:00
"from primaite.simulator.network.hardware.nodes.host.computer import Computer\n",
"from primaite.simulator.system.applications.red_applications.ransomware_script import RansomwareScript"
2024-07-30 12:15:37 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def basic_network() -> Network:\n",
" \"\"\"Utility function for creating a default network to demonstrate Terminal functionality\"\"\"\n",
" network = Network()\n",
" node_a = Computer(hostname=\"node_a\", ip_address=\"192.168.0.10\", subnet_mask=\"255.255.255.0\", start_up_duration=0)\n",
" node_a.power_on()\n",
" node_b = Computer(hostname=\"node_b\", ip_address=\"192.168.0.11\", subnet_mask=\"255.255.255.0\", start_up_duration=0)\n",
" node_b.power_on()\n",
" network.connect(node_a.network_interface[1], node_b.network_interface[1])\n",
" return network"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-07-31 15:47:18 +01:00
"The terminal can be accessed from a `HostNode` via the `software_manager` as demonstrated below. \n",
"\n",
"In the example, we have a basic network consisting of two computers "
2024-07-30 12:15:37 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"network: Network = basic_network()\n",
"computer_a: Computer = network.get_node_by_hostname(\"node_a\")\n",
"terminal_a: Terminal = computer_a.software_manager.software.get(\"Terminal\")\n",
"computer_b: Computer = network.get_node_by_hostname(\"node_b\")\n",
2024-08-01 08:37:51 +01:00
"terminal_b: Terminal = computer_b.software_manager.software.get(\"Terminal\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To be able to send commands from `node_a` to `node_b`, you will need to `login` to `node_b` first, using valid user credentials. In the example below, we are logging in to the 'admin' account on `node_b`. \n",
"If you are not logged in, any commands sent will be rejected."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2024-07-30 16:58:40 +01:00
"# Login to the remote (node_b) from local (node_a)\n",
2024-08-05 09:29:17 +01:00
"from primaite.simulator.system.services.terminal.terminal import RemoteTerminalConnection\n",
"\n",
"\n",
"term_a_term_b_remote_connection: RemoteTerminalConnection = terminal_a.login(username=\"admin\", password=\"Admin123!\", ip_address=computer_b.network_interface[1].ip_address)"
2024-07-30 16:58:40 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2024-08-05 09:29:17 +01:00
"computer_b.software_manager.show()"
2024-07-30 16:58:40 +01:00
]
},
{
2024-08-05 09:29:17 +01:00
"cell_type": "code",
"execution_count": null,
2024-07-30 16:58:40 +01:00
"metadata": {},
2024-08-05 09:29:17 +01:00
"outputs": [],
2024-07-30 16:58:40 +01:00
"source": [
2024-08-05 09:29:17 +01:00
"print(type(term_a_term_b_remote_connection))\n",
"term_a_term_b_remote_connection.execute([\"software_manager\", \"application\", \"install\", \"RansomwareScript\"])"
2024-07-30 12:15:37 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2024-08-05 09:29:17 +01:00
"computer_b.software_manager.show()"
2024-07-30 16:58:40 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-08-05 09:29:17 +01:00
"You can view all remote connections to a terminal through use of the `show()` method"
2024-07-30 16:58:40 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2024-08-05 09:29:17 +01:00
"terminal_b.show()"
2024-07-30 12:15:37 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2024-08-05 09:29:17 +01:00
"The Terminal can be used to send requests to install new software. The code block below demonstrates how the Terminal class allows the user of `terminal_a`, on `computer_a`, to send a command to `computer_b` to install the `RansomwareScript` application. \n"
2024-07-30 12:15:37 +01:00
]
},
{
2024-08-05 09:29:17 +01:00
"cell_type": "markdown",
2024-07-30 12:15:37 +01:00
"metadata": {},
"source": [
2024-08-05 09:29:17 +01:00
"The below example shows how you can send a command via the terminal to create a folder on the target Node.\n",
2024-07-30 12:15:37 +01:00
"\n",
2024-08-05 09:29:17 +01:00
"Here, we send a command to `computer_b` to create a new folder titled \"Downloads\"."
2024-07-30 12:15:37 +01:00
]
2024-07-30 15:24:37 +01:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The resultant call to `computer_b.file_system.show()` shows that the new folder has been created."
]
2024-07-30 12:15:37 +01:00
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}