forked from RowTeam/SharpSQLTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (112 loc) · 4.14 KB
/
Program.cs
File metadata and controls
120 lines (112 loc) · 4.14 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
using SharpSQLTools.Domain;
using SharpSQLTools.FunModule;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Threading;
namespace SharpSQLTools
{
class Program
{
public static void OnInfoMessage(object mySender, SqlInfoMessageEventArgs args)
{
var value = String.Empty;
foreach (SqlError err in args.Errors)
{
value = err.Message;
Console.WriteLine(value);
}
}
/// <summary>
/// 数据库连接
/// </summary>
static SqlConnection SqlConnet(string target, string username, string password)
{
SqlConnection Conn = null;
var connectionString = $"Server = \"{target}\";Database = \"master\";User ID = \"{username}\";Password = \"{password}\";";
try
{
Conn = new SqlConnection(connectionString);
Conn.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage);
Conn.Open();
Console.WriteLine("[*] Database connection is successful!");
}
catch (Exception ex)
{
Console.WriteLine("[!] Error log: \r\n" + ex.Message);
Environment.Exit(0);
}
return Conn;
}
static void Main(string[] args)
{
if (args.Length != 3)
{
Info.ShowUsage();
return;
}
var Conn = SqlConnet(args[0], args[1], args[2]);
var setting = new Setting(Conn);
var filesOptions = new FilesOptions(Conn, setting);
var execOptions = new ExecOptions(Conn, setting);
try
{
do
{
Console.Write("SQL> ");
string str = Console.ReadLine();
if (str.ToLower() == "exit") { Conn.Close(); break; }
else if (str.ToLower() == "help") { Info.ShowModuleUsage(); continue; }
string[] cmdline = str.Split(new char[] { ' ' }, 3);
String s = String.Empty;
for (int i = 1; i < cmdline.Length; i++) { s += cmdline[i] + " "; }
switch (cmdline[0].ToLower())
{
case "enable_xp_cmdshell":
setting.Enable_xp_cmdshell();
break;
case "disable_xp_cmdshell":
setting.Disable_xp_cmdshell();
break;
case "xp_cmdshell":
execOptions.xp_cmdshell(s);
break;
case "enable_ole":
setting.Enable_ola();
break;
case "disable_ole":
setting.Disable_ole();
break;
case "sp_cmdshell":
execOptions.sp_cmdshell(s);
break;
case "upload":
filesOptions.UploadFiles(cmdline[1], cmdline[2]);
break;
case "download":
filesOptions.DownloadFiles(cmdline[2], cmdline[1]);
break;
default:
Console.WriteLine(Batch.RemoteExec(Conn, str, true));
break;
}
if (!ConnectionState.Open.Equals(Conn.State))
{
Console.WriteLine("[!] Disconnect....");
break;
}
}
while (true);
}
catch (Exception ex)
{
Conn.Close();
Console.WriteLine("[!] Error log: \r\n" + ex.Message);
}
}
}
}