Skip to content

Commit

Permalink
Merge pull request #1115 from proditis/cli-metadata
Browse files Browse the repository at this point in the history
Cli metadata commands
  • Loading branch information
proditis authored Feb 21, 2024
2 parents 51e65ea + 637acb1 commit ea83404
Show file tree
Hide file tree
Showing 4 changed files with 323 additions and 53 deletions.
185 changes: 185 additions & 0 deletions backend/commands/MetadataController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php
namespace app\commands;

use Yii;
use yii\console\Controller;
use app\modules\infrastructure\models\TargetMetadata;
use yii\console\ExitCode;

class MetadataController extends Controller
{
public $defaultAction='view';

/**
* Delete a given target metadata
* @param $id The id of a specific target to delete metadata
* @param $name The name of a specific target to delete metadata
*/
public function actionDelete(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;
}
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');
}

}
110 changes: 57 additions & 53 deletions backend/commands/TargetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -121,70 +122,73 @@ public function actionRestart($uptime=1440, $limit=1)
}
} // end docker servers
}

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{
if($dopf!==false)
{
Pf::kill_table($dc->name,true);
Pf::kill_table($dc->name.'_clients',true);
}
} catch (\Exception $e) {}
$val->delete();
$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) { }
}
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
{
echo "OnDemand:", $e->getMessage(),"\n";
$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();
}
}
catch (\Exception $e)
{
echo "OnDemand:", $e->getMessage(),"\n";
}
}
}
27 changes: 27 additions & 0 deletions backend/views/console/metadata/view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Metadata for <?=$metadata->target->name?> (ID: <?=$metadata->target_id?>)

<?php if($metadata->scenario):?>
## Scenario
<?=$metadata->scenario."\n"?>
<?php endif;?>
<?php if($metadata->solution):?>
## Solution
<?=$metadata->solution."\n"?>
<?php endif;?>
<?php if($metadata->pre_credits):?>
## Pre Credits
<?=$metadata->pre_credits."\n"?>
<?php endif;?>
<?php if($metadata->post_credits):?>
## Post Credits
<?=$metadata->post_credits."\n"?>
<?php endif;?>
<?php if($metadata->pre_exploitation):?>
## Pre Exploitation
<?=$metadata->pre_exploitation."\n"?>
<?php endif;?>
<?php if($metadata->post_exploitation):?>
## Post Exploitation
<?=$metadata->post_exploitation."\n"?>
<?php endif;?>

Loading

0 comments on commit ea83404

Please sign in to comment.