例如,本機的MAC地址為: 00-1C-47-CE-FE-02,在發送數據包的時候,如何將其修改為00-11-42-DF-EE-01
1.不能采用修改網卡的MAC地址的方式,因為修改後的地址是隨機動態的,要像方法中的一個參數一樣,隨時修改,不會影響網絡狀態,不會閃斷
2.修改發送數據包的MAC地址後,要能夠接收到返回的數據包
小弟不大懂網絡方面的知識,但無奈領導布置了任務,
不知能否實現,能實現,麻煩說明具體實現方法,如不能實現,也麻煩具體說明一下不能實現的理由。
如有興趣的大神可以加我QQ392124082研究研究
小弟感激不盡
物理MAC 地址是不能在系統或者程序中修改的,但對方通信端是 web服務器或者電信服務器,修改系統注冊表就可以了,能達到模擬真實MAC達到欺騙的目的,本人實測是完全可行的,但同一網域相同的MAC 會沖突,會導致一方不能聯網,如果路由端經過ARP雙向綁定你是沒有任何辦法的,這種概率非常低,下面是一個C#實例(非本人所寫,但實測可行),可以隨機修改也可以改成 指定MAC,主要還是看你要實現什麼目的,請詳細說明。
public class MACHelper
{
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(int Description, int ReservedValue);
/// <summary>
/// 是否能連接上Internet
/// </summary>
/// <returns></returns>
public bool IsConnectedToInternet()
{
int Desc = 0;
return InternetGetConnectedState(Desc, 0);
}
/// <summary>
/// 獲取MAC地址
/// </summary>
public string GetMACAddress()
{
//得到 MAC的注冊表鍵
RegistryKey macRegistry = Registry.LocalMachine.OpenSubKey("SYSTEM").OpenSubKey("CurrentControlSet").OpenSubKey("Control")
.OpenSubKey("Class").OpenSubKey("{4D36E972-E325-11CE-BFC1-08002bE10318}");
IList<string> list = macRegistry.GetSubKeyNames().ToList();
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
var adapter = nics.First(o => o.Name == "本地連接");
if (adapter == null)
return null;
return string.Empty;
}
/// <summary>
/// 設置MAC地址
/// </summary>
/// <param name="newMac"></param>
public void SetMACAddress(string newMac)
{
string macAddress;
string index = GetAdapterIndex(out macAddress);
if (index == null)
return;
//得到 MAC的注冊表鍵
RegistryKey macRegistry = Registry.LocalMachine.OpenSubKey("SYSTEM").OpenSubKey("CurrentControlSet").OpenSubKey("Control")
.OpenSubKey("Class").OpenSubKey("{4D36E972-E325-11CE-BFC1-08002bE10318}").OpenSubKey(index, true);
if (string.IsNullOrEmpty(newMac))
{
macRegistry.DeleteValue("NetworkAddress");
}
else
{
macRegistry.SetValue("NetworkAddress", newMac);
macRegistry.OpenSubKey("Ndi", true).OpenSubKey("params", true).OpenSubKey("NetworkAddress", true).SetValue("Default", newMac);
macRegistry.OpenSubKey("Ndi", true).OpenSubKey("params", true).OpenSubKey("NetworkAddress", true).SetValue("ParamDesc", "Network Address");
}
Thread oThread = new Thread(new ThreadStart(ReConnect));//new Thread to ReConnect
oThread.Start();
}
/// <summary>
/// 重設MAC地址
/// </summary>
public void ResetMACAddress()
{
SetMACAddress(string.Empty);
}
/// <summary>
/// 重新連接
/// </summary>
private void ReConnect()
{
NetSharingManagerClass netSharingMgr = new NetSharingManagerClass();
INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection;
foreach (INetConnection connection in connections)
{
INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection);
if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN)
{
connection.Disconnect(); //禁用網絡
connection.Connect(); //啟用網絡
}
}
}
/// <summary>
/// 生成隨機MAC地址
/// </summary>
/// <returns></returns>
public string CreateNewMacAddress()
{
//return "0016D3B5C493";
int min = 0;
int max = 16;
Random ro = new Random();
var sn = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}",
ro.Next(min, max).ToString("x"),//0
ro.Next(min, max).ToString("x"),//
ro.Next(min, max).ToString("x"),
ro.Next(min, max).ToString("x"),
ro.Next(min, max).ToString("x"),
ro.Next(min, max).ToString("x"),//5
ro.Next(min, max).ToString("x"),
ro.Next(min, max).ToString("x"),
ro.Next(min, max).ToString("x"),
ro.Next(min, max).ToString("x"),
ro.Next(min, max).ToString("x"),//10
ro.Next(min, max).ToString("x")
).ToUpper();
return sn;
}
/// <summary>
/// 得到Mac地址及注冊表對應Index
/// </summary>
/// <param name="macAddress"></param>
/// <returns></returns>
public string GetAdapterIndex(out string macAddress)
{
ManagementClass oMClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection colMObj = oMClass.GetInstances();
macAddress = string.Empty;
int indexString = 1;
foreach (ManagementObject objMO in colMObj)
{
indexString++;
if (objMO["MacAddress"] != null && (bool)objMO["IPEnabled"] == true)
{
macAddress = objMO["MacAddress"].ToString().Replace(":", "");
break;
}
}
if (macAddress == string.Empty)
return null;
else
return indexString.ToString().PadLeft(4, '0');
}
#region Temp
public void noting()
{
//ManagementClass oMClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementClass oMClass = new ManagementClass("Win32_NetworkAdapter");
ManagementObjectCollection colMObj = oMClass.GetInstances();
foreach (ManagementObject objMO in colMObj)
{
if (objMO["MacAddress"] != null)
{
if (objMO["Name"] != null)
{
//objMO.InvokeMethod("Reset", null);
objMO.InvokeMethod("Disable", null);//Vista only
objMO.InvokeMethod("Enable", null);//Vista only
}
//if ((bool)objMO["IPEnabled"] == true)
//{
// //Console.WriteLine(objMO["MacAddress"].ToString());
// //objMO.SetPropertyValue("MacAddress", CreateNewMacAddress());
// //objMO["MacAddress"] = CreateNewMacAddress();
// //objMO.InvokeMethod("Disable", null);
// //objMO.InvokeMethod("Enable", null);
// //objMO.Path.ReleaseDHCPLease();
// var iObj = objMO.GetMethodParameters("EnableDHCP");
// var oObj = objMO.InvokeMethod("ReleaseDHCPLease", null, null);
// Thread.Sleep(100);
// objMO.InvokeMethod("RenewDHCPLease", null, null);
//}
}
}
}
public void no()
{
Shell32.Folder networkConnectionsFolder = GetNetworkConnectionsFolder();
if (networkConnectionsFolder == null)
{
Console.WriteLine("Network connections folder not found.");
return;
}
Shell32.FolderItem2 networkConnection = GetNetworkConnection(networkConnectionsFolder, string.Empty);
if (networkConnection == null)
{
Console.WriteLine("Network connection not found.");
return;
}
Shell32.FolderItemVerb verb;
try
{
IsNetworkConnectionEnabled(networkConnection, out verb);
verb.DoIt();
Thread.Sleep(1000);
IsNetworkConnectionEnabled(networkConnection, out verb);
verb.DoIt();
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// Gets the Network Connections folder in the control panel.
/// </summary>
/// <returns>The Folder for the Network Connections folder, or null if it was not found.</returns>
static Shell32.Folder GetNetworkConnectionsFolder()
{
Shell32.Shell sh = new Shell32.Shell();
Shell32.Folder controlPanel = sh.NameSpace(3); // Control panel
Shell32.FolderItems items = controlPanel.Items();
foreach (Shell32.FolderItem item in items)
{
if (item.Name == "網絡連接")
return (Shell32.Folder)item.GetFolder;
}
return null;
}
/// <summary>
/// Gets the network connection with the specified name from the specified shell folder.
/// </summary>
/// <param name="networkConnectionsFolder">The Network Connections folder.</param>
/// <param name="connectionName">The name of the network connection.</param>
/// <returns>The FolderItem for the network connection, or null if it was not found.</returns>
static Shell32.FolderItem2 GetNetworkConnection(Shell32.Folder networkConnectionsFolder, string connectionName)
{
Shell32.FolderItems items = networkConnectionsFolder.Items();
foreach (Shell32.FolderItem2 item in items)
{
if (item.Name == "本地連接")
{
return item;
}
}
return null;
}
/// <summary>
/// Gets whether or not the network connection is enabled and the command to enable/disable it.
/// </summary>
/// <param name="networkConnection">The network connection to check.</param>
/// <param name="enableDisableVerb">On return, receives the verb used to enable or disable the connection.</param>
/// <returns>True if the connection is enabled, false if it is disabled.</returns>
static bool IsNetworkConnectionEnabled(Shell32.FolderItem2 networkConnection, out Shell32.FolderItemVerb enableDisableVerb)
{
Shell32.FolderItemVerbs verbs = networkConnection.Verbs();
foreach (Shell32.FolderItemVerb verb in verbs)
{
if (verb.Name == "啟用(&A)")
{
enableDisableVerb = verb;
return false;
}
else if (verb.Name == "停用(&B)")
{
enableDisableVerb = verb;
return true;
}
}
throw new ArgumentException("No enable or disable verb found.");
}
#endregion
}