Anti SQL Injection (I've tried everything but Havij still catches it)

3

Hello! I'm going through a lot of bugs, I've tried ALL the techniques I found on the internet that could prevent SQL Injection, and even then Havij can get my data.

This is the code for the page that I'm doing the attack:

$id = mysql_real_escape_string($_GET['id']);

$sql = $id;
$sql = preg_replace(sql_regcase("/(from|select|insert|delete|where|drop table|show tables|or|=|#|\*|--|\\)/"),"",$sql);
$sql = trim($sql);
$sql = strip_tags($sql);
$sql = addslashes($sql);
$id = $sql;
$id = intval($id);

// Connect to the database
$mysqli     = new mysqli($MySQL_HOST, $MySQL_USER, $MySQL_USER_PASS, $MySQL_DB) or die("Erro ao conectar ao bando de dados");   

// Prepare the query
$sql        = $mysqli->prepare('SELECT autor, noticia, data, titulo, tipo, logo FROM noticias WHERE id = ?') or die("erro ao preparar consulta");

// Bind the parameter, i --> int, datatype of column
$sql->bind_param('i', $id);

// Execute SQL
$sql->execute() or die("erro ao executar consulta");

$sql->bind_result($autor, $noticia, $data, $titulo, $tipo, $imagem);

$sql->fetch();

$sql->close();

I have tried to get the parameter directly from the sql query, changing it by a fixed number. I already tried to take the search line, remove the connection line, and even BOTH! The site does not even work but havij attacks like crazy! No point in explaining! And to make matters worse, I activated the sql logs thinking he might be picking up the connection alone and sending the prompts alone or through another page, and all the answer I have is that he does half a dozen searches on the table with id 999999, nothing different!. Now how does a program grab all my database with half a dozen SELECTs in an nonexistent id?

    
asked by anonymous 11.07.2015 / 02:20

1 answer

1

If you want to try to avoid a sql injection, try using a "stored procedure", so mysql will accept any sql code as a variable, regardless of what is typed.

My specialty is oracle, and at least in my case vulnerabilities go down a lot by using a stored procedure. In the case of Oracle, I use a NUMERIC parameter, equivalent to INT for input and a SYS_REFCURSOR parameter, which would be the query as output. As the procedure expects an integer as a parameter, it will give the procedure error if an sql is inserted and the function returns a failure. It seems to me that MySql does not need this SYS_REFCURSOR and returns the query as a result without needing the output variable

    
20.07.2015 / 16:26