크로스 사이트 요청 위조 예제

Posted by Breeze24
2017. 10. 2. 17:15 Web

크로스 사이트 요청 위조를 하는 방법에 대해 알아보자. 


자신이 운영하는 사이트가 크로스 사이트 요청 위조가 가능한지 여부를 판단하기 위해서 알아야 할 때 아래 내용을 참고하면 된다. 


얼마전에 사이트 웹취약점 검사를 해봤더니 크로스 사이트 요청 위조가 가능하다는 진단을 받았다. Referer 위조가 가능하다고 나와서 어떻게 위조가 가능하다는 것인지에 대해 알아야 했다. 



몇 시간을 검색을 하다가 결국 비슷한 내용을 찾아 크로스 사이트 요청 위조를 하는 내용을 만들어 봤다. 


아래에 보면 알겠지만, 소스가 길지 않으므로 이해하기 어렵지 않을 것이다. 

.


크로스 사이트 요청 위조 예제

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
 
public class Test {
      public static void main(String[] args) throws ClientProtocolException, IOException {
            Test test = new Test();
            test.action();
      }
 
      private static final String USER_AGENT = "Mozila/5.0";
      private static final String GET_URL = "http://aaa.com/CheckTest.jsp";
 
      public static void action() throws ClientProtocolException, IOException {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(GET_URL);
            httpGet.addHeader("User-Agent", USER_AGENT);
            httpGet.addHeader("REFERER", "http://naver.com");
 
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
 
            System.out.println(httpResponse.getStatusLine().getStatusCode());
 
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
 
            String inputLine;
            StringBuffer response = new StringBuffer();
 
            while ((inputLine = reader.readLine()) != null) {
                  response.append(inputLine);
            }
 
            reader.close();
 
            System.out.println("결과: " +response.toString());
            httpClient.close();
      }
}

위의 코드는 aaa.com을 호출하면서 referer를 naver.com으로 하는 예제이다. CheckTest.jsp 화면에서는 입력을 받아 referer를 로깅하는 역할을 하는데, 위의 코드를 실행하게 되면 해당 페이지를 호출한 화면에서 referer가 naver.com으로 되어 있는 것을 알 수 있다. 

.


이 방법으로 웨취약점을 확인할 수 있었고, 이 내용을 참고해서 보안을 강화할 수 있었다.