# 杂项JS技巧和相关信息
☁️ HackTricks云 ☁️ -🐦 推特 🐦 - 🎙️ Twitch 🎙️ - 🎥 Youtube 🎥 * 你在一家**网络安全公司**工作吗?你想在HackTricks中看到你的**公司广告**吗?或者你想获得**PEASS的最新版本或下载HackTricks的PDF**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)! * 发现我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品[**The PEASS Family**](https://opensea.io/collection/the-peass-family) * 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com) * **加入**[**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组**](https://discord.gg/hRep4RUj7f)或[**电报群组**](https://t.me/peass),或者**关注**我在**Twitter**上的[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**。** * **通过向**[**hacktricks repo**](https://github.com/carlospolop/hacktricks) **和**[**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud) **提交PR来分享你的黑客技巧。**
## Javascript模糊测试 ### 有效的JS注释字符 ```javascript //This is a 1 line comment /* This is a multiline comment*/ #!This is a 1 line comment, but "#!" must to be at the beggining of the line -->This is a 1 line comment, but "-->" must to be at the beggining of the line for (let j = 0; j < 128; j++) { for (let k = 0; k < 128; k++) { for (let l = 0; l < 128; l++) { if (j == 34 || k ==34 || l ==34) continue; if (j == 0x0a || k ==0x0a || l ==0x0a) continue; if (j == 0x0d || k ==0x0d || l ==0x0d) continue; if (j == 0x3c || k ==0x3c || l ==0x3c) continue; if ( (j == 47 && k == 47) ||(k == 47 && l == 47) ) continue; try { var cmd = String.fromCharCode(j) + String.fromCharCode(k) + String.fromCharCode(l) + 'a.orange.ctf"'; eval(cmd); } catch(e) { var err = e.toString().split('\n')[0].split(':')[0]; if (err === 'SyntaxError' || err === "ReferenceError") continue err = e.toString().split('\n')[0] } console.log(err,cmd); } } } //From: https://balsn.tw/ctf_writeup/20191012-hitconctfquals/#bounty-pl33z // From: Heyes, Gareth. JavaScript for hackers: Learn to think like a hacker (p. 43). Kindle Edition. log=[]; for(let i=0;i<=0xff;i++){ for(let j=0;j<=0xfff;j++){ try { eval(`${String.fromCodePoint(i,j)}%$£234$`) log.push([i,j]) }catch(e){} } } console.log(log)//[35,33],[47,47] ``` ### 有效的JS换行字符 In JavaScript, there are several characters that can be used to represent a new line. These characters can be useful in certain situations, such as when trying to obfuscate code or bypass input filters. Here are some of the valid JS new line characters: - `\n`: This is the most commonly used new line character in JavaScript. It represents a line feed. - `\r`: This character represents a carriage return. - `\u2028`: This is the Unicode character for line separator. - `\u2029`: This is the Unicode character for paragraph separator. When using these characters, it's important to keep in mind that different platforms and browsers may interpret them differently. Therefore, it's recommended to test the code on the target platform to ensure compatibility. ### 有效的JS换行字符 在JavaScript中,有几个字符可以用来表示换行。这些字符在某些情况下非常有用,比如在尝试混淆代码或绕过输入过滤器时。以下是一些有效的JS换行字符: - `\n`:这是JavaScript中最常用的换行字符。它表示换行。 - `\r`:这个字符表示回车。 - `\u2028`:这是行分隔符的Unicode字符。 - `\u2029`:这是段落分隔符的Unicode字符。 在使用这些字符时,需要注意不同的平台和浏览器可能会以不同的方式解释它们。因此,建议在目标平台上测试代码,以确保兼容性。 ```javascript //Javascript interpret as new line these chars: String.fromCharCode(10) //0x0a String.fromCharCode(13) //0x0d String.fromCharCode(8232) //0xe2 0x80 0xa8 String.fromCharCode(8233) //0xe2 0x80 0xa8 for (let j = 0; j < 65536; j++) { try { var cmd = '"aaaaa";'+String.fromCharCode(j) + '-->a.orange.ctf"'; eval(cmd); } catch(e) { var err = e.toString().split('\n')[0].split(':')[0]; if (err === 'SyntaxError' || err === "ReferenceError") continue; err = e.toString().split('\n')[0] } console.log(`[${err}]`,j,cmd); } //From: https://balsn.tw/ctf_writeup/20191012-hitconctfquals/#bounty-pl33z ``` ### 函数调用中的有效JS空格 In some cases, when trying to bypass filters or evade detection, it may be necessary to use valid JavaScript spaces in a function call. These spaces can help obfuscate the payload and make it more difficult for security measures to detect. 在某些情况下,为了绕过过滤器或规避检测,可能需要在函数调用中使用有效的JavaScript空格。这些空格可以帮助混淆有效载荷,使其更难被安全措施检测到。 Here are some examples of valid JavaScript spaces that can be used: 以下是一些可以使用的有效JavaScript空格的示例: - **No-Break Space**: This is a non-breaking space character that can be used instead of a regular space. It can be represented by the Unicode character `\u00A0`. - **不间断空格**:这是一个非间断空格字符,可以用来替代普通空格。它可以用Unicode字符`\u00A0`表示。 - **Zero-Width Space**: This is a non-printable character that has no width when displayed. It can be represented by the Unicode character `\u200B`. - **零宽度空格**:这是一个不可打印的字符,在显示时没有宽度。它可以用Unicode字符`\u200B`表示。 - **Zero-Width Non-Joiner**: This is another non-printable character that has no width when displayed and is used to prevent the joining of two adjacent characters. It can be represented by the Unicode character `\u200C`. - **零宽度非连接符**:这是另一个不可打印的字符,在显示时没有宽度,并且用于防止两个相邻字符的连接。它可以用Unicode字符`\u200C`表示。 By using these valid JavaScript spaces in a function call, you can add an extra layer of obfuscation to your payload and increase the chances of bypassing security measures. 通过在函数调用中使用这些有效的JavaScript空格,您可以为有效载荷添加额外的混淆层,并增加绕过安全措施的机会。 ```javascript // Heyes, Gareth. JavaScript for hackers: Learn to think like a hacker (pp. 40-41). Kindle Edition. // Check chars that can be put in between in func name and the () function x(){} log=[]; for(let i=0;i<=0x10ffff;i++){ try { eval(`x${String.fromCodePoint(i)}()`) log.push(i) }catch(e){} } console.log(log)v//9,10,11,12,13,32,160,5760,8192,8193,8194,8195,8196,8197,8198,8199,8200,8201,8202,813 232,8233,8239,8287,12288,65279 ``` ### **生成字符串的有效字符** The following characters can be used to generate strings in various contexts: 以下字符可用于在不同的上下文中生成字符串: - Alphanumeric characters (a-z, A-Z, 0-9) - Special characters (!, @, #, $, %, ^, &, *, (, ), -, _, +, =, [, ], {, }, |, \, :, ;, ", ', <, >, ,, ., ?, /) - Whitespace characters (space, tab, newline) - Unicode characters - 字母数字字符(a-z,A-Z,0-9) - 特殊字符(!,@,#,$,%,^,&,*,(,),-,_,+,=,[,],{,},|,\,:,;,",',<,>,,,.,?,/) - 空白字符(空格,制表符,换行符) - Unicode字符 These characters can be combined and manipulated to create strings that serve various purposes, such as injecting malicious code or bypassing input validation. 可以组合和操作这些字符,以创建用于注入恶意代码或绕过输入验证的字符串。 ```javascript // Heyes, Gareth. JavaScript for hackers: Learn to think like a hacker (pp. 41-42). Kindle Edition. // Check which pairs of chars can make something be a valid string log=[]; for(let i=0;i<=0x10ffff;i++){ try { eval(`${String.fromCodePoint(i)}%$£234${String.fromCodePoint(i)}`) log.push(i) }catch(e){} } console.log(log) //34,39,47,96 //single quote, quotes, backticks & // (regex) ``` ### **Surrogate Pairs BF(代理对暴力破解)** 这种技术对于XSS来说并不是很有用,但是它可以用于绕过WAF的保护。这段Python代码接收两个字节作为输入,并搜索具有第一个字节作为高代理对的最后一个字节和最后一个字节作为低代理对的最后一个字节的代理对。 ```python def unicode(findHex): for i in range(0,0xFFFFF): H = hex(int(((i - 0x10000) / 0x400) + 0xD800)) h = chr(int(H[-2:],16)) L = hex(int(((i - 0x10000) % 0x400 + 0xDC00))) l = chr(int(L[-2:],16)) if(h == findHex[0]) and (l == findHex[1]): print(H.replace("0x","\\u")+L.replace("0x","\\u")) ``` 更多信息: * [https://github.com/dreadlocked/ctf-writeups/blob/master/nn8ed/README.md](https://github.com/dreadlocked/ctf-writeups/blob/master/nn8ed/README.md) * [https://mathiasbynens.be/notes/javascript-unicode](https://mathiasbynens.be/notes/javascript-unicode) [https://mathiasbynens.be/notes/javascript-encoding](https://mathiasbynens.be/notes/javascript-encoding) ### `javascript{}:`协议模糊测试 ```javascript // Heyes, Gareth. JavaScript for hackers: Learn to think like a hacker (p. 34). Kindle Edition. log=[]; let anchor = document.createElement('a'); for(let i=0;i<=0x10ffff;i++){ anchor.href = `javascript${String.fromCodePoint(i)}:`; if(anchor.protocol === 'javascript:') { log.push(i); } } console.log(log)//9,10,13,58 // Note that you could BF also other possitions of the use of multiple chars // Test one option let anchor = document.createElement('a'); anchor.href = `javascript${String.fromCodePoint(58)}:alert(1337)`; anchor.append('Click me') document.body.append(anchor) // Another way to test Test ``` ### URL模糊测试 URL模糊测试是一种常用的Web应用程序渗透测试技术,用于发现可能存在的漏洞。通过对URL进行模糊测试,可以尝试各种不同的输入和参数组合,以寻找可能导致安全漏洞的情况。 URL模糊测试的目标是发现应用程序中的潜在漏洞,例如路径遍历、文件包含、SQL注入等。通过构造特定的URL请求,可以触发应用程序中的漏洞,并获取敏感信息或执行恶意操作。 URL模糊测试可以使用各种工具和技术来实现。其中一种常见的方法是使用字典文件,包含各种可能的URL路径和参数组合。通过将这些字典文件与目标URL进行组合,可以生成大量的URL请求,以便进行测试。 URL模糊测试的关键是选择合适的字典文件和参数组合。根据应用程序的特点和已知的漏洞类型,可以选择不同的字典文件和参数组合来进行测试。同时,还可以使用自动化工具来加快测试过程,并提供更全面的测试覆盖。 URL模糊测试是一项重要的渗透测试技术,可以帮助发现Web应用程序中的安全漏洞。通过对URL进行模糊测试,可以提高应用程序的安全性,并防止潜在的攻击。 ```javascript // Heyes, Gareth. JavaScript for hackers: Learn to think like a hacker (pp. 36-37). Kindle Edition. // Before the protocol a=document.createElement('a'); log=[]; for(let i=0;i<=0x10ffff;i++){ a.href = `${String.fromCodePoint(i)}https://hacktricks.xyz`; if(a.hostname === 'hacktricks.xyz'){ log.push(i); } } console.log(log) //0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32 // Between the slashes a=document.createElement('a'); log=[]; for(let i=0;i<=0x10ffff;i++){ a.href = `/${String.fromCodePoint(i)}/hacktricks.xyz`; if(a.hostname === 'hacktricks.xyz'){ log.push(i); } } console.log(log) //9,10,13,47,92 ``` ### HTML模糊测试 HTML模糊测试是一种测试Web应用程序的技术,旨在发现和利用HTML中的漏洞。通过向输入字段注入恶意或异常数据,可以揭示潜在的安全问题。以下是一些常见的HTML模糊测试技术: - **标签闭合测试**:在输入字段中注入不完整的HTML标签,以测试应用程序是否正确处理标签闭合。例如,将``。 - **属性注入测试**:在HTML标签的属性中注入恶意代码,以测试应用程序是否正确过滤和转义用户输入。例如,将``标签的`src`属性注入为`" onerror="alert('XSS')" />`。 - **特殊字符测试**:在输入字段中注入特殊字符,如尖括号、引号和斜杠,以测试应用程序是否正确处理和转义这些字符。例如,将`<`注入为`<`。 - **编码绕过测试**:尝试绕过应用程序对特殊字符的编码和转义机制,以执行恶意操作。例如,使用十六进制编码绕过`<`字符的过滤。 - **标签嵌套测试**:在输入字段中嵌套HTML标签,以测试应用程序是否正确处理和解析嵌套标签。例如,将``标签嵌套在``标签内。 - **事件处理程序测试**:在HTML标签的事件处理程序中注入恶意代码,以测试应用程序是否正确过滤和处理用户输入。例如,将``标签的`onerror`事件注入为`alert('XSS')`。 - **URL注入测试**:在URL参数中注入恶意代码,以测试应用程序是否正确处理和解析URL。例如,将`