Razan Paul Blog

Explaining thoughts and findings is a great way to learn

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: }


Ubaid Ali said:

Sir i Need The .Zip file i am getting Problem While Attempting to Place your code in Visual Studio can u Give me the ZIP file..

I shall be very thankfu to you

# October 21, 2009 2:50 AM

RazanPaul said:

Sorry, the working code is the part of a commercial project. Bur if you have question, I will love to answer

# October 21, 2009 2:26 PM

Veldignee said:

Я все понял и ни чего не понял:)

# November 1, 2009 1:49 AM

Ashutosh Vyas said:


The function is unknown here......

# February 16, 2010 9:23 PM

Aftab said:

hii... i have to make a software to generate fake visitors to a website for which ip spoofing would be needed. I want to know how to proceed with the project and what topics to study to make the project.

# March 29, 2010 4:57 AM

Pradeep said:

plz can u tell me that how can use proxy list in my application to send multiple packets anonymously..

# April 30, 2010 1:38 PM

Ben said:

How is the method "IpAddressToUInt32(...)" implement?

thanks for answer

# June 2, 2010 8:43 PM

Ar4iks said:

Code didn't work IP address dosn't changed. May be I need write something in code to do this task?

# June 17, 2010 4:29 AM

sildenafil said:

Do you have any more info on this?

# March 28, 2011 1:40 PM