annyoung

Overriding getter and setter on document.cookie 본문

모의해킹

Overriding getter and setter on document.cookie

nopsled 2022. 8. 23. 15:52
var old_cookie = document.cookie;

Object.defineProperty(document, 'cookie', {
    get: function() {
        console.log('Getting cookie');
        return this._value;
    },
    set: function(val) {
        console.log('Setting cookie', arguments);
        this._value = val;
        return this._value;
    }
});

document.cookie = old_cookie;

javascript에서 object에서 getter와 setter를 사용할 수 있는데, 이를 통해 cookie 리시버를 할 수 있지 않을까 생각해보았는데 실제로 코드가 존재했다. https://stackoverflow.com/questions/18317202/defining-a-getter-for-document-cookie#answer-20203840

 

하지만, getter와 setter 속성을 추가하는 경우 이전에 사용하던 쿠키값이 지워지므로 1번라인과 마지막 라인을 추가해준 것으로 확인된다.

 

클라이언트나 웹뷰에서 set 하는 경우 리시버 역할을 하기 때문에 디버깅하기 수월할 것으로 판단된다.

단, 웹뷰 객체가 생성되고 executeJavaScript로 실행 후 loadUrl을 통해 웹뷰를 로드하는 경우 리시버가 정상적으로 동작하지 않을 수 있다.

 

// If call the window.close(), then return false
window.close = () => {
	return false;
}

// If call the self.close(), then return false
self.close = () => {
	return false;
}​

이전에 call function에 오버라이딩 해야하는 상황이 생기면 위와 같이 오버라이딩을 해서 window가 닫히지 않도록 구현했었는데, getter와 setter를 통해 receive 하는 방식도 가능한 것 같다.

 

document.cookie 자체가 네이티브라 getter와 setter로 오버라이딩할 생각을 못했는데 혹시나하고 찾아봤는데 역시 생각한건 이미 만들어져있는 세상인걸 새삼 느끼고 간다.

 

* 220905 추가: 생각해보니까 차라리 frida hooking해서 보는게 더 편할것 같긴하다.

Comments