C# How to spoof IP address for WebRequest - c#

I have asp.net website hosted and I am making WebRequest to post data and get response. The website is having IP filtering. I want to spoof sender IP address for testing purpose. Is it possible to do it programmatically or I have to use any tool.
public string GetResponse(string request)
{
lock (Obj)
{
request = request + _dataControlInfo.SendEndingWith;
Logger.Info(request);
var req = (HttpWebRequest)WebRequest.Create(_serviceUrl);
req.Headers.Add("SOAPAction", "\"\"");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
var stm = req.GetRequestStream();
var bytes = UtfEncoding.StringToUtf8ByteArray(request);
stm.Write(bytes, 0, bytes.Length);
stm.Close();
var resp = req.GetResponse();
var stmr = new StreamReader(resp.GetResponseStream());
var strResponseXml = stmr.ReadToEnd();
Logger.Info(strResponseXml);
return strResponseXml;
}
}
Please specify any possibilities.

What your looking for is SharpPCap which is a .NET port of WinPCap.. it allows you to do IP Spoofing, which is what your talking about. The only problem with your idea is that you wont be able to get a response back. You can send requests out, but if you dont have a proper return address then the request will be lost in the interwebs.
Edit
To do this yoruself w/out the help of a library you will need to construct the raw packets yourself. This has been answered here.

If you're expecting to get a response, then no. Without the correct IP address, the server won't send the response to the correct destination.
If you insist on trying anyway, see this article for programmatically setting the client's IP address.

Or you can use Web Performance Tests and a load test with IP Switching enabled

You can try to use a proxy, as documented here.
( http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.proxy.aspx ).
Setting up a proxy on a different computer, then configuring that computer as your requests proxy server should make the request appear as if it came from the proxy's IP, not yours.

Some servers can also consider X-Forwarded-For and X-Real-IP headers.
So if server checks for these headers you can add them to your Web request.
But it depends on server implementation.

Use the Spoof class found in the System.Security namespace...

Related

Http request with specified ip

Can I send http request with custom server IP? For examle I have a domain example.com (real ip is 1.2.3.4) but DNS returns secondary ip (6.7.8.9). Can I do request to 1.2.3.4 istead of 6.7.8.9?
This sounds like a DNS issue and not a code issue. If you are explicitly trying to communicate with a given URL, you should not have to use any "hack" to send packets to a predetermined IPAddress. That being said, you should be able to communicate directly with your given IP if necessary, as you wont need DNS if you are targeting a client directly via IPAddress. If you are working locally you might need to update your ARP table.
(edit: Are you trying to spoof your IPAddress or are you trying to connect to a client? Your wording of server/client is not clear, so if my answer is incorrect, please let me know.)
A little time ago.
With this code I test the webservices on 7 diferents nodes:
HttpWebRequest request = WebRequest.CreateHttp(server + _tempWS);
//DataConnection getUser = GetValidUser(server);
try
{
string cook = ConfigurationManager.AppSettings["IdentityGeneratedValidToken"];
request.Method = "POST";
request.Headers.Add("Cookie", "FedAuth=" + cook);
request.ContentType = "application/json; charset=UTF-8";
request.Referer = server ;
string data = "{\"Pagina\":1,\"Registros\":10,\"Orden\":\"{}\",\"Filtro\":\"{ }\",\"EsInternacional\":false}";
byte[] postBytes = Encoding.ASCII.GetBytes(data);
request.ContentLength = (long)postBytes.Length;
request.ContentLength = 0L;
request.Timeout = 3000;
}
On header set request.Referer to your server.

Using System.Net.WebRequest with a known IP end point

I have a large collection of DNS names that have already been resolved to IP addresses. With this collection I need to download HTML from them. It's a very large list and I need to do it as efficiently as possible.
I'm using System.Net.HttpWebRequest to download HTML from the each domain. HttpWebRequest is repeating the DNS lookup, and this is adding to the connection time. I've run tests to see if sockets for those IPs on port 80 would connect faster and they do.
So I'd like to use HttpWebRequest with a known IP address, but I don't know how. All WebRequest factory methods require a URL.
Now I thought I could do something like this (where 1.2.3.4 is the IP)
var req = WebRequest.Create("http://1.2.3.4/");
req.Headers.Add(....); <-- add something here
I need to somehow add to the HTTP header what the target domain is, but I'm not sure how to do it.
Pretty simple:
var ip = "93.184.216.119";
var host = "example.com";
var ipUri = new UriBuilder(Uri.UriSchemeHttp, ip).Uri;
var request = WebRequest.CreateHttp(ipUri);
request.Host = host;
using (var response = request.GetResponse())
{
// do something with response
}

