I have an app that makes a request to a server and gets authenticated.
I'm wondering if it is a possible to write a listener that listens for requests and if any request matches that particular request the listener sends back a message, without it actually getting to the server.
In essence I want to make the app. think it has been authenticated when it actually hasn't.
This is what the tcp stream looks like on WireShack. The highlighted response is exactly what I want my code to respond to.
Is this possible?
Yes. It's called a "proxy", and you can do that in C# by using System.Net.Sockets namespace. Or you can use Fiddler and forget about programming at all. :) (Btw - AFAIK Fiddler is written in C# too)
Related
I have a C# application and I want to capture outgoing http requests that have been made through the application (I can also identify that it was from the app because I have a specific server name).
When searching on the web all I could find was capturing incoming requests (so the code is on the server side) with TcpListener and HttpListener.
But the code must be client side so it must be outgoing requests.
And I cannot use any third party libraries (like FiddlerCore for example).
So I'm really looking for a code sample to start from.
Do you want to store the request or just access it for debugging?
If you are going after debugging, then you can use Fiddler. Fiddler is an HTTP debugging proxy server application. And if you are planning to modify or read data in the request or the response you can use HTTPRequestWrapper and HTTPResponseWrapper to access.
I want to write a little c# tool for a web application (which runs inside a browser). Unfortunately, I have no idea how to hang into the HTTP communication.
There are a lot of tools that listen to network traffic and extracting relevant data out of it so I guess there must be a solution. My current approach is to catch the responses by listening on the corresponding port. But:
HTTPListener seems to be a server-side class that accepts HTTP requests
HTTPRequest is just the message object and has to be received before
I don't think TCPListener would be applicable because I don't want to create a connection
Is there another solution how to just get the server response without sending a request? Or might another approach be more reasonable?
I don't want to write a proxy because I simply don't want a server, just a lightweight, easy-to-use tool.
I was not able to find any hints on this topic since all results of my searches were about receiving responses to own requests or similar.
Thanks in advance for any solutions or suggestions!
Phil
If you don't got the idea of the tool, here is an example:
A user has firefox running and navigates to the target web application
He does anything and the web application sends the corresponding data to the web server
The web server processes the request and sends back a response
The web application updates itself inside the browser to display the received data
My tool (C# application) should do the following:
Catch the response of step 3 and evaluate the response to also update its own state
OR: do anything else (suggestions?) to keep its state up-to-date
I'm writing a simple reverse proxy which will need to handle http GETs and POSTs and WebSocket connections. Numbers of simultaneous clients will be low so I had hoped to use HttpListener. I'm struggling to see how to use that to proxy a WebSocket connection though.
I think responses have to be sent via HttpListenerResponse. For GETs and POSTs this is easy. For WebSockets I'd need to send handshake data then keep the connection open to send further messages from the server being proxyed. The only way I can see to send data using HttpListenerResponse is to call Close(), presumably preventing further use of the underlying socket.
Similar issues presumably exist with trying to use HttpListenerRequest to receive later websocket messages from the client.
Am I missing something here or is there no way to use HttpListener with websockets?
Seems there is no way with HttpListener right now. You have to wait .NET 4.5.
Presently I am doing a project on designing and implementing a firewall. Everything is working fine. Here I am filtering all packets going through a TCP port. But I need to send a custom page if a page is being blocked. Like "Your page is blocked by admin". I don't have any idea how to do it. Can I do it using raw sockets? If so please tell me, how to? But as I know raw socket does not work for sending on Windows XP SP2 and later, is there any other solution?
EDIT: I used C++ to create a DLL for an IP address filter. Then I imported it in my C++ program. IP addresses are blocking fine. But my customer needs the custom message when a browser is not finding its page.
If you're selectively allowing access to certain web pages, you're essentially acting like a proxy. And you'll need to act more like one if you want to respond to clients with an error page.
A browser making an HTTP request will expect the response on the same connection it opened. In order to return a "blocked" page, you'll need to determine whether the connection is to someplace you don't want the user to go, and if not, return a valid HTTP response (even if that response is an HTTP error like "403 Forbidden" or something more appropriate to a proxy) on that same connection.
If you're blocking the connection before it's even opened, ie: blocking access to certain IP addresses, then you're kind of stuck. The most you could do is return an ICMP message saying the host isn't available. You need to at least accept the connection if you can, accept the incoming request, and reply with your error message. Anything less, and a browser typically won't know what to do with it.
Hey, Since you're working on that low level
Can't you redirect the request by modifying its HTTP header?
I'm writing UI to test an asmx web service. Server and client are .NET. Client proxy has been generated using wsdl.exe.
I would like to intercept and store a string representation of outgoing and incoming SOAP messages generated as a result of calling methods on the web proxy, so I can add a feature to the UI which will show the message just sent/received.
I dimly recall there are two pairs of extension points where code can can be added to intecept the message but I cannot remember how this was done. I think the examples I have in mind involved compressing some part of the message on the client and the reverse on the server, even though in my scenario, I want to store rather than alter the message.
Any hints and help gratefully received.
(I've partially implemented a SoapExtension. I don't understand how the ChainStream method works, and I'm not sure how to notify a listener that a soap message has been trapped (since I'm not in control of instantiating the soap extension).'
You're on the right track with SoapExtension. Did you see the documentation and example here? http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapextension.aspx
The idea with ChainStream is you get passed the network stream that the request would be written to, and you have the option of returning a different stream. So if you want to save a copy of the request, return a MemoryStream, which the web services client will write the request into, and then in the ProcessMessage call you can copy the data out of there and pass it to your UI.
Another way to capture the XML is sent through the Wireshark application. It intercepts the communication network card.
In my case, I called a service that had as part of his address to string PIOSOS. I used the Find Packet window and searched.
Then located the XML.
See the picture.
(I know ... it's not a programmatic way, but it has its value. Lol)
I would suggest 2 tricks :
subclassing the proxy and overloading your methods (a little bit boring but you can generate code like in this project : http://ftwsf.codeplex.com/)
using Async signatures and subcribe to 'Completed' events of each methods (you can do this by reflection to avoid writting to much code)
If you need more info about these tricks, just let me know.
You'd really be better off using WCF as your client technology. You could then simply use WCF message-level tracing to log the incoming and outgoing messages. This is done simply through configuration.
There's no reason you have to use an ASMX client just because you are using an ASMX service.