Skip to content

Instantly share code, notes, and snippets.

@fipso
Created June 7, 2017 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fipso/e98c745d8b54bdcc40d22e83cc063392 to your computer and use it in GitHub Desktop.
Save fipso/e98c745d8b54bdcc40d22e83cc063392 to your computer and use it in GitHub Desktop.
MailKit Socks5 Fail
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using MailKit.Net.Imap;
using MailKit;
namespace SocksTest
{
internal class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Working *");
var socksClient = new TcpClient();
socksClient.Connect("127.0.0.1", 1080);
var bw = new BinaryWriter(socksClient.GetStream());
var br = new BinaryReader(socksClient.GetStream());
//Tell the Socks5 to Connect to the IMAP:
//Get the IP of the IMAP
var ip = Dns.GetHostAddresses("mx.freenet.de")[0];
//Me: Hello
bw.Write(new byte[] {0x05, 0x01, 0x00});
//Server: Hello
Print(br.ReadBytes(2));
//Me: Connect to the IMAP on port 993
byte[] data = {0x05, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, getPortBytes(993)[0], getPortBytes(993)[1]};
//Fill the IP in the DATA
for (int i = 0; i < 4; i++)
{
data[i + 4] = ip.GetAddressBytes()[i];
}
bw.Write(data);
//Server: Connection rdy
Print(br.ReadBytes(10));
//Rdy
//Give the Socket to the IMAP client
getMails(socksClient.Client);
}
public static void Print(byte[] data)
{
foreach (var b in data)
{
Console.WriteLine(b);
}
}
public static Byte[] getPortBytes(int port)
{
byte high = (byte)(float) (port / 256);
byte low = (byte) (port % 256);
return new byte[] {high,low};
//https://de.wikipedia.org/wiki/SOCKS
}
public static void getMails(Socket soc)
{
using (var client = new ImapClient ()) {
// For demo-purposes, accept all SSL certificates
client.ServerCertificateValidationCallback = (s,c,h,e) => true;
//client.Connect(socksClient.Client, "mx.freenet.de", 933);
client.Connect(soc, "mx.freenet.de");
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove ("XOAUTH2");
client.Authenticate ("fipso@freenet.de", "<HIDDEN>");
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open (FolderAccess.ReadOnly);
Console.WriteLine ("Total messages: {0}", inbox.Count);
Console.WriteLine ("Recent messages: {0}", inbox.Recent);
client.Disconnect (true);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment