Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,910 --> 00:00:07,810
In the previous lecture, we learned how to allow the user to set variables using the input or zero
2
00:00:07,810 --> 00:00:08,890
input function.
3
00:00:09,640 --> 00:00:14,350
Now this is very useful and works perfectly as shown in that lecture.
4
00:00:15,040 --> 00:00:21,640
The only problem with this is that the way we use the variable and here is not very secure.
5
00:00:22,240 --> 00:00:28,120
Now, this might not be a problem when it comes to simple scripts like this, but I just want to give
6
00:00:28,120 --> 00:00:33,220
you this note so that you keep it in mind whenever you're writing your own programs.
7
00:00:33,790 --> 00:00:40,150
It's not very secure because we're allowing the user to input anything they want in here, and then
8
00:00:40,150 --> 00:00:42,430
we're not checking what this input is.
9
00:00:42,430 --> 00:00:50,440
So we're supposed to only allow the person to enter a name for the interface and a value for a new Mac.
10
00:00:51,040 --> 00:00:56,020
But the user can misuse this program and enter Linux commands.
11
00:00:56,470 --> 00:01:03,280
Then these commands are going to be substituted in here and the sub process dot called module and they'll
12
00:01:03,280 --> 00:01:05,650
actually be executed on the system.
13
00:01:06,310 --> 00:01:11,170
Now again, like I said, in this scenario of this program that we're writing right now, this might
14
00:01:11,170 --> 00:01:18,430
not be a very big of an issue, but you can argue the fact that this program is not very secure or is
15
00:01:18,430 --> 00:01:25,060
not very well implemented because it allows the user to use it to do other things than its purpose.
16
00:01:25,390 --> 00:01:27,760
So let me show you what I'm talking about.
17
00:01:28,540 --> 00:01:34,330
We all know if we do ls it's a list, all the files in the current directory.
18
00:01:34,330 --> 00:01:38,110
And as you can see, I have my mac changer and VM directory.
19
00:01:38,920 --> 00:01:41,440
Now let's run our Python script.
20
00:01:44,670 --> 00:01:51,630
And then when it asks me for interface, I'm going to put zero, but I'm also going to put a semicolon
21
00:01:52,020 --> 00:01:53,290
and put another command.
22
00:01:53,310 --> 00:01:53,890
Now the second.
23
00:01:53,940 --> 00:02:01,230
Now the semicolon in Linux is used to tell the terminal that I want to execute another command after
24
00:02:01,230 --> 00:02:02,310
the current command.
25
00:02:02,310 --> 00:02:04,170
So that's why I'm putting it here.
26
00:02:04,170 --> 00:02:08,100
And then I'm going to put LS, which is my second command.
27
00:02:08,669 --> 00:02:16,920
So what's going to happen now is the interface variable is going to have a value of zero semicolon L's
28
00:02:17,580 --> 00:02:19,910
that's going to be substituted in here.
29
00:02:19,920 --> 00:02:25,650
So this command is going to become if config line zero and of command because we have the semicolon
30
00:02:26,010 --> 00:02:28,890
and then the next command is going to be RLS.
31
00:02:29,460 --> 00:02:36,540
So I'm also going to put another semicolon after it to make sure that whatever that comes after this
32
00:02:36,540 --> 00:02:39,560
command is going to be treated as another command.
33
00:02:39,570 --> 00:02:45,750
And now when I hit enter, you'll see that we'll manage to hijack marked change or dot py, which is
34
00:02:45,750 --> 00:02:52,320
our program and get it to execute other commands on the system that the program is not supposed to execute.
35
00:02:52,560 --> 00:02:54,690
So we're just going to put anything for Mark.
36
00:02:55,820 --> 00:03:02,030
So as you can see, the first command was if config land zero and this is its result.
37
00:03:02,970 --> 00:03:07,380
And then it executed LZ, which is my second command.
38
00:03:07,560 --> 00:03:09,180
And this is its result.
39
00:03:09,180 --> 00:03:12,720
As you can see, it's listed in the files and the current working directory.
40
00:03:13,230 --> 00:03:20,100
Now, this output is repeated three times because the interface is used three times in here in these
41
00:03:20,100 --> 00:03:21,030
three lines.
42
00:03:21,690 --> 00:03:28,350
So basically what's happening is a user can hijack our program and get it to execute other commands
43
00:03:28,350 --> 00:03:34,620
that the program is not supposed to execute and the user is not supposed to be allowed to do these things
44
00:03:34,620 --> 00:03:35,880
through our program.
45
00:03:36,300 --> 00:03:43,140
And again, this might not be vital in this simple script that we're writing right now, but you want
46
00:03:43,140 --> 00:03:46,650
to keep this in mind whenever you're writing your own programs.
47
00:03:47,190 --> 00:03:53,850
Now, there is a very simple fix to this issue, and it can be implemented using the second way of calling
48
00:03:53,870 --> 00:03:54,900
sub process.
49
00:03:55,110 --> 00:04:00,780
So remember the first time I showed you the sub process module and I said that there is two ways of
50
00:04:00,780 --> 00:04:02,400
using the call function.
51
00:04:02,400 --> 00:04:08,070
We can use it using this way, which is the one that we've been using in all of the previous lectures.
52
00:04:08,520 --> 00:04:13,160
Or you can use it using this way, which is a little bit confusing.
53
00:04:13,170 --> 00:04:16,800
Hence I went with the second way to start up with you.
54
00:04:17,220 --> 00:04:21,649
Now, if you think of it this way is actually kind of simple once you just analyze it.
55
00:04:21,660 --> 00:04:24,870
So you still call in sub process, don't call.
56
00:04:25,140 --> 00:04:32,310
And the only difference is, instead of giving the command as a string like this, you're splitting
57
00:04:32,310 --> 00:04:36,930
the command in a number of elements inside a list.
58
00:04:37,590 --> 00:04:43,350
So each element or each string in here is actually one word.
59
00:04:43,350 --> 00:04:49,260
So every time you see a space in the command, you're supposed to close the quote, separate it with
60
00:04:49,260 --> 00:04:51,940
a comma, and put the rest of the command.
61
00:04:51,960 --> 00:04:57,840
The next time you hit a space, you close the quote, put a comma, and onto the next word.
62
00:04:58,320 --> 00:05:03,990
When you do this, python will know that the first element in here is the command.
63
00:05:04,110 --> 00:05:10,040
And everything that comes after it is arguments or is part of that command.
64
00:05:10,050 --> 00:05:15,510
So even if we try to hijack it the same way that we just did, Python will know that this should be
65
00:05:15,510 --> 00:05:20,610
part of the first command and should never be executed as a second command.
66
00:05:21,120 --> 00:05:24,450
Let me put this in practice and it will become clearer to you.
67
00:05:24,990 --> 00:05:29,180
So I'm actually going to retype the commands here below.
68
00:05:29,190 --> 00:05:32,880
So we're doing sub process, dot call.
69
00:05:34,580 --> 00:05:38,840
And then instead of giving a script, we're going to give a list.
70
00:05:38,840 --> 00:05:41,990
So we're going to open a square bracket and close it.
71
00:05:42,260 --> 00:05:45,580
And again, each element in here is going to be a string.
72
00:05:45,590 --> 00:05:48,860
And every time we see a space, we're going to close the element.
73
00:05:49,010 --> 00:05:52,700
So the command that we always use is usually if config.
74
00:05:54,650 --> 00:05:56,680
Then we're not going to put a space.
75
00:05:56,690 --> 00:06:04,100
We're going to go outside this string, put a comma space, and then type the next part, which is our
76
00:06:04,100 --> 00:06:06,050
interface, as you can see here.
77
00:06:06,230 --> 00:06:12,350
Now, because the interface is a variable, we don't need to enclose it in two quotation marks, so
78
00:06:12,350 --> 00:06:14,270
we're just going to type that the way it is.
79
00:06:16,190 --> 00:06:21,560
Then the next argument of the if config command is down.
80
00:06:21,950 --> 00:06:28,220
And again, we're enclosing this with two quotation marks because down is a string, it's part of the
81
00:06:28,220 --> 00:06:28,940
command.
82
00:06:30,160 --> 00:06:34,120
So if you compare this with that, it's actually very similar.
83
00:06:34,150 --> 00:06:41,080
The only difference is, instead of adding everything in a string, we have a list in which each element
84
00:06:41,110 --> 00:06:43,690
is a word in that command.
85
00:06:43,750 --> 00:06:47,860
So every time we see a space, we put it as a separate element.
86
00:06:48,520 --> 00:06:54,040
Now, this is actually the first time you see a list, and it's a very good example.
87
00:06:54,040 --> 00:07:02,020
As you can see, a list is just a number of elements that we enclose within two square brackets, this
88
00:07:02,020 --> 00:07:04,060
bracket and this bracket.
89
00:07:05,060 --> 00:07:08,090
Now we're going to have to do the same with the next two commands.
90
00:07:08,090 --> 00:07:13,250
So to to duplicate the current line, I'm just going to do control the twice.
91
00:07:13,340 --> 00:07:18,680
As you can see, it's doing it for me automatically and now I just need to do the second command.
92
00:07:18,680 --> 00:07:22,040
So the second command is already if config interface.
93
00:07:22,040 --> 00:07:31,280
Now this shouldn't be done, this should be set to h w and put a comma and the next word is either put
94
00:07:31,280 --> 00:07:36,950
a comma and finally we put the new Mac again.
95
00:07:36,950 --> 00:07:41,000
We don't need to enclose this in quotation marks because it's a variable.
96
00:07:41,510 --> 00:07:49,160
And finally, the last command is going to be if config interface up instead of down and that's it.
97
00:07:49,160 --> 00:07:55,340
So these three commands are a much more secure version of these three commands.
98
00:07:56,180 --> 00:08:03,800
It's more secure because now Python knows that the interface and down are all part of the same command,
99
00:08:03,800 --> 00:08:05,060
which is if config.
100
00:08:05,060 --> 00:08:11,690
So even if I try to hijack the interface variable and put a command in here, Python will know that
101
00:08:11,690 --> 00:08:18,260
this should not be treated as a command and it will try to execute it within the if config command here.
102
00:08:18,260 --> 00:08:24,260
This will return an error, but it won't allow an attacker to hijack the flow of our program.
103
00:08:25,240 --> 00:08:27,490
Now let's delete all of this.
104
00:08:28,480 --> 00:08:31,750
Save and test our program.
105
00:08:31,870 --> 00:08:37,120
So I'm going to clear all of this and we have our program here.
106
00:08:37,270 --> 00:08:42,520
It's asking me for the interface and I'm going to do one zero and follow it by LZ.
107
00:08:42,700 --> 00:08:44,260
Like we did before.
108
00:08:44,290 --> 00:08:47,230
I'm going to hit Enter, put anything for Mark.
109
00:08:47,500 --> 00:08:54,190
And as you can see, it's complaining about the existence of the code and the LZ, and then it's complaining
110
00:08:54,190 --> 00:09:00,820
about the interface name, but it's not executing the list on the system.
111
00:09:00,820 --> 00:09:06,760
So a person cannot use this program to do anything other that we want them to do.
112
00:09:07,360 --> 00:09:11,110
Now let's test the program and make sure that it actually works.
113
00:09:11,470 --> 00:09:16,300
So we set the interface to zero and put a new mark.
114
00:09:18,210 --> 00:09:20,910
And I'm just going to do three, three, three, three this time.
115
00:09:22,650 --> 00:09:24,450
We got the right message.
116
00:09:24,480 --> 00:09:27,990
Let's do if config land zero.
117
00:09:29,070 --> 00:09:34,440
And as you can see, the MAC address is changed properly to one one, two, two, three, three, three,
118
00:09:34,440 --> 00:09:35,550
three, three, three.
119
00:09:35,550 --> 00:09:38,820
And the program is working as expected.
12562
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.