{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Sensor Override Attack Example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import display, HTML\n", "display(HTML('\"Open'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example demonstrastes how to add a sensor overriding attack to a scenario -- more information can be found in the [documentation](https://epyt-flow.readthedocs.io/en/stable/tut.events.html#sensor-reading-attacks)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install epyt-flow --quiet" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from epyt_flow.data.benchmarks import load_leakdb_scenarios\n", "from epyt_flow.simulation import ScenarioSimulator, SENSOR_TYPE_LINK_FLOW\n", "from epyt_flow.simulation.events import SensorOverrideAttack\n", "from epyt_flow.utils import to_seconds, plot_timeseries_data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the first Hanoi scenario in [LeakDB](https://epyt-flow.readthedocs.io/en/stable/epyt_flow.data.benchmarks.html#epyt_flow.data.benchmarks.leakdb.load_scenarios):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "config = load_leakdb_scenarios(scenarios_id=[\"1\"], use_net1=False, verbose=False)[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim = ScenarioSimulator(scenario_config=config)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set simulation duration to two days:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.set_general_parameters(simulation_duration=to_seconds(days=2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Override the sensor readings of the flow sensor at link \"1\" with the value \"42\" for 2hrs -- i.e. time steps 10 - 15.\n", "\n", "This is done by creating an instance of the [SensorOverrideAttack](https://epyt-flow.readthedocs.io/en/stable/epyt_flow.simulation.events.html#epyt_flow.simulation.events.sensor_reading_attack.SensorOverrideAttack) class and adding this sensor reading event to the scenario by calling [add_sensor_reading_event()](https://epyt-flow.readthedocs.io/en/stable/epyt_flow.simulation.html#epyt_flow.simulation.scenario_simulator.ScenarioSimulator.add_sensor_reading_event):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_sensor_values = np.array([42]*5)\n", "sensor_override_attack = SensorOverrideAttack(new_sensor_values,\n", " start_time=to_seconds(hours=5),\n", " end_time=to_seconds(hours=7),\n", " sensor_id=\"1\",\n", " sensor_type=SENSOR_TYPE_LINK_FLOW)\n", "\n", "sim.add_sensor_reading_event(sensor_override_attack)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run simulation and and plot flow readings:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "scada_data = sim.run_simulation()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "scada_data.plot_flows(sensor_locations=[\"1\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sensor readings WITHOUT the sensor overriding attack:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Remove attack\n", "scada_data.sensor_reading_events = []\n", "\n", "# Recompute and show final sensor readings\n", "scada_data.plot_flows(sensor_locations=[\"1\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do not forget to close the simulation!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim.close()" ] } ], "metadata": { "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.9.20" } }, "nbformat": 4, "nbformat_minor": 2 }