Computer/Software

[웹해킹] 패킷인코딩 방식

Detacher 2011. 7. 15. 00:25
와이어샤크를 이용해 어느사이트를 패킷분석해봤다.
(보안상 별표처리)

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
GET /static/functions/masterpc.js HTTP/1.1
Host: *******.net
Connection: keep-alive
Referer: http://*******.net/index.php
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.30 (KHTML, like Gecko) 

Chrome/12.0.742.112 Safari/534.30
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: windows-949,utf-8;q=0.7,*;q=0.3
Cookie: PHPSESSID=**********************************
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

여기서 인코딩 방식을 주목해보면,

Accept-Encoding: gzip,deflate,sdch
 

이 문장이 눈에띈다.

gzip, deflate, sdch에 대해 알아보자

다음은 gzip에 대한 위키의 설명이다

Gzip is any of several software applications used for file compression and decompression. The term usually refers to the GNU Project's implementation, "gzip" standing for GNU zip. It is based on the DEFLATEalgorithm, which is a combination of Lempel-Ziv (LZ77) and Huffman coding. The program was created by Jean-Loup Gailly and Mark Adler as a free softwarereplacement for the compress program used in early Unixsystems, and intended for use by the Project. Version 0.1 was first publicly released on October 30, 1992, and version 1.0 followed in February 1993.

대충 요약하자면, gzip은 GNU zip의 약자이며 deflate 알고리즘에 기반한 유닉스용 파일 압축 툴이다.

Deflate 알고리즘에 대한 자세한 설명은 여기로 - http://www.gzip.org/deflate.html 



sdch는 다음과 같다. 역시위키펌...

Shared Dictionary Compression over HTTP (SDCH) is a data compressionmethod aimed to reduce redundancy across HTTP responses proposed byGoogle [1]. This is implemented in Google Chrome. This is proposed modification to HTTP/1.1. SDCH is using VCDIFF (RFC 3284) for the compression. 

SDCH는 간단히 말해 구글 크롬의 데이터 압축방식이다.
크롬브라우저 나름의(?) 패킷보안 방식인듯하다.

그래서 비교를 위해 익스플로러로 다시 패킷을 분석해 봤다.

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
GET /static/styles/public/style.css HTTP/1.1
Accept: text/css
Referer: http://*******.net/login.php
Accept-Language: ko-KR
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Accept-Encoding: gzip, deflate
Host: *******.net
If-Modified-Since: Sat, 19 Dec 2009 12:25:50 GMT
If-None-Match: "32b9bd-3a3-************"
Connection: Keep-Alive
Cookie: PHPSESSID=*********************************
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
 
역시 IE에는 SDCH 인코딩방식이 없다.

따라서, 크롬과 IE의 인코딩된 쿠키가 다르다. 이건 쉽게 확인할수 있을듯



아래내용은 지식인에서 관련 용어에대한 펌자료이다.

 Q. 특정 파일이 Deflate 압축으로 되어있는지 알고 싶은데 Deflate 압축 알고리즘이 사용되었는지 알 수 있는 헤더의 특징 같은 것을 알고 싶습니다.

 A. 구분할 수 있는 특정 헤더는 없습니다.
http protocol에서는 데이터를 deflate로 보낼 경우
content-Encoding 헤더에 deflate로 되어 있다고 나타내 줍니다.
 
예> content-Encoding: deflate
 
----------------------------------------------
deflate는 3bit의 header bits가 있습니다.
 
첫번째 1bit는 FINAL인지를 나타내는 것이고
두번째 2bit는 compression이 어떻게 되어 있는지를 알려줍니다.
 
ED와 같이 한 바이트가 있는 경우
11101101
              ^--- first bit로 1이면 end of last block
          ^^---- 00 : no compression,            01:fixed huffman code,
                    10:danamic huffman code,  11:reserved  or Error(사용하고 있지 않습니다)
               
특정 파일이 deflate로 되어 있는지를 알려주는 것은 특정 파일마다 spec문서가 있습니다.
그 문서에서 deflate는 어떻게 나타낸다 라고 하는데
PNG specification version 1.2에서는
IDAT chunk에 첫번째 바이트의 하위 4bit가 0x8(1000)이면 deflate로 압축되어 있다는 것을 나타낸다고 명시하고 있습니다.
 
각 압축파일마다 spec문서가 있는데 그것은 검색하셔야 합니다.
찾을 수 있는데 대부분은 영어권 site에 많습니다.
특히 학교에 많이 올라와 있더군요.



 Q. deflate 압축 알고리즘은 허프만 압축 알고리즘과 LZ77 압축 알고리즘을 차례로 적용한 것이라고 하는 말을 들었는데 허프만으로 압축을 하고 압축한 결과물에 LZ77로 다시 압축하는 것입니까?

 A. RFC 1951에 정의 되어 있습니다.
허프만 압축 알고리즘을 사용해서 code을 형성합니다.
이렇게 형성된 코드는 LZ77 압축 알고리즘으로 데이터를 풀게 됩니다.

RFC 1951에는 허프만 코드 테이블이 있습니다.
허프만 코드 테이블을 사용해서 LZ77 압축 알고리즘을 사용하면 되는데요
데이터의 형식은
(length/literal, distance) 형식으로 되어 있습니다.
0-255 literal, 256은 end of block, 257 - 285의 데이터는 length 값입니다.

distance은 뒤로 몇 바이트 입니다.

Example1>
기존 데이터 xy가 나와 있다면
(259, 1) -> 5자, 뒤로 2번째, xyxyx 값이 나옵니다.
결과 적으로 xyxyxyx가 됩니다.
참고하는 글자 수보다 반복하는 횟수가 많은 경우는 rotation으로 반복해서 읽게 됩니다.

Example2>
output data : abcdef
(257, 4(bit:0)) -> 3자, 뒤로 5번째 bcd 값이 나옵니다.
output data : abcdefbcd

ref 위치, 개념 설명이 RFC 1951에 있으니 소스를 참고해서 보시면 됩니다.
해당 알고리즘은 HTTP, PNG에서 사용하고 있습니다.

'Computer > Software' 카테고리의 다른 글

[C언어] typedef와 #define의 차이점  (0) 2011.08.09
[웹해킹] 패킷암호화 알고리즘  (0) 2011.07.15
[Linux] Promiscuous mode 설정방법  (0) 2011.07.14
[Linux] 페도라15 설치  (0) 2011.07.14
[Tool] promqry 1.0  (0) 2011.07.14