1 00:00:00,180 --> 00:00:06,330 In this video you're going to learn how to change the MAC address of your devices with the python. 2 00:00:06,330 --> 00:00:09,480 Now let's start with some basic terminology. 3 00:00:09,630 --> 00:00:12,940 First what is a MAC address. 4 00:00:12,960 --> 00:00:19,350 So if a MAC address is being assigned to every network device by its manufacturer and it's actually 5 00:00:19,350 --> 00:00:26,220 being used for identification and communication purposes across the network it often is that the MAC 6 00:00:26,220 --> 00:00:28,820 address is Brendan on the device itself. 7 00:00:28,830 --> 00:00:32,760 So the the address is hardcoded in the device. 8 00:00:32,820 --> 00:00:39,220 However it can be spoofed or temporarily changed for different reasons or purposes. 9 00:00:39,240 --> 00:00:46,110 There are a lot of legitimate reasons when changing the MAC address makes sense such as when creating 10 00:00:46,110 --> 00:00:49,320 wireless connections to a particular network. 11 00:00:49,320 --> 00:00:57,240 And of course there are also many illegitimate reasons to do to do it such as for gaining unauthorized 12 00:00:57,360 --> 00:00:59,510 access to a network for example. 13 00:00:59,510 --> 00:01:04,200 And they need to say that you should only use the information in this video for legitimate purposes 14 00:01:04,260 --> 00:01:05,460 only. 15 00:01:05,460 --> 00:01:06,140 OK. 16 00:01:06,330 --> 00:01:13,770 One thing to add is that when spoofed the MAC address is changed until rebooting the device or the system. 17 00:01:13,920 --> 00:01:19,140 That said what we're going to build today is a python code that will change your mac address with a 18 00:01:19,140 --> 00:01:20,250 brand new one. 19 00:01:20,560 --> 00:01:26,580 So procedurally we're going to make this code compatible with Linux systems. 20 00:01:26,580 --> 00:01:33,790 And as you can see I have a linux Bharat operating system up and running here. 21 00:01:33,960 --> 00:01:36,200 So I've told you what's a MAC address. 22 00:01:36,210 --> 00:01:39,240 But how does it actually look like. 23 00:01:39,330 --> 00:01:49,900 So for example it can look like this A one B to a six for example 34 one B six D. 24 00:01:51,360 --> 00:01:57,730 So any combination of two characters from eight to f 0 2 9. 25 00:01:58,020 --> 00:02:00,020 This is all a MAC address looks like. 26 00:02:00,710 --> 00:02:01,200 All right. 27 00:02:01,200 --> 00:02:02,100 Now the fun part. 28 00:02:02,100 --> 00:02:07,060 The coding lets actually go to a nano Ed.. 29 00:02:07,290 --> 00:02:10,710 Well that's a new Mac dot view why. 30 00:02:11,010 --> 00:02:11,460 All right. 31 00:02:11,490 --> 00:02:18,840 So first of all in fourth I can just edit code in nano like they do in Visual Studio on windows. 32 00:02:18,870 --> 00:02:19,100 OK. 33 00:02:19,110 --> 00:02:22,700 So first we'll import random. 34 00:02:23,040 --> 00:02:30,790 We'll also need to import os for operating system commands and also sub process. 35 00:02:31,080 --> 00:02:38,460 So we'll create two functions that will help us get a valid random new mac address and this code is 36 00:02:38,460 --> 00:02:43,620 actually inspired by a post on Reddit which I'm willing to link in the code or in the resources of this 37 00:02:43,620 --> 00:02:44,630 video. 38 00:02:44,670 --> 00:02:53,340 We'll start with the basic function which is called get Rand and then this function is going to return 39 00:02:54,230 --> 00:03:05,220 random so we're going to look into random choice ABC the e f 0 1 2 3 4 5 6 7 8 9. 40 00:03:05,520 --> 00:03:15,910 So like I said from 8 to F and from 0 to 9 so this function is going to return a random character from 41 00:03:15,910 --> 00:03:17,390 this string. 42 00:03:17,470 --> 00:03:22,760 Then we create another function to actually create a completely new mac address. 43 00:03:22,760 --> 00:03:25,900 So we'll say get or not get less. 44 00:03:25,920 --> 00:03:34,550 OK actually seeing new Mac not you man but new Mac red an empty string. 45 00:03:35,350 --> 00:03:38,670 We start with an empty string like I said and we use a for loop. 46 00:03:38,680 --> 00:03:44,170 So for I in range 0 5 47 00:03:47,040 --> 00:04:00,750 we have new underscore plus equals get Rand plus get Rand plus this. 48 00:04:00,750 --> 00:04:06,130 Then we create a new Mac and I say that with a sub process call. 49 00:04:07,200 --> 00:04:24,630 So this is gonna give us something like let's say a B zero forty five 1 4 6 0 4 B and it also up until 50 00:04:24,720 --> 00:04:27,760 at this point if we run the new Mac this is what we can get. 51 00:04:28,200 --> 00:04:38,190 But we also see that we have common and this is unfinished because we have only one two three four five 52 00:04:38,190 --> 00:04:44,860 pairs and we need six pairs six double pairs in this case or any two. 53 00:04:45,000 --> 00:04:51,750 Another two character combination and to get to actually get the value MAC address so we exit the loop 54 00:04:51,870 --> 00:04:53,250 we exit the for loop. 55 00:04:53,250 --> 00:04:56,860 In this case let me just indented correctly. 56 00:04:57,180 --> 00:05:08,240 So we exit the for loop and then we'll say new on score plus equals get underscore Rand plus get underscore 57 00:05:08,310 --> 00:05:08,930 Rand. 58 00:05:09,600 --> 00:05:13,640 And this is going to complete our address with the last two characters. 59 00:05:13,710 --> 00:05:19,020 And finally this function is going to return the new underscore. 60 00:05:19,200 --> 00:05:19,550 OK. 61 00:05:19,590 --> 00:05:21,000 So these are the functions. 62 00:05:21,010 --> 00:05:24,140 Now let's actually change the Mac. 63 00:05:24,180 --> 00:05:25,620 What system calls. 64 00:05:25,620 --> 00:05:27,030 We change the MAC address. 65 00:05:27,030 --> 00:05:28,230 What system calls. 66 00:05:28,230 --> 00:05:30,400 So first let's pretty old address. 67 00:05:30,410 --> 00:05:45,420 We'll say print OS starts system I have config 88 zero and we'll grab for ether minus o e I'm gonna 68 00:05:45,420 --> 00:05:46,770 explain this shortly. 69 00:05:46,800 --> 00:05:49,560 0 from 9 2 0 from 9. 70 00:05:49,590 --> 00:05:57,990 ABC the E F we close the square brackets 17 characters and we close this one too. 71 00:05:58,140 --> 00:06:02,680 And I like to think I vote a veto on Reddit for this regex. 72 00:06:02,680 --> 00:06:08,340 Okay now we changed the address with some process and first we need to turn the ether 0 down. 73 00:06:08,670 --> 00:06:09,660 But let me show you. 74 00:06:09,690 --> 00:06:11,530 So if I do. 75 00:06:11,980 --> 00:06:17,800 And I have config here we can see 88 0. 76 00:06:17,820 --> 00:06:22,940 So we do I have conflict 88 0 if I do I have config 88 0. 77 00:06:23,100 --> 00:06:32,580 It's only going to show me 0 and if I grab for it ether like I did in this case it's going to only give 78 00:06:32,580 --> 00:06:39,970 me the line with the MAC address and then minus all we is going to give me exactly the MAC address itself. 79 00:06:40,020 --> 00:06:47,970 So if I do the entire thing here until shift C control shift the 80 00:06:51,340 --> 00:07:04,060 grep ether No such file or directory in this case it's being taken so 0 9 A B C D E F There is also 81 00:07:04,060 --> 00:07:04,960 another we're at 82 00:07:07,900 --> 00:07:08,860 so crap 83 00:07:11,710 --> 00:07:12,480 like this. 84 00:07:17,020 --> 00:07:19,510 And we only get the MAC address as you can see. 85 00:07:19,520 --> 00:07:25,520 So this is the command to get the MAC address and in order to change the address with the process with 86 00:07:25,520 --> 00:07:29,030 the sub process module WE NEED TO TURN THE PAGE 0 down. 87 00:07:29,030 --> 00:07:37,640 So we need sudo to make these changes and I'll just say so we need to make some process call and this 88 00:07:37,640 --> 00:07:42,760 call is going to run the command sudo f config. 89 00:07:43,220 --> 00:07:51,500 So you have to specify each command individually when you do some process call some process and os o 90 00:07:51,600 --> 00:07:56,090 our system and sub process call have similar functionalities. 91 00:07:56,090 --> 00:08:02,550 So 88 0 and also down this is going to turn it down. 92 00:08:03,050 --> 00:08:10,970 Then we get a new mac address instantiated in a variable so that's a new underscore am new Mac so we 93 00:08:10,970 --> 00:08:17,510 run the new Mac function that we've just created and then we make a system call to assign the new mac 94 00:08:17,540 --> 00:08:25,040 address to each DHS 0 so we'll also going to use the saw process call and then we're gonna send the 95 00:08:25,040 --> 00:08:27,110 following commands Sudo. 96 00:08:27,410 --> 00:08:29,780 I f config 97 00:08:32,550 --> 00:08:33,260 88. 98 00:08:33,280 --> 00:08:39,960 0 h w e thir. 99 00:08:40,040 --> 00:08:52,580 So for the either net present as first and new underscore M which is going to assign the new mac address 100 00:08:53,060 --> 00:08:54,340 with the sub process. 101 00:08:54,340 --> 00:08:56,900 Paul this is really cool. 102 00:09:00,070 --> 00:09:08,760 Then we can turn it 0 on with uh with the new mac address assigned to it another sub process start call 103 00:09:10,060 --> 00:09:12,960 Sudo. 104 00:09:13,510 --> 00:09:24,110 I have one thing e.g. zero up here we turned it down and now we turn it up or turn it on. 105 00:09:24,340 --> 00:09:28,710 And finally we can print the new mac address so save print. 106 00:09:28,900 --> 00:09:35,320 Actually OS that system I have config 88 0. 107 00:09:35,820 --> 00:09:42,870 We're at aether so it's very similar to the line here. 108 00:09:43,030 --> 00:09:57,190 Another grab it's actually the same thing minus all we from 0 to 9 A B C D E F close that 17 characters 109 00:09:59,410 --> 00:10:01,280 and we close everything. 110 00:10:01,750 --> 00:10:08,740 Awesome now control all to save control X to quit and let's actually run this. 111 00:10:08,860 --> 00:10:14,190 So our new if I do want I have config 88 0. 112 00:10:14,350 --> 00:10:20,730 We can see that our mac address is this 1 0 8 0 0 0 terminating in 0 9. 113 00:10:20,730 --> 00:10:28,060 And if I do Python new Mac that P Why of course we have the valid syntax that we eat that it's actually 114 00:10:28,060 --> 00:10:29,270 necessary. 115 00:10:29,500 --> 00:10:33,940 So it's not like this doesn't have to be in quotes. 116 00:10:34,900 --> 00:10:36,570 Let's run it again. 117 00:10:37,150 --> 00:10:44,950 Some process call when we have another valid and invalid syntax in this case. 118 00:10:44,950 --> 00:10:46,810 So we go back and correct that 119 00:10:50,850 --> 00:10:53,750 e.g. age 0 ether over here 120 00:10:56,780 --> 00:11:01,020 clear this and then run in one more time. 121 00:11:01,040 --> 00:11:06,050 Let's actually write with pseudo Python 122 00:11:08,830 --> 00:11:10,890 to underscore a mad dog P Y 123 00:11:13,630 --> 00:11:19,900 so if we run it with pseudo it actually changes the first time it didn't change. 124 00:11:19,900 --> 00:11:29,620 So if I do an IV config 88 to zero Now we can see that it changed it didn't want to change it the first 125 00:11:29,620 --> 00:11:34,210 time I have a hunch that it works without the pseudo. 126 00:11:35,170 --> 00:11:40,420 So if I run it again let's try that again yeah. 127 00:11:40,960 --> 00:11:46,840 So I don't have to actually run with Sudo it's probably because it doesn't actually change so fast as 128 00:11:46,840 --> 00:11:48,320 I run these commands. 129 00:11:48,460 --> 00:11:50,930 But anyway there you have it. 130 00:11:50,950 --> 00:11:53,450 This is how you change your mac address and by phone. 131 00:11:53,460 --> 00:11:58,630 Now I would encourage you to learn more about the sub process and the OS modules as they come in the 132 00:11:58,630 --> 00:12:02,710 python standard library and you can do some pretty heavy stuff with.