1
00:00:00,240 --> 00:00:02,460
Hello everybody and welcome to this tutorial.

2
00:00:02,460 --> 00:00:06,740
And right now let's cover the sports Canada conversion to a class.

3
00:00:06,930 --> 00:00:07,250
All right.

4
00:00:07,260 --> 00:00:13,110
So anyone who's actually covered and learned Python before knows what classes are and knows why they're

5
00:00:13,140 --> 00:00:14,220
important.

6
00:00:14,220 --> 00:00:20,760
And in this case in our case we want to make sure that we converted class for the better usage inside

7
00:00:20,760 --> 00:00:23,550
of these vulnerabilities cannot project.

8
00:00:23,550 --> 00:00:24,080
All right.

9
00:00:24,090 --> 00:00:29,990
So first thing that we're going to do is we're going to create the class at the top of this program.

10
00:00:30,060 --> 00:00:32,860
We're going to create it with the keyword class.

11
00:00:32,880 --> 00:00:35,850
And then we're going to call it let's say port scan

12
00:00:38,600 --> 00:00:40,950
open and close brackets and add two dots.

13
00:00:40,960 --> 00:00:43,370
And this is how we can create the class.

14
00:00:43,420 --> 00:00:48,610
Now all of these functions that we have below we want to make sure that they belong to our ports can

15
00:00:48,610 --> 00:00:50,130
class.

16
00:00:50,460 --> 00:00:53,750
So what we can do is we can tap each and every line.

17
00:00:53,760 --> 00:00:59,670
So let's do it one by one like this and you will see that some of these actual keyboards will start

18
00:00:59,670 --> 00:01:03,150
changing colors as they start belonging to our ports.

19
00:01:03,150 --> 00:01:04,440
Can class.

20
00:01:04,440 --> 00:01:04,830
All right.

21
00:01:04,830 --> 00:01:06,470
So let's do it like this

22
00:01:11,190 --> 00:01:13,050
all of it should be tapped once.

23
00:01:13,050 --> 00:01:16,890
So let's go like this and scan port at the end

24
00:01:27,820 --> 00:01:28,200
OK.

25
00:01:28,240 --> 00:01:29,550
So here it is.

26
00:01:29,650 --> 00:01:33,710
Now this part of the program you might be asking what we're going to do with this.

27
00:01:33,730 --> 00:01:37,700
Well in this case we don't need it so we can simply just delete that.

28
00:01:38,620 --> 00:01:42,300
All we need are our class with these functions right here.

29
00:01:42,850 --> 00:01:47,290
Let me just create space between each of these functions so we can see each and every one of them a

30
00:01:47,290 --> 00:01:48,220
little bit better.

31
00:01:48,730 --> 00:01:52,820
And now let's see what we need to do in order to get this to work.

32
00:01:53,290 --> 00:01:58,350
Well first of all we are missing a function that every class needs and that is the in its function.

33
00:01:58,390 --> 00:02:04,600
This init function will be coded at the top of the class or right below the initiation of the class

34
00:02:04,600 --> 00:02:05,050
itself.

35
00:02:05,050 --> 00:02:10,380
We are going to type def underscore underscore in it underscore underscore.

36
00:02:10,800 --> 00:02:11,440
All right.

37
00:02:11,680 --> 00:02:17,230
And you will notice that by default if I open and close brackets it will add this self argument as a

38
00:02:17,230 --> 00:02:18,220
parameter to this.

39
00:02:18,230 --> 00:02:24,730
And it function or in its method right here and this self argument basically means that it is belonging

40
00:02:24,730 --> 00:02:25,810
to this class.

41
00:02:25,840 --> 00:02:32,020
And what we're going to defined inside of this in its method is all of this stuff or all of the parameters

42
00:02:32,260 --> 00:02:35,080
that are going to define the object to our class.

43
00:02:35,080 --> 00:02:41,200
For example we want to define that target parameter and deport port number parameter that is an actual

44
00:02:41,230 --> 00:02:44,740
attribute to our class which defines our objects.

45
00:02:44,740 --> 00:02:45,520
All right.

46
00:02:45,550 --> 00:02:49,900
So next to the self argument we need to define those two attributes.

47
00:02:50,200 --> 00:02:52,860
So the first one we can call target.

48
00:02:52,870 --> 00:02:58,630
And the second one which is the new one will be called Port underscore number.

49
00:02:58,630 --> 00:02:59,290
All right.

50
00:02:59,290 --> 00:03:01,170
So simple as that.

51
00:03:01,270 --> 00:03:06,910
And in order to define them inside of the init function we simply just type self-doubt target will be

52
00:03:06,910 --> 00:03:14,320
equal to target and self that port number will be equal to port number.

53
00:03:14,320 --> 00:03:16,870
And this is just a python way to actually define them.

54
00:03:16,870 --> 00:03:18,640
So nothing really important there.

55
00:03:18,940 --> 00:03:25,630
Let me just delete this space and now that we have our init function we need to add this self argument

56
00:03:26,260 --> 00:03:32,710
or self parameter to each and every function that belongs to this class so we're simply just going to

57
00:03:32,710 --> 00:03:36,550
go right here and type self.

58
00:03:36,550 --> 00:03:38,590
We're also going to type self right here

59
00:03:43,130 --> 00:03:44,380
self right here

60
00:03:47,900 --> 00:03:50,440
and self right here.

61
00:03:54,770 --> 00:04:01,790
And another very important thing that we should consider is that we don't need any of these other parameters

62
00:04:01,910 --> 00:04:03,680
outside of the self parameter.

63
00:04:04,520 --> 00:04:05,720
And why is that.

64
00:04:05,720 --> 00:04:12,710
Well once you define those attributes that we need inside of this init method right here we can access

65
00:04:12,710 --> 00:04:17,240
these variables throughout each and every function in our class.

66
00:04:17,240 --> 00:04:23,060
So we don't need to paste them as parameters let us just delete everything but the self parameter from

67
00:04:23,150 --> 00:04:24,400
each and every class

68
00:04:28,670 --> 00:04:32,630
and right here as well.

69
00:04:32,630 --> 00:04:35,090
Make sure you do not deleted from the init method.

70
00:04:35,540 --> 00:04:37,420
So let's leave it like this.

71
00:04:37,460 --> 00:04:42,050
So now that we've fixed all of the methods Let's restructure our program a little bit.

72
00:04:42,050 --> 00:04:46,210
So I will start off with this can port function.

73
00:04:46,480 --> 00:04:46,910
All right.

74
00:04:46,910 --> 00:04:49,440
So what we're going to do with this can port function.

75
00:04:49,610 --> 00:04:54,810
Besides it's doing the usual stuff that it did inside of our port scanner project.

76
00:04:54,890 --> 00:05:03,050
We're going to add the converted IP into the scanned port function instead of this can function.

77
00:05:03,180 --> 00:05:06,320
So we're going to delete it from this can function first.

78
00:05:06,570 --> 00:05:09,100
Let's go right here.

79
00:05:09,210 --> 00:05:14,760
We also are not interested into printing anymore since printing we're going to do in the main program

80
00:05:14,760 --> 00:05:16,790
which is going to be the vulnerability scanner.

81
00:05:16,920 --> 00:05:22,290
Servers can function will be left with just these two lines of code while the conversion of the IP address

82
00:05:22,290 --> 00:05:25,090
will be moved right here into this can port function.

83
00:05:25,740 --> 00:05:28,320
So we're going to call the check IP function

84
00:05:32,370 --> 00:05:38,220
and you will notice right away that some of the stuff inside of our class is actually red underlined

85
00:05:38,780 --> 00:05:43,750
and by some of the stuff I mean a lot of things such as for example this check IP is underlined.

86
00:05:43,860 --> 00:05:49,080
These two variables are underlined this get banner is underlined the port is underlined.

87
00:05:49,110 --> 00:05:54,210
So all of this stuff are underlined which means that they are not recognized by the pie chart.

88
00:05:54,960 --> 00:05:56,930
Well why is that.

89
00:05:56,940 --> 00:06:01,660
Let's start off first with the functions themselves as to why they're underlined.

90
00:06:01,950 --> 00:06:06,540
Once you create the class you need to actually rename those functions when you call them inside of the

91
00:06:06,540 --> 00:06:07,380
class.

92
00:06:07,380 --> 00:06:10,710
You need to add the self argument before.

93
00:06:10,710 --> 00:06:18,000
So for example if I type self dot check IP you will notice that it will no longer be a red underlined.

94
00:06:18,000 --> 00:06:21,170
And this will get recognized by the pie chart.

95
00:06:21,180 --> 00:06:24,800
This is just a way to call different methods from the class itself.

96
00:06:24,840 --> 00:06:32,040
So the class can recognize that this check IP belongs to its own methods and therefore it knows which

97
00:06:32,160 --> 00:06:39,610
actual method to call Same goes with this get better function right here which we can simply just add

98
00:06:39,700 --> 00:06:44,730
self-doubt get better and it will stop being read underlying.

99
00:06:44,820 --> 00:06:47,970
Let's see whether we have another function which is read underlined.

100
00:06:48,020 --> 00:06:48,780
Here it is.

101
00:06:48,800 --> 00:06:59,170
Can port if I simply just typed self dot scan port we can see it works successfully now but what are

102
00:06:59,170 --> 00:07:03,510
we going to do with these actual variables which are red underlined.

103
00:07:03,520 --> 00:07:09,460
Well first of all we don't really need this IP address variable anymore as we are actually getting the

104
00:07:09,460 --> 00:07:14,530
IP address from our main part of the program which is going to be the target's IP variable right here

105
00:07:15,070 --> 00:07:20,470
and then we're going to paste it into our class which will then get stored inside of the cell the target

106
00:07:20,530 --> 00:07:24,220
variable which then we can use throughout our class.

107
00:07:24,220 --> 00:07:26,240
So let's change it everywhere we can.

108
00:07:26,260 --> 00:07:32,650
First of all we are going to change it in the check IP method so we're no longer checking the IP from

109
00:07:32,650 --> 00:07:39,070
the IP we are checking the IP from the self the target and make sure that throughout of this class you

110
00:07:39,070 --> 00:07:42,760
also use the self argument when specifying the variable name.

111
00:07:43,120 --> 00:07:47,620
So we are trying the IP function from cell to target and in case it works.

112
00:07:47,620 --> 00:07:51,970
We are returning cells to target in case it doesn't work.

113
00:07:52,030 --> 00:07:56,420
We want to return the get host by name from the cell the target.

114
00:07:56,620 --> 00:08:01,300
Once again keep in mind that this will store the IP address from our target machine.

115
00:08:02,300 --> 00:08:04,910
Same goes right here to the scan port.

116
00:08:04,910 --> 00:08:09,230
We don't really need this converted IP anymore and wide when we need it.

117
00:08:09,270 --> 00:08:14,900
While we don't need it because as you can see this can port function doesn't take the IP address as

118
00:08:14,900 --> 00:08:16,130
a parameter anymore.

119
00:08:16,190 --> 00:08:19,700
Therefore this is Red underlined so we can simply delete it.

120
00:08:20,780 --> 00:08:22,450
But what with this port.

121
00:08:22,460 --> 00:08:28,510
No well we actually need to send this port as a parameter because we are inside of this for loop.

122
00:08:28,520 --> 00:08:35,300
Therefore this port will change through each iteration and we need to specify to this method right here

123
00:08:35,600 --> 00:08:38,270
which iteration it is currently at.

124
00:08:38,270 --> 00:08:40,760
So we need to send the port as an argument.

125
00:08:40,760 --> 00:08:47,150
Therefore we're going to go to the scan port and next to the self we're going to add port as a parameter

126
00:08:47,480 --> 00:08:54,890
and you will see right here at these three spots the red underline will go away as this port now exists

127
00:08:54,980 --> 00:08:56,100
in this program.

128
00:08:56,300 --> 00:09:01,760
And the last part which red underlined is this IP address right here.

129
00:09:01,760 --> 00:09:04,850
Once again we don't really need this IP address anymore.

130
00:09:04,910 --> 00:09:10,520
We have self-doubt targets and right here since at the beginning of this trite statement we converted

131
00:09:10,520 --> 00:09:11,480
the IP.

132
00:09:11,480 --> 00:09:14,180
We don't need to specify cell targets right here.

133
00:09:14,180 --> 00:09:21,500
We can specify converted IP alright since this will be the IP address whether the target was specified

134
00:09:21,500 --> 00:09:24,890
as a domain or simply as an IP address.

135
00:09:24,900 --> 00:09:26,130
All right.

136
00:09:26,150 --> 00:09:30,980
Another thing that we want to make sure is that we don't have unnecessary functions that can be put

137
00:09:31,010 --> 00:09:32,620
inside of a different functions.

138
00:09:32,630 --> 00:09:37,650
For example this get better can also be put inside of this port function.

139
00:09:37,700 --> 00:09:46,550
Therefore we don't really need this method right here so we can simply just delete the get banner and

140
00:09:46,550 --> 00:09:50,350
we can put it right here as you will see.

141
00:09:50,350 --> 00:09:54,130
This will get flagged as get banned or doesn't exist anymore.

142
00:09:54,130 --> 00:09:59,800
Therefore instead of trying to call this function but we're going to do is we're going to write to get

143
00:09:59,800 --> 00:10:01,700
better function code instead right here.

144
00:10:01,810 --> 00:10:07,150
So SOC that receive.

145
00:10:07,180 --> 00:10:14,250
We want to receive 2024 bites and we are simply just using the sock object that we created right here.

146
00:10:14,350 --> 00:10:15,810
So no worries about that.

147
00:10:15,820 --> 00:10:18,860
We don't need to name it anything differently.

148
00:10:18,880 --> 00:10:20,830
We also want to decode the response

149
00:10:23,960 --> 00:10:29,180
and the reason why we are decoding the response is so we didn't really have to do it later on right

150
00:10:29,180 --> 00:10:37,630
here so once we decode the response we then want to strip it from any unnecessary characters such as

151
00:10:37,630 --> 00:10:38,970
for example backslash.

152
00:10:39,400 --> 00:10:43,760
And also we want to strip it from backslash R.

153
00:10:44,400 --> 00:10:50,560
And the reason why we are performing this stripping part is because especially in this program right

154
00:10:50,560 --> 00:10:52,240
here a number we're about Iskander.

155
00:10:52,360 --> 00:10:57,910
It is important to strip everything that we don't need from the response as this banner variable goes

156
00:10:57,910 --> 00:11:03,440
toward the most important and crucial part to our vulnerability scanner as inside of this program we're

157
00:11:03,460 --> 00:11:10,780
going to compare this banner variable with the actual content from this portability file in order if

158
00:11:10,780 --> 00:11:11,940
they match.

159
00:11:11,980 --> 00:11:16,840
So for example if we have the same banner in a vulnerability file and the same banner gets retrieved

160
00:11:16,900 --> 00:11:23,680
into this variable and imagine that we do not strip these actual characters from it while our program

161
00:11:23,680 --> 00:11:26,520
will not really find the match as they will be different.

162
00:11:26,560 --> 00:11:28,770
Only by this character.

163
00:11:28,770 --> 00:11:33,220
So that's why we're stripping it as the new line character is not important to us.

164
00:11:33,250 --> 00:11:34,530
All right.

165
00:11:34,540 --> 00:11:36,770
So simple as that.

166
00:11:37,170 --> 00:11:42,610
And the next thing we actually don't need is these print statements right here we need them inside of

167
00:11:42,610 --> 00:11:47,560
our port scanner project but we don't need them anymore as we are not really interested in printing

168
00:11:47,560 --> 00:11:55,390
which ports are closed and which ports are open since this is not a port scanner but however there is

169
00:11:55,390 --> 00:12:03,420
another problem that will occur and that is that this banner can only store one banner at the time.

170
00:12:03,430 --> 00:12:08,420
But we need to retrieve multiple banners if we find multiple ports open on the targets.

171
00:12:08,470 --> 00:12:12,670
And if we also manage to retrieve multiple ballots from those open ports.

172
00:12:12,670 --> 00:12:16,410
So we will need to store multiple banners and not just one.

173
00:12:16,580 --> 00:12:18,090
So how can we fix that.

174
00:12:18,160 --> 00:12:20,710
Well we can actually easily fix that.

175
00:12:20,710 --> 00:12:25,960
We can simply just add a list which will be at the beginning of our class.

176
00:12:25,960 --> 00:12:31,180
Right here we're going to call it banners and in order to define a list we specified these square brackets

177
00:12:31,180 --> 00:12:34,240
right here by specifying open and close square brackets.

178
00:12:34,240 --> 00:12:40,130
We initiate that these pandas list will be empty for now and then every time we actually managed to

179
00:12:40,130 --> 00:12:49,600
retrieve the banner right here with this line we can then write after it below append the actual banner

180
00:12:49,690 --> 00:12:56,110
to the banners list just like this and you will notice that this banner is that these banners is read

181
00:12:56,140 --> 00:12:56,660
underlined.

182
00:12:56,680 --> 00:13:04,330
That means that we need to add these self dot banners argument right here and everything will work correctly.

183
00:13:04,660 --> 00:13:09,810
And in case we don't manage to retrieve the banner we're simply just going to pass for now.

184
00:13:09,820 --> 00:13:10,850
All right.

185
00:13:10,900 --> 00:13:16,030
And at the end we can simply just close the connection with socket out close.

186
00:13:16,570 --> 00:13:18,520
So simple as that.

187
00:13:18,520 --> 00:13:21,980
Let me see if everything is correct for now.

188
00:13:22,300 --> 00:13:27,440
Everything seems to be good our get check I.P. function is good.

189
00:13:27,510 --> 00:13:29,590
Our scan is good.

190
00:13:29,590 --> 00:13:31,750
But don't worry.

191
00:13:31,750 --> 00:13:37,090
There is another thing that we actually have to do which is going to be to create another list which

192
00:13:37,090 --> 00:13:41,750
is going to be the open ports list.

193
00:13:41,820 --> 00:13:45,440
Now you might be asking why are we actually doing this.

194
00:13:45,640 --> 00:13:48,020
And this is more easily shoulder than explained.

195
00:13:48,040 --> 00:13:50,590
But I will try to explain it any void right now.

196
00:13:50,590 --> 00:13:52,290
And once you run the program you will get it.

197
00:13:52,300 --> 00:13:53,650
Why we need these open ports.

198
00:13:53,650 --> 00:13:55,050
List for now on.

199
00:13:55,060 --> 00:14:01,120
Let me just try to explain it well once we actually created this class right here with these three methods

200
00:14:01,620 --> 00:14:08,020
you'll notice that we also had to create these banners list right here in order to store multiple banners.

201
00:14:08,020 --> 00:14:13,390
Once we actually get to actually printing those banners and open ports into our vulnerability scanner

202
00:14:13,840 --> 00:14:18,330
we want to make sure that each open port will match to each banner.

203
00:14:18,400 --> 00:14:23,820
And since we removed all of the print statements we cannot really print open port one by one.

204
00:14:23,890 --> 00:14:29,380
We have to store all of the open ports somewhere and all of the band or somewhere and then we have to

205
00:14:29,380 --> 00:14:31,820
print them each element one by one.

206
00:14:32,440 --> 00:14:36,910
That's why we also need the open port list that we created right here.

207
00:14:36,910 --> 00:14:42,690
And after each time we manage to connect to a port we will add that port to the open ports list.

208
00:14:42,880 --> 00:14:48,260
So South that open ports and then dot append the same way we are adding the Benders.

209
00:14:48,270 --> 00:14:53,780
We're also going to add open ports and we're simply just going to specify right here port.

210
00:14:53,800 --> 00:14:55,110
All right.

211
00:14:55,120 --> 00:15:03,580
Now that is not the end of our problems you won't noticed once we actually had ports can projects that

212
00:15:03,700 --> 00:15:10,180
we had more open ports then more vendors retrieved for example some of the ports that were open and

213
00:15:10,180 --> 00:15:16,480
that we tagged as open weren't sending us any banner therefore we just didn't have banner for that open

214
00:15:16,480 --> 00:15:23,920
port and that could present us a problem because if we have 10 open ports for example and we retrieve

215
00:15:23,980 --> 00:15:30,400
only three banners then in one list which will be the open ports list we will have 10 elements or 10

216
00:15:30,400 --> 00:15:34,370
ports and in Debenhams list we will have three elements.

217
00:15:34,510 --> 00:15:40,420
And therefore once we want to print each element one by one for example the element one from the open

218
00:15:40,420 --> 00:15:47,140
ports should correspond to the element one from banners and so on and so on it will get confused in

219
00:15:47,140 --> 00:15:50,070
some of the open ports which don't have banners.

220
00:15:50,080 --> 00:15:55,170
We'll get banners and it will all get mixed up and it will not be correct.

221
00:15:55,210 --> 00:16:00,670
Therefore we want to make sure that the open port list has the exact same amount of elements as the

222
00:16:00,670 --> 00:16:02,570
banners list has.

223
00:16:02,890 --> 00:16:08,350
So each element can respond to each element from the different list.

224
00:16:08,440 --> 00:16:12,970
How can we do that since we are obviously going to have less banners than open ports.

225
00:16:12,970 --> 00:16:18,050
Well we can fix that just by instead of the past statement right here under the accept.

226
00:16:18,250 --> 00:16:21,120
We can also point to the vendors list.

227
00:16:21,440 --> 00:16:27,820
So for each open port we're going to append anyway even if it manages to retrieve the banner We are

228
00:16:27,820 --> 00:16:33,850
going to spend and if it doesn't manage to retrieve the banner we're also going to append so self banners

229
00:16:33,970 --> 00:16:34,870
dot append.

230
00:16:34,990 --> 00:16:38,860
But in this case we are simply just going to append empty space.

231
00:16:38,860 --> 00:16:43,630
We are not going to spend any stream or anything else it will simply just be there.

232
00:16:43,630 --> 00:16:47,680
So an element can get added to the banners list.

233
00:16:47,680 --> 00:16:48,560
All right.

234
00:16:48,670 --> 00:16:55,900
So we simply change this so we can have the same amount of elements in both banners and open ports.

235
00:16:55,900 --> 00:17:02,200
And with this we successfully transformed our port scanner into an actual class that we can use inside

236
00:17:02,200 --> 00:17:07,930
of our vulnerability scan project and in the next video we're going to see how we can call this class

237
00:17:07,930 --> 00:17:10,080
from our vulnerabilities scanner.

238
00:17:10,220 --> 00:17:10,550
OK.

239
00:17:10,600 --> 00:17:15,430
So thank you for watching this tutorial and I will see you in the next one by.
