prefix . 'svabg_' . $name; } public static function tables() { return array( 'findings', 'scans', 'log', 'lockouts', 'filehash', 'knowledge', 'links', 'traffic', 'scanlist', 'quarantine' ); } public static function install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); $findings = self::table( 'findings' ); $scans = self::table( 'scans' ); $log = self::table( 'log' ); $lockouts = self::table( 'lockouts' ); $filehash = self::table( 'filehash' ); $knowledge = self::table( 'knowledge' ); $links = self::table( 'links' ); $traffic = self::table( 'traffic' ); $scanlist = self::table( 'scanlist' ); $quarantine = self::table( 'quarantine' ); $sql = array(); $sql[] = "CREATE TABLE {$findings} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, scan_id bigint(20) unsigned NOT NULL DEFAULT 0, fingerprint char(32) NOT NULL DEFAULT '', category varchar(32) NOT NULL DEFAULT 'file', location text NOT NULL, line int(11) NOT NULL DEFAULT 0, severity varchar(16) NOT NULL DEFAULT 'medium', confidence tinyint(3) unsigned NOT NULL DEFAULT 50, signature varchar(64) NOT NULL DEFAULT '', title varchar(255) NOT NULL DEFAULT '', description text NOT NULL, snippet mediumtext NULL, status varchar(24) NOT NULL DEFAULT 'open', verdict varchar(32) NOT NULL DEFAULT 'suspicious', remediation varchar(48) NOT NULL DEFAULT '', ai_verdict varchar(32) NOT NULL DEFAULT '', ai_confidence tinyint(3) unsigned NOT NULL DEFAULT 0, ai_analysis longtext NULL, meta longtext NULL, file_hash char(32) NOT NULL DEFAULT '', created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', updated_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), UNIQUE KEY fingerprint (fingerprint), KEY scan_id (scan_id), KEY status (status), KEY severity (severity) ) {$charset};"; $sql[] = "CREATE TABLE {$scans} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, type varchar(16) NOT NULL DEFAULT 'manual', status varchar(16) NOT NULL DEFAULT 'running', started_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', finished_at datetime NULL, stats longtext NULL, summary longtext NULL, PRIMARY KEY (id), KEY status (status) ) {$charset};"; $sql[] = "CREATE TABLE {$log} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', level varchar(12) NOT NULL DEFAULT 'info', event varchar(48) NOT NULL DEFAULT '', ip varchar(45) NOT NULL DEFAULT '', user_id bigint(20) unsigned NOT NULL DEFAULT 0, message text NOT NULL, data longtext NULL, PRIMARY KEY (id), KEY created_at (created_at), KEY event (event), KEY ip (ip) ) {$charset};"; $sql[] = "CREATE TABLE {$lockouts} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, ip varchar(45) NOT NULL DEFAULT '', type varchar(16) NOT NULL DEFAULT 'login', reason varchar(255) NOT NULL DEFAULT '', hits int(11) NOT NULL DEFAULT 1, locked_until datetime NULL, created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), KEY ip (ip), KEY locked_until (locked_until) ) {$charset};"; $sql[] = "CREATE TABLE {$filehash} ( path_hash char(32) NOT NULL, path text NOT NULL, md5 char(32) NOT NULL DEFAULT '', size bigint(20) unsigned NOT NULL DEFAULT 0, mtime bigint(20) unsigned NOT NULL DEFAULT 0, scan_id bigint(20) unsigned NOT NULL DEFAULT 0, PRIMARY KEY (path_hash), KEY scan_id (scan_id) ) {$charset};"; $sql[] = "CREATE TABLE {$knowledge} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, name varchar(190) NOT NULL DEFAULT '', slug varchar(190) NOT NULL DEFAULT '', source varchar(16) NOT NULL DEFAULT 'imported', purpose varchar(16) NOT NULL DEFAULT 'malware', content longtext NULL, active tinyint(1) NOT NULL DEFAULT 1, created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), KEY slug (slug) ) {$charset};"; $sql[] = "CREATE TABLE {$links} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, url_hash char(32) NOT NULL DEFAULT '', url text NOT NULL, state varchar(16) NOT NULL DEFAULT 'pending', status smallint(5) unsigned NOT NULL DEFAULT 0, error varchar(255) NOT NULL DEFAULT '', redirect_to text NULL, sources longtext NULL, seen tinyint(1) NOT NULL DEFAULT 1, ignored tinyint(1) NOT NULL DEFAULT 0, checked_at datetime NULL, created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), UNIQUE KEY url_hash (url_hash), KEY state (state), KEY checked_at (checked_at) ) {$charset};"; $sql[] = "CREATE TABLE {$traffic} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', ip varchar(45) NOT NULL DEFAULT '', country char(2) NOT NULL DEFAULT '', method varchar(10) NOT NULL DEFAULT 'GET', uri varchar(1000) NOT NULL DEFAULT '', status smallint(5) unsigned NOT NULL DEFAULT 200, visitor varchar(10) NOT NULL DEFAULT 'human', action varchar(24) NOT NULL DEFAULT '', detail varchar(255) NOT NULL DEFAULT '', user_id bigint(20) unsigned NOT NULL DEFAULT 0, ua varchar(255) NOT NULL DEFAULT '', referer varchar(500) NOT NULL DEFAULT '', PRIMARY KEY (id), KEY created_at (created_at), KEY ip (ip), KEY action (action) ) {$charset};"; // The files a running scan still has to read (kept here, never in a file on the server). $sql[] = "CREATE TABLE {$scanlist} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, scan_id bigint(20) unsigned NOT NULL DEFAULT 0, path text NOT NULL, PRIMARY KEY (id), KEY scan_id (scan_id) ) {$charset};"; // Copies of quarantined files, compressed, so no copy of malicious code is ever written to the disk. $sql[] = "CREATE TABLE {$quarantine} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, finding_id bigint(20) unsigned NOT NULL DEFAULT 0, kind varchar(16) NOT NULL DEFAULT 'quarantined', path text NOT NULL, md5 char(32) NOT NULL DEFAULT '', size bigint(20) unsigned NOT NULL DEFAULT 0, content longtext NULL, created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), KEY finding_id (finding_id), KEY created_at (created_at) ) {$charset};"; // A fingerprint identifies one finding. Tables from before 1.1 had a plain index there, which dbDelta // cannot turn into a unique one by itself: only then remove duplicates and drop the old index. These // MySQL-only steps run only when an older install is upgraded, never on a new one, and print nothing. $old = get_option( 'svabg_db_version' ); if ( is_string( $old ) && version_compare( $old, '1.1.0', '<' ) && $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $findings ) ) ) { $quiet = $wpdb->suppress_errors( true ); self::migrate_lockouts( $lockouts ); $index = $wpdb->get_row( $wpdb->prepare( "SHOW INDEX FROM %i WHERE Key_name = 'fingerprint'", $findings ), ARRAY_A ); if ( $index && ! empty( $index['Non_unique'] ) ) { $wpdb->query( $wpdb->prepare( "DELETE f1 FROM %i f1 INNER JOIN %i f2 ON f1.fingerprint = f2.fingerprint AND f1.id < f2.id", $findings, $findings ) ); $wpdb->query( $wpdb->prepare( "ALTER TABLE %i DROP INDEX fingerprint", $findings ) ); } $wpdb->suppress_errors( $quiet ); } foreach ( $sql as $statement ) { dbDelta( $statement ); } // Only a complete install counts: a table that could not be created is retried later and shown to the // administrator, instead of being taken for done. $missing = array(); foreach ( self::tables() as $name ) { if ( ! $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', self::table( $name ) ) ) ) { $missing[] = self::table( $name ); } } if ( $missing ) { update_option( 'svabg_db_error', array( 'tables' => $missing, 'error' => (string) $wpdb->last_error, 'time' => time(), ), false ); return false; } delete_option( 'svabg_db_error' ); update_option( 'svabg_db_version', SOFTVOLT_AI_BODYGUARD_DB_VERSION, false ); return true; } /** * Until 1.5.0 the lockouts table had a column named "until", a reserved word in MySQL 8 and in the * SQLite layer of WordPress Playground: rename it, so dbDelta only has to check the table. * * @param string $lockouts Table name. */ private static function migrate_lockouts( $lockouts ) { global $wpdb; if ( ! $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $lockouts ) ) ) { return; } $columns = (array) $wpdb->get_col( $wpdb->prepare( "SHOW COLUMNS FROM %i", $lockouts ) ); if ( in_array( 'until', $columns, true ) && ! in_array( 'locked_until', $columns, true ) ) { if ( $wpdb->get_var( $wpdb->prepare( "SHOW INDEX FROM %i WHERE Key_name = 'until'", $lockouts ) ) ) { $wpdb->query( $wpdb->prepare( "ALTER TABLE %i DROP INDEX `until`", $lockouts ) ); } $wpdb->query( $wpdb->prepare( "ALTER TABLE %i CHANGE `until` locked_until datetime NULL", $lockouts ) ); } } /** * Tables that could not be created on this server (for an admin notice), or null. * * @return array|null */ public static function error() { $error = get_option( 'svabg_db_error' ); return is_array( $error ) && ! empty( $error['tables'] ) ? $error : null; } /** * Create a table that should exist but does not (for example after a failed upgrade). */ public static function install_if_missing( $name ) { global $wpdb; $table = self::table( $name ); if ( ! $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) ) { self::install(); } } public static function maybe_upgrade() { if ( get_option( 'svabg_db_version' ) === SOFTVOLT_AI_BODYGUARD_DB_VERSION ) { return; } // A failed install is tried again every ten minutes, not on every page load. $error = self::error(); if ( $error && time() - (int) $error['time'] < 10 * MINUTE_IN_SECONDS ) { return; } if ( ! self::install() ) { return; } self::drop_legacy_lists(); if ( method_exists( 'Softvolt_Ai_Bodyguard_Remediation', 'import_legacy_files' ) ) { Softvolt_Ai_Bodyguard_Remediation::import_legacy_files(); } } /** * Up to 1.5.0 the scan wrote its list of files into uploads; the list is a table now, so the old files go. * * @since 1.5.1 */ private static function drop_legacy_lists() { $upload = wp_upload_dir( null, false ); $parent = trailingslashit( wp_normalize_path( $upload['basedir'] ) ); // The old folder (IP lists, backups) moves to its random name now, so nothing looks for it there. if ( is_dir( $parent . Softvolt_Ai_Bodyguard_Helpers::LEGACY_FOLDER ) ) { Softvolt_Ai_Bodyguard_Helpers::work_dir(); } foreach ( array( Softvolt_Ai_Bodyguard_Helpers::LEGACY_FOLDER, Softvolt_Ai_Bodyguard_Helpers::WORK_FOLDER . '/*' ) as $folder ) { foreach ( (array) glob( $parent . $folder . '/scans/*.list*' ) as $file ) { wp_delete_file( $file ); } } } public static function drop() { global $wpdb; foreach ( self::tables() as $name ) { $table = self::table( $name ); $wpdb->query( $wpdb->prepare( "DROP TABLE IF EXISTS %i", $table ) ); } delete_option( 'svabg_db_version' ); } }