SlideShare a Scribd company logo
Juliano Atanazio
Neutralizando SQL Injection no PostgreSQLNeutralizando SQL Injection no PostgreSQL
Neutralizing SQL Injection in PostgreSQLNeutralizing SQL Injection in PostgreSQL
2/59
About me
Juliano Atanazio
● Graduated in Computer Science for Business Management (Informática para Gestão de
Negócios), FATEC Zona Sul, São Paulo – SP;
● PostgreSQL DBA;
● Linux admin;
● Instructor (PostgreSQL);
● LPIC-1, LPIC-2 Certified;
● Linux user since 2000;
● Free Software enthusiast;
● Favorite technologies: PostgreSQL, Linux, Python, Shell Script, FreeBSD, etc...;
● Headbanger :) m/
3/59
SQL Injection
Definition
SQL Injection is a method to introducing malicious SQL code to
get unauthorized access or even damage a system.
Definição
SQL Injection é um método para introduzir código SQL maligno
para obter acesso indevido ou mesmo danificar um sistema.
4/59
SQL Injection: Practice
$DBHOST enviroment variable to database server address:
Variável de ambiente $DBHOST para o endereço do servidor de
banco de dados:
$ read -p 'Type the database host address: ' DBHOST
Type the database host address:
Type the server address.
Digite o endereço do servidor.
5/59
SQL Injection: Practice
Database user with encrypted stored password, login permission,
no superuser:
Usuário de banco de dados com senha armazenada
criptografada, permissão de login, não superuser:
$ psql -U postgres -h ${DBHOST} -c 
"CREATE ROLE u_sql_injection 
ENCRYPTED PASSWORD 'secret' LOGIN NOSUPERUSER;"
6/59
SQL Injection: Practice
Database creation "db_sql_injection" with user "u_sql_injection"
as owner:
Criação de banco de dados "db_sql_injection" com o usuário
"u_sql_injection" como proprietário:
$ psql -U postgres -h ${DBHOST} -c 
"CREATE DATABASE db_sql_injection OWNER u_sql_injection;"
7/59
SQL Injection: Practice
Accessing the database via psql:
Acessando a base de dados via psql:
$ psql -U u_sql_injection db_sql_injection -h ${DBHOST}
8/59
SQL Injection: Practice
User table creation for the application (without hashing):
Criação de tabela de usuários para a aplicação (sem hashing):
> CREATE TABLE tb_user(
username varchar(50) PRIMARY KEY, -- natural primary key
password VARCHAR(72) NOT NULL);
Inserting a application user in the table:
Inserindo um usuário do aplicativo na tabela:
> INSERT INTO tb_user (username, password)
VALUES ('foo', 'mypassword');
9/59
SQL Injection: Practice
Script (1):
__________ sql_injection_1.py ___________________________
#_*_ encoding: utf-8 _*_
import getpass
user = input('User: ')
password = getpass.getpass('Password: ')
sql = """
SELECT TRUE FROM tb_user
WHERE username = '{}'
AND password = '{}';
""".format(user, password)
print('n{}'.format(sql))
____________________________________________________
10/59
SQL Injection: Practice
A simple test:
Um teste simples:
$ python3 sql_injection_1.py
User: foo
Password:
SELECT TRUE FROM tb_user
WHERE username = 'foo'
AND password = 'mypassword';
11/59
SQL Injection: About the Script
The script is pretty simple, does not yet have any interaction with
the database, but it serves to illustrate.
O script é bem simples, ainda não possui qualquer interação com
o banco de dados, mas serve para ilustrar.
12/59
SQL Injection: Practice
Script (2):
__________ sql_injection_2.py ___________________________
# _*_ encoding: utf-8 _*_
import getpass
import psycopg2
import sys
# DB server as first argument
dbhost = sys.argv[1]
# Connection string
conn_string = """
host='{}'
dbname='db_sql_injection'
user='u_sql_injection'
password='secret'
port='5432'
""".format(dbhost)
→
13/59
SQL Injection: Practice
Script (2):
__________ sql_injection_2.py ___________________________
try:
# Connection
conn = psycopg2.connect(conn_string)
# Cursor creation to execute SQL commands
cursor = conn.cursor()
# User input
user = input('User: ')
# Password input
password = getpass.getpass('Password: ')
→
14/59
SQL Injection: Practice
Script (2):
__________ sql_injection_2.py ___________________________
# SQL string
sql = """
SELECT TRUE FROM tb_user 
WHERE username = '{}' 
AND password = '{}';
""".format(user, password)
# Print the sql string after user and password input
print('{}n'.format(sql))
# Execute the SQL string in database
cursor.execute(sql)
# The result of the string SQL execution
res = cursor.fetchone()
→
15/59
SQL Injection: Practice
Script (2):
__________ sql_injection_2.py ___________________________
# User login validation
if res:
print('nAcessed!')
else:
print('nError: Invalid user and password combination!')
sys.exit(1)
except psycopg2.Error as e:
print('nAn error has occurred!n{}'.format(e))
# Close the database connection
conn.close()
____________________________________________________
16/59
SQL Injection: Practice
A simple test access with correct password:
Um teste simples de acesso com senha correta:
$ python3 sql_injection_2.py ${DBHOST}
User: foo
Password:
SELECT TRUE FROM tb_user
WHERE username = 'foo'
AND password = 'mypassword';
Acessed!
17/59
SQL Injection: Practice
A simple test access with wrong password:
Um teste simples de acesso com senha errada:
$ python3 sql_injection_2.py ${DBHOST}
User: foo
Password:
SELECT TRUE FROM tb_user
WHERE username = 'foo'
AND password = '123';
Error: Invalid user and password combination!
18/59
SQL Injection: Practice
Malicious code at user login input:
Código malicioso na entrada de login de usuário:
$ python3 sql_injection_2.py ${DBHOST}
User: ' OR 1 = 1; DROP TABLE tb_user; --
Password:
SELECT TRUE FROM tb_user
WHERE username = '' OR 1 = 1; DROP TABLE tb_user; –-'
AND password = '';
An error has occurred!
no results to fetch
Does the table has been deleted?
Será que a tabela foi apagada?
19/59
SQL Injection: Practice
Checking the table in the database:
Verificando a tabela na base de dados:
> SELECT TRUE FROM tb_user;
bool
------
t
Everithing is OK... for a while...
No commit...
Está tudo OK... por enquanto...
Sem efetivação...
20/59
SQL Injection: Practice
Malicious code at user login input (with COMMIT):
Código malicioso na entrada de login de usuário (com COMMIT):
$ python3 sql_injection.py
User: ' OR 1 = 1; DROP TABLE tb_user; COMMIT; --
Password:
SELECT TRUE FROM tb_user
WHERE username = '' OR 1 = 1; DROP TABLE tb_user; COMMIT; –-'
AND password = '';
An error has occurred!
no results to fetch
21/59
SQL Injection: Practice
Checking the table in the database:
Verificando a tabela na base de dados:
> SELECT TRUE FROM tb_user;
ERROR: relation "tb_user" does not exist
LINE 1: SELECT id FROM tb_user;
^
The table was dropped and must be created with the data again.
A tabela foi apagada e terá que ser criada com os dados
novamente.
:(
22/59
Dollar Quoting
It consists of a dollar sign ($), an optional “tag” of zero or more
characters, another dollar sign, an arbitrary sequence of
characters that makes up the string content, a dollar sign, the
same tag that began this dollar quote, and a dollar sign. For
example, here are two different ways to specify the string
“Dianne's horse” using dollar quoting:
Consiste de um caractere de dólar, uma “tag” opcional de zero ou
mais caracteres, outro caractere de dólar, uma sequência
arbitrária de caracteres que é o conteúdo da string, um caractere
de dólar, a mesma tag que começou o dollar quoting e um
caractere de dólar. Por exemplo, há duas maneiras diferentes de
especificar a string “Dianne's horse” usando dollar quoting:
$$Dianne's horse$$
$SomeTag$Dianne's horse$SomeTag$
23/59
Dollar Quoting
Dollar quoting is also a very nice feature to avoid SQL injection,
particularly when the application generates a random tag.
This tag must start with either a letter or with an underscore, the
rest can have underscore, letters or numbers.
Dollar quoting também é um recurso muito interessante para se
evitar SQL injection, principalmente quando a aplicação gera uma
tag aleatória.
Essa tag deve começar ou com uma letra ou com underscore, o
resto pode ter underscore, letras ou números.
24/59
Dollar Quoting: Practice
Script (3):
__________ sql_injection_3.py ___________________________
# _*_ encoding: utf-8 _*_
import getpass
import psycopg2
import sys
# DB server as first argument
dbhost = sys.argv[1]
# Connection string
conn_string = """
host='{}'
dbname='db_sql_injection'
user='u_sql_injection'
password='secret'
port='5432'
""".format(dbhost)
→
25/59
Dollar Quoting: Practice
Script (3):
__________ sql_injection_3.py ___________________________
try:
# Connection
conn = psycopg2.connect(conn_string)
# Cursor creation to execute SQL commands
cursor = conn.cursor()
# User input
user = input('User: ')
# Password input
password = getpass.getpass('Password: ')
→
26/59
Dollar Quoting: Practice
Script (3):
__________ sql_injection_3.py ___________________________
# SQL string
sql = """
SELECT TRUE FROM tb_user
WHERE username = $${}$$
AND password = $${}$$;
""".format(user, password)
# Print the sql string after user and password input
print('{}n'.format(sql))
# Execute the SQL string in database
cursor.execute(sql)
# The result of the string SQL execution
res = cursor.fetchone()
→
27/59
Dollar Quoting: Practice
Script (3):
__________ sql_injection_3.py ___________________________
# User login validation
if res:
print('nAcessed!n')
else:
print('nError: Invalid user and password combination!n')
sys.exit(1)
except psycopg2.Error as e:
print('nAn error has occurred!n{}'.format(e))
# Close the database connection
conn.close()
____________________________________________________
28/59
Dollar Quoting: Practice
Normal access:
Acesso normal:
$ python3 sql_injection_3.py ${DBHOST}
User: foo
Password:
SELECT TRUE FROM tb_user
WHERE username = $$foo$$
AND password = $$mypassword$$;
Acessed!
29/59
Dollar Quoting: Practice
Attempted malicious code (with apostrophe):
Tentativa de código malicioso (com apóstrofo):
$ python3 sql_injection_3.py ${DBHOST}
User: ' OR 1 = 1; DROP TABLE tb_user; COMMIT; --
Password:
SELECT TRUE FROM tb_user
WHERE username = $$' OR 1 = 1; DROP TABLE tb_user; COMMIT; --$$
AND password = $$$$;
Error: Invalid user and password combination!
Neutralized malicious code.
Código malicioso neutralizado.
30/59
Dollar Quoting: Practice
Attempted malicious code (with double dollar sign):
Tentativa de código malicioso (com dólar duplo):
$ python3 sql_injection_3.py ${DBHOST}
User: $$ OR 1 = 1; DROP TABLE tb_user; COMMIT; --
Password:
SELECT TRUE FROM tb_user
WHERE username = $$$$ OR 1 = 1; DROP TABLE tb_user; COMMIT; --$$
AND password = $$$$;
An error has occurred!
no results to fetch
31/59
Dollar Quoting: Practice
Checking the table in the database:
Verificando a tabela na base de dados:
> SELECT TRUE FROM tb_user;
ERROR: relation "tb_user" does not exist
LINE 1: SELECT id FROM tb_user;
^
The table was dropped and must be created with the data again.
A tabela foi apagada e terá que ser criada com os dados
novamente.
:(
32/59
Dollar Quoting: Practice
Script (4):
__________ sql_injection_4.py ___________________________
# _*_ encoding: utf-8 _*_
import getpass
import psycopg2
import sys
import string
import random
# DB server as first argument
dbhost = sys.argv[1]
→
33/59
Dollar Quoting: Practice
Script (4):
__________ sql_injection_4.py ___________________________
# Connection string
conn_string = """
host='{}'
dbname='db_sql_injection'
user='u_sql_injection'
password='secret'
port='5432'
""".format(dbhost)
→
34/59
Dollar Quoting: Practice
Script (4):
__________ sql_injection_4.py ___________________________
# Function: tag generator
def tag_gen(size):
first_char = '{}_'.format(string.ascii_letters)
last_chars = '{}{}'.format(string.digits, first_char)
tag = random.choice(first_char)
for i in range(size - 1):
tag = '{}{}'.format(tag, random.choice(last_chars))
return tag
# Tag for dollar quoting
tag = tag_gen(7)
→
35/59
Dollar Quoting: Practice
Script (4):
__________ sql_injection_4.py ___________________________
try:
# Connection
conn = psycopg2.connect(conn_string)
# Cursor creation to execute SQL commands
cursor = conn.cursor()
# User input
user = input('User: ')
# Password input
password = getpass.getpass('Password: ')
→
36/59
Dollar Quoting: Practice
Script (4):
__________ sql_injection_4.py ___________________________
# SQL string
sql = """
SELECT TRUE FROM tb_user
WHERE username = ${}${}${}$
AND password = ${}${}${}$;
""".format(tag, user, tag, tag, password, tag)
# Print the sql string after user and password input
print('{}n'.format(sql))
# Execute the SQL string in database
cursor.execute(sql)
# The result of the string SQL execution
res = cursor.fetchone()
→
37/59
Dollar Quoting: Practice
Script (4):
__________ sql_injection_4.py ___________________________
# User login validation
if res:
print('nAcessed!n')
else:
print('nError: Invalid user and password combination!n')
sys.exit(1)
except psycopg2.Error as e:
print('nAn error has occurred!n{}'.format(e))
# Close the database connection
conn.close()
____________________________________________________
38/59
Dollar Quoting: Practice
A simple test access with correct password:
Um teste simples de acesso com senha correta:
$ python3 sql_injection_4.py ${DBHOST}
User: foo
Password:
SELECT TRUE FROM tb_user
WHERE username = $PJPWqvS$foo$PJPWqvS$
AND password = $PJPWqvS$mypassword$PJPWqvS$;
Acessed!
39/59
Dollar Quoting: Practice
Attempted malicious code (with apostrophe):
Tentativa de código malicioso (com apóstrofo):
$ python3 sql_injection_4.py ${DBHOST}
User: ' OR 1 = 1; DROP TABLE tb_user; COMMIT; --
Password:
SELECT TRUE FROM tb_user
WHERE username = $EbVRSoG$' OR 1 = 1; DROP TABLE tb_user; COMMIT; --
$EbVRSoG$
AND password = $EbVRSoG$$EbVRSoG$;
Error: Invalid user and password combination!
Neutralized malicious code.
Código malicioso neutralizado.
40/59
Dollar Quoting: Practice
Attempted malicious code (with double dollar sign):
Tentativa de código malicioso (com dólar duplo):
$ python3 sql_injection_4.py ${DBHOST}
User: $$ OR 1 = 1; DROP TABLE tb_user; COMMIT; --
Password:
SELECT TRUE FROM tb_user
WHERE username = $Re7Gqwb$$$ OR 1 = 1; DROP TABLE tb_user; COMMIT; --
$Re7Gqwb$
AND password = $Re7Gqwb$$Re7Gqwb$;
Error: Invalid user and password combination!
Neutralized malicious code.
Código malicioso neutralizado.
41/59
Prepared Statement
A prepared statement is a server-side object that can be used to
optimize performance.
Um prepared statement (comando preparado) é um objeto do
lado do servidor que pode ser usado para otimizar performance.
When the PREPARE statement is executed, the statement is
analyzed, statistics collections are made (ANALYZE) and
rewritten.
Quando PREPARE statement é executado, o comando
(statement) é analisado, são feitas coletas de estatísticas
(ANALYZE) e reescrito.
42/59
Prepared Statement
When given an EXECUTE statement, the statement is planned
and prepared executed.
Quando é dado um comando EXECUTE, o prepared statement é
planejado e executado.
This division of labor prevents repetitive tasks of collecting
statistics, while allowing the execution plan depend on specific
parameters that can be provided.
Essa divisão de trabalho evita repetitivos trabalhos de coleta de
estatística, enquanto permite ao plano de execução de depender
de parâmetros específicos que podem ser fornecidos.
43/59
Prepared Statement
Steps / Etapas
Normal query:
Consulta normal:
1) Parser → 2) Rewrite System → 3) Planner / Optimizer → 4) Executor
Prepared Statement:
1) Planner / Optimizer → 2) Executor
44/59
Prepared Statement: Practice
Create a prepared statement:
Criar um prepared statement:
> PREPARE q_user(text, text) AS
SELECT TRUE FROM tb_user
WHERE username = $1
AND password = $2;
45/59
Prepared Statement: Practice
Execute a prepared statement:
Executar um prepared statement:
> EXECUTE q_user('foo', 'mypassword');
bool
------
t
46/59
Prepared Statement: Practice
Script (5):
__________ sql_injection_5.py ___________________________
# _*_ encoding: utf-8 _*_
import getpass
import psycopg2
import sys
# DB server as first argument
dbhost = sys.argv[1]
# Connection string
conn_string = """
host='{}'
dbname='db_sql_injection'
user='u_sql_injection'
password='secret'
port='5432'
""".format(dbhost)
→
47/59
Prepared Statement: Practice
Script (5):
__________ sql_injection_5.py ___________________________
try:
# Connection
conn = psycopg2.connect(conn_string)
# Cursor creation to execute SQL commands
cursor = conn.cursor()
# User input
user = input('User: ')
# Password input
password = getpass.getpass('Password: ')
→
48/59
Prepared Statement: Practice
Script (5):
__________ sql_injection_5.py ___________________________
# SQL string
sql = """
PREPARE q_user (text, text) AS
SELECT TRUE FROM tb_user
WHERE username = $1
AND password = $2;
"""
# Print the sql string after user and password input
print('{}n'.format(sql))
# Execute the SQL string in database
cursor.execute(sql)
→
49/59
Prepared Statement: Practice
Script (5):
__________ sql_injection_5.py ___________________________
# SQL string with EXECUTE
sql = "EXECUTE q_user('{}', '{}');".format(user, password)
# Print the SQL string
print('{}n'.format(sql))
# Execute the SQL string in database
cursor.execute(sql)
# The result of the string SQL execution
res = cursor.fetchone()
→
50/59
Prepared Statement: Practice
Script (5):
__________ sql_injection_5.py ___________________________
# User login validation
if res:
print('nAcessed!')
else:
print('nError: Invalid user and password combination!')
sys.exit(1)
except psycopg2.Error as e:
print('nAn error has occurred!n{}'.format(e))
# Close the database connection
conn.close()
____________________________________________________
51/59
Prepared Statement: Practice
A simple test access with correct password:
Um teste simples de acesso com senha correta:
$ python3 sql_injection_5.py ${DBHOST}
User: foo
Password:
PREPARE q_user (text, text) AS
SELECT TRUE FROM tb_user
WHERE username = $1
AND password = $2;
EXECUTE q_user('foo', 'mypassword');
Acessed!
52/59
Prepared Statement: Practice
A simple test access with wrong password:
Um teste simples de acesso com senha errada:
$ python3 sql_injection_5.py ${DBHOST}
User: foo
Password:
PREPARE q_user (text, text) AS
SELECT TRUE FROM tb_user
WHERE username = $1
AND password = $2;
EXECUTE q_user('foo', '123');
Error: Invalid user and password combination!
53/59
Prepared Statement: Practice
Attempted malicious code (with apostrophe):
Tentativa de código malicioso (com apóstrofo):
$ python3 sql_injection_5.py ${DBHOST}
User: ' OR 1 = 1; DROP TABLE tb_user; COMMIT; --
Password:
PREPARE q_user (text, text) AS
SELECT TRUE FROM tb_user
WHERE username = $1
AND password = $2;
EXECUTE q_user('' OR 1 = 1; DROP TABLE tb_user; COMMIT; --', '');
An error has occurred!
syntax error at or near ";"
LINE 1: EXECUTE q_user('' OR 1 = 1; DROP TABLE tb_user; COMMIT; --',...
^
Neutralized malicious code. / Código malicioso neutralizado
54/59
Prepared Statement: Practice
Attempted malicious code (with double dollar sign):
Tentativa de código malicioso (com dólar duplo):
$ python3 sql_injection_5.py ${DBHOST}
User: $$ OR 1 = 1; DROP TABLE tb_user; COMMIT; --
Password:
PREPARE q_user (text, text) AS
SELECT TRUE FROM tb_user
WHERE username = $1
AND password = $2;
EXECUTE q_user('$$ OR 1 = 1; DROP TABLE tb_user; COMMIT; --', '');
Error: Invalid user and password combination!
Neutralized malicious code. / Código malicioso neutralizado.
55/59
Conclusion / Conclusão
PostgreSQL has its own mechanisms against SQL injection which
makes it very independent of the application.
O PostgreSQL possui mecanismos próprios contra SQL injection
que o torna muito independente da aplicação.
56/59
Conclusion / Conclusão
This makes it easier for the application
developer, may delegate such tasks to the
database, avoiding technical adjustments
in the application and finally provide a
robust solution independent of language.
Isso facilita para o desenvolvedor da
aplicação, podendo confiar tais tarefas ao
banco de dados, evitando adaptações
técnicas na aplicação e por fim prover
uma solução robusta independente da
linguagem.
57/59
Donate!
The elephant needs you!
O Elefante precisa de você!
Contribute!
Contribua!
:)
http://www.postgresql.org/about/donate/
58/59
Save our planet!Save our planet!
59/59
See you soon!!!
Até a próxima!!!
Juliano Atanazio
juliano777@gmail.com
http://slideshare.net/spjuliano
https://speakerdeck.com/julianometalsp
https://juliano777.wordpress.com
:)
Ad

