Thursday, February 11, 2010

Changing the MAC address of the NIC

Recently I wanted to change the MAC id of the NIC, and was searching in the net to find a solution for how to do this from a computer program (By not manually changing the Windows Registry). So I gonna share what I have found from my search.

Please reply to this post with ur findings.

'VB.NET 2005

Imports System.Management
Imports System.Security.AccessControl

Public Class Form1
Private Sub FillNetworkAdapters()
Dim mc As System.Management.ManagementClass
Dim mo As ManagementObject
mc = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances()
For Each mo In moc
If mo.Item("IPEnabled") = True Then
Dim strAdapter As String
strAdapter = mo.Item("Caption").ToString().Substring(11)
ListBox1.Items.Add(strAdapter)
End If
Next
End Sub

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim rs As New RegistrySecurity()


FillNetworkAdapters()
Label1.Text = ""
End Sub
Private Function GetMACAddress(ByVal Adapter As String) As String
Dim mc As System.Management.ManagementClass
Dim mo As ManagementObject
mc = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances()
For Each mo In moc
If mo.Item("IPEnabled") = True Then
Dim strAdapter As String
strAdapter = mo.Item("Caption").ToString().Substring(11)
If strAdapter = Adapter Then
Return mo.Item("MacAddress").ToString()
End If
End If
Next
End Function
Private Function DoPadding(ByVal x As String) As String
Dim Ret As String
Dim z As Integer

Ret = x
If Len(x) <>
For z = 1 To 4 - Len(x)
Ret = "0" & Ret
Next
End If

Return Ret
End Function
Private Function GetRoot(ByVal Adapter As String) As String

Dim rs As New RegistrySecurity()

' Allow the current user to read and delete the key.
'
rs.AddAccessRule(New RegistryAccessRule("Karnna", _
RegistryRights.FullControl, _
InheritanceFlags.None, _
PropagationFlags.None, _
AccessControlType.Allow))


Dim regKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine

Dim i As Integer = 0

Do
Dim Root As String = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\"
Dim Last As String = DoPadding(i)
regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Root & Last, True)
regKey.SetAccessControl(rs)
Try
Dim cAdapter As String = regKey.GetValue("DriverDesc").ToString()
If cAdapter = Adapter Then
Return Root & Last
End If
Catch

' Exit Do
End Try
i += 1
Loop


End Function

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim regKey As Microsoft.Win32.RegistryKey
Dim Addr As String = GetRoot(ListBox1.SelectedItem.ToString())
regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(Addr, True)

regKey.SetValue("NetworkAddress", "00e04c4d1dA2")

End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim mc2 As System.Management.ManagementClass
Dim mo2 As ManagementObject
mc2 = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc2 As ManagementObjectCollection = mc2.GetInstances()
Dim xIP As String()
For Each mo2 In moc2
If mo2.Item("IPEnabled") = True Then
Dim strAdapter
strAdapter = mo2.Item("Caption").ToString().Substring(11)
If strAdapter = ListBox1.Text Then
xIP = mo2.Item("IPAddress")
Label1.Text = xIP(0)
End If
End If
Next
End Sub
End Class

No comments:

Post a Comment