GNU/Linux >> Linux Esercitazione >  >> Linux

Come monitorare la sicurezza del server Linux con Osquery

Osquery è uno strumento di monitoraggio delle minacce alla sicurezza open source sviluppato da Facebook. Viene utilizzato per eseguire query sulle informazioni di sistema, inclusa la versione del sistema, le informazioni sul kernel, i processi in esecuzione, le informazioni sulla memoria, le porte in ascolto, gli utenti di accesso e altro ancora. Osquery aiuta gli amministratori di sistema a scrivere query SQL per identificare, sondare ed eliminare vari tipi di minacce. Supporta diversi sistemi operativi tra cui Windows, Linux, FreeBSD e macOS.

In questo post spiegheremo come installare e utilizzare Osquery su Ubuntu 20.04.

Prerequisiti

  • Un server Ubuntu 20.04 sulla piattaforma Atlantic.Net Cloud
  • Una password di root configurata sul tuo server

Fase 1:crea un server cloud Atlantic.Net

Per prima cosa, accedi al tuo server Atlantic.Net Cloud. Crea un nuovo server, scegliendo Ubuntu 20.04 come sistema operativo con almeno 2GB di RAM. Collegati al tuo Cloud Server tramite SSH e accedi utilizzando le credenziali evidenziate in alto nella pagina.

Dopo aver effettuato l'accesso al tuo server Ubuntu 20.04, esegui il comando seguente per aggiornare il tuo sistema di base con gli ultimi pacchetti disponibili.

apt-get update -y

Passaggio 2:installazione di Osquery

Innanzitutto, installa le dipendenze richieste utilizzando il comando seguente:

apt-get install gnupg2 software-properties-common wget unzip -y

Quindi, aggiungi la chiave GPG Osquery con il seguente comando:

export OSQUERY_KEY=1484120AC4E9F8A1A577AEEE97A80C63C9D8B80B
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $OSQUERY_KEY

Quindi, aggiungi il repository Osquery ad APT usando il seguente comando:

add-apt-repository 'deb [arch=amd64] https://pkg.osquery.io/deb deb main'

Una volta aggiunto il repository, aggiorna il repository e installa Osquery con il seguente comando:

apt-get update -y
apt-get install osquery -y

Fase 3:connessione a Osquery Console

Osquery fornisce una shell interattiva per eseguire query ed esplorare lo stato corrente del tuo sistema operativo.

Puoi connetterti alla shell Osquery usando il seguente comando:

osqueryi

Una volta connesso, dovresti ottenere il seguente output:

Using a virtual database. Need help, type '.help'
osquery>

Successivamente, visualizza le impostazioni predefinite di Osquery utilizzando il seguente comando:

osquery> .show

Dovresti vedere il seguente output:

osquery - being built, with love.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
osquery 4.9.0
using SQLite 3.35.5

General settings:
     Flagfile: 
       Config: filesystem (/etc/osquery/osquery.conf)
       Logger: filesystem (/var/log/osquery/)
  Distributed: tls
     Database: ephemeral
   Extensions: core
       Socket: /root/.osquery/shell.em

Shell settings:
         echo: off
      headers: on
         mode: pretty
    nullvalue: ""
       output: stdout
    separator: "|"
        width: 

Non-default flags/options:
  database_path: /root/.osquery/shell.db
  disable_database: true
  disable_events: true
  disable_logging: true
  disable_watchdog: true
  extensions_socket: /root/.osquery/shell.em
  hash_delay: 0
  logtostderr: true
  stderrthreshold: 0

Osquery ha molte tabelle disponibili per la query. Puoi elencarli con il seguente comando:

osquery> .tables

Esempio di output:

  => acpi_tables
  => apparmor_events
  => apparmor_profiles
  => apt_sources
  => arp_cache
  => atom_packages
  => augeas
  => authorized_keys
  => azure_instance_metadata
  => azure_instance_tags
  => block_devices
  => bpf_process_events
  => bpf_socket_events
  => carbon_black_info
  => carves
  => chrome_extension_content_scripts
  => chrome_extensions
  => cpu_time
  => cpuid
  => crontab
  => curl
  => curl_certificate
  => deb_packages
  => device_file
  => device_hash
  => device_partitions
  => disk_encryption
  => dns_resolvers
  => docker_container_fs_changes
  => docker_container_labels
  => docker_container_mounts
  => docker_container_networks
  => docker_container_ports 