Recommended

Introduction To Ethical Hacking
Introduction To Ethical Hacking
Raghav Bisht
 
Cyber Kill Chain サイバーキルチェーン
Cyber Kill Chain サイバーキルチェーン
shuna roo
 
Threat Hunting
Threat Hunting
Splunk
 
PowerShell Inside Out: Applied .NET Hacking for Enhanced Visibility by Satosh...
PowerShell Inside Out: Applied .NET Hacking for Enhanced Visibility by Satosh...
CODE BLUE
 
Web hacking 1.0
Web hacking 1.0
Q Fadlan
 
password cracking using John the ripper, hashcat, Cain&abel
password cracking using John the ripper, hashcat, Cain&abel
Shweta Sharma
 
Unityティーチャートレーニングデイ -認定プログラマー編-
Unityティーチャートレーニングデイ -認定プログラマー編-
Unity Technologies Japan K.K.
 
Maltego Webinar Slides
Maltego Webinar Slides
ThreatConnect
 
Tools for Open Source Intelligence (OSINT)
Tools for Open Source Intelligence (OSINT)
Sudhanshu Chauhan
 
Docker国内外本番環境サービス事例のご紹介
Docker国内外本番環境サービス事例のご紹介
ThinkIT_impress
 
One Shellcode to Rule Them All: Cross-Platform Exploitation
One Shellcode to Rule Them All: Cross-Platform Exploitation
Quinn Wilton
 
