IP address spoofing in c# using P/Invoke

According to Wiki, the term IP (Internet Protocol) address spoofing refers to the creation of IP packets with a forged (spoofed) source IP address with the purpose of concealing the identity of the sender or impersonating another computing system. For one of my project, I needed IP spoofing. According to a requirement, I need to commutate with a device over crossover cable for configuring it. But the problem is , when device boots  up , it gets an arbitrary IP when it is operating  over crossover connection as it cannot contract with DHCP Server. My PC is in a different network from the device. So i cannot communicate with that device thru socket programming. But one thing I can do, i can search the device using some proprietary protocol and find its information like its IP.

As I can get the IP of device, if I change the pc IP according to Device IP so that they are in the same network then I will be able to communicate with the device and configure it.  AddIPAddress Function of iphlpapi.dll can be used to add a specified IPv4 address to the specified adapter and    DeleteIPAddress function can be used to delete an IP address previously added using AddIPAddress. So using these two functions you can do IP spoofing .Your real IP4 address will be changed for a very short time when you are sending the packet. Another thing to note, a network interface can hold multiple IIP address and holds it in a IP table.

The code that is used is in the following:

   1: [DllImport("iphlpapi.dll", SetLastError = true)]
   2:  static extern UInt32 AddIPAddress(UInt32 Address, UInt32 IpMask, int IfIndex, out IntPtr NTEContext, out IntPtr NTEInstance);
   4: [DllImport("iphlpapi.dll", SetLastError = true)]
   5: static extern UInt32 DeleteIPAddress(IntPtr NTEContext);
   7:  static IntPtr ptrNteContext = new IntPtr(0);
   9: public static UInt32 AddIPAddressToInterface(string ipAddress, string subnetMask, int ifIndex)
  10: {
  11:   System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(ipAddress);
  12:   System.Net.IPAddress subNet = System.Net.IPAddress.Parse(subnetMask);
  13:   unsafe
  14:   {
  15:       int nteContext = 0;
  16:       int nteInstance = 0;
  17:       ptrNteContext = new IntPtr(nteContext);
  18:       IntPtr ptrNteInstance = new IntPtr(nteInstance);
  19:       return AddIPAddress(IpAddressToUInt32(ipAdd), IpAddressToUInt32(subNet), ifIndex, out ptrNteContext, out ptrNteInstance);
  20:   }
  21: }
  23: public static void  DeletePreviouslyAddedIP()
  24: {
  25:   DeleteIPAddress(ptrNteContext);
  26: }


Comments have been disabled for this content.