forked from 3gstudent/Homework-of-C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharpSSHRunCmd_SSH.NET.cs
More file actions
147 lines (140 loc) · 6.04 KB
/
Copy pathSharpSSHRunCmd_SSH.NET.cs
File metadata and controls
147 lines (140 loc) · 6.04 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
using System;
using System.IO;
using Renci.SshNet;
namespace SharpSSHRunCmd_SSH.NET
{
class Program
{
static void ShowUsage()
{
string Usage = @"
SharpSSHRunCmd_SSH.NET
Remote command execution via SSH(Based on SSH.NET).
Support password and privatekeyfile.
Author:3gstudent
Reference:https://github.com/sshnet/SSH.NET
Note:
You need to reference Renci.SshNet.dll.
You can download Renci.SshNet.dll from https://github.com/sshnet/SSH.NET/releases/download/2016.1.0/SSH.NET-2016.1.0-bin.zip
Complie:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe SharpSSHRunCmd_SSH.NET.cs /r:Renci.SshNet.dll
Usage:
SharpSSHRunCmd_SSH.NET.exe <SSH ServerIP> <SSH ServerPort> <mode> <user> <password> <cmd>
<mode>:
- plaintext
- keyfile
If the <cmd> is shell,you will get an interactive shell.
Eg:
SharpSSHRunCmd_SSH.NET.exe 192.168.1.1 22 plaintext root toor shell
SharpSSHRunCmd_SSH.NET.exe 192.168.1.1 22 keyfile root id_rsa ps
";
Console.WriteLine(Usage);
}
static void Main(string[] args)
{
if (args.Length != 6)
ShowUsage();
else
{
try
{
String Host = args[0];
String Port = args[1];
String Username = args[3];
String Password = null;
String Keypath = null;
String cmd = args[5];
if (args[2] == "plaintext")
{
Password = args[4];
var connectionInfo = new PasswordConnectionInfo(Host, Int32.Parse(Port), Username, Password);
connectionInfo.Timeout = TimeSpan.FromSeconds(10);
var ssh = new SshClient(connectionInfo);
ssh.Connect();
Console.WriteLine("[+] Valid: " + Username + " " + Password);
if (cmd == "shell")
while(true)
{
Console.Write("\n#");
cmd = Console.ReadLine();
if(cmd == "exit")
{
Console.Write("[*] Exit.");
ssh.Disconnect();
ssh.Dispose();
System.Environment.Exit(0);
}
var runcmd = ssh.CreateCommand(cmd);
var res = runcmd.Execute();
Console.Write(res);
}
else
{
var runcmd = ssh.CreateCommand(cmd);
var res = runcmd.Execute();
Console.Write(res);
ssh.Disconnect();
ssh.Dispose();
}
}
else if (args[2] == "keyfile")
{
Keypath = args[4];
FileStream keyFileStream = File.OpenRead(Keypath);
byte[] byData = new byte[40];
keyFileStream.Read(byData, 0, 40);
string keyData = System.Text.Encoding.Default.GetString(byData);
if (keyData.Contains("OPENSSH"))
{
Console.WriteLine("[!] Bad format of key file. You should use puttygen to convert the format.");
System.Environment.Exit(0);
}
keyFileStream.Seek(0, SeekOrigin.Begin);
var connectionInfo = new PrivateKeyConnectionInfo(Host, Int32.Parse(Port), Username, new PrivateKeyFile(keyFileStream));
connectionInfo.Timeout = TimeSpan.FromSeconds(10);
var ssh = new SshClient(connectionInfo);
ssh.Connect();
Console.WriteLine("[+] Valid: " + Username + " " + Keypath);
if (cmd == "shell")
while(true)
{
Console.Write("\n#");
cmd = Console.ReadLine();
if(cmd == "exit")
{
Console.Write("[*] Exit.");
ssh.Disconnect();
ssh.Dispose();
System.Environment.Exit(0);
}
var runcmd = ssh.CreateCommand(cmd);
var res = runcmd.Execute();
Console.Write(res);
}
else
{
var runcmd = ssh.CreateCommand(cmd);
var res = runcmd.Execute();
Console.Write(res);
ssh.Disconnect();
ssh.Dispose();
}
}
else
{
Console.WriteLine("[!] Wrong parameter");
System.Environment.Exit(0);
}
}
catch (Renci.SshNet.Common.SshException ex)
{
Console.WriteLine("[!] " + ex.Message);
}
catch (Exception exception)
{
Console.WriteLine("[!] " + exception.Message);
}
}
}
}
}