Advanced SQL injection to operating system full control (slides)
Advanced SQL injection to operating system full control (slides)
Bernardo Damele A. G.
 
Osint presentation nov 2019
Osint presentation nov 2019
Priyanka Aash
 
RISC-Vのセキュリティ技術(TEE, Root of Trust, Remote Attestation)
RISC-Vのセキュリティ技術(TEE, Root of Trust, Remote Attestation)
Kuniyasu Suzaki
 
Stripeを使った簡単なサブスク型課金サービスの作り方【WESEEK Tech Conf #15】
Stripeを使った簡単なサブスク型課金サービスの作り方【WESEEK Tech Conf #15】
WESEEKWESEEK
 
Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8
AppDynamics
 
HADOにおけるUniRxのObjectPool
HADOにおけるUniRxのObjectPool
Yasuyuki Kado
 
関数プログラミング入門
関数プログラミング入門
masatora atarashi
 
スマホゲームのチート手法とその対策 [DeNA TechCon 2019]
スマホゲームのチート手法とその対策 [DeNA TechCon 2019]
DeNA
 
PHP版レガシーコード改善に役立つ新パターン #wewlc_jp
PHP版レガシーコード改善に役立つ新パターン #wewlc_jp
Yahoo!デベロッパーネットワーク
 
犬でもわかる公開鍵暗号
犬でもわかる公開鍵暗号
akakou
 
実践 NestJS
実践 NestJS
Ayumi Goto
 
今さら聞けないバックアップの基礎
今さら聞けないバックアップの基礎
富士通クラウドテクノロジーズ株式会社
 
Introduction To Exploitation & Metasploit
Introduction To Exploitation & Metasploit
Raghav Bisht
 
iOS Application Static Analysis - Deepika Kumari.pptx
iOS Application Static Analysis - Deepika Kumari.pptx
deepikakumari643428
 
OSINT: Open Source Intelligence gathering
OSINT: Open Source Intelligence gathering
Jeremiah Tillman
 
[CB19] S-TIP: サイバー脅威インテリジェンスのシームレスな活用プラットフォーム by 山田 幸治, 里見 敏孝
[CB19] S-TIP: サイバー脅威インテリジェンスのシームレスな活用プラットフォーム by 山田 幸治, 里見 敏孝
CODE BLUE
 
Vapt life cycle
Vapt life cycle
penetration Tester
 
SQL Injection in PHP
SQL Injection in PHP
Dave Ross
 
Sql Injection Tutorial!
Sql Injection Tutorial!
ralphmigcute
 

More Related Content

What's hot (20)

Tools for Open Source Intelligence (OSINT)
Tools for Open Source Intelligence (OSINT)
Sudhanshu Chauhan
 
Docker国内外本番環境サービス事例のご紹介
Docker国内外本番環境サービス事例のご紹介
ThinkIT_impress
 
One Shellcode to Rule Them All: Cross-Platform Exploitation
One Shellcode to Rule Them All: Cross-Platform Exploitation
Quinn Wilton
 
Advanced SQL injection to operating system full control (slides)
Advanced SQL injection to operating system full control (slides)
Bernardo Damele A. G.
 
Osint presentation nov 2019
Osint presentation nov 2019
Priyanka Aash
 
RISC-Vのセキュリティ技術(TEE, Root of Trust, Remote Attestation)
RISC-Vのセキュリティ技術(TEE, Root of Trust, Remote Attestation)
Kuniyasu Suzaki
 
Stripeを使った簡単なサブスク型課金サービスの作り方【WESEEK Tech Conf #15】
Stripeを使った簡単なサブスク型課金サービスの作り方【WESEEK Tech Conf #15】
WESEEKWESEEK
 
Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8
AppDynamics
 
HADOにおけるUniRxのObjectPool
HADOにおけるUniRxのObjectPool
Yasuyuki Kado
 
関数プログラミング入門
関数プログラミング入門
masatora atarashi
 
スマホゲームのチート手法とその対策 [DeNA TechCon 2019]
スマホゲームのチート手法とその対策 [DeNA TechCon 2019]
DeNA
 
PHP版レガシーコード改善に役立つ新パターン #wewlc_jp
PHP版レガシーコード改善に役立つ新パターン #wewlc_jp
Yahoo!デベロッパーネットワーク
 
犬でもわかる公開鍵暗号
犬でもわかる公開鍵暗号
akakou
 
実践 NestJS
実践 NestJS
Ayumi Goto
 
今さら聞けないバックアップの基礎
今さら聞けないバックアップの基礎
富士通クラウドテクノロジーズ株式会社
 
Introduction To Exploitation & Metasploit
Introduction To Exploitation & Metasploit
Raghav Bisht
 
iOS Application Static Analysis - Deepika Kumari.pptx
iOS Application Static Analysis - Deepika Kumari.pptx
deepikakumari643428
 
OSINT: Open Source Intelligence gathering
OSINT: Open Source Intelligence gathering
Jeremiah Tillman
 
[CB19] S-TIP: サイバー脅威インテリジェンスのシームレスな活用プラットフォーム by 山田 幸治, 里見 敏孝
[CB19] S-TIP: サイバー脅威インテリジェンスのシームレスな活用プラットフォーム by 山田 幸治, 里見 敏孝
CODE BLUE
 
Vapt life cycle
Vapt life cycle
penetration Tester
 
Tools for Open Source Intelligence (OSINT)
Tools for Open Source Intelligence (OSINT)
Sudhanshu Chauhan
 
Docker国内外本番環境サービス事例のご紹介
Docker国内外本番環境サービス事例のご紹介
ThinkIT_impress
 
One Shellcode to Rule Them All: Cross-Platform Exploitation
One Shellcode to Rule Them All: Cross-Platform Exploitation
Quinn Wilton
 
Advanced SQL injection to operating system full control (slides)
Advanced SQL injection to operating system full control (slides)
Bernardo Damele A. G.
 
Osint presentation nov 2019
Osint presentation nov 2019
Priyanka Aash
 
RISC-Vのセキュリティ技術(TEE, Root of Trust, Remote Attestation)
RISC-Vのセキュリティ技術(TEE, Root of Trust, Remote Attestation)
Kuniyasu Suzaki
 
Stripeを使った簡単なサブスク型課金サービスの作り方【WESEEK Tech Conf #15】
Stripeを使った簡単なサブスク型課金サービスの作り方【WESEEK Tech Conf #15】
WESEEKWESEEK
 
Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8
AppDynamics
 
HADOにおけるUniRxのObjectPool
HADOにおけるUniRxのObjectPool
Yasuyuki Kado
 
関数プログラミング入門
関数プログラミング入門
masatora atarashi
 
スマホゲームのチート手法とその対策 [DeNA TechCon 2019]
スマホゲームのチート手法とその対策 [DeNA TechCon 2019]
DeNA
 
犬でもわかる公開鍵暗号
犬でもわかる公開鍵暗号
akakou
 
Introduction To Exploitation & Metasploit
Introduction To Exploitation & Metasploit
Raghav Bisht
 
iOS Application Static Analysis - Deepika Kumari.pptx
iOS Application Static Analysis - Deepika Kumari.pptx
deepikakumari643428
 
OSINT: Open Source Intelligence gathering
OSINT: Open Source Intelligence gathering
Jeremiah Tillman
 
[CB19] S-TIP: サイバー脅威インテリジェンスのシームレスな活用プラットフォーム by 山田 幸治, 里見 敏孝
[CB19] S-TIP: サイバー脅威インテリジェンスのシームレスな活用プラットフォーム by 山田 幸治, 里見 敏孝
CODE BLUE
 

Viewers also liked (20)

SQL Injection in PHP
SQL Injection in PHP
Dave Ross
 
Sql Injection Tutorial!
Sql Injection Tutorial!
ralphmigcute
 
SQL Injection - The Unknown Story
SQL Injection - The Unknown Story
Imperva
 
Defcon 17-joseph mccray-adv-sql_injection
Defcon 17-joseph mccray-adv-sql_injection
Ahmed AbdelSatar
 
Blind SQL Injection - Optimization Techniques
Blind SQL Injection - Optimization Techniques
guest54de52
 
Sql Injection and XSS
Sql Injection and XSS
Mike Crabb
 
Sql Injection Attacks Siddhesh
Sql Injection Attacks Siddhesh
Siddhesh Bhobe
 
Advanced SQL Injection: Attacks
Advanced SQL Injection: Attacks
Nuno Loureiro
 