HttpWebRequest.Create and firewalls

I need to check whether a given host is replying to HTTP web requests.
So, all I need to do is HttpWebRequest.Create(someURI) and check the response, right?
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(targetUri);
TimeSpan timeOut = TimeSpan.FromSeconds(timeOutInSeconds);
webRequest.Timeout = (int)timeOut.TotalMilliseconds;
webRequest.Proxy = proxy;
webRequest.AllowAutoRedirect = false; // get extra information with this turned off, but doesn't affect the problem
response = webRequest.GetResponse() as HttpWebResponse;
Well, the problem that's come up is that if someURI is blocked by our firewall (e.g. "gibber.com" is blocked), then I get back a valid HttpWebResponse, it doesn't throw, and the ResponseURI property is set to the blocked URI ("gibber.com") even though it's actually our server responding.
Within the HTTP header there will be a Location key and its value is the IP of the local server. But I don't want to parse text to work out if a request actually bounced. What's the correct way to do this?
Thanks
check to see if response.ResponseUri corresponds to the given host or your firewall.

Adding Headers and Post data in RESTfull/HTTP Request in C#

I'm having problems with sending POST request in C# and it seems I misunderstood some HTTP basics. So basically I'm implementing RESTfull service client, which work as follows:
Make POST request with username/password and get token
Use this token in header (Authorization:TOKEN) while making other GET/POST/PUT requests
I use WebRequest to make GET requests (with Authorization header) and it's working. But when I use following code to make PUT requests, service is giving "Authentication failed - not logged in" message back:
String url = String.Format("{0}/{1}", AN_SERVER, app);
WebRequest theRequest = WebRequest.Create(url);
theRequest.Method = "POST";
theRequest.ContentType = "text/x-json";
theRequest.ContentLength = json.Length;
Stream requestStream = theRequest.GetRequestStream();
requestStream.Write(Encoding.ASCII.GetBytes(json), 0, json.Length);
requestStream.Close();
theRequest.Headers.Add("Authorization", authToken);
HttpWebResponse response = (HttpWebResponse)theRequest.GetResponse();
I must be making minor mistake (at least I hope so) while sending POST request. So what am I doing wrong?
Thanks.
Moving Headers before the request steam works (as per AI W's comment), because the request stream is adding the body.
The way webrequest is implemented internally, you need to finish the header before writing body, and once its in stream format, its ready to send.
If you look at the implementation of webrequest in reflector or some such decompiling tool, you'll be able to see the logic.
Hope this helps

Cannot handle redirect from HTTP/HTTPS protocols to other dissimilar ones

Basically, I'm trying to grab an EXE from CNet's Download.com
So i created web parser and so far all is going well.
Here is a sample link pulled directly from their site:
http://dw.com.com/redir?edId=3&siteId=4&oId=3001-20_4-10308491&ontId=20_4&spi=e6323e8d83a8b4374d43d519f1bd6757&lop=txt&tag=idl2&pid=10566981&mfgId=6250549&merId=6250549&pguid=PlvcGQoPjAEAAH5rQL0AAABv&destUrl=ftp%3A%2F%2F202.190.201.108%2Fpub%2Fryl2%2Fclient%2Finstaller-ryl2_v1673.exe
Here is the problem: When you attempt to download, it begins with HTTP, then redirects to an FTP site. I have tried .NET's WebClient and HttpWebRequest Objects, and it looks like Neither can support Redirects.
This Code Fails at GetResponse();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://dw.com.com/redir");
WebResponse response = req.GetResponse();
Now, I also tried this:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://dw.com.com/redir");
req.AllowAutoRedirect = false;
WebResponse response = req.GetResponse();
string s = new StreamReader(response.GetResponseStream()).ReadToEnd();
And it does not throw the error anymore, however variable s turns out to be an empty string.
I'm at a loss! Can anyone help out?
You can get the value of the "Location" header from the response.headers, and then create a new FtpWebRequest to download that resource.
in your first code snippet you will be redirected to a link using a different protocol (i.e it's no longer Http as in HttpWebRequest) so it fails du to a malformed http response.
In the second part you're no longer redirected and hence you don't receive a FTP response (which is not malform when interpreted as HTTP response).
You need to acquire FTP link,as ferozo wrote you can do this by getting the value of the header "location", and use a FtpWebRequest to access the file

Categories

Resources