From 9a377430dbb61ac0f36fe16cbe432a92b72a1739 Mon Sep 17 00:00:00 2001 From: Pantelis Roditis Date: Tue, 20 Feb 2024 20:34:44 +0200 Subject: [PATCH 1/3] minor format changes --- backend/commands/TargetController.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backend/commands/TargetController.php b/backend/commands/TargetController.php index c573bc716..67526a277 100644 --- a/backend/commands/TargetController.php +++ b/backend/commands/TargetController.php @@ -36,6 +36,7 @@ public function actionSpin($target=false) 'id' => intval($target), ]); } + foreach($query->all() as $t) { if($t->ondemand===null || $t->ondemand->state===1 || $target!==false ) @@ -121,6 +122,7 @@ public function actionRestart($uptime=1440, $limit=1) } } // end docker servers } + public function actionDestroyInstances($id=false,$dopf=false) { $t=TargetInstance::find(); @@ -138,7 +140,8 @@ public function actionDestroyInstances($id=false,$dopf=false) // ignore errors of destroy try { $dc->destroy(); } catch (\Exception $e) { } } - try{ + try + { if($dopf!==false) { Pf::kill_table($dc->name,true); @@ -157,6 +160,7 @@ public function actionDestroyInstances($id=false,$dopf=false) } } } + public function actionDestroyOndemand($id=false,$dopf=false) { try From ee1deceaedf23997f9a3c4dbc62c2fe1cd0e638e Mon Sep 17 00:00:00 2001 From: Pantelis Roditis Date: Tue, 20 Feb 2024 21:30:52 +0200 Subject: [PATCH 2/3] and some indentation --- backend/commands/TargetController.php | 106 +++++++++++++------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/backend/commands/TargetController.php b/backend/commands/TargetController.php index 67526a277..6bc3de7ae 100644 --- a/backend/commands/TargetController.php +++ b/backend/commands/TargetController.php @@ -125,70 +125,70 @@ public function actionRestart($uptime=1440, $limit=1) public function actionDestroyInstances($id=false,$dopf=false) { - $t=TargetInstance::find(); - if(boolval($id)!==false) - $t->where(['=','target_id',$id]); - foreach($t->all() as $val) - { - $dc=new DockerContainer($val->target); - $dc->name=$val->name; - $dc->server=$val->server->connstr; - printf(" %s for %s (%s)\n",$val->target->name,$val->player->username,$dc->name); - try { - if($val->ip!==null) - { - // ignore errors of destroy - try { $dc->destroy(); } catch (\Exception $e) { } - } - try + $t=TargetInstance::find(); + if(boolval($id)!==false) + $t->where(['=','target_id',$id]); + foreach($t->all() as $val) + { + $dc=new DockerContainer($val->target); + $dc->name=$val->name; + $dc->server=$val->server->connstr; + printf(" %s for %s (%s)\n",$val->target->name,$val->player->username,$dc->name); + try { + if($val->ip!==null) { - if($dopf!==false) - { - Pf::kill_table($dc->name,true); - Pf::kill_table($dc->name.'_clients',true); - } - } catch (\Exception $e) {} - $val->delete(); + // ignore errors of destroy + try { $dc->destroy(); } catch (\Exception $e) { } } - catch (\Exception $e) + try { - if(method_exists($e,'getErrorResponse')) - echo $e->getErrorResponse()->getMessage(),"\n"; - else - echo $e->getMessage(),"\n"; + if($dopf!==false) + { + Pf::kill_table($dc->name,true); + Pf::kill_table($dc->name.'_clients',true); + } + } catch (\Exception $e) {} + $val->delete(); + } + catch (\Exception $e) + { + if(method_exists($e,'getErrorResponse')) + echo $e->getErrorResponse()->getMessage(),"\n"; + else + echo $e->getMessage(),"\n"; - } } } + } - public function actionDestroyOndemand($id=false,$dopf=false) + public function actionDestroyOndemand($id=false,$dopf=false) + { + try { - try + $demands=\app\modules\gameplay\models\TargetOndemand::find(); + if(boolval($id)!==false) { - $demands=\app\modules\gameplay\models\TargetOndemand::find(); - if(boolval($id)!==false) - { - $demands->where(['target_id'=>$id]); - } - else - { - $demands->andWhere(['state'=>1]); - } - - foreach($demands->all() as $ondemand) - { - printf("Destroying ondemand target %s\n", $ondemand->target->fqdn); - try { $ondemand->target->destroy(); } catch (\Exception $e) { } - $ondemand->state=-1; - $ondemand->heartbeat=null; - if($dopf!==false) - Pf::del_table_ip('heartbeat',$ondemand->target->ipoctet); - $ondemand->save(); - } + $demands->where(['target_id'=>$id]); } - catch (\Exception $e) + else + { + $demands->andWhere(['state'=>1]); + } + + foreach($demands->all() as $ondemand) { - echo "OnDemand:", $e->getMessage(),"\n"; + printf("Destroying ondemand target %s\n", $ondemand->target->fqdn); + try { $ondemand->target->destroy(); } catch (\Exception $e) { } + $ondemand->state=-1; + $ondemand->heartbeat=null; + if($dopf!==false) + Pf::del_table_ip('heartbeat',$ondemand->target->ipoctet); + $ondemand->save(); } } + catch (\Exception $e) + { + echo "OnDemand:", $e->getMessage(),"\n"; + } + } } From 637acb19900df70592f14f82822c8ed17163b44c Mon Sep 17 00:00:00 2001 From: Pantelis Roditis Date: Wed, 21 Feb 2024 09:25:24 +0200 Subject: [PATCH 3/3] introduce cli metadata manipulation --- backend/commands/MetadataController.php | 185 ++++++++++++++++++++++++ backend/views/console/metadata/view.php | 27 ++++ docs/console-commands/Metadata.md | 54 +++++++ 3 files changed, 266 insertions(+) create mode 100644 backend/commands/MetadataController.php create mode 100644 backend/views/console/metadata/view.php create mode 100644 docs/console-commands/Metadata.md diff --git a/backend/commands/MetadataController.php b/backend/commands/MetadataController.php new file mode 100644 index 000000000..4e0764c1c --- /dev/null +++ b/backend/commands/MetadataController.php @@ -0,0 +1,185 @@ +0 && ($metadata = TargetMetadata::findOne($id))==null) + { + $this->stderr("Error: No record with given ID found!\n"); + return ExitCode::UNSPECIFIED_ERROR; + } + if($name!==false && ($metadata = TargetMetadata::find()->joinWith('target')->where(['target.name'=>$name])->one())==null) + { + $this->stderr("Error: No record with given name found!\n"); + return ExitCode::UNSPECIFIED_ERROR; + } + if($metadata->delete()!==false) + return ExitCode::OK; + } + + /** + * View target metadata + * @param $id The id of a specific target to view metadata + * @param $name The name of a specific target to view metadata + */ + public function actionView(int $id=0,$name=false) + { + if($id>0 && ($metadata = TargetMetadata::findOne($id))==null) + { + $this->stderr("Error: No record with given ID found!\n"); + return ExitCode::UNSPECIFIED_ERROR; + } + if($name!==false && ($metadata = TargetMetadata::find()->joinWith('target')->where(['target.name'=>$name])->one())==null) + { + $this->stderr("Error: No record with given name found!\n"); + return ExitCode::UNSPECIFIED_ERROR; + } + echo $this->renderPartial('view',[ + 'metadata'=>$metadata + ]); + } + + /** + * Get/Set target scenario + * @param $id The id of a specific target to view metadata + * @param $content (optional) If provided set the scenario instead of displaying it + */ + public function actionScenario(int $id,$content=null) + { + if($id>0 && ($metadata = TargetMetadata::findOne($id))==null) + { + $this->stderr("Error: No record with given ID found!\n"); + return ExitCode::UNSPECIFIED_ERROR; + } + if($content!==null) + { + $metadata->scenario=$content; + $metadata->save(); + } + echo $metadata->scenario,"\n"; + } + + /** + * Get/Set target solution + * @param $id The id of a specific target to view metadata + * @param $content (optional) If provided set the solution instead of displaying it + */ + public function actionSolution(int $id,$content=null) + { + if($id>0 && ($metadata = TargetMetadata::findOne($id))==null) + { + $this->stderr("Error: No record with given ID found!\n"); + return ExitCode::UNSPECIFIED_ERROR; + } + if($content!==null) + { + $metadata->solution=$content; + $metadata->save(); + } + echo $metadata->solution,"\n"; + } + + /** + * Get/Set target pre exploitation credits + * @param $id The id of a specific target to view metadata + * @param $content (optional) If provided set the pre_credits instead of displaying it + */ + public function actionPrecredits(int $id,$content=null) + { + if($id>0 && ($metadata = TargetMetadata::findOne($id))==null) + { + $this->stderr("Error: No record with given ID found!\n"); + return ExitCode::UNSPECIFIED_ERROR; + } + if($content!==null) + { + $metadata->pre_credits=$content; + $metadata->save(); + } + echo $metadata->pre_credits,"\n"; + } + + /** + * Get/Set target pre exploitation details + * @param $id The id of a specific target to view metadata + * @param $content (optional) If provided set the pre_exploitation instead of displaying it + */ + public function actionPreexploitation(int $id,$content=null) + { + if($id>0 && ($metadata = TargetMetadata::findOne($id))==null) + { + $this->stderr("Error: No record with given ID found!\n"); + return ExitCode::UNSPECIFIED_ERROR; + } + if($content!==null) + { + $metadata->pre_exploitation=$content; + $metadata->save(); + } + echo $metadata->pre_exploitation,"\n"; + } + + /** + * Get/Set target post exploitation credits + * @param $id The id of a specific target to view metadata + * @param $content (optional) If provided set the post_credits instead of displaying it + */ + public function actionPostcredits(int $id,$content=null) + { + if($id>0 && ($metadata = TargetMetadata::findOne($id))==null) + { + $this->stderr("Error: No record with given ID found!\n"); + return ExitCode::UNSPECIFIED_ERROR; + } + if($content!==null) + { + $metadata->post_credits=$content; + $metadata->save(); + } + echo $metadata->post_credits,"\n"; + } + + /** + * Get/Set target post exploitation details + * @param $id The id of a specific target to view metadata + * @param $content (optional) If provided set the post_exploitation instead of displaying it + */ + public function actionPostexploitation(int $id,$content=null) + { + if($id>0 && ($metadata = TargetMetadata::findOne($id))==null) + { + $this->stderr("Error: No record with given ID found!\n"); + return ExitCode::UNSPECIFIED_ERROR; + } + if($content!==null) + { + $metadata->post_exploitation=$content; + $metadata->save(); + } + echo $metadata->post_exploitation,"\n"; + } + + + /** + * Override for the command view paths + */ + public function getViewPath() + { + return Yii::getAlias('@app/views/console/metadata'); + } + +} \ No newline at end of file diff --git a/backend/views/console/metadata/view.php b/backend/views/console/metadata/view.php new file mode 100644 index 000000000..ef9c1b8da --- /dev/null +++ b/backend/views/console/metadata/view.php @@ -0,0 +1,27 @@ +# Metadata for target->name?> (ID: target_id?>) + +scenario):?> +## Scenario +scenario."\n"?> + +solution):?> +## Solution +solution."\n"?> + +pre_credits):?> +## Pre Credits +pre_credits."\n"?> + +post_credits):?> +## Post Credits +post_credits."\n"?> + +pre_exploitation):?> +## Pre Exploitation +pre_exploitation."\n"?> + +post_exploitation):?> +## Post Exploitation +post_exploitation."\n"?> + + diff --git a/docs/console-commands/Metadata.md b/docs/console-commands/Metadata.md new file mode 100644 index 000000000..0c4f1e7e5 --- /dev/null +++ b/docs/console-commands/Metadata.md @@ -0,0 +1,54 @@ +# Target Metadata commands +Perform operations on a given target metadata + +* Optional command line arguments are enclosed in `[]` +* Required command line arguments are enclosed in `<>` + +## View metadata details for a given target +Display the target metadata on screen as a markdown (no content transformation take place). + +Usage: `./backend/yii metadata/view ` + +Requires id or name of the target to display the metadata. + +## View/Update scenario +Display or update the metadata scenario field. + +Usage: `./backend/yii metadata/scenario [content]` + +If `content` is given update the existing otherwise view the existing value. + +## View/Update solution +Display or update the metadata solution field. + +Usage: `./backend/yii metadata/scenario [content]` + +If `content` is given update the existing otherwise view the existing value. + +## View/Update pre credits +Display or update the metadata pre credits field. + +Usage: `./backend/yii metadata/scenario [content]` + +If `content` is given update the existing otherwise view the existing value. + +## View/Update pre exploitation +Display or update the metadata pre exploitation details field. + +Usage: `./backend/yii metadata/scenario [content]` + +If `content` is given update the existing otherwise view the existing value. + +## View/Update post credits +Display or update the metadata post credits field. + +Usage: `./backend/yii metadata/scenario [content]` + +If `content` is given update the existing otherwise view the existing value. + +## View/Update post exploitation +Display or update the metadata post exploitation details field. + +Usage: `./backend/yii metadata/scenario [content]` + +If `content` is given update the existing otherwise view the existing value.