Understanding and preventing sql injection attacks
Understanding and preventing sql injection attacks
Kevin Kline
 
Web Security - OWASP - SQL injection & Cross Site Scripting XSS
Web Security - OWASP - SQL injection & Cross Site Scripting XSS
Ivan Ortega
 
Sql Injection and Entity Frameworks
Sql Injection and Entity Frameworks
Rich Helton
 
D:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql Injection
avishkarm
 
ShmooCON 2009 : Re-playing with (Blind) SQL Injection
ShmooCON 2009 : Re-playing with (Blind) SQL Injection
Chema Alonso
 
Web application attacks using Sql injection and countermasures
Web application attacks using Sql injection and countermasures
Cade Zvavanjanja
 
Google Dorks and SQL Injection
Google Dorks and SQL Injection
Mudassir Hassan Khan
 
SQL INJECTION
SQL INJECTION
Anoop T
 
Database security issues
Database security issues
n|u - The Open Security Community
 
SQL Injection
SQL Injection
Adhoura Academy
 
Sql injection
Sql injection
Pallavi Biswas
 
Incas, maias e astecas
Incas, maias e astecas
Guilherme De Martini
 
SQL Injection in PHP
SQL Injection in PHP
Dave Ross
 
Sql Injection Tutorial!
Sql Injection Tutorial!
ralphmigcute
 
SQL Injection - The Unknown Story
SQL Injection - The Unknown Story
Imperva
 
Defcon 17-joseph mccray-adv-sql_injection
Defcon 17-joseph mccray-adv-sql_injection
Ahmed AbdelSatar
 
Blind SQL Injection - Optimization Techniques
Blind SQL Injection - Optimization Techniques
guest54de52
 
Sql Injection and XSS
Sql Injection and XSS
Mike Crabb
 
Sql Injection Attacks Siddhesh
Sql Injection Attacks Siddhesh
Siddhesh Bhobe
 
Advanced SQL Injection: Attacks
Advanced SQL Injection: Attacks
Nuno Loureiro
 
Understanding and preventing sql injection attacks
Understanding and preventing sql injection attacks
Kevin Kline
 
Web Security - OWASP - SQL injection & Cross Site Scripting XSS
Web Security - OWASP - SQL injection & Cross Site Scripting XSS
Ivan Ortega
 
Sql Injection and Entity Frameworks
Sql Injection and Entity Frameworks
Rich Helton
 
D:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql Injection
avishkarm
 
ShmooCON 2009 : Re-playing with (Blind) SQL Injection
ShmooCON 2009 : Re-playing with (Blind) SQL Injection
Chema Alonso
 
Web application attacks using Sql injection and countermasures
Web application attacks using Sql injection and countermasures
Cade Zvavanjanja
 
SQL INJECTION
SQL INJECTION
Anoop T
 
Ad

Similar to Neutralizing SQL Injection in PostgreSQL (20)

5-databasevuln.pdf
5-databasevuln.pdf
SalmanAlfarizhi2
 
Chapter 14 sql injection
Chapter 14 sql injection
newbie2019
 
PHP - Introduction to Advanced SQL
PHP - Introduction to Advanced SQL
Vibrant Technologies & Computers
 
Sql injection
Sql injection
Nitish Kumar
 
SQL Injection Tutorial
SQL Injection Tutorial
Magno Logan
 
SQL Injections - 2016 - Huntington Beach
SQL Injections - 2016 - Huntington Beach
Jeff Prom
 
Sql Injection Adv Owasp
Sql Injection Adv Owasp
Aung Khant
 
Advanced SQL Injection
Advanced SQL Injection
amiable_indian
 
SQL INJECTION
SQL INJECTION
Ziaullah Khan
 
SQL Injection in action with PHP and MySQL
SQL Injection in action with PHP and MySQL
Pradeep Kumar
 
Sql injection
Sql injection
Safwan Hashmi
 
A Brief Introduction About Sql Injection in PHP and MYSQL
A Brief Introduction About Sql Injection in PHP and MYSQL
kobaitari
 
Sql injection
Sql injection
Nikunj Dhameliya
 
Sql injection course made by Cristian Alexandrescu
Sql injection course made by Cristian Alexandrescu
Cristian Alexandrescu
 
Sql injection
Sql injection
Mehul Boghra
 
SQL Injection 101 : It is not just about ' or '1'='1 - Pichaya Morimoto
SQL Injection 101 : It is not just about ' or '1'='1 - Pichaya Morimoto
Pichaya Morimoto
 
03. sql and other injection module v17
03. sql and other injection module v17
Eoin Keary
 
Sq linjection
Sq linjection
Mahesh Gupta (DBATAG) - SQL Server Consultant
 
SQL Injection
SQL Injection
Magno Logan
 
Sql injection attack
Sql injection attack
RajKumar Rampelli
 
Chapter 14 sql injection
Chapter 14 sql injection
newbie2019
 
SQL Injection Tutorial
SQL Injection Tutorial
Magno Logan
 
SQL Injections - 2016 - Huntington Beach
SQL Injections - 2016 - Huntington Beach
Jeff Prom
 
Sql Injection Adv Owasp
Sql Injection Adv Owasp
Aung Khant
 
Advanced SQL Injection
Advanced SQL Injection
amiable_indian
 
SQL Injection in action with PHP and MySQL
SQL Injection in action with PHP and MySQL
Pradeep Kumar
 
A Brief Introduction About Sql Injection in PHP and MYSQL
A Brief Introduction About Sql Injection in PHP and MYSQL
kobaitari
 
Sql injection course made by Cristian Alexandrescu
Sql injection course made by Cristian Alexandrescu
Cristian Alexandrescu
 
SQL Injection 101 : It is not just about ' or '1'='1 - Pichaya Morimoto
SQL Injection 101 : It is not just about ' or '1'='1 - Pichaya Morimoto
Pichaya Morimoto
 
03. sql and other injection module v17
03. sql and other injection module v17
Eoin Keary
 
Ad

More from Juliano Atanazio (9)

PL/Python: Programando em Python no PostgreSQL
PL/Python: Programando em Python no PostgreSQL
Juliano Atanazio
 
Por que Python? Vamos Conhecer? Vamos Aprender?
Por que Python? Vamos Conhecer? Vamos Aprender?
Juliano Atanazio
 
Por que FreeBSD?
Por que FreeBSD?
Juliano Atanazio
 
PostgreSQL: How to Store Passwords Safely
PostgreSQL: How to Store Passwords Safely
Juliano Atanazio
 
Postgresql + Python = Power!
Postgresql + Python = Power!
Juliano Atanazio
 
Boas praticas em um Projeto de Banco de Dados
Boas praticas em um Projeto de Banco de Dados
Juliano Atanazio
 
Por que PostgreSQL?
Por que PostgreSQL?
Juliano Atanazio
 
Full Text Search - Busca Textual no PostgreSQL
Full Text Search - Busca Textual no PostgreSQL
Juliano Atanazio
 
Gerenciamento de Backups PostgreSQL com pgbarman
Gerenciamento de Backups PostgreSQL com pgbarman
Juliano Atanazio
 
PL/Python: Programando em Python no PostgreSQL
PL/Python: Programando em Python no PostgreSQL
Juliano Atanazio
 
Por que Python? Vamos Conhecer? Vamos Aprender?
Por que Python? Vamos Conhecer? Vamos Aprender?
Juliano Atanazio
 
PostgreSQL: How to Store Passwords Safely
PostgreSQL: How to Store Passwords Safely
Juliano Atanazio
 
Postgresql + Python = Power!
Postgresql + Python = Power!
Juliano Atanazio
 
Boas praticas em um Projeto de Banco de Dados
Boas praticas em um Projeto de Banco de Dados
Juliano Atanazio
 
Full Text Search - Busca Textual no PostgreSQL
Full Text Search - Busca Textual no PostgreSQL
Juliano Atanazio
 
Gerenciamento de Backups PostgreSQL com pgbarman
Gerenciamento de Backups PostgreSQL com pgbarman
Juliano Atanazio
 

