Ricerca…


introduzione

SQL injection è una tecnica di iniezione di codice, utilizzata per attaccare applicazioni basate sui dati, in cui nefaste istruzioni SQL vengono inserite in un campo di immissione per l'esecuzione (ad esempio per scaricare il contenuto del database sull'attaccante).

In questa sezione parleremo di questo e della sua relazione con la dichiarazione JDBC.

Istruzione e SQL Injection evil

Nota in questo esempio useremo DBMS PostgreSQL, ma puoi usare qualsiasi DBMS

Useremo un database bd_test witch che contiene uno Schema: sch_test e due tabelle users e test :

CREATE TABLE sch_test.users
(
  id serial NOT NULL,
  username character varying,
  password character varying,
  CONSTRAINT utilisateur_pkey PRIMARY KEY (id)
) 

CREATE TABLE sch_test.test
(
  id serial NOT NULL,
  "column" character varying
)

Login semplice usando Statement

static String DRIVER = "org.postgresql.Driver";
static String DB_USERNAME = "postgres";
static String DB_PASSWOR = "admin";
static String DB_URL = "jdbc:postgresql://localhost:5432/bd_test";

public static void sqlInjection() {
    try {
        Class.forName(DRIVER);
        Connection connection = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWOR);
        Statement statement = connection.createStatement();
        String username = "admin";
        String password = "admin";
        String query = "SELECT * FROM sch_test.users where username = '" 
                + username + "' and password = '" + password + "'";

        ResultSet result = statement.executeQuery(query);

        if (result.next()) {
            System.out.println("id = " + result.getInt("id") + " | username = "
                    + result.getString("username") + " | password = " + result.getString("password"));
        }else{
           System.out.println("Login not correct");
        }

    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
}

Fino ad ora ogni cosa è normale e sicura.

Accedi con nome utente e password falsi

L'hacker o il tester possono semplicemente accedere o elencare tutti i tuoi utenti utilizzando questo:

String username = " ' or ''='";
String password = " ' or ''='";

INSERISCI un nuovo utente

Puoi inserire dati nella tua tabella usando:

String username = "'; INSERT INTO sch_test.utilisateur(id, username, password) 
                        VALUES (2, 'hack1', 'hack2');--";
String password = "any";

CANCELLA Tutti gli utenti

considera che l'hacker conosce lo schema del tuo database in modo che possa cancellare tutti i tuoi utenti

String username = "'; DELETE FROM sch_test.utilisateur WHERE id>0;--";
String password = "any"; 

Utenti della tabella DROP

L'hacker può anche cancellare il tuo tavolo

String username = "'; drop table sch_test.table2;--";
String password = "any";

DROP DATABASE

Il peggio è abbandonare il database

String username = "'; DROP DATABASE bd_test;--";
String password = "any";

e ce ne sono molti altri.

Perché tutto questo?

Tutto questo perché l' Statement non è abbastanza sicura esegue la query come è, per questo si consiglia di utilizzare PreparedStatement invece, è più sicuro che Statement .

Potete trovare qui maggiori dettagli PreparedStatement



Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow