메뉴 건너뛰기

조회 수 846 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
SQL Injection 은 사용자가 요상한 문자열을 입력해서 쿼리를 요상하게 바꿔 버려서 아무 아이디로나 로그인이 훅 되버린다던지 하는 쪼금은 싱거운 해킹방법중의 하나라고 나는 알고 있다.


뭐 예를들어 아이디 비번을 입력하고 로그인 할때

아이디로  
admin' OR '1'='1 요런걸 입력하고 로그인을 했을때 admin 으로 싹 로그인 되 버리는 어처구니 없는 일이 발생하는 것이 SQL Injection 처리를 쪽바로 해주지 않았을때 일어나는 참혹한 일이다.


SQL Injection 이 발생하는 전형적인 코드는 다음과 같다.

String userid=request.getParameter("userid");
String password=request.getParameter("password");
 
....
....
 
Statement stmt = conn.createStatement();
ResultSet rs = 
   stmt.executeQuery("select count(*) as cnt from member where userid='"+userid+"' and password='"+password+"'");
 
....
....


위 코드처럼 사용자 정보를 받아서 쿼리를 실행하고 실행결과가 1 이면 로그인 시켜주는 코드가 있다.

그런데 사용자가 아디디로 admin' OR '1'='1  을 입력하고 비밀번호로 abcd 를 입력한 후 로그인 시켜주셈~ 이라고 하면 우째될까?

위 코드대로라면  select count(*) from member where userid='amdin' OR '1'='1' and password = 'abcd'   이 쿼리가 실행되 버린다 -_-;;;

무섭다. 만약 사용자 테이블에 userid 가 admin 이라는 레코드가 있다면 admin 으로 로그인 되버리는 것이다. 

이런 어처구니 없는일이 놀랍게도 몇몇 허접한 사이트에서는 더러 통하는데도 있다 . -_-



아무튼 헛소리는 고만하고 본론으로 들어가서 SQL Injection 을 방지하려면 우째해야 될까? 

Statement 대신  PreparedStatement 를 쓰면 된다. 요렇게.
....
....
 
PreparedStatement stmt = conn.prepareStatement("select count(*) from member where userid=? and password=?");
stmt.setString(1, userid);
stmt.setString(2, password);
 
ResultSet rs = stmt.executeQuery();
 
....
....



PreparedStatement 만 쓰면 해결된다고 해서 또 요렇게는 쓰지 말길 -.-;

PreparedStatement stmt = 
   conn.prepareStatement("select count(*) from member where userid='" + userid + "'" and password='" + password + "'");




List of Articles
번호 제목 날짜 조회 수
51 TCP 소켓 프로그래밍 01 - Server/Client 일대일 연결 file 2021.03.31 119
50 System.out.println(); 이클립스에서 자동화기능 사용 file 2016.09.19 5158
49 static멤버와 관련된 예제 2016.09.21 3768
48 static 2016.09.13 3354
47 Spring Boot 프로젝트 생성 file 2016.09.02 4160
46 Singleton Pattern 과 DeadLock file 2023.02.15 74
45 set get 파라미터 2016.08.18 3658
44 Select statements cannot be executed as an update. 에러 해결방법 file 2016.08.29 3853
43 request header 로부터 접속 정보 확인 file 2023.02.15 75
42 Reflection을 활용한 메서드, 필드 값 불러오기. 2021.03.31 124
41 public static void main(String [] args) 2016.09.13 3143
40 No bean name '***Service' is defined 오류나는 이유 및 해결방법 file 2016.08.29 4561
39 MySQL에 All-in-one 설치시 webmaster로 로그인 안되는 문제 해결을 위한 2가지 수정사항 file 2016.08.29 4954
38 My-SQL 을 이용한 JDBC file 2016.09.21 4282
37 log4j에서 로그가 출력되지 않는 문제 수정 2021.03.25 405
36 jstl <c:url value=""> 사용시 ;jsessionid= 붙는 현상 file 2021.03.31 230
35 JSP, Spring, GMail 메일발송 간단 예제 2016.09.12 32791
34 JSON 문자열을 Map 으로 변환하기(Jackson 사용) 2019.01.08 1010
33 JQuery 자동완성 플러그인 JSDT설치 file 2016.09.19 8896
32 jquery 스크롤(scroll) 따라다니는 배너 레이어 / 위로 버튼 / 화면 상단으로 이동 / scroll layer 이벤트 file 2017.07.05 4057
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved