以下のコードはPython + Django + PostgreSQLについてです。 しかし、質問はpsycopg2の動作に限定されるかもしれません。SQLインジェクションの学習:テーブルを削除できません
私はWebのセキュリティ、つまりSQLインジェクションを研究しています。学習目的のみ。
これは世界のサンプルデータベース(http://pgfoundry.org/frs/?group_id=1000150&release_id=366#world-world-1.0-title-content)です。
私はこれらのSQL注射を発明した:
1'; drop table city cascade;select 1 where 'me'='me
1'; drop owned by admin;select 1 where 'me'='me
私はデバッグモードではので、私は、もしあればエラーを見ることができます。 まあ、これらの注入はエラーを生じません。
テーブルはまだ存在します。
そして、私はpsqlの中にこれらのコマンドを実行することができます。
world=# drop owned by admin;
DROP OWNED
world=# \dt
No relations found.
質問:あなたは私のSQLインジェクションは、同じ結果を生成しない理由を私は理解する助けてもらえますか?
世界=#1 \ dtの
List of relations
Schema | Name | Type | Owner
--------+-----------------+-------+-------
public | city | table | admin
public | country | table | admin
public | countrylanguage | table | admin
世界=#\ D +都市
Table "public.city"
Column | Type | Modifiers | Storage | Stats target | Description
-------------+--------------+-----------+----------+--------------+-------------
id | integer | not null | plain | |
name | text | not null | extended | |
countrycode | character(3) | not null | extended | |
district | text | not null | extended | |
population | integer | not null | plain | |
views.py
class FormView(View):
def get(self, request):
return render(request,
"home/city.html")
def post(self, request):
city = request.POST.get("city")
try:
conn = psycopg2.connect(dbname='world', user='admin', password='password')
except psycopg2.OperationalError:
print("Unable to connect to db!")
exit();
cur = conn.cursor()
select = "select countrycode, district from city where name='{}'".format(city)
cur.execute(select);
selection = cur.fetchall()
return render(request,
"home/city.html",
context={'city': city, 'selection': selection})
city.html
{% extends 'base.html' %}
{% block content %}
<h2>City</h2>
<form method="post">
{% csrf_token %}
<input name="city">
<button>Submit</button>
</form>
<h2>Country code and district for {{ city }}:</h2>
<ul>
{% for row in selection %}
<li>{{ row.0 }} {{ row.1 }}</li>
{% empty %}
<p>Nothing so far.</p>
{% endfor %}
</ul>
{% endblock %}
'postgresql.conf'の' log_statements = 'all''を有効にし、データベースログのSQL文を見ます。それはあなたが何がデータベースに到着したかを理解するのに役立ちます。 –