SlideShare a Scribd company logo
1 of 16
SQLインジェクション
波多浩昭
1
セキィリティ
 セキュアな状態
 意図しないことをなされないように、 何らかの対策が講じられ
ている状態
 セキュリティインシデント
 意図しないことをされてしまうこと(攻撃を受ける)
 意図せずに、セキュアではない状態に陥ること(物をなくす)
 セキュアな状態はセキュリティインシデントが発生しない状態と
は言えない
2
セキュリティインシデントとWebサーバの
関わり
 Webサーバそのものが被害を受ける場合
 Webサーバからパスワードやクレジットカード流出
 SQLインジェクション
 標的型メールによるウィルス感染
 Webサーバがセキュリティインシデントを助長
 成りかわりで、掲示板へ書き込みをされる
 クロスサイトリクエストフォージェリ
 ID乗っ取りによる
 セッションフィクセーション
 意図しないプログラムの実行
 クロスサイトスクリプティング
3
よくあるWebの構成(Servlet)
 よくある構成=フレームワーク
JavaEE
(glassfish/wildflyなど)
Java
プログラム
(Servlet)
JSPファイル
Servlet型アーキテクチャ
RDBMS
(Oarcle,mysql,postg
reSQLなど)
JDBC
ブラウザ
4
サンプルアプリケーション SampleBBS
簡易掲示板アプリ SampleBBS
機能
ログインすると投稿可能
ログインしなければ検索閲覧のみ可能
タイトル検索画面
sssと入れて検索ボ
タンを押す
検索結果表示
5
SampleBBSの検索機能の概要
<form action=
“<%= response.encodeURL(”/SampleBBS/SearchArticles“) %>”
method="GET">タイトル検索
<input type="text" name="word" value="${param.word}">
<input type="submit" value="検索">
</form>
try(Statement st=con.createStatement();){
String sql = “SELECT * FROM articles A JOIN users U ON U.id=user_id WHERE title LIKE '%" + word +"%' ORDER BY 1";
ResultSet rs = st.executeQuery(sql); // 検索結果をリストに格納
while (rs.next()) {
articles.add(new Article(
rs.getInt("id"),rs.getInt("user_id"), rs.getString("email"), rs.getString("title"), rs.getString("body")));
}
}
検索画面 JSP
検索 Servlet
GET http://hostname/SampleBBS/SearchArticles?word=“sss”
SQL文の作成にStringオブ
ジェクトの結合を使う
6
データベーステーブル構造
+----+---------+--------+-----------------------------------------+
| id | user_id | title | body |
+----+---------+--------+-----------------------------------------+
| 1 | 2 | Titl1 | This is a contents of Article 1 |
| 2 | 2 | Titl2 | This is a contents of Article 2 of hiro |
| 3 | 1 | Titl3 | This is a contents of Article 3 of hata |
| 4 | 1 | Title4 | This is a contents of Article 4 of hata |
| 5 | 1 | sss | dfdsfsa |
| 6 | 2 | tt | tt |
+----+---------+--------+-----------------------------------------+
+----+--------------+----------+-----------------------+
| id | name | pass | email |
+----+--------------+----------+-----------------------+
| 1 | hata | hatahata | h.hata@olt.tokyo |
| 2 | hiro | hiro | test@ntt.com |
| 3 | hata2 | hahata | hata@olt.tokyo |
| 4 | hata2 | hahata | hata@olt.tokyo |
| 5 | hata2 | hahata | hata@olt.tokyo |
| 6 | rrr | 8888 | hata@qc5.so-net.ne.jp |
+----+--------------+----------+-----------------------+
検索実行のために2つの表を結合して、3つのカラムを取り出す
SELECT * FROM articles A JOIN users U ON U.id=user_id WHERE
title LIKE '%sss%' ORDER BY 1
+----+---------+-------+---------+----+------+----------+------------------+
| id | user_id | title | body | id | name | pass | email |
+----+---------+-------+---------+----+------+----------+------------------+
| 5 | 1 | sss | dfdsfsa | 1 | hata | hatahata | h.hata@olt.tokyo |
+----+---------+-------+---------+----+------+----------+------------------+
投稿記事とユーザ管理の2枚の表で構成される
投稿者特定のために、ユーザIDでリレーションされる
7
パスワードの流出
検索結果表示
' UNION SELECT 0,0,name,pass,0,'a','a','a'
from users where name like '%
検索窓にSQL文の断片を入力する
タイトルの部分にユーザID
本文の部分にパスワードが表示されている
8
SELECT、UNIONとJOINについて
SELECT:1つの表から別の新しい表を作り出す
JOIN:2つの表を横方向に並べて別の表を作り出す
UNION:2つの表を縦方向に並べて別の表を作り出す
A B
A B+ A B
A B+ A
B
SELECT
JOIN
UNION
9
不正SQL文の動作
なぜパスワードが表示されたのか?
SELECT * FROM articles A JOIN users U ON U.id=user_id WHERE title LIKE '%'
UNION SELECT 0,0,name,pass,0,'a','a','a' from users where name like '%%' ORDER BY 1
+----+---------+-------+---------+----+------+----------+------------------+
| id | user_id | title | body | id | name | pass | email |
+----+---------+-------+---------+----+------+----------+------------------+
| 5 | 1 | sss | dfdsfsa | 1 | hata | hatahata | h.hata@olt.tokyo |
+---+---+--------------+----------+---+---+---+---+
| 0 | 0 | name | pass | 0 | a | a | a |
+---+---+--------------+----------+---+---+---+---+
| 0 | 0 | hata | hatahata | 0 | a | a | a |
| 0 | 0 | hiro | hiro | 0 | a | a | a |
| 0 | 0 | hata2 | hahata | 0 | a | a | a |
| 0 | 0 | hata2 | hahata | 0 | a | a | a |
| 0 | 0 | hata2 | hahata | 0 | a | a | a |
1個目のSELECTで生成された表(もともと意図していた表=8カラムを持ち、うち3カラムを使う)
2個目のSELECTで生成された表
+----+---------+---------+------------+----+------+----------+-----------+
| id | user_id | title | body | id | name | pass | email |
+----+---------+---------+------------+----+------+----------+-----------+
| 0 | 0 | hata | hatahata | 0 | a | a | a |
| 0 | 0 | hiro | hiro | 0 | a | a | a |
| 0 | 0 | hata2 | hahata | 0 | a | a | a |
| 0 | 0 | rrr | 8888 | 0 | a | a | a |
| 0 | 0 | trew | 5555 | 0 | a | a | a |
| 0 | 0 | hhaat | 11111 | 0 | a | a | a |
UNION
10
SQLインジェクションの簡単な防止策
 検索窓などに入れられた文字列をSQL文の一部として解釈した