Recently uploaded (20)

Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
joybepari360
 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
 
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
 
Advanced Token Development - Decentralized Innovation
Advanced Token Development - Decentralized Innovation
arohisinghas720
 
Emvigo Capability Deck 2025: Accelerating Innovation Through Intelligent Soft...
Emvigo Capability Deck 2025: Accelerating Innovation Through Intelligent Soft...
Emvigo Technologies
 
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
Transmission Media. (Computer Networks)
Transmission Media. (Computer Networks)
S Pranav (Deepu)
 
How Insurance Policy Management Software Streamlines Operations
How Insurance Policy Management Software Streamlines Operations
Insurance Tech Services
 
FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025
Safe Software
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Insurance Underwriting Software Enhancing Accuracy and Efficiency
Insurance Underwriting Software Enhancing Accuracy and Efficiency
Insurance Tech Services
 
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Intelli grow
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
SAP PM Module Level-IV Training Complete.ppt
SAP PM Module Level-IV Training Complete.ppt
MuhammadShaheryar36
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
Zoneranker’s Digital marketing solutions
Zoneranker’s Digital marketing solutions
reenashriee
 
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
Smadav Pro 2025 Rev 15.4 Crack Full Version With Registration Key
joybepari360
 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
 
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
 
Advanced Token Development - Decentralized Innovation
Advanced Token Development - Decentralized Innovation
arohisinghas720
 
Emvigo Capability Deck 2025: Accelerating Innovation Through Intelligent Soft...
Emvigo Capability Deck 2025: Accelerating Innovation Through Intelligent Soft...
Emvigo Technologies
 
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
Transmission Media. (Computer Networks)
Transmission Media. (Computer Networks)
S Pranav (Deepu)
 
How Insurance Policy Management Software Streamlines Operations
How Insurance Policy Management Software Streamlines Operations
Insurance Tech Services
 
FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025
Safe Software
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Insurance Underwriting Software Enhancing Accuracy and Efficiency
Insurance Underwriting Software Enhancing Accuracy and Efficiency
Insurance Tech Services
 
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Smart Financial Solutions: Money Lender Software, Daily Pigmy & Personal Loan...
Intelli grow
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
SAP PM Module Level-IV Training Complete.ppt
SAP PM Module Level-IV Training Complete.ppt
MuhammadShaheryar36
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 

