forked from 3gstudent/Homework-of-C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSLCertScan.cs
More file actions
165 lines (153 loc) · 6.67 KB
/
Copy pathSSLCertScan.cs
File metadata and controls
165 lines (153 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Security;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace SSLCertScan
{
class Program
{
static void ShowUsage()
{
string Usage = @"
SSLCertScan
Use to scan the website SSL certificate.
Modified by 3gstudent
Reference:https://github.com/ryanries/SharpTLSScan
Usage:
SSLCertScan.exe <IP> <Port>
Eg:
SSLCertScan.exe 192.168.1.1 443
";
Console.WriteLine(Usage);
}
static void Main(string[] args)
{
if (args.Length != 2)
ShowUsage();
else
{
try
{
cpClient tcpClient = new TcpClient(args[0], Convert.ToInt32(args[1]));
Console.WriteLine("[+] " + args[0] + " responds to TCP on " + args[1] + ".\n");
SslStream sslStream = new SslStream(tcpClient.GetStream(), true, CertificateValidationCallBack);
sslStream.AuthenticateAsClient(args[0]);
Console.WriteLine("SChannel negotiated the following:\n");
Console.WriteLine("Protocol Version : " + sslStream.SslProtocol);
Console.WriteLine("Cipher Algorithm : " + sslStream.CipherAlgorithm);
Console.WriteLine("Cipher Strength : " + sslStream.CipherStrength + " bits");
Console.WriteLine("Hash Algorithm : " + sslStream.HashAlgorithm);
Console.WriteLine("Hash Strength : " + sslStream.HashStrength + " bits");
Console.Write("Key Exchange Algorithm: ");
if (sslStream.KeyExchangeAlgorithm.ToString() == "44550")
Console.WriteLine("ECDH Ephemeral");
else
Console.WriteLine(sslStream.KeyExchangeAlgorithm);
Console.WriteLine("Key Exchange Strength : " + sslStream.KeyExchangeStrength + " bits");
}
catch (Exception ex)
{
Console.WriteLine("[!] " + ex.Message);
return;
}
}
}
private static bool CertificateValidationCallBack(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
// Certificate2 is better than Certificate1, right?
X509Certificate2 cert = (X509Certificate2)certificate;
string[] subjectPieces = splitDN(cert.Subject);
Console.Write("Certificate Subject : ");
for (int x = 0; x < subjectPieces.Length; x++)
{
if (x == 0)
Console.WriteLine(subjectPieces[x]);
else
Console.WriteLine(" " + subjectPieces[x]);
}
string[] issuerPieces = splitDN(cert.Issuer);
Console.Write("Certificate Issuer : ");
for (int x = 0; x < issuerPieces.Length; x++)
{
if (x == 0)
Console.WriteLine(issuerPieces[x]);
else
Console.WriteLine(" " + issuerPieces[x]);
}
Console.WriteLine("Certificate Begins : " + cert.NotBefore);
Console.WriteLine("Certificate Expires : " + cert.NotAfter);
Console.WriteLine("Certificate Version : " + cert.Version);
if (cert.SignatureAlgorithm.FriendlyName.ToLower().Contains("md5"))
{
Console.WriteLine("Signature Algorithm : " + cert.SignatureAlgorithm.FriendlyName + " (" + cert.SignatureAlgorithm.Value + ")");
}
else
{
Console.WriteLine("Signature Algorithm : " + cert.SignatureAlgorithm.FriendlyName + " (" + cert.SignatureAlgorithm.Value + ")");
}
Console.WriteLine("Key Exchange Algorithm: " + cert.PublicKey.Key.KeyExchangeAlgorithm);
Console.WriteLine("Public Key Algorithm : " + new System.Security.Cryptography.Oid(cert.GetKeyAlgorithm()).FriendlyName);
Console.WriteLine("Public Key Size : " + cert.PublicKey.Key.KeySize);
byte[] RSAkey = cert.GetPublicKey();
string strRSAkey = "";
for (int i = 0; i < RSAkey.Length; i++)
{
strRSAkey += RSAkey[i].ToString("X2");
}
Console.WriteLine("Public Key : " + strRSAkey);
foreach (X509Extension extension in cert.Extensions)
{
if (extension.Oid.FriendlyName == "Subject Alternative Name")
{
AsnEncodedData asnData = new AsnEncodedData(extension.Oid, extension.RawData);
string[] sans = asnData.Format(false).Split(',');
Console.Write("Alternative Names : ");
for (int x = 0; x < sans.Length; x++)
{
if (x == 0)
Console.WriteLine(sans[x]);
else
Console.WriteLine(" " + sans[x]);
}
}
}
Console.Write("Certificate Validated : ");
if (sslPolicyErrors == SslPolicyErrors.None)
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No (" + sslPolicyErrors + ")");
}
return true;
}
private static string[] splitDN(string input)
{
string[] splitString = input.Split(',');
List<string> correctedSplitString = new List<string>();
int index = 0;
foreach (string part in splitString)
{
if (part.Contains('='))
{
correctedSplitString.Add(part.Trim());
index++;
}
else
{
if (index > 0)
correctedSplitString[index - 1] = correctedSplitString[index - 1] + ", " + part.Trim();
else
correctedSplitString.Add(part.Trim());
index++;
}
}
return correctedSplitString.ToArray();
}
}
}