ことが原因
 入力された文字列に、’(シングルクォート文字)が含まれていたらエスケープする
 ’ => ¥’ “=>¥” #=>##
SELECT * FROM articles A JOIN users U ON U.id=user_id WHERE title LIKE '%
¥' UNION SELECT 0,0,name,pass,0,¥'a¥',¥'a¥',¥'a¥' from users where name like¥'%
%' ORDER BY 1
入力された文字列は、SQL文として解釈されない 11
SQLインジェクション対策
 データベース技術者としてできること
 プログラマーとしてできること
 ネットワーク技術者としてできること
12
データベース対策
 適切な権限の付与
 カラムごとにアクセス権限の設定できるデータベースの場合
サーブレットにはパスワードやクレジットカード情報を
含むカラムにはアクセス権限を持たないデータベースロ
グインIDを付与する
 表ごとにアクセス権限を設定できるデータベース
パスワードやクレジットカード情報などは、特別の表を
作ってサーブレッドからはアクセスできないようにする
13
プログラミング対策
String sql = "SELECT * FROM articles A JOIN users U ON U.id=user_id"+" WHERE title LIKE '%" + word +"%' ORDER BY 1";
ResultSet rs = st.executeQuery(sql);
String sql = "SELECT * FROM articles JOIN users ON user_id=users.id"+" WHERE title LIKE ? ORDER BY 1";
PreparedStatement st = con.prepareStatement(sql);
st.setString(1, "%" + word + "%");
ResultSet rs = st.executeQuery();
修正されたコード
言語で用意されているSQLインジェクション対策機能を利用する
Stringの結合ではなく、PreparedStatementクラスのsetStringを使う
(注意)PreparedStatementを使えばいいのではなく、
プレースホルダに値をセットするのにsetStringを使うことで初めて対策になる
特殊文字をエスケープする。ただし、手動ではなく言語ごとに用意されている
PreparedStatementクラスを使う
14
ネットワーク側での対策
脆弱性を持つ
Webサーバ
WAF
GET http://hostname/SampleBBS/SearchArticles?word=“sss”
GET http://hostname/SampleBBS/SearchArticles?word=“' UNION SELECT
0,0,name,pass,0,'a','a','a' from users where name like '%”
X
WAFに設定できるポリシー例
HTTPリクエストに”SELECT”が含まれると通信遮断
善良なユーザ
攻撃者
WAFを使う
(WAF:Web Application Firewall :
HTTPの中を見て通信の疎通を判断するファイヤウォール)
15
まとめ
 SQLインジェクションとは
 Web画面の入力データにSQL文(の一部)が入力して意図しない動作
