new: [CLI] UserShell::ip_user command

pull/7776/head
Jakub Onderka 2021-09-27 15:01:53 +02:00
parent 9f6905be3e
commit 4c75854d07
2 changed files with 83 additions and 28 deletions

View File

@ -8,7 +8,31 @@ class AdminShell extends AppShell
{
public $uses = array('Event', 'Post', 'Attribute', 'Job', 'User', 'Task', 'Allowedlist', 'Server', 'Organisation', 'AdminSetting', 'Galaxy', 'Taxonomy', 'Warninglist', 'Noticelist', 'ObjectTemplate', 'Bruteforce', 'Role', 'Feed');
public $tasks = array('ConfigLoad');
public function getOptionParser()
{
$parser = parent::getOptionParser();
$parser->addSubcommand('updateJSON', array(
'help' => __('Update the JSON definitions of MISP.'),
));
$parser->addSubcommand('setSetting', [
'help' => __('Set setting in PHP config file.'),
'parser' => [
'arguments' => [
'name' => ['help' => __('Setting name'), 'required' => true],
'value' => ['help' => __('Setting value'), 'required' => true],
],
'options' => [
'force' => array(
'short' => 'f',
'help' => 'Force the command.',
'default' => false,
'boolean' => true
)
]
],
]);
return $parser;
}
public function jobGenerateCorrelation()
{
@ -330,7 +354,6 @@ class AdminShell extends AppShell
public function setSetting()
{
$this->ConfigLoad->execute();
$setting_name = !isset($this->args[0]) ? null : $this->args[0];
$value = !isset($this->args[1]) ? null : $this->args[1];
if ($value === 'false') {
@ -395,7 +418,9 @@ class AdminShell extends AppShell
public function getAuthkey()
{
$this->ConfigLoad->execute();
if (Configure::read("Security.advanced_authkeys")) {
$this->error('Advanced autkeys enabled, it is not possible to get user authkey.');
}
if (empty($this->args[0])) {
echo 'Invalid parameters. Usage: ' . APP . 'Console/cake Admin getAuthkey [user_email]' . PHP_EOL;
} else {
@ -425,7 +450,6 @@ class AdminShell extends AppShell
public function clearBruteforce()
{
$this->ConfigLoad->execute();
$conditions = array('Bruteforce.username !=' => '');
if (!empty($this->args[0])) {
$conditions = array('Bruteforce.username' => $this->args[0]);
@ -504,30 +528,6 @@ class AdminShell extends AppShell
echo 'Updated, new key:' . PHP_EOL . $authKey . PHP_EOL;
}
public function getOptionParser()
{
$this->ConfigLoad->execute();
$parser = parent::getOptionParser();
$parser->addSubcommand('updateJSON', array(
'help' => __('Update the JSON definitions of MISP.'),
'parser' => array(
'arguments' => array(
'update' => array('help' => __('Update the submodules before ingestion.'), 'short' => 'u', 'boolean' => 1)
)
)
));
$parser->addOption('force', array(
'short' => 'f',
'help' => 'Force the command.',
'default' => false,
'boolean' => true
));
return $parser;
}
public function recoverSinceLastSuccessfulUpdate()
{
$this->ConfigLoad->execute();
@ -673,6 +673,9 @@ class AdminShell extends AppShell
);
}
/**
* @deprecated Use UserShell instead
*/
public function IPUser()
{
$this->ConfigLoad->execute();

View File

@ -66,6 +66,17 @@ class UserShell extends AppShell
],
],
]);
$parser->addSubcommand('ip_user', [
'help' => __('Get user ID for user IP. If multiple users use the same IP, only last user ID will be returned.'),
'parser' => [
'arguments' => [
'ip' => ['help' => __('IPv4 or IPv6 address.'), 'required' => true],
],
'options' => [
'json' => ['help' => __('Output as JSON.'), 'boolean' => true],
],
],
]);
return $parser;
}
@ -182,6 +193,47 @@ class UserShell extends AppShell
}
}
public function ip_user()
{
list($ip) = $this->args;
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
$this->error("IP `$ip` is not valid IPv4 or IPv6 address");
}
if (empty(Configure::read('MISP.log_user_ips'))) {
$this->out('<warning>Storing user IP addresses is disabled.</warning>');
}
$userId = $this->User->setupRedisWithException()->get('misp:ip_user:' . $ip);
if (empty($userId)) {
$this->out('No hits.');
$this->_stop();
}
$user = $this->User->find('first', array(
'recursive' => -1,
'conditions' => array('User.id' => $userId),
'fields' => ['id', 'email'],
));
if (empty($user)) {
$this->error("User with ID $userId doesn't exists anymore.");
}
if ($this->params['json']) {
$this->out($this->json([
'ip' => $ip,
'id' => $user['User']['id'],
'email' => $user['User']['email'],
]));
} else {
$this->out(sprintf(
'%s==============================%sIP: %s%s==============================%sUser #%s: %s%s==============================%s',
PHP_EOL, PHP_EOL, $ip, PHP_EOL, PHP_EOL, $user['User']['id'], $user['User']['email'], PHP_EOL, PHP_EOL
));
}
}
/**
* @param string|int $userId
* @return array