SSLHandshake in C#? - c#

I sent Upgrade Header in httpwebrequest header on port 80 to use TLS.Server responded successfully with 101 switching protocol response and hence upgrading the protocol.
Now I want to know how to start SSL handshake on the same port in C#.?

You don't want to be intervening in the headers (by adding "Upgrade"). Let HttpWebRequest do it's thing.
Have you tried using this which specifically tells it to use SSL, but over port 80:
var req = (HttpWebRequest)WebRequest.Create("https://your.host.com:80/test.htm");

Related

Connect TcpClient through Fiddler proxy

How can I use a Fiddler proxy with a TcpClient? The answer on this similar question did not work for me: How to use Proxy with TcpClient.ConnectAsync()?
var client = new Pop3Client();
var tcpClient = new TcpClient(hostname, port);
var sslStream = new SslStream(tcpClient.GetStream());
sslStream.AuthenticateAsClient(hostname);
client.Connect(sslStream);
After some discussion it turned out that the code to create a connection through a proxy which was referenced in the question actually worked, but
SSL decryption need to be off in Fiddler.
Otherwise Fiddler will not pass the original TLS handshake through but will create one between Fiddler and Server and another one between Client and Fiddler, where the last one has a certificate created by Fiddler. The client will usually not trust this certificate by default and thus fail the TLS handshake.
Moreover, Fiddler expects the traffic inside the TLS connection to be HTTP, i.e. the client sends a HTTP request and the server sends a HTTP response back. POP3 works differently by having both a different message syntax and by having the server start with sending and not the client.
It really has to be client.Connect(sslStream) as shown in the question and not something like client.Connect(tcpStream) as the OP had in its actual code. In the last case the client will just try to read the encrypted data from the connection and thus fail.

Enabling Web Requests go via reverse proxy in F#

Code is in F# but also tagging C# in case for any suggestions. It's an SSL based request for which it couldn't resolve the proxy name, I've tried both http://localhost and https://localhost besides 127.0.0.1
Code:
let request = WebRequest.Create("https://foo.example.com") :?> HttpWebRequest
let myproxy = WebProxy("http://127.0.0.1", 60103);
myproxy.BypassProxyOnLocal <- false;
request.Proxy <- myproxy;
Error:
System.Net.WebException: An error occurred while sending the request. Couldn't resolve proxy name ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.CurlException: Couldn't resolve proxy name
My configurations
Reverse Proxy Settings
Local port: 60103
Remote Hot: foo.example.com
Remote Port: 443
(Check box is enabled for both "Enable Reverse Proxies" and the above record itself)
SSL Proxy Settings
Host: foo.example.com
Port: 443
Both checkboxes are checked for "Enable SSL Proxying" and the record itself.
Under Help -> Proxying -> Install Charles Root Certificate, the certificate was installed and marked as trusted in keychain
It has been painful and I realize the mistake and here's the answer with the solution.
The cause of problem was a solution that worked for me in nodejs and it caused a confusion in here while not realizing that C#/F# provides a WebProxy class for that. nodejs doesn't have a WebProxy class (as per my knowledge) it so the concept is to use reverse proxy in nodejs, that is to send the request to localhost and a specific port which maps to the remote url.
So keep your URL same as where it should be pointed to and use WebProxy class here to point to localhost and port (8888 in my case) where Charles Proxy is listening and intercepting requests.
Thanks Fyodor Soikin for point out not to use the url (http:// etc.) and just use hostname.
// let url = "http://localhost:60103"; // don't do this, reverse proxy settings
let url = "https://example.com"; // keep the url intact
let request = WebRequest.Create(url) :?> HttpWebRequest
let myproxy = WebProxy("localhost", 8888); // port where Charles proxy is running
myproxy.BypassProxyOnLocal <- false;
request.Proxy <- myproxy;

What is different in the .NET (c#) implementation of webclient versus java or firefox RESTClient?

I need to post JSON to a https endpoint using c# .
I am using System.Net.WebClient (or HttpWebRequest ).
When I post the JSON to the endpoint using JAVA or the firefox RESTClient everything works fine (from the same machine).
With Wireshark I can see that the receiving server RESETs the connection, resulting in this .NET exception:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
I don't use any proxy servers.
I have set the request timeout to -1 (and other values).
What can the .NET runtime be adding to (or removing from) the requests that the firefox RESTPlugin en JAVA are not ?
There must be a difference.
Fiddler shows me two http(s) requests with response status 200, but no data seems to be coming back (and Fiddler introduces a proxy...)
#Mason thanks for making me look once more at the fiddler data.
After setting the protocol to TLS1.2
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
It works.
I have seen posts that actual get an error message hinting at the minimal TLS support. But here I had to go through StackOverflow first.
Just the exercise of formulating the question and the first quick responders helpt me fix this quickly !

tcp connection to router how to communicate? (Telnet Client)

I am trying to build a program that will connect to an IP address (preferably that of a router) to a specific port (mainly 80) and will try to authenticate and then go on with further actions.
I started without knowing how to communicate with the router/server so i did this:
while (tcpSocket.Available > 0)
{
int input = tcpSocket.GetStream().ReadByte();
But it always gets a tcpSocket.Available = 0
So then i found out that i have to send a specific cmd for it to talk to me.
http://msdn.microsoft.com/en-us/library/cc247846.aspx
and made this
var client = new TcpClient(ip, port);
var data = Encoding.GetEncoding(1252).GetBytes(cmd);
var stm = client.GetStream();
stm.Write(data, 0, data.Length);
Now I dont understand how to format the cmds the cmd based on this http://www.ietf.org/rfc/rfc2941.txt
Would be 37 - 1?
Thank you for reading
P.S dont know if i should point this to SuperUser or ServerFault
I think you need to go back to simpler questions and investigations.
First: What protocol is actually running on the server you are connecting to? Port 80 suggests it is HTTP (port 80 is typically reserved for HTTP). Telnet typically runs on port 23.
If it is HTTP you need to follow the protocol defined in RFC 2616 (with the authentication options defined in RFC 2617).
Even simpler yet: connect to the server using PuTTY (or other preferred telnet client). What do you need to do in order to log in? If it is a telnet server then it will probably show a banner followed by a login prompt. You will type the username followed by return, then it will show you a password prompt. If it is a HTTP server then it will probably show you nothing at all, but type HTTP/1.0 (return) HEAD / (return) and you should see a HTTP message response. Whatever you need to do using PuTTY, your program will need to do exactly the same thing.

TcpClient -> How to retrieve a stream from a sub-directory like http://localhost:8000/admin?

I have a Stream running at this URL: http://localhost:8000/admin
I want to connect to it via a TcpClient, but don't know where I can specify the directory.
currently I do this:
tcpClient.Connect(IPAddress.Parse("127.0.0.1"), 8000);
TCP is the underyling protocol as does not have any concept of the 'directories'. The portion of the URI that you are talking about is used by the HTTP web-server to specify the web page resource. In terms of TCP, an HTTP request for http://localhost:8000/admin translates to a TCP connection being made to port 8000 on the local host with the following request text:
GET /admin HTTP/1.1
Host: localhost
...
(There will be some more request headers than those shown, but those are the basic ones.)
You may wish to switch to using WebClient or somesuch instead.
See URI, TCP and HTTP.
TCP is a protocol for transmitting data in a stream from one endpoint to another endpoint. An endpoint for TCP is an (ip-address, port) pair.
To specify that you want to open a connection with a particular service on a given machine with a known IP address, that service must be listening on a specific known port. Then that combination of known (ip-address, port) is the endpoint to connect to.

Categories

Resources