SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
Chetan Soni – Sr. Security Specialist
Email ID - chetansoni@live.com

XPATH Injection
In a typical Web Application architecture, the data is stored on a Database server. This
Database server store data in various formats like an LDAP, XML or RDBMS database.
The application queries the server and accesses the information based on the user input.
Normally attackers try to extract more information than allowed by manipulating or using the
query with specially crafted inputs.

First we’ll cover some basic concepts,

XML - XML stands for Extensible Markup Language and was designed or used to describe
data.
It provide platform for programmers to create their own customized tags to store data on
database server. An XML document is mostly similar to an RDBMS Database except for the
way data is stored in them. In case of a normal database, data is stored in a table rows and
columns and in XML the data is stored in nodes in a tree form.

XPATH - XPath injection, much like SQL injection, exists when a malicious user can insert
arbitrary XPath code into form fields and URL query parameters in order to inject this code
directly into the XPath query evaluation engine.
Doing so would allow a malicious user to bypass authentication (if an XML-based authentication
system is used) or to access restricted data from the XML data source.

XPath Injection is an attack technique used to exploit applications that construct XPath (XML
Path Language) queries from user-supplied input to query or navigate XML documents.

It can be used directly by an application to query an XML document, as part
of a larger operation such as applying an XSLT transformation to an XML
document, or applying an XQuery to an XML document.
Chetan Soni – Sr. Security Specialist
Email ID - chetansoni@live.com
In this, we try to inject data into an application so that it executes user-controlled XPath queries.
When successfully injected, this vulnerability may allow the hackers to bypass complete
authentication systems or access information without proper authorization.
Here’s the simple xml database file,
<?xml version="1.0" encoding="ISO-8859-1"?>
<chetan_database>
<chetan_user>
<username>chetansoni</username>
<password>password</password>
<account>Administrator</account>
</chetan_user>
<chetan_user>
<username>chetan</username>
<password>pass</password>
<account>Subscriber</account>
</chetan_user>
<chetan_user>
<username>chetanprojects</username>
<password>pass123</password>
<account>Subscriber</account>
</chetan_user>
</chetan_database>
Here’s the basic format how XML file that is used to store the sensitive information.
Now if we want to retrieve the information about Administrator from the above XML file, we
have to write XPath query as,
string(//chetan_user[username/text()='chetansoni' and password/text()='password']/account/text())

Now if the web designer has not property filtered the user input, then the hacker will be able to
inject XPath code into the website and hence interfere with the query result.
Here is the example of XPath query that hacker will use to hack the XML file database:
string(//chetan_user[username/text()='' or '1' = '1' and password/text()='' or '1' = '1']/account/text())
Chetan Soni – Sr. Security Specialist
Email ID - chetansoni@live.com
Actual Code:<?php
$login = simplexml_load_file("chetan_database.xml");
$result=$login->xpath("//chetan_user[username/test()='".$_POST['chetansoni']." AND
password/text()='".$_POST['password']."'";
?>
Types of XPATH Injection –
1. Blind XPATH Injection
2. Advanced XPATH Injection

Prevention –
XPATH Injection can be prevented in the same way as SQL injection since XPath injection
attacks are much like SQL injection attacks. Most of these preventative methods are the same
as well to prevent other typical code injection attacks.
Input Validation: It is one of the best measures to defend applications from XPATH injection
attacks. The developer has to ensure that the application does accept only legitimate input.
Parameterization: In Parameterized queries, the queries are precompiled and instead of
passing user input as expressions, parameters are passed.
Most sites have a way to store data, the most common of which is a database. However some
sites use XML to store data, and use a method of looking at the data known as XPath.



When using SQL Databases to store site data, a site owner has to beware of SQL
Injection attacks which can steal or alter data.
Similarly, sites which store information using XML and XPath have to beware XPath
injection to prevent data theft or modification.

Imagine that you have user data stored in an XML file, and that XML file looks something like
this:
<users>
<user ID =1>
<username>Admin</username>
<password>Password</password>
<role>admin</role>
</userid>
</users>
Looking at the code the site uses to log in, it dynamically builds an XPath query in PHP, after a
user has given username and password in a form (with user and pass variables):
Chetan Soni – Sr. Security Specialist
Email ID - chetansoni@live.com
<?php
$login = simplexml_load_file("users.xml");
$result=$login->xpath("//User[username/test()='".$_POST['user']." AND
password/text()='".$_POST['pass']."'";
?>
The main problem above is that data from the user is not sanitized; the POST variable is being
used to pull data directly from the login form, and placing it into the XPATH query.
At this point, an attacker could try putting in a special string for username:

' or 1=1 or '
Chetan Soni – Sr. Security Specialist
Email ID - chetansoni@live.com

Black box Testing Example –
The XPath attack pattern was first published by Amit Klein and is very similar to the
usual SQL Injection.
In order to get a first grasp of the problem, let's imagine a login page that manages the
authentication to an application in which the user must enter his/her username and
password.
Let's assume that our database is represented by the following XML file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<users>
<user>
<username>admin</username>
<password>server123</password>
<account>admin</account>
</user>
<user>
<username>mohit</username>
<password>Mohit123</password>
<account>guest</account>
</user>
<user>
<username>chetan</username>
<password>Chetan123</password>
<account>guest</account>
</user>
</users>
An XPath query that returns the account whose username is "admin" and the password
is "server123" would be the following:

string(//user[username/text()='admin' and password/text()='server123']/account/text())

If the application does not properly filter user input, the tester will be able to inject XPath
code and interfere with the query result. For instance, the tester could input the
following values:

Username:
Password:

' or '1' = '1
' or '1' = '1
Chetan Soni – Sr. Security Specialist
Email ID - chetansoni@live.com

Using these parameters, the query becomes:
string(//user[username/text()='' or '1' = '1' and password/text()='' or '1' = '1']/account/text())

As in a common SQL Injection attack, we have created a query that always evaluates to
true, which means that the application will authenticate the user even if a username or a
password have not been provided.
And as in a common SQL Injection attack, with XPath injection, the first step is to insert
a single quote (') in the field to be tested, introducing a syntax error in the query, and to
check whether the application returns an error message.

For any query, contact us at chetansoni@live.com

Mais conteúdo relacionado

Último

Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 

Último (20)

Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 

Destaque

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Destaque (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

XPATH INJECTION by CHETAN SONI - Cyber Security Expert

  • 1. Chetan Soni – Sr. Security Specialist Email ID - chetansoni@live.com XPATH Injection In a typical Web Application architecture, the data is stored on a Database server. This Database server store data in various formats like an LDAP, XML or RDBMS database. The application queries the server and accesses the information based on the user input. Normally attackers try to extract more information than allowed by manipulating or using the query with specially crafted inputs. First we’ll cover some basic concepts, XML - XML stands for Extensible Markup Language and was designed or used to describe data. It provide platform for programmers to create their own customized tags to store data on database server. An XML document is mostly similar to an RDBMS Database except for the way data is stored in them. In case of a normal database, data is stored in a table rows and columns and in XML the data is stored in nodes in a tree form. XPATH - XPath injection, much like SQL injection, exists when a malicious user can insert arbitrary XPath code into form fields and URL query parameters in order to inject this code directly into the XPath query evaluation engine. Doing so would allow a malicious user to bypass authentication (if an XML-based authentication system is used) or to access restricted data from the XML data source. XPath Injection is an attack technique used to exploit applications that construct XPath (XML Path Language) queries from user-supplied input to query or navigate XML documents. It can be used directly by an application to query an XML document, as part of a larger operation such as applying an XSLT transformation to an XML document, or applying an XQuery to an XML document.
  • 2. Chetan Soni – Sr. Security Specialist Email ID - chetansoni@live.com In this, we try to inject data into an application so that it executes user-controlled XPath queries. When successfully injected, this vulnerability may allow the hackers to bypass complete authentication systems or access information without proper authorization. Here’s the simple xml database file, <?xml version="1.0" encoding="ISO-8859-1"?> <chetan_database> <chetan_user> <username>chetansoni</username> <password>password</password> <account>Administrator</account> </chetan_user> <chetan_user> <username>chetan</username> <password>pass</password> <account>Subscriber</account> </chetan_user> <chetan_user> <username>chetanprojects</username> <password>pass123</password> <account>Subscriber</account> </chetan_user> </chetan_database> Here’s the basic format how XML file that is used to store the sensitive information. Now if we want to retrieve the information about Administrator from the above XML file, we have to write XPath query as, string(//chetan_user[username/text()='chetansoni' and password/text()='password']/account/text()) Now if the web designer has not property filtered the user input, then the hacker will be able to inject XPath code into the website and hence interfere with the query result. Here is the example of XPath query that hacker will use to hack the XML file database: string(//chetan_user[username/text()='' or '1' = '1' and password/text()='' or '1' = '1']/account/text())
  • 3. Chetan Soni – Sr. Security Specialist Email ID - chetansoni@live.com Actual Code:<?php $login = simplexml_load_file("chetan_database.xml"); $result=$login->xpath("//chetan_user[username/test()='".$_POST['chetansoni']." AND password/text()='".$_POST['password']."'"; ?> Types of XPATH Injection – 1. Blind XPATH Injection 2. Advanced XPATH Injection Prevention – XPATH Injection can be prevented in the same way as SQL injection since XPath injection attacks are much like SQL injection attacks. Most of these preventative methods are the same as well to prevent other typical code injection attacks. Input Validation: It is one of the best measures to defend applications from XPATH injection attacks. The developer has to ensure that the application does accept only legitimate input. Parameterization: In Parameterized queries, the queries are precompiled and instead of passing user input as expressions, parameters are passed. Most sites have a way to store data, the most common of which is a database. However some sites use XML to store data, and use a method of looking at the data known as XPath.   When using SQL Databases to store site data, a site owner has to beware of SQL Injection attacks which can steal or alter data. Similarly, sites which store information using XML and XPath have to beware XPath injection to prevent data theft or modification. Imagine that you have user data stored in an XML file, and that XML file looks something like this: <users> <user ID =1> <username>Admin</username> <password>Password</password> <role>admin</role> </userid> </users> Looking at the code the site uses to log in, it dynamically builds an XPath query in PHP, after a user has given username and password in a form (with user and pass variables):
  • 4. Chetan Soni – Sr. Security Specialist Email ID - chetansoni@live.com <?php $login = simplexml_load_file("users.xml"); $result=$login->xpath("//User[username/test()='".$_POST['user']." AND password/text()='".$_POST['pass']."'"; ?> The main problem above is that data from the user is not sanitized; the POST variable is being used to pull data directly from the login form, and placing it into the XPATH query. At this point, an attacker could try putting in a special string for username: ' or 1=1 or '
  • 5. Chetan Soni – Sr. Security Specialist Email ID - chetansoni@live.com Black box Testing Example – The XPath attack pattern was first published by Amit Klein and is very similar to the usual SQL Injection. In order to get a first grasp of the problem, let's imagine a login page that manages the authentication to an application in which the user must enter his/her username and password. Let's assume that our database is represented by the following XML file: <?xml version="1.0" encoding="ISO-8859-1"?> <users> <user> <username>admin</username> <password>server123</password> <account>admin</account> </user> <user> <username>mohit</username> <password>Mohit123</password> <account>guest</account> </user> <user> <username>chetan</username> <password>Chetan123</password> <account>guest</account> </user> </users> An XPath query that returns the account whose username is "admin" and the password is "server123" would be the following: string(//user[username/text()='admin' and password/text()='server123']/account/text()) If the application does not properly filter user input, the tester will be able to inject XPath code and interfere with the query result. For instance, the tester could input the following values: Username: Password: ' or '1' = '1 ' or '1' = '1
  • 6. Chetan Soni – Sr. Security Specialist Email ID - chetansoni@live.com Using these parameters, the query becomes: string(//user[username/text()='' or '1' = '1' and password/text()='' or '1' = '1']/account/text()) As in a common SQL Injection attack, we have created a query that always evaluates to true, which means that the application will authenticate the user even if a username or a password have not been provided. And as in a common SQL Injection attack, with XPath injection, the first step is to insert a single quote (') in the field to be tested, introducing a syntax error in the query, and to check whether the application returns an error message. For any query, contact us at chetansoni@live.com