WEBVTT

00:01.850 --> 00:07.850
Now, let's talk about how you can secure your website from file upload vulnerability's.

00:09.660 --> 00:16.890
The main thing to keep in mind is that never allow people to upload executables, there shouldn't be

00:16.890 --> 00:20.340
a need for you to allow people to upload executables, really.

00:20.610 --> 00:25.980
So stuff like Fibi scripts, executables, lik e e com.

00:25.980 --> 00:31.590
All these files, you should never allow people to upload them because there is a high possibility that

00:31.590 --> 00:33.450
these files could harm your server.

00:34.860 --> 00:42.960
The second thing is you need to check that the file type is what the what you want is what you're expecting

00:43.170 --> 00:46.220
and check the extension at the same time.

00:46.710 --> 00:53.670
So when we see in when we are checking with our tests and the previous videos at the low security level,

00:53.670 --> 00:57.120
obviously there was no checks at the medium security level.

00:57.240 --> 01:00.120
There was checking for only the file type.

01:00.450 --> 01:05.790
And at the high security level, they were checking for the extension only.

01:07.500 --> 01:14.130
So what you want to do really is never allow executables make sure the uploaded file is what you're

01:14.130 --> 01:14.640
expecting.

01:14.670 --> 01:20.820
So if you're uploading an image, make sure that the file type is an image and the and the extension

01:20.850 --> 01:22.200
is an image extension.

01:22.210 --> 01:26.660
So it's a JPEG, RPG or any other image type only.

01:27.210 --> 01:34.020
And once you do these two checks, so that ad that make sure that you recreate the upload file, so

01:34.320 --> 01:41.040
you check that the file is an image and check that the extension is an image extension, then analyze

01:41.040 --> 01:43.600
the file using some sort of a library.

01:43.620 --> 01:49.320
So depending on the programming language you're using on your server, make sure that you analyze you

01:49.320 --> 01:55.590
pass this file to a library and analyze its content and recreate the file to suit your website.

01:55.740 --> 02:01.290
Now, this is actually a good step anyway, because since you're displaying this on your website, you

02:01.290 --> 02:07.520
want to make sure that the images are in too big and that they are optimized for Web applications.

02:07.800 --> 02:10.590
So it's a good thing for your website to run faster.

02:10.680 --> 02:16.460
And it's also a good thing to make sure that this image is actually an image and it's not a harmful

02:16.470 --> 02:16.880
file.

02:17.190 --> 02:19.140
So let's just have a look on the source code.

02:19.170 --> 02:23.760
Now, I know I always say we're doing blackbox testing, but we're talking about the security now.

02:23.770 --> 02:26.710
So you actually have to see the source code to see how you can secure it.

02:27.570 --> 02:33.380
So I'm just going to set the security level to medium because the load just doesn't really check anything.

02:33.690 --> 02:39.150
So we're just going to go with the medium for now and we're going to go on the upload.

02:42.230 --> 02:48.680
And I'm going to view the source so as you can see now in the media, it's only checking if the uploaded

02:48.680 --> 02:50.780
file type is an image.

02:52.020 --> 02:56.970
Now, I don't want to get into too much details about coding, because this isn't a coding course,

02:57.480 --> 03:02.180
we're only talking about how you secure yourself and about penetration testing.

03:02.190 --> 03:03.950
So I have to cover the security part.

03:04.170 --> 03:09.180
So we're going to talk about the concepts, but not the actual code that you need to use because you

03:09.180 --> 03:11.050
could be using a different programming language.

03:11.400 --> 03:12.990
So in here, they're using you.

03:13.320 --> 03:19.020
And what they're doing is they have just a simple if statement and they're checking if the if the image

03:19.020 --> 03:21.350
of the uploaded file type is an image.

03:21.690 --> 03:28.080
So we see how we can bypass this easily by uploading an image and then changing the extension to by

03:28.080 --> 03:34.530
renaming our shell to the jpg, which we will upload it, which will be accepted as an image, and then

03:34.530 --> 03:39.030
we'll just rename it as it's been uploaded to a file.

03:40.380 --> 03:44.220
Now, if we send this to a high security level.

03:49.100 --> 03:52.820
And you go back to the upload and have a look at the source code.

03:54.050 --> 03:56.250
You can see that what they're doing here.

03:56.480 --> 04:05.150
They're checking that the extension is a jpg or EEG and then you can upload the picture so they're not

04:05.150 --> 04:06.920
checking for the file type this time.

04:07.340 --> 04:09.260
So, as I said, you need to check for both.

04:10.340 --> 04:16.970
Now, the new versions of DVDs and actually has an extra security, something called impossible and

04:16.970 --> 04:22.480
what that is, it basically just shows you the right implementation of each one of these pages.

04:22.670 --> 04:24.520
So it's it's actually can't be hacked.

04:24.530 --> 04:25.590
It's the right way to do it.

04:26.420 --> 04:32.300
The DVD version that comes in with me transplantable doesn't have that security setting, but we don't

04:32.300 --> 04:33.620
really need to use it anyway.

04:33.620 --> 04:34.760
We just need to see the code.

04:34.940 --> 04:36.380
So I have it here on GitHub.

04:36.980 --> 04:42.620
And the way the right way to implement this is if we look at this is the same if statement.

04:42.920 --> 04:47.840
And basically what they're checking is they check the extension is is an image extension.

04:47.840 --> 04:52.580
So it's a JPEG or JPEG or Pangea's.

04:52.610 --> 04:53.510
So that's all good.

04:53.840 --> 05:00.230
So it has to be one of these things and it has to be of an image type.

05:00.230 --> 05:07.250
So it has to be of type image jpeg or image PMG and it doesn't stop or that actually checks the sizes.

05:07.250 --> 05:08.020
But that's OK.

05:08.540 --> 05:13.620
What it also does, which I was speaking about, which is recreating the image itself.

05:14.240 --> 05:20.480
So what they're doing is they're they're stripping the metadata of the image and they're using a library

05:20.760 --> 05:22.520
actually they're not using that library.

05:22.520 --> 05:29.150
But you should be using that library, which is called magic or whatever there is that there are similar

05:29.150 --> 05:36.320
libraries for Python and Ruby and all of that to basically what they do is they recreate another image

05:36.320 --> 05:37.820
from the uploaded image.

05:41.060 --> 05:48.320
Once they do that, they actually delete and destroy the old image and use the new image with a new

05:48.320 --> 05:54.170
name, so they create a new image from the old image, they rename it and then they use that image.

05:54.410 --> 06:00.170
So these are three things you need to check for the extension, check for the file type, recreate the

06:00.170 --> 06:00.980
image itself.

06:01.520 --> 06:09.620
That way, even if a person tries to upload a bad file or a malicious file, then you'll be able to

06:09.620 --> 06:13.350
protect your Web application and your server from that file.

06:13.970 --> 06:19.970
So even if they manage to upload the file, then the file will be destroyed because of the recreation

06:19.970 --> 06:20.540
process.