を招く攻撃
 SQLインジェクションの脅威
 隠すべきデータの漏洩
 データベースの破壊(DROP文や、DELETE文の混入)
 対策
 データベースアクセス者への適切な権限の付与
 プログラミングにおけるSQL文生成にPreparedStatementクラスを使う
 WAFによるSQL文混入のHTTPリクエストの排除
 総合対策の重要性
 一つの対策では、攻撃側の進歩には追従できない
 現時点で過剰と思われる程度の複数対策が将来の攻撃に有効 16

More Related Content

Recently uploaded

ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学ssusere0a682
 
UniProject Workshop Make a Discord Bot with JavaScript
UniProject Workshop Make a Discord Bot with JavaScriptUniProject Workshop Make a Discord Bot with JavaScript
UniProject Workshop Make a Discord Bot with JavaScriptyuitoakatsukijp
 
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料Takayuki Itoh
 
The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024koheioishi1
 
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2Tokyo Institute of Technology
 
TokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationTokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationYukiTerazawa
 
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学ssusere0a682
 

Recently uploaded (7)

ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習105 -n人囚人のジレンマモデル- #ゲーム理論 #gametheory #数学
 
UniProject Workshop Make a Discord Bot with JavaScript
UniProject Workshop Make a Discord Bot with JavaScriptUniProject Workshop Make a Discord Bot with JavaScript
UniProject Workshop Make a Discord Bot with JavaScript
 
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
 
The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024
 
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
 
TokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationTokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentation
 
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
 

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

SQLインジェクション

Editor's Notes

  1. サーブレット:画面デザインをJSPファイルに、プログラムをJavaプログラムに担当させる JSPとサーブレットの関係付けをJavaEEで行う 新しいフレームワークとして、JFSがあるが、セキュリティ分野では現在稼働している台数が多いサーブレットをケーススタディに使う
  2. 掲示板には、3つのテータ(タイトル、著者メールアドレス、本文)が表示される 表示されている画面はJSPファイルに記述して、検索ボタンを押すとサーバ側でサーブレットが起動される
  3. サーブレットは入力パラメータを元に検索するためのSQL文を作って、データベースに実行を依頼します
  4. 2つの4つのカラムを持つ表をJOINして8カラムを持つ新しいテーブルを生成しています。 その中から3つのカラムを取り出して画面に表示します。