Neutralizing SQL Injection in PostgreSQL

  • 1. Juliano Atanazio Neutralizando SQL Injection no PostgreSQLNeutralizando SQL Injection no PostgreSQL Neutralizing SQL Injection in PostgreSQLNeutralizing SQL Injection in PostgreSQL
  • 2. 2/59 About me Juliano Atanazio ● Graduated in Computer Science for Business Management (Informática para Gestão de Negócios), FATEC Zona Sul, São Paulo – SP; ● PostgreSQL DBA; ● Linux admin; ● Instructor (PostgreSQL); ● LPIC-1, LPIC-2 Certified; ● Linux user since 2000; ● Free Software enthusiast; ● Favorite technologies: PostgreSQL, Linux, Python, Shell Script, FreeBSD, etc...; ● Headbanger :) m/
  • 3. 3/59 SQL Injection Definition SQL Injection is a method to introducing malicious SQL code to get unauthorized access or even damage a system. Definição SQL Injection é um método para introduzir código SQL maligno para obter acesso indevido ou mesmo danificar um sistema.
  • 4. 4/59 SQL Injection: Practice $DBHOST enviroment variable to database server address: Variável de ambiente $DBHOST para o endereço do servidor de banco de dados: $ read -p 'Type the database host address: ' DBHOST Type the database host address: Type the server address. Digite o endereço do servidor.
  • 5. 5/59 SQL Injection: Practice Database user with encrypted stored password, login permission, no superuser: Usuário de banco de dados com senha armazenada criptografada, permissão de login, não superuser: $ psql -U postgres -h ${DBHOST} -c "CREATE ROLE u_sql_injection ENCRYPTED PASSWORD 'secret' LOGIN NOSUPERUSER;"
  • 6. 6/59 SQL Injection: Practice Database creation "db_sql_injection" with user "u_sql_injection" as owner: Criação de banco de dados "db_sql_injection" com o usuário "u_sql_injection" como proprietário: $ psql -U postgres -h ${DBHOST} -c "CREATE DATABASE db_sql_injection OWNER u_sql_injection;"
  • 7. 7/59 SQL Injection: Practice Accessing the database via psql: Acessando a base de dados via psql: $ psql -U u_sql_injection db_sql_injection -h ${DBHOST}
  • 8. 8/59 SQL Injection: Practice User table creation for the application (without hashing): Criação de tabela de usuários para a aplicação (sem hashing): > CREATE TABLE tb_user( username varchar(50) PRIMARY KEY, -- natural primary key password VARCHAR(72) NOT NULL); Inserting a application user in the table: Inserindo um usuário do aplicativo na tabela: > INSERT INTO tb_user (username, password) VALUES ('foo', 'mypassword');
  • 9. 9/59 SQL Injection: Practice Script (1): __________ sql_injection_1.py ___________________________ #_*_ encoding: utf-8 _*_ import getpass user = input('User: ') password = getpass.getpass('Password: ') sql = """ SELECT TRUE FROM tb_user WHERE username = '{}' AND password = '{}'; """.format(user, password) print('n{}'.format(sql)) ____________________________________________________
  • 10. 10/59 SQL Injection: Practice A simple test: Um teste simples: $ python3 sql_injection_1.py User: foo Password: SELECT TRUE FROM tb_user WHERE username = 'foo' AND password = 'mypassword';
  • 11. 11/59 SQL Injection: About the Script The script is pretty simple, does not yet have any interaction with the database, but it serves to illustrate. O script é bem simples, ainda não possui qualquer interação com o banco de dados, mas serve para ilustrar.
  • 12. 12/59 SQL Injection: Practice Script (2): __________ sql_injection_2.py ___________________________ # _*_ encoding: utf-8 _*_ import getpass import psycopg2 import sys # DB server as first argument dbhost = sys.argv[1] # Connection string conn_string = """ host='{}' dbname='db_sql_injection' user='u_sql_injection' password='secret' port='5432' """.format(dbhost) →
  • 13. 13/59 SQL Injection: Practice Script (2): __________ sql_injection_2.py ___________________________ try: # Connection conn = psycopg2.connect(conn_string) # Cursor creation to execute SQL commands cursor = conn.cursor() # User input user = input('User: ') # Password input password = getpass.getpass('Password: ') →
  • 14. 14/59 SQL Injection: Practice Script (2): __________ sql_injection_2.py ___________________________ # SQL string sql = """ SELECT TRUE FROM tb_user WHERE username = '{}' AND password = '{}'; """.format(user, password) # Print the sql string after user and password input print('{}n'.format(sql)) # Execute the SQL string in database cursor.execute(sql) # The result of the string SQL execution res = cursor.fetchone() →
  • 15. 15/59 SQL Injection: Practice Script (2): __________ sql_injection_2.py ___________________________ # User login validation if res: print('nAcessed!') else: print('nError: Invalid user and password combination!') sys.exit(1) except psycopg2.Error as e: print('nAn error has occurred!n{}'.format(e)) # Close the database connection conn.close() ____________________________________________________
  • 16. 16/59 SQL Injection: Practice A simple test access with correct password: Um teste simples de acesso com senha correta: $ python3 sql_injection_2.py ${DBHOST} User: foo Password: SELECT TRUE FROM tb_user WHERE username = 'foo' AND password = 'mypassword'; Acessed!
  • 17. 17/59 SQL Injection: Practice A simple test access with wrong password: Um teste simples de acesso com senha errada: $ python3 sql_injection_2.py ${DBHOST} User: foo Password: SELECT TRUE FROM tb_user WHERE username = 'foo' AND password = '123'; Error: Invalid user and password combination!
  • 18. 18/59 SQL Injection: Practice Malicious code at user login input: Código malicioso na entrada de login de usuário: $ python3 sql_injection_2.py ${DBHOST} User: ' OR 1 = 1; DROP TABLE tb_user; -- Password: SELECT TRUE FROM tb_user WHERE username = '' OR 1 = 1; DROP TABLE tb_user; –-' AND password = ''; An error has occurred! no results to fetch Does the table has been deleted? Será que a tabela foi apagada?
  • 19. 19/59 SQL Injection: Practice Checking the table in the database: Verificando a tabela na base de dados: > SELECT TRUE FROM tb_user; bool ------ t Everithing is OK... for a while... No commit... Está tudo OK... por enquanto... Sem efetivação...
  • 20. 20/59 SQL Injection: Practice Malicious code at user login input (with COMMIT): Código malicioso na entrada de login de usuário (com COMMIT): $ python3 sql_injection.py User: ' OR 1 = 1; DROP TABLE tb_user; COMMIT; -- Password: SELECT TRUE FROM tb_user WHERE username = '' OR 1 = 1; DROP TABLE tb_user; COMMIT; –-' AND password = ''; An error has occurred! no results to fetch
  • 21. 21/59 SQL Injection: Practice Checking the table in the database: Verificando a tabela na base de dados: > SELECT TRUE FROM tb_user; ERROR: relation "tb_user" does not exist LINE 1: SELECT id FROM tb_user; ^ The table was dropped and must be created with the data again. A tabela foi apagada e terá que ser criada com os dados novamente. :(
  • 22. 22/59 Dollar Quoting It consists of a dollar sign ($), an optional “tag” of zero or more characters, another dollar sign, an arbitrary sequence of characters that makes up the string content, a dollar sign, the same tag that began this dollar quote, and a dollar sign. For example, here are two different ways to specify the string “Dianne's horse” using dollar quoting: Consiste de um caractere de dólar, uma “tag” opcional de zero ou mais caracteres, outro caractere de dólar, uma sequência arbitrária de caracteres que é o conteúdo da string, um caractere de dólar, a mesma tag que começou o dollar quoting e um caractere de dólar. Por exemplo, há duas maneiras diferentes de especificar a string “Dianne's horse” usando dollar quoting: $$Dianne's horse$$ $SomeTag$Dianne's horse$SomeTag$
  • 23. 23/59 Dollar Quoting Dollar quoting is also a very nice feature to avoid SQL injection, particularly when the application generates a random tag. This tag must start with either a letter or with an underscore, the rest can have underscore, letters or numbers. Dollar quoting também é um recurso muito interessante para se evitar SQL injection, principalmente quando a aplicação gera uma tag aleatória. Essa tag deve começar ou com uma letra ou com underscore, o resto pode ter underscore, letras ou números.
  • 24. 24/59 Dollar Quoting: Practice Script (3): __________ sql_injection_3.py ___________________________ # _*_ encoding: utf-8 _*_ import getpass import psycopg2 import sys # DB server as first argument dbhost = sys.argv[1] # Connection string conn_string = """ host='{}' dbname='db_sql_injection' user='u_sql_injection' password='secret' port='5432' """.format(dbhost) →
  • 25. 25/59 Dollar Quoting: Practice Script (3): __________ sql_injection_3.py ___________________________ try: # Connection conn = psycopg2.connect(conn_string) # Cursor creation to execute SQL commands cursor = conn.cursor() # User input user = input('User: ') # Password input password = getpass.getpass('Password: ') →
  • 26. 26/59 Dollar Quoting: Practice Script (3): __________ sql_injection_3.py ___________________________ # SQL string sql = """ SELECT TRUE FROM tb_user WHERE username = $${}$$ AND password = $${}$$; """.format(user, password) # Print the sql string after user and password input print('{}n'.format(sql)) # Execute the SQL string in database cursor.execute(sql) # The result of the string SQL execution res = cursor.fetchone() →
  • 27. 27/59 Dollar Quoting: Practice Script (3): __________ sql_injection_3.py ___________________________ # User login validation if res: print('nAcessed!n') else: print('nError: Invalid user and password combination!n') sys.exit(1) except psycopg2.Error as e: print('nAn error has occurred!n{}'.format(e)) # Close the database connection conn.close() ____________________________________________________
  • 28. 28/59 Dollar Quoting: Practice Normal access: Acesso normal: $ python3 sql_injection_3.py ${DBHOST} User: foo Password: SELECT TRUE FROM tb_user WHERE username = $$foo$$ AND password = $$mypassword$$; Acessed!
  • 29. 29/59 Dollar Quoting: Practice Attempted malicious code (with apostrophe): Tentativa de código malicioso (com apóstrofo): $ python3 sql_injection_3.py ${DBHOST} User: ' OR 1 = 1; DROP TABLE tb_user; COMMIT; -- Password: SELECT TRUE FROM tb_user WHERE username = $$' OR 1 = 1; DROP TABLE tb_user; COMMIT; --$$ AND password = $$$$; Error: Invalid user and password combination! Neutralized malicious code. Código malicioso neutralizado.
  • 30. 30/59 Dollar Quoting: Practice Attempted malicious code (with double dollar sign): Tentativa de código malicioso (com dólar duplo): $ python3 sql_injection_3.py ${DBHOST} User: $$ OR 1 = 1; DROP TABLE tb_user; COMMIT; -- Password: SELECT TRUE FROM tb_user WHERE username = $$$$ OR 1 = 1; DROP TABLE tb_user; COMMIT; --$$ AND password = $$$$; An error has occurred! no results to fetch
  • 31. 31/59 Dollar Quoting: Practice Checking the table in the database: Verificando a tabela na base de dados: > SELECT TRUE FROM tb_user; ERROR: relation "tb_user" does not exist LINE 1: SELECT id FROM tb_user; ^ The table was dropped and must be created with the data again. A tabela foi apagada e terá que ser criada com os dados novamente. :(
  • 32. 32/59 Dollar Quoting: Practice Script (4): __________ sql_injection_4.py ___________________________ # _*_ encoding: utf-8 _*_ import getpass import psycopg2 import sys import string import random # DB server as first argument dbhost = sys.argv[1] →
  • 33. 33/59 Dollar Quoting: Practice Script (4): __________ sql_injection_4.py ___________________________ # Connection string conn_string = """ host='{}' dbname='db_sql_injection' user='u_sql_injection' password='secret' port='5432' """.format(dbhost) →
  • 34. 34/59 Dollar Quoting: Practice Script (4): __________ sql_injection_4.py ___________________________ # Function: tag generator def tag_gen(size): first_char = '{}_'.format(string.ascii_letters) last_chars = '{}{}'.format(string.digits, first_char) tag = random.choice(first_char) for i in range(size - 1): tag = '{}{}'.format(tag, random.choice(last_chars)) return tag # Tag for dollar quoting tag = tag_gen(7) →
  • 35. 35/59 Dollar Quoting: Practice Script (4): __________ sql_injection_4.py ___________________________ try: # Connection conn = psycopg2.connect(conn_string) # Cursor creation to execute SQL commands cursor = conn.cursor() # User input user = input('User: ') # Password input password = getpass.getpass('Password: ') →
  • 36. 36/59 Dollar Quoting: Practice Script (4): __________ sql_injection_4.py ___________________________ # SQL string sql = """ SELECT TRUE FROM tb_user WHERE username = ${}${}${}$ AND password = ${}${}${}$; """.format(tag, user, tag, tag, password, tag) # Print the sql string after user and password input print('{}n'.format(sql)) # Execute the SQL string in database cursor.execute(sql) # The result of the string SQL execution res = cursor.fetchone() →
  • 37. 37/59 Dollar Quoting: Practice Script (4): __________ sql_injection_4.py ___________________________ # User login validation if res: print('nAcessed!n') else: print('nError: Invalid user and password combination!n') sys.exit(1) except psycopg2.Error as e: print('nAn error has occurred!n{}'.format(e)) # Close the database connection conn.close() ____________________________________________________
  • 38. 38/59 Dollar Quoting: Practice A simple test access with correct password: Um teste simples de acesso com senha correta: $ python3 sql_injection_4.py ${DBHOST} User: foo Password: SELECT TRUE FROM tb_user WHERE username = $PJPWqvS$foo$PJPWqvS$ AND password = $PJPWqvS$mypassword$PJPWqvS$; Acessed!
  • 39. 39/59 Dollar Quoting: Practice Attempted malicious code (with apostrophe): Tentativa de código malicioso (com apóstrofo): $ python3 sql_injection_4.py ${DBHOST} User: ' OR 1 = 1; DROP TABLE tb_user; COMMIT; -- Password: SELECT TRUE FROM tb_user WHERE username = $EbVRSoG$' OR 1 = 1; DROP TABLE tb_user; COMMIT; -- $EbVRSoG$ AND password = $EbVRSoG$$EbVRSoG$; Error: Invalid user and password combination! Neutralized malicious code. Código malicioso neutralizado.
  • 40. 40/59 Dollar Quoting: Practice Attempted malicious code (with double dollar sign): Tentativa de código malicioso (com dólar duplo): $ python3 sql_injection_4.py ${DBHOST} User: $$ OR 1 = 1; DROP TABLE tb_user; COMMIT; -- Password: SELECT TRUE FROM tb_user WHERE username = $Re7Gqwb$$$ OR 1 = 1; DROP TABLE tb_user; COMMIT; -- $Re7Gqwb$ AND password = $Re7Gqwb$$Re7Gqwb$; Error: Invalid user and password combination! Neutralized malicious code. Código malicioso neutralizado.
  • 41. 41/59 Prepared Statement A prepared statement is a server-side object that can be used to optimize performance. Um prepared statement (comando preparado) é um objeto do lado do servidor que pode ser usado para otimizar performance. When the PREPARE statement is executed, the statement is analyzed, statistics collections are made (ANALYZE) and rewritten. Quando PREPARE statement é executado, o comando (statement) é analisado, são feitas coletas de estatísticas (ANALYZE) e reescrito.
  • 42. 42/59 Prepared Statement When given an EXECUTE statement, the statement is planned and prepared executed. Quando é dado um comando EXECUTE, o prepared statement é planejado e executado. This division of labor prevents repetitive tasks of collecting statistics, while allowing the execution plan depend on specific parameters that can be provided. Essa divisão de trabalho evita repetitivos trabalhos de coleta de estatística, enquanto permite ao plano de execução de depender de parâmetros específicos que podem ser fornecidos.
  • 43. 43/59 Prepared Statement Steps / Etapas Normal query: Consulta normal: 1) Parser → 2) Rewrite System → 3) Planner / Optimizer → 4) Executor Prepared Statement: 1) Planner / Optimizer → 2) Executor
  • 44. 44/59 Prepared Statement: Practice Create a prepared statement: Criar um prepared statement: > PREPARE q_user(text, text) AS SELECT TRUE FROM tb_user WHERE username = $1 AND password = $2;
  • 45. 45/59 Prepared Statement: Practice Execute a prepared statement: Executar um prepared statement: > EXECUTE q_user('foo', 'mypassword'); bool ------ t
  • 46. 46/59 Prepared Statement: Practice Script (5): __________ sql_injection_5.py ___________________________ # _*_ encoding: utf-8 _*_ import getpass import psycopg2 import sys # DB server as first argument dbhost = sys.argv[1] # Connection string conn_string = """ host='{}' dbname='db_sql_injection' user='u_sql_injection' password='secret' port='5432' """.format(dbhost) →
  • 47. 47/59 Prepared Statement: Practice Script (5): __________ sql_injection_5.py ___________________________ try: # Connection conn = psycopg2.connect(conn_string) # Cursor creation to execute SQL commands cursor = conn.cursor() # User input user = input('User: ') # Password input password = getpass.getpass('Password: ') →
  • 48. 48/59 Prepared Statement: Practice Script (5): __________ sql_injection_5.py ___________________________ # SQL string sql = """ PREPARE q_user (text, text) AS SELECT TRUE FROM tb_user WHERE username = $1 AND password = $2; """ # Print the sql string after user and password input print('{}n'.format(sql)) # Execute the SQL string in database cursor.execute(sql) →
  • 49. 49/59 Prepared Statement: Practice Script (5): __________ sql_injection_5.py ___________________________ # SQL string with EXECUTE sql = "EXECUTE q_user('{}', '{}');".format(user, password) # Print the SQL string print('{}n'.format(sql)) # Execute the SQL string in database cursor.execute(sql) # The result of the string SQL execution res = cursor.fetchone() →
  • 50. 50/59 Prepared Statement: Practice Script (5): __________ sql_injection_5.py ___________________________ # User login validation if res: print('nAcessed!') else: print('nError: Invalid user and password combination!') sys.exit(1) except psycopg2.Error as e: print('nAn error has occurred!n{}'.format(e)) # Close the database connection conn.close() ____________________________________________________
  • 51. 51/59 Prepared Statement: Practice A simple test access with correct password: Um teste simples de acesso com senha correta: $ python3 sql_injection_5.py ${DBHOST} User: foo Password: PREPARE q_user (text, text) AS SELECT TRUE FROM tb_user WHERE username = $1 AND password = $2; EXECUTE q_user('foo', 'mypassword'); Acessed!
  • 52. 52/59 Prepared Statement: Practice A simple test access with wrong password: Um teste simples de acesso com senha errada: $ python3 sql_injection_5.py ${DBHOST} User: foo Password: PREPARE q_user (text, text) AS SELECT TRUE FROM tb_user WHERE username = $1 AND password = $2; EXECUTE q_user('foo', '123'); Error: Invalid user and password combination!
  • 53. 53/59 Prepared Statement: Practice Attempted malicious code (with apostrophe): Tentativa de código malicioso (com apóstrofo): $ python3 sql_injection_5.py ${DBHOST} User: ' OR 1 = 1; DROP TABLE tb_user; COMMIT; -- Password: PREPARE q_user (text, text) AS SELECT TRUE FROM tb_user WHERE username = $1 AND password = $2; EXECUTE q_user('' OR 1 = 1; DROP TABLE tb_user; COMMIT; --', ''); An error has occurred! syntax error at or near ";" LINE 1: EXECUTE q_user('' OR 1 = 1; DROP TABLE tb_user; COMMIT; --',... ^ Neutralized malicious code. / Código malicioso neutralizado
  • 54. 54/59 Prepared Statement: Practice Attempted malicious code (with double dollar sign): Tentativa de código malicioso (com dólar duplo): $ python3 sql_injection_5.py ${DBHOST} User: $$ OR 1 = 1; DROP TABLE tb_user; COMMIT; -- Password: PREPARE q_user (text, text) AS SELECT TRUE FROM tb_user WHERE username = $1 AND password = $2; EXECUTE q_user('$$ OR 1 = 1; DROP TABLE tb_user; COMMIT; --', ''); Error: Invalid user and password combination! Neutralized malicious code. / Código malicioso neutralizado.
  • 55. 55/59 Conclusion / Conclusão PostgreSQL has its own mechanisms against SQL injection which makes it very independent of the application. O PostgreSQL possui mecanismos próprios contra SQL injection que o torna muito independente da aplicação.
  • 56. 56/59 Conclusion / Conclusão This makes it easier for the application developer, may delegate such tasks to the database, avoiding technical adjustments in the application and finally provide a robust solution independent of language. Isso facilita para o desenvolvedor da aplicação, podendo confiar tais tarefas ao banco de dados, evitando adaptações técnicas na aplicação e por fim prover uma solução robusta independente da linguagem.
  • 57. 57/59 Donate! The elephant needs you! O Elefante precisa de você! Contribute! Contribua! :) http://www.postgresql.org/about/donate/
  • 59. 59/59 See you soon!!! Até a próxima!!! Juliano Atanazio [email protected] http://slideshare.net/spjuliano https://speakerdeck.com/julianometalsp https://juliano777.wordpress.com :)