Skip to content

Commit

Permalink
New. Scanner. Several weakspots curing implemented. Weakspots workwit…
Browse files Browse the repository at this point in the history
…h log implemented.
  • Loading branch information
alexandergull committed Dec 23, 2024
1 parent 11804fc commit e9af9d8
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 140 deletions.
2 changes: 1 addition & 1 deletion css/spbc-settings.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions css/src/spbc-settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

.spbc---gray{color: gray;}
.spbc---red{color: red;}
.spbc---green{color: #037603;}

.spbc_bold{font-weight: 800;}

Expand Down
20 changes: 10 additions & 10 deletions inc/spbc-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4407,11 +4407,11 @@ function spbc_list_table__get_args_by_type($table_type)
'if_empty_items' => __('There are no automatically cured files.', 'security-malware-firewall'),
'columns' => array(
'cb' => array('heading' => '<input type=checkbox>', 'class' => 'check-column', 'width_percent' => 2),
'real_path' => array('heading' => 'Path','primary' => true,),
'last_cure_date' => array('heading' => 'Cure date',),
'cured' => array('heading' => 'Status',),
'cci_cured' => array('heading' => 'Threats cured count',),
'fail_reason' => array('heading' => 'Reason of fail',),
'real_path' => array('heading' => 'Path','primary' => true, 'width_percent' => 27),
'last_cure_date' => array('heading' => 'Cure date', 'width_percent' => 10),
'cured' => array('heading' => 'Status', 'width_percent' => 13),
'weak_spots_cured' => array('heading' => 'Threats cured', 'width_percent' => 24),
'weak_spots_uncured' => array('heading' => 'Threats uncured', 'width_percent' => 24),
),
'order_by' => array('real_path' => 'asc'),
'pagination' => array(
Expand All @@ -4422,7 +4422,7 @@ function spbc_list_table__get_args_by_type($table_type)

$cure_log = new Scanner\CureLog\CureLog();
if ( !$cure_log->hasFailedCureTries() ) {
unset($args['columns']['fail_reason']);
unset($args['columns']['weak_spots_uncured']);
}
break;
case 'skipped':
Expand Down Expand Up @@ -5785,7 +5785,7 @@ function spbc_scanner__cure_log_data_prepare(&$table)
if ($table->items_count) {
foreach ($table->rows as $_key => $row) {
// Add Cure action if file was not cure
if ($row->cured !== 'FAILED') {
if ($row->cured === 'CURED') {
unset($row->actions['cure']);
}

Expand All @@ -5799,9 +5799,9 @@ function spbc_scanner__cure_log_data_prepare(&$table)
'actions' => $row->actions,
'real_path' => $row->real_path,
'last_cure_date' => $row->last_cure_date,
'cured' => $cure_status_string,
'cci_cured' => $row->cci_cured,
'fail_reason' => $row->fail_reason,
'cured' => $cure_status_string,
'weak_spots_cured' => $row->weak_spots_cured,
'weak_spots_uncured' => $row->weak_spots_uncured,
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/CleantalkSP/SpbctWP/DB/SQLSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ class SQLSchema extends \CleantalkSP\Common\DB\SQLSchema
array('field' => 'cured_hash', 'type' => 'char(32)', 'null' => 'no'),
array('field' => 'real_path', 'type' => 'varchar(512)', 'null' => 'no'),
array('field' => 'cured', 'type' => 'tinyint', 'null' => 'no',),
array('field' => 'weak_spots_cured', 'type' => 'varchar(512)', 'null' => 'no',),
array('field' => 'weak_spots_uncured', 'type' => 'varchar(512)', 'null' => 'no',),
array('field' => 'has_backup', 'type' => 'tinyint', 'null' => 'no',),
array('field' => 'cci_cured', 'type' => 'tinyint', 'null' => 'yes', 'default' => 'NULL'),
array('field' => 'fail_reason', 'type' => 'varchar(512)', 'null' => 'yes', 'default' => 'NULL'),
Expand Down
33 changes: 25 additions & 8 deletions lib/CleantalkSP/SpbctWP/Scanner/Cure.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,47 @@ class Cure

public $result = true;

public $weak_spots_result = array();

public function __construct($file)
{
global $wpdb;

$weak_spots = json_decode($file['weak_spots'], true);
$counter = 0;

if ( ! empty($weak_spots['SIGNATURES']) ) {
foreach ( $weak_spots['SIGNATURES'] as $_string => $signatures_in_string ) {
$this->weak_spots_result[$counter] = array(
'weak_spots_file_line' => $_string,
'signature_id' => '',
'cured' => 0,
'error' => ''
);
foreach ( $signatures_in_string as $signature_id ) {
$this->weak_spots_result[$counter]['signature_id'] = $signature_id;
$tmp = $wpdb->get_results(
'SELECT * FROM ' . SPBC_TBL_SCAN_SIGNATURES . ' WHERE id = "' . $signature_id . '"',
OBJECT
);
$this->signature = $tmp[0];

$result = $this->signatureCure($file, $this->signature);
if ( ! empty($result['error']) ) {
$this->result = $result;
return;
}
$result = $this->signatureCure($file, $this->signature, $this->weak_spots_result[$counter]);
$this->weak_spots_result[$counter]['cured'] = (int)$result;
}
$counter++;
}
} else {
$this->result = array('error' => 'COULD NOT GET SIGNATURE FROM DB');
}
}

public function signatureCure($file, $signature)
/**
* @param $file
* @param $signature
* @param array $weak_spots_log
* @return bool
*/
public function signatureCure($file, $signature, &$weak_spots_log)
{
global $spbc;

Expand Down Expand Up @@ -114,7 +127,8 @@ public function signatureCure($file, $signature)
$this->objects[] = $object;

if ( ! empty($result['error']) ) {
return $result;
$weak_spots_log['error'] = $result['error'];
return false;
}

if ( ! $spbc->settings['there_was_signature_treatment'] ) {
Expand All @@ -123,7 +137,10 @@ public function signatureCure($file, $signature)
}
}
}
return true;
}
$weak_spots_log['error'] = __('No cure instruction found for line.', 'security-malware-firewall');
return false;
}

/**
Expand Down
158 changes: 115 additions & 43 deletions lib/CleantalkSP/SpbctWP/Scanner/CureLog/CureLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,13 @@ public function getCountData()
return (int)$result->cnt;
}

/**
* Clear cure log table.
* @return void
* @psalm-suppress PossiblyUnusedMethod
*/
public function clearLogDataFromFailedCures()
{
$query = 'DELETE FROM ' . SPBC_TBL_CURE_LOG . ' WHERE cured <> 1';
$this->db->execute($query);
}

/**
* Check if there is failed cure tries
* @return bool
*/
public function hasFailedCureTries()
{
$query = 'SELECT COUNT(*) as cnt FROM ' . SPBC_TBL_CURE_LOG . ' WHERE cured = 0';
$query = 'SELECT COUNT(*) as cnt FROM ' . SPBC_TBL_CURE_LOG . ' WHERE cured <> 1';
$result = $this->db->fetch($query);
return (bool)$result->cnt;
}
Expand All @@ -56,19 +45,42 @@ public function getDataToAccordion($offset = 0, $amount = 20)
{
$offset = intval($offset);
$amount = intval($amount);
$query = 'SELECT fast_hash, real_path, cured, cci_cured, has_backup, fail_reason, last_cure_date FROM ' . SPBC_TBL_CURE_LOG . ' ORDER BY last_cure_date DESC LIMIT ' . $offset . ',' . $amount . ';';
$query = 'SELECT fast_hash, real_path, cured, cci_cured, weak_spots_cured, weak_spots_uncured, has_backup, fail_reason, last_cure_date FROM ' . SPBC_TBL_CURE_LOG . ' ORDER BY cured LIMIT ' . $offset . ',' . $amount . ';';
$result = $this->db->fetchAll($query, OBJECT);

if ( empty($result) ) {
return new \stdClass();
}

foreach ($result as $row) {
if ( isset($row->cured) && $row->cured == 1 ) {
$row->cured = 'CURED';
if ( isset($row->cured) ) {
if ( $row->cured == '1' ) {
$row->cured = 'CURED';
} else if ( $row->cured == '0' ) {
$row->cured = 'FAILED';
} else {
$row->cured = 'PARTIALLY CURED';
}
}

if ( !empty($row->weak_spots_cured) && is_string($row->weak_spots_cured) ) {
$template = '<div>';
$template .= self::getThreatTemplate($row->weak_spots_cured, true);
$template .= '</div>';
$row->weak_spots_cured = $template;
} else {
$row->weak_spots_cured = '-';
}

if ( !empty($row->weak_spots_uncured) && is_string($row->weak_spots_uncured) ) {
$template = '<div>';
$template .= self::getThreatTemplate($row->weak_spots_uncured, false);
$template .= '</div>';
$row->weak_spots_uncured = $template;
} else {
$row->cured = 'FAILED';
$row->weak_spots_uncured = '-';
}

if ( !empty($row->last_cure_date) ) {
$row->last_cure_date = date("M d Y H:i:s", $row->last_cure_date);
} else {
Expand All @@ -78,6 +90,48 @@ public function getDataToAccordion($offset = 0, $amount = 20)
return $result;
}

private static function getThreatTemplate($weak_spots_json, $is_cured = true)
{
$out = '';
$array = json_decode($weak_spots_json, true);
if ( !is_array($array) ) {
$array = array();
}

$details_class = $is_cured ? 'spbc-icon-ok spbc---green' : 'spbc-icon-cancel spbc---red';

$out .= count($array)
? '<span>' . __('Total', "security-malware-firewall") . ': <b>' . count($array) . '</b> </span>'
: __('-', "security-malware-firewall");


foreach ($array as $data) {
$line = isset($data['line']) ? $data['line'] : '';
$signature_id = isset($data['signature_id']) ? $data['signature_id'] : '';
$error = isset($data['error']) ? $data['error'] : '';
$out .= sprintf(
'
<div style="border-top: 1px dashed gray; border-bottom: 1px dashed gray; margin-bottom: 2px">
<p style="font-size: smaller; color: gray; margin: 2px 0"><span class="' . $details_class . '"></span>'
. __('Code line number', "security-malware-firewall") . ': %s, '
. __('SID', "security-malware-firewall") . ': %s
<br>
<p style="font-size: smaller; color: gray; min-height: 25px; max-height: 25px; margin: 2px 0;">
<b>'
. (!empty($error) ? __('Error:', "security-malware-firewall") : 'OK') . ' %s
</b>
</p>
</p>
</div>
',
$line,
$signature_id,
$error
);
}
return empty($out) ? '-' : $out;
}

/**
* Returns cure log data for PDF report
* @return array|object
Expand All @@ -94,11 +148,16 @@ public function getDataToPDF()
}

foreach ($result as &$row) {
if ( isset($row['cured']) && $row['cured'] == 1 ) {
$row['cured'] = 'CURED';
} else {
$row['cured'] = 'FAILED';
if ( isset($row->cured) ) {
if ( $row->cured == '1' ) {
$row['cured'] = 'CURED';
} else if ( $row->cured === '0' ) {
$row['cured'] = 'FAILED';
} else {
$row['cured'] = 'PARTIALLY CURED';
}
}

if ( !empty($row['last_cure_date']) ) {
$row['last_cure_date'] = date("M d Y H:i:s", $row['last_cure_date']);
} else {
Expand All @@ -123,8 +182,8 @@ public function logCureResult(CureLogRecord $cure_log_record)

$this->db->prepare(
'INSERT INTO ' . SPBC_TBL_CURE_LOG
. ' (`fast_hash`, `full_hash`, `cured_hash`, `real_path`, `cured`, `cci_cured`,`has_backup`,`fail_reason`, `last_cure_date`, `scanner_start_local_date`) VALUES'
. "(%s, %s, %s, %s, %d, %s, %d, %s, %d, %s)"
. ' (`fast_hash`, `full_hash`, `cured_hash`, `real_path`, `cured`, `weak_spots_cured`, `weak_spots_uncured`, `cci_cured`, `has_backup`,`fail_reason`, `last_cure_date`, `scanner_start_local_date`) VALUES'
. "(%s, %s, %s, %s, %d, %s, %s, %d, %d, %s, %d, %s)"
. 'ON DUPLICATE KEY UPDATE
cured = VALUES(`cured`),
last_cure_date = VALUES(`last_cure_date`),
Expand All @@ -133,40 +192,53 @@ public function logCureResult(CureLogRecord $cure_log_record)
cci_cured = VALUES(`cci_cured`),
has_backup = VALUES(`has_backup`),
fail_reason = VALUES(`fail_reason`),
weak_spots_cured = VALUES(`weak_spots_cured`),
weak_spots_uncured = VALUES(`weak_spots_uncured`),
last_cure_date = VALUES(`last_cure_date`)',
array($cure_log_record->fast_hash,
$cure_log_record->full_hash,
$cure_log_record->cured_hash,
$cure_log_record->real_path,
$cure_log_record->cured,
$cure_log_record->weak_spots_cured,
$cure_log_record->weak_spots_uncured,
$cure_log_record->cci_cured,
$cure_log_record->has_backup,
$cure_log_record->fail_reason,
$cure_log_record->last_cure_date,
$cure_log_record->scanner_start_local_date,
)
)->execute();
}

// $this->db->prepare(
// 'INSERT INTO ' . SPBC_TBL_CURE_LOG
// . ' (`fast_hash`, `full_hash`, `cured_hash`, `real_path`, `cured`, `cci_cured`,`has_backup`,`fail_reason`, `last_cure_date`, `scanner_start_local_date`) VALUES'
// . "(%s, %s, %s, %s, %d, %s, %d, %s, %d, %s)"
// . 'ON DUPLICATE KEY UPDATE
// cured = VALUES(`cured`),
// last_cure_date = VALUES(`last_cure_date`),
// fail_reason = VALUES(`fail_reason`),
// scanner_start_local_date = VALUES(`scanner_start_local_date`)',
// array($cure_log_record->fast_hash,
// $cure_log_record->full_hash,
// $cure_log_record->cured_hash,
// $cure_log_record->real_path,
// $cure_log_record->cured,
// $cure_log_record->cci_cured,
// $cure_log_record->has_backup,
// $cure_log_record->fail_reason,
// $cure_log_record->last_cure_date,
// $cure_log_record->scanner_start_local_date,
// )
// )->execute();
/**
* @param $weak_spots_result
* @return string[]
*/
public static function parseWeakSpotsFromCureResult($weak_spots_result)
{
$cured = array();
$uncured = array();
foreach ($weak_spots_result as $_counter => $data) {
$result_row = array(
'line' => isset($data['weak_spots_file_line']) ? $data['weak_spots_file_line'] : '',
'signature_id' => isset($data['signature_id']) ? $data['signature_id'] : '',
'error' => isset($data['error']) ? $data['error'] : '',
);
if (isset($data['cured']) && $data['cured'] === 1) {
$cured[] = $result_row;
} else {
$uncured[] = $result_row;
}
}
$cured = json_encode($cured);
$uncured = json_encode($uncured);
if (empty($cured)) {
$cured = '';
}
if (empty($cured)) {
$uncured = '';
}
return array('cured' => $cured, 'uncured' => $uncured);
}
}
Loading

0 comments on commit e9af9d8

Please sign in to comment.