SlideShare a Scribd company logo
1 of 2
Download to read offline
TRUNG TÂM ĐÀO TẠO AN NINH MẠNG FIREWALL
                                   ĐT: (84.8) 22429682 -22426933
                           www.tuonglua.net, www.anninhmang.edu.vn


     Cách phòng chống tấn công SQLinjection cho website
Dành cho: Lập trình viên

   A- Trên ngôn ngữ asp, aspx

   1. Lọc dấu nháy “’”
             function filter1( input )
             input = replace(input, "'", "''")
             filter1 = input
             end function
   2. Black list
             function blacklist( input )
             known_bad = array( "select", "insert", "update", "delete", "drop", "--", "'" )
             blacklist = true
             for i = lbound( known_bad ) to ubound( known_bad )
             if ( instr( 1, input, known_bad(i), vbtextcompare ) <> 0 ) then
             blacklist = false
             exit function
             end if
             next
             end function
   3. whitelist
             function whitelist( input )
             good_password_chars =
             "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678
             9"
             whitelist = true
             for i = 1 to len( input )
             c = mid( input, i, 1 )
             if ( InStr( good_password_chars, c ) = 0 ) then
             whitelist = false
             exit function
             end if
             next
             end function

   B- Trên ngôn ngữ php
      1. Chống bypass
         Thay vì lập trình như thông thường để nhận giá trị từ form đăng nhập

          if(isset($_POST['username']) && isset($_POST['passwd']) ){
               $username    = $_POST['username'];
               $passwd      = $_POST['passwd'];
          Fix:
          if(isset($_POST['username']) && isset($_POST['passwd']) ){
               $username = addslashes($_POST['username']);
               $password = addslashes($_POST['password']);
       2. Dùng hàm str_ireplace
TRUNG TÂM ĐÀO TẠO AN NINH MẠNG FIREWALL
                            ĐT: (84.8) 22429682 -22426933
                  www.tuonglua.net, www.anninhmang.edu.vn

   tại link lỗi được thể hiện code như sau:
   $id = $_GET['id'];
   if(!isset($id))
                 $id=1;
        $sql = "SELECT * FROM products WHERE id=$id limit 0,1";
   Fix:
        $id = str_ireplace('union','*',$_GET['id']);
        //$id = $_GET['id'];
        if(!isset($id))
                 $id=1;
        $sql = "SELECT * FROM products WHERE id=$id limit 0,1";

   Lưu ý :
   a. Từ khóa union trên là đơn cử, bạn có thể dùng mãng. Mãng này chứa các giá trị
      blacklist
   b. hàm str_replace cũng tương tự như hàm str_ireplacc, nhưng ở đây tôi dung hàm
      str_ireplace vì có ưu điểm là không phân biệt ký tự hoa, thường.

       vd: $id = str_replace('union','*',$_GET['id']);
       Câu truy vấn union select all 1,2,3,4,5,6,7-- : sẽ không vượt qua
       Nhưng Câu truy vấn uNioN select all 1,2,3,4,5,6,7-- : sẽ vượt qua
       với $id = str_ireplace('union','*',$_GET['id']); bạn an toàn

   c. Nên dung ký tự thay thế trong hàm str_ireplace

       vd: $id = str_replace('union','',$_GET['id']);
       Câu truy vấn union select all 1,2,3,4,5,6,7-- : sẽ không vượt qua
       Nhưng Câu truy vấn uniUNIONon select all 1,2,3,4,5,6,7-- : sẽ vượt qua
       Vì thế, nên dung $id = str_replace('union','*',$_GET['id']);

3. Ép kiểu
   Một cách khác khá an toàn là ép kiểu. Cách này không xuất thông báo lỗi, giữ cho
   website an toàn mà vẫn “đẹp”

       $id = (int)$_GET['id'];
       //$id = $_GET['id'];
       if(!isset($id))
                $id=1;
       $sql = "SELECT * FROM products WHERE id=$id limit 0,1";




                                                                Ths. Phạm Đình Thắng

More Related Content

Viewers also liked

Sociedad limitada nueva empresa
Sociedad limitada nueva empresaSociedad limitada nueva empresa
Sociedad limitada nueva empresaNorberta01
 
Tla apresentação português
Tla   apresentação português Tla   apresentação português
Tla apresentação português Tla Transport
 
Presentación1
Presentación1Presentación1
Presentación1natalia098
 
Fashion Academy - Academic Course
Fashion Academy - Academic CourseFashion Academy - Academic Course
Fashion Academy - Academic Coursezoolab
 
健保被鎖卡沒就醫女流產大量失血亡 詹翔霖教授
健保被鎖卡沒就醫女流產大量失血亡 詹翔霖教授健保被鎖卡沒就醫女流產大量失血亡 詹翔霖教授
健保被鎖卡沒就醫女流產大量失血亡 詹翔霖教授文化大學
 
Videoreflexion und Wissenskooperation in der Fahrlehrerausbildung
Videoreflexion und Wissenskooperation in der FahrlehrerausbildungVideoreflexion und Wissenskooperation in der Fahrlehrerausbildung
Videoreflexion und Wissenskooperation in der FahrlehrerausbildungTamara Ranner
 
Documento politico patto con la città2
Documento politico   patto con la città2Documento politico   patto con la città2
Documento politico patto con la città2redazione gioianet
 

Viewers also liked (14)

Sociedad limitada nueva empresa
Sociedad limitada nueva empresaSociedad limitada nueva empresa
Sociedad limitada nueva empresa
 
Css tutorial-12
Css tutorial-12Css tutorial-12
Css tutorial-12
 
Tla apresentação português
Tla   apresentação português Tla   apresentação português
Tla apresentação português
 
Ejercicio graficos
Ejercicio graficosEjercicio graficos
Ejercicio graficos
 
Presentación1
Presentación1Presentación1
Presentación1
 
Jonathan lynch
Jonathan  lynchJonathan  lynch
Jonathan lynch
 
Genre mood board
Genre mood boardGenre mood board
Genre mood board
 
Equipamentos
EquipamentosEquipamentos
Equipamentos
 
Fashion Academy - Academic Course
Fashion Academy - Academic CourseFashion Academy - Academic Course
Fashion Academy - Academic Course
 
PRESENTACIONES
PRESENTACIONESPRESENTACIONES
PRESENTACIONES
 
健保被鎖卡沒就醫女流產大量失血亡 詹翔霖教授
健保被鎖卡沒就醫女流產大量失血亡 詹翔霖教授健保被鎖卡沒就醫女流產大量失血亡 詹翔霖教授
健保被鎖卡沒就醫女流產大量失血亡 詹翔霖教授
 
Videoreflexion und Wissenskooperation in der Fahrlehrerausbildung
Videoreflexion und Wissenskooperation in der FahrlehrerausbildungVideoreflexion und Wissenskooperation in der Fahrlehrerausbildung
Videoreflexion und Wissenskooperation in der Fahrlehrerausbildung
 
Aminhaescola.net
Aminhaescola.netAminhaescola.net
Aminhaescola.net
 
Documento politico patto con la città2
Documento politico   patto con la città2Documento politico   patto con la città2
Documento politico patto con la città2
 

Approach sqlinjection

  • 1. TRUNG TÂM ĐÀO TẠO AN NINH MẠNG FIREWALL ĐT: (84.8) 22429682 -22426933 www.tuonglua.net, www.anninhmang.edu.vn Cách phòng chống tấn công SQLinjection cho website Dành cho: Lập trình viên A- Trên ngôn ngữ asp, aspx 1. Lọc dấu nháy “’” function filter1( input ) input = replace(input, "'", "''") filter1 = input end function 2. Black list function blacklist( input ) known_bad = array( "select", "insert", "update", "delete", "drop", "--", "'" ) blacklist = true for i = lbound( known_bad ) to ubound( known_bad ) if ( instr( 1, input, known_bad(i), vbtextcompare ) <> 0 ) then blacklist = false exit function end if next end function 3. whitelist function whitelist( input ) good_password_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678 9" whitelist = true for i = 1 to len( input ) c = mid( input, i, 1 ) if ( InStr( good_password_chars, c ) = 0 ) then whitelist = false exit function end if next end function B- Trên ngôn ngữ php 1. Chống bypass Thay vì lập trình như thông thường để nhận giá trị từ form đăng nhập if(isset($_POST['username']) && isset($_POST['passwd']) ){ $username = $_POST['username']; $passwd = $_POST['passwd']; Fix: if(isset($_POST['username']) && isset($_POST['passwd']) ){ $username = addslashes($_POST['username']); $password = addslashes($_POST['password']); 2. Dùng hàm str_ireplace
  • 2. TRUNG TÂM ĐÀO TẠO AN NINH MẠNG FIREWALL ĐT: (84.8) 22429682 -22426933 www.tuonglua.net, www.anninhmang.edu.vn tại link lỗi được thể hiện code như sau: $id = $_GET['id']; if(!isset($id)) $id=1; $sql = "SELECT * FROM products WHERE id=$id limit 0,1"; Fix: $id = str_ireplace('union','*',$_GET['id']); //$id = $_GET['id']; if(!isset($id)) $id=1; $sql = "SELECT * FROM products WHERE id=$id limit 0,1"; Lưu ý : a. Từ khóa union trên là đơn cử, bạn có thể dùng mãng. Mãng này chứa các giá trị blacklist b. hàm str_replace cũng tương tự như hàm str_ireplacc, nhưng ở đây tôi dung hàm str_ireplace vì có ưu điểm là không phân biệt ký tự hoa, thường. vd: $id = str_replace('union','*',$_GET['id']); Câu truy vấn union select all 1,2,3,4,5,6,7-- : sẽ không vượt qua Nhưng Câu truy vấn uNioN select all 1,2,3,4,5,6,7-- : sẽ vượt qua với $id = str_ireplace('union','*',$_GET['id']); bạn an toàn c. Nên dung ký tự thay thế trong hàm str_ireplace vd: $id = str_replace('union','',$_GET['id']); Câu truy vấn union select all 1,2,3,4,5,6,7-- : sẽ không vượt qua Nhưng Câu truy vấn uniUNIONon select all 1,2,3,4,5,6,7-- : sẽ vượt qua Vì thế, nên dung $id = str_replace('union','*',$_GET['id']); 3. Ép kiểu Một cách khác khá an toàn là ép kiểu. Cách này không xuất thông báo lỗi, giữ cho website an toàn mà vẫn “đẹp” $id = (int)$_GET['id']; //$id = $_GET['id']; if(!isset($id)) $id=1; $sql = "SELECT * FROM products WHERE id=$id limit 0,1"; Ths. Phạm Đình Thắng