Osquery ha diverse modalità per visualizzare l'output della query. Qui useremo la modalità linea per visualizzare l'output.

Per impostare la modalità linea, esegui il comando seguente:

osquery> .mode line

Fase 4 – Come usare Osquery

Per ottenere informazioni sul tuo sistema operativo in esecuzione, esegui:

osquery> SELECT * FROM system_info;

Esempio di output:

          hostname = ubuntu2004
              uuid = a83cffe2-50f4-4fea-9ef4-423853fdc122
          cpu_type = x86_64
       cpu_subtype = 6
         cpu_brand = QEMU Virtual CPU version 2.5+
cpu_physical_cores = 1
 cpu_logical_cores = 1
     cpu_microcode = 0x1
   physical_memory = 2084278272
   hardware_vendor = QEMU
    hardware_model = Standard PC (i440FX + PIIX, 1996)
  hardware_version = pc-i440fx-bionic
   hardware_serial = 
      board_vendor = 
       board_model = 
     board_version = 
      board_serial = 
     computer_name = ubuntu2004
    local_hostname = ubuntu2004

Per filtrare l'output delle informazioni di sistema, eseguire:

osquery> SELECT hostname, cpu_type, physical_memory, hardware_vendor, hardware_model FROM system_info;

Esempio di output:

       hostname = ubuntu2004
       cpu_type = x86_64
physical_memory = 2084278272
hardware_vendor = QEMU
 hardware_model = Standard PC (i440FX + PIIX, 1996)

Per visualizzare la versione del tuo sistema operativo, esegui:

osquery> SELECT * FROM os_version;

Esempio di output:

         name = Ubuntu
      version = 20.04 LTS (Focal Fossa)
        major = 20
        minor = 4
        patch = 0
        build = 
     platform = ubuntu
platform_like = debian
     codename = focal
         arch = x86_64

Per visualizzare le informazioni sul kernel, esegui:

osquery> SELECT * FROM kernel_info;

Esempio di output:

  version = 5.4.0-29-generic
arguments = ro net.ifnames=0 biosdevname=0 console=ttyS0 console=tty0
     path = /boot/vmlinuz-5.4.0-29-generic
   device = UUID=29a0b164-1ba1-45a7-b23a-cdb98f23edbc

Per visualizzare tutte le porte in ascolto con il nome del servizio e il PID, eseguire:

osquery> SELECT DISTINCT processes.name, listening_ports.port, processes.pid FROM listening_ports JOIN processes USING (pid) WHERE listening_ports.address = '0.0.0.0';

Esempio di output:

 name = unbound
 port = 53
  pid = 3407

 name = sshd
 port = 22
  pid = 649

 name = unbound
 port = 953
  pid = 3407

 name = darkstat
 port = 666
  pid = 6100

 name = darkstat
 port = 667
  pid = 6109

 name = apt-cacher-ng
 port = 3142
  pid = 4071

 name = ntpd
 port = 123
  pid = 483

Per visualizzare le informazioni per il pacchetto Apache, esegui:

osquery> SELECT name, version FROM deb_packages WHERE name="apache2";

Esempio di output:

+---------+-------------------+
| name    | version           |
+---------+-------------------+
| apache2 | 2.4.41-4ubuntu3.4 |
+---------+-------------------+

Per visualizzare le informazioni sulla memoria di sistema, eseguire:

osquery> SELECT * FROM memory_info;

Esempio di output:

+--------------+-------------+----------+-----------+-------------+-----------+-----------+------------+-----------+
| memory_total | memory_free | buffers  | cached    | swap_cached | active    | inactive  | swap_total | swap_free |
+--------------+-------------+----------+-----------+-------------+-----------+-----------+------------+-----------+
| 2084278272   | 1358233600  | 44519424 | 520896512 | 0           | 406622208 | 222449664 | 495411200  | 495411200 |
+--------------+-------------+----------+-----------+-------------+-----------+-----------+------------+-----------+

Per visualizzare le informazioni di tutte le interfacce di rete, eseguire:

osquery> SELECT * FROM interface_addresses;

Esempio di output:

+-----------+-------------------------------+-----------------------------------------+---------------+----------------+---------+
| interface | address                       | mask                                    | broadcast     | point_to_point | type    |
+-----------+-------------------------------+-----------------------------------------+---------------+----------------+---------+
| lo        | 127.0.0.1                     | 255.0.0.0                               |               | 127.0.0.1      | unknown |
| eth0      | 69.87.221.220                 | 255.255.255.0                           | 69.87.221.255 |                | unknown |
| lo        | ::1                           | ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff |               |                | unknown |
| eth0      | fe80::200:45ff:fe57:dddc%eth0 | ffff:ffff:ffff:ffff::                   |               |                | unknown |
+-----------+-------------------------------+-----------------------------------------+---------------+----------------+---------+

Per controllare il tempo di attività del sistema, esegui:

osquery> SELECT * FROM uptime;

Esempio di output:

+------+-------+---------+---------+---------------+
| days | hours | minutes | seconds | total_seconds |
+------+-------+---------+---------+---------------+
| 0    | 1     | 55      | 5       | 6905          |
+------+-------+---------+---------+---------------+

Per elencare tutti gli utenti il ​​cui UID è maggiore di 1000, esegui:

osquery> SELECT * FROM users WHERE uid>=1000;

Esempio di output:

+-------+-------+------------+------------+----------+-------------+--------------+-------------------+------+
| uid   | gid   | uid_signed | gid_signed | username | description | directory    | shell             | uuid |
+-------+-------+------------+------------+----------+-------------+--------------+-------------------+------+
| 65534 | 65534 | 65534      | 65534      | nobody   | nobody      | /nonexistent | /usr/sbin/nologin |      |
| 65534 | 65534 | 65534      | 65534      | nobody   | nobody      | /            | /usr/sbin/nologin |      |
+-------+-------+------------+------------+----------+-------------+--------------+-------------------+------+

Per controllare gli ultimi utenti che hanno effettuato l'accesso, esegui:

osquery> SELECT * FROM last;

Esempio di output:

+----------+-------+------+------+------------+-----------------+
| username | tty   | pid  | type | time       | host            |
+----------+-------+------+------+------------+-----------------+
| root     | pts/0 | 1013 | 7    | 1629008887 | 106.213.193.155 |
| root     | pts/1 | 3372 | 7    | 1629010656 | 106.213.193.155 |
| root     | pts/2 | 4158 | 7    | 1629013021 | 106.213.193.155 |
+----------+-------+------+------+------------+-----------------+

Per visualizzare tutti gli utenti che hanno effettuato l'accesso, esegui:

osquery> SELECT * FROM logged_in_users;

Esempio di output:

+-----------+----------+------------+------------------+------------+------+
| type      | user     | tty        | host             | time       | pid  |
+-----------+----------+------------+------------------+------------+------+
| boot_time | reboot   | ~          | 5.4.0-29-generic | 1629008369 | 0    |
| init      |          | /dev/tty1  |                  | 1629008378 | 491  |
| init      |          | /dev/ttyS0 |                  | 1629008378 | 484  |
| login     | LOGIN    | ttyS0      |                  | 1629008378 | 484  |
| login     | LOGIN    | tty1       |                  | 1629008378 | 491  |
| runlevel  | runlevel | ~          | 5.4.0-29-generic | 1629008383 | 53   |
| user      | root     | pts/0      | 106.213.193.155  | 1629008887 | 1013 |
| user      | root     | pts/1      | 106.213.193.155  | 1629010656 | 3372 |
| user      | root     | pts/2      | 106.213.193.155  | 1629013021 | 4158 |
+-----------+----------+------------+------------------+------------+------+

Conclusione

Nella guida precedente, abbiamo spiegato come installare e utilizzare Osquery per ottenere dati dal sistema operativo eseguendo query basate su SQL. È uno strumento molto utile e facile da usare per trovare backdoor, malware, processi zombie e altro ancora. Inizia oggi stesso con Osquery sul tuo VPS da Atlantic.Net!


Linux
  1. Come monitorare il tuo server Linux?

  2. 5 suggerimenti per iniziare con la sicurezza del server Linux

  3. Come monitorare le prestazioni del sistema Linux con Sysstat

  4. Come lavorare con il client SFTP in Linux – 10 comandi sftp

  5. Come registro il server Linux con il server DNS di Windows

Come monitorare i server Linux utilizzando CloudStats

Come monitorare l'utilizzo della rete con nload in Linux

Come gestire in remoto un server Linux con SSH

Come monitorare i tuoi server Linux con Checkmk

Come monitorare l'integrità del server Linux con gli strumenti della riga di comando

Come controllare i parametri del server VPS con il comando Linux