WEBVTT

00:00:00.900 --> 00:00:04.800
Local File Inclusion (LFI)

00:00:06.033 --> 00:00:09.300
Before we examine Local File Inclusion,

00:00:09.300 --> 00:00:12.166
let’s take a moment to
explore the differences

00:00:12.166 --> 00:00:15.633
between File Inclusion
and Directory Traversal.

00:00:16.166 --> 00:00:18.933
These two concepts often get mixed up

00:00:18.933 --> 00:00:22.333
by penetration testers
and security professionals.

00:00:22.733 --> 00:00:25.933
If we confuse the type
of vulnerability we find,

00:00:25.933 --> 00:00:29.933
we may miss an opportunity
to obtain code execution.

00:00:30.366 --> 00:00:33.266
As covered in the last Learning Unit,

00:00:33.266 --> 00:00:36.400
we can use directory
traversal vulnerabilities

00:00:36.400 --> 00:00:41.333
to obtain the contents of a file outside
of the web server’s web root.

00:00:41.666 --> 00:00:44.300
File inclusion vulnerabilities

00:00:44.300 --> 00:00:48.900
allow us to “include” a file in
the application’s running code.

00:00:49.500 --> 00:00:53.400
This means we can use
file inclusion vulnerabilities

00:00:53.400 --> 00:00:56.200
to execute local or remote files,

00:00:56.200 --> 00:01:01.400
while directory traversal only allows
us to read the contents of a file.

00:01:01.800 --> 00:01:05.700
Since we can include files in
the application’s running code

00:01:05.700 --> 00:01:08.300
with file inclusion vulnerabilities,

00:01:08.300 --> 00:01:13.133
we can also display the file
contents of non-executable files.

00:01:13.566 --> 00:01:14.800
For example,

00:01:14.800 --> 00:01:20.400
if we leverage a directory traversal
vulnerability in a PHP web application

00:01:20.400 --> 00:01:24.000
and specify the file admin.php,

00:01:24.000 --> 00:01:27.966
 the source code of the
PHP file will be displayed.

00:01:28.400 --> 00:01:29.866
On the other hand,

00:01:29.866 --> 00:01:33.166
when dealing with a file
inclusion vulnerability,

00:01:33.166 --> 00:01:37.533
the admin.php file
will be executed instead.

00:01:37.966 --> 00:01:39.666
In the following example,

00:01:39.666 --> 00:01:45.533
our goal is to obtain Remote Code
Execution via an LFI vulnerability.

00:01:45.933 --> 00:01:49.600
We’ll do this with the
help of Log Poisoning.

00:01:49.966 --> 00:01:55.000
Log Poisoning works by modifying
data we send to a web application

00:01:55.000 --> 00:01:58.000
so that the logs contain executable code.

00:01:58.566 --> 00:02:01.566
In an LFI vulnerability scenario,

00:02:01.566 --> 00:02:07.166
the local file we include is executed
if it contains executable content.

00:02:07.566 --> 00:02:11.900
This means that if we manage to
write executable code to a file

00:02:11.900 --> 00:02:14.100
and include it within the running code,

00:02:14.100 --> 00:02:15.800
it will be executed.

00:02:16.200 --> 00:02:18.033
In the following case study,

00:02:18.033 --> 00:02:21.966
we’ll try to write executable
code to Apache’s log file.

00:02:23.966 --> 00:02:28.066
We’ll first need to review what
information is controlled by us 

00:02:28.066 --> 00:02:31.233
and saved by Apache in the related log.

00:02:31.666 --> 00:02:32.866
In this case,

00:02:32.866 --> 00:02:36.033
“controlled” means that we
can modify the information

00:02:36.033 --> 00:02:39.033
before we send it to the web application.

00:02:39.333 --> 00:02:44.366
Let’s use curl to analyze which
elements comprise a log entry

00:02:44.366 --> 00:02:50.266
by displaying access.log using the
previously-found LFI vulnerability.

00:02:50.766 --> 00:02:54.700
This means we’ll use the
relative path of the log file

00:02:54.700 --> 00:02:57.033
in the vulnerable “page” parameter 

00:02:57.033 --> 00:03:00.166
in the “Mountain Desserts”
web application.

00:03:08.066 --> 00:03:13.433
The output shows that the
User Agent is included in the log entry.

00:03:14.466 --> 00:03:16.666
Before we send a request,

00:03:16.666 --> 00:03:19.433
we can modify the User Agent in Burp

00:03:19.433 --> 00:03:22.800
and specify what will be
written to the log file.

00:03:23.233 --> 00:03:25.500
Apart from the specified file,

00:03:25.500 --> 00:03:28.966
this command is equivalent
to the directory traversal attack

00:03:28.966 --> 00:03:31.033
from the previous Learning Unit.

00:03:31.466 --> 00:03:36.400
The exploitation of directory
traversal and LFI vulnerabilities

00:03:36.400 --> 00:03:40.800
mainly differs when handling
executable files or content.

00:03:41.200 --> 00:03:42.566
Let’s start Burp,

00:03:42.566 --> 00:03:44.033
open the browser,

00:03:44.033 --> 00:03:47.600
and navigate to the
“Mountain Desserts” web page.

00:04:14.833 --> 00:04:18.933
We’ll click on the Admin link
at the bottom of the page,

00:04:26.533 --> 00:04:32.200
then switch back to Burp
and click on the HTTP history tab.

00:04:38.400 --> 00:04:43.166
Let’s select the related request
and send it to Repeater.

00:04:54.133 --> 00:05:00.100
We can now modify the User Agent
to include a PHP code snippet.

00:05:00.500 --> 00:05:05.100
This snippet accepts a command
via the cmd parameter

00:05:05.100 --> 00:05:10.633
and executes it via the PHP system
function on the target system.

00:05:11.200 --> 00:05:15.000
We’ll use echo to display command output.

00:05:17.000 --> 00:05:20.000
After modifying the User Agent,

00:05:26.800 --> 00:05:28.900
let’s click Send.

00:05:33.466 --> 00:05:38.500
The PHP code snippet was
written to Apache’s log file.

00:05:38.933 --> 00:05:43.233
By including the log file
via the LFI vulnerability,

00:05:43.233 --> 00:05:46.233
we can execute the PHP code.

00:05:46.700 --> 00:05:48.400
To execute our snippet,

00:05:48.400 --> 00:05:50.966
we’ll first update the page parameter

00:05:50.966 --> 00:05:54.366
in the current Burp request
with a relative path.

00:06:01.033 --> 00:06:03.833
We also need to add the cmd parameter

00:06:03.833 --> 00:06:08.266
 to the URL to enter a command
for the PHP snippet.

00:06:11.933 --> 00:06:17.666
We’ll use the ps command to verify
that the log poisoning is working.

00:06:18.733 --> 00:06:22.333
Since we want to provide
values for the two parameters,

00:06:22.333 --> 00:06:25.333
we can use an ampersand as a delimiter.

00:06:26.466 --> 00:06:29.500
We’ll also remove the User Agent line

00:06:29.500 --> 00:06:33.566
from the current Burp request
to avoid poisoning the log again,

00:06:41.633 --> 00:06:44.766
which would lead to multiple
executions of our command

00:06:44.766 --> 00:06:49.300
due to two PHP snippets
being included in the log.

00:06:49.700 --> 00:06:51.966
After sending our request,

00:06:54.700 --> 00:06:59.233
let’s scroll down and review the
output in the Response section.

00:07:08.433 --> 00:07:12.100
Our ps command executed successfully.

00:07:13.133 --> 00:07:18.300
Let’s update the command
parameter with ls -la.

00:07:26.400 --> 00:07:31.800
The output in the Response section
shows that our input triggers an error.

00:07:32.666 --> 00:07:37.466
This happens due to the space
between the command and the parameters.

00:07:37.900 --> 00:07:42.233
There are different techniques we
can use to bypass this limitation,

00:07:42.233 --> 00:07:46.833
such as using Input Field
Separators or URL encoding.

00:07:47.266 --> 00:07:52.966
With URL encoding,
a space is represented as “%20”.

00:07:54.766 --> 00:07:59.800
Let’s replace the space
with “%20” and press Send.

00:08:17.366 --> 00:08:21.200
This time, our command executed correctly.

00:08:22.233 --> 00:08:26.333
We have achieved command
execution on the target system

00:08:26.333 --> 00:08:29.500
and can leverage this
to get a reverse shell

00:08:29.500 --> 00:08:35.066
or add our SSH key to the
authorized_keys file for a user.

00:08:35.500 --> 00:08:41.333
Let’s attempt to obtain a reverse shell by
adding a command to the cmd parameter.

00:08:41.766 --> 00:08:46.833
We can use a common Bash
TCP reverse shell one-liner.

00:08:47.200 --> 00:08:52.266
Since we’ll execute our command
through the PHP system function,

00:08:52.266 --> 00:08:57.133
we should be aware that the command
may be executed via the Bourne Shell,

00:08:57.133 --> 00:08:59.366
also known as sh,

00:08:59.366 --> 00:09:00.900
rather than Bash.

00:09:01.266 --> 00:09:03.466
Our reverse shell one-liner

00:09:03.466 --> 00:09:07.400
contains syntax that is not
supported by the Bourne Shell.

00:09:07.800 --> 00:09:11.533
To ensure the reverse
shell is executed via Bash,

00:09:11.533 --> 00:09:14.666
we need to modify the
reverse shell command.

00:09:16.866 --> 00:09:20.700
We can do this by providing
the reverse shell one-liner

00:09:20.700 --> 00:09:23.966
as argument to bash -c,

00:09:23.966 --> 00:09:26.866
which executes a command with Bash.

00:09:27.300 --> 00:09:32.433
We’ll once again encode the special
characters with URL encoding.

00:09:43.466 --> 00:09:46.100
Before we send the request,

00:09:46.100 --> 00:09:52.700
let’s start a Netcat listener on
port 4444 on our Kali machine.

00:10:06.733 --> 00:10:11.566
It will receive the incoming
reverse shell from the target system.

00:10:11.833 --> 00:10:17.433
Once the listener is started,
we can press Send to send the request.

00:10:29.466 --> 00:10:33.933
We successfully received the
reverse shell in our Netcat listener

00:10:33.933 --> 00:10:37.500
and now have an interactive 
shell on the target.
