본문 바로가기
보안관련 및 네트워크

SQL Injection 조치

by rudgns99 2021. 5. 4.
반응형

SQL Injection 조치

SQL Injection 취약점은 사용자가 입력한 데이터의 유효성을 검증하지 않아, 입력값을 포함하는 동적 

데이터베이스 Query 를 만들 때 발생합니다.

 

sample

 

String query = "SELCT all FROM user WHERE name = " + request.getParameter("user_name");

 

대응방안

- 유효성 검증, 매개변수 바인딩, 저장 프로시저 조치가 있습니다.

 

입력 값 유효성 검증

Replace 구문 예제

 

String query = "SELECT account_balance FROM user_data WHERE user_name =" +customerName.replaceAll("<", "&lt;");

 

DB 구문에 영향 줄 수 있는 입력값 유효성 검증

/*,-,', ",?,#, (,),;,@,=,*,+ union , select , drop , update, from , where , join , substr, user_tables....

 

 

매개 변수 바인딩

 

string sql= "SELECT * FROM Customers WHERE CustomerId = @CustomerId;

sqlCommand command = new SqlCommand(sql);

command.Parameters.Add(new SqlParameter("@CustomerId", System.Data.SqlDbType.Int));

command.Parameters ["@CustomerId"]. Value = 1; 

 

저장 프러시저

 

저장 프로시저가 SQL Injection으로부터 항상 안전하지는 않지만 표준을 준수하고 안전하게 구현될 때 매개변수화 된 

Query 사용과 동일한 효과가 있습니다.