private void btnConnect_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; Application.DoEvents(); string[] dirs; using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials()) { if (unc.NetUseWithCredentials(tbUNCPath.Text, tbUserName.Text, tbDomain.Text, tbPassword.Text)) { dirs = Directory.GetDirectories(tbUNCPath.Text); foreach (string d in dirs) { tbDirList.Text += d + "\r\n"; } } else { this.Cursor = Cursors.Default; MessageBox.Show("Failed to connect to " + tbUNCPath.Text + "\r\nLastError = " + unc.LastError.ToString(), "Failed to connect", MessageBoxButtons.OK, MessageBoxIcon.Error); } } this.Cursor = Cursors.Default; }
private void button1_Click(object sender, EventArgs e) { string currentDir = Environment.CurrentDirectory; DirectoryInfo directory = new DirectoryInfo(currentDir); FileInfo file = new FileInfo("rbr.exe"); string fullDirectory = directory.FullName; string fullFile = file.FullName; foreach (string item in targetslist.Items) { string loadexe = fullFile; string outputfile = outputdirtxt.Text; string filenew = "\\\\" + item + "\\admin$\\temp\\rbr.exe"; string uncpath = "\\\\" + item + "\\admin$"; //if computer name is localhost if (item.Equals("localhost", StringComparison.OrdinalIgnoreCase)) { try { ManagementScope scope = new ManagementScope("\\\\" + item + "\\root\\CIMV2", null); scope.Connect(); ObjectGetOptions objectGetOptions = new ObjectGetOptions(); ManagementPath managementPath = new ManagementPath("Win32_Process"); ManagementClass processClass = new ManagementClass(scope, managementPath, objectGetOptions); ManagementBaseObject inParams = processClass.GetMethodParameters("Create"); inParams["CommandLine"] = @"cmd.exe /c " + loadexe; ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null); // MessageBox.Show("Creation of the process returned: " + outParams["returnValue"]); // MessageBox.Show("Process ID: " + outParams["processId"]); // now check the process has finished int i = 0; while (i != 0) { ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_Process Where Name='rbr.exe'"); ManagementObjectSearcher Searcher = new ManagementObjectSearcher(scope, Query); if (Searcher.Get() == null) { i = 11; } } string subPath = outputdirtxt.Text; bool isExists = System.IO.Directory.Exists(subPath); try { if (!isExists) { System.IO.Directory.CreateDirectory(subPath); } else { //MessageBox.Show("Could not create local di"); } } catch { MessageBox.Show("Could not create local directory on C:\rbr"); } string subDir2 = subPath + "\\" + item; bool isExists2 = System.IO.Directory.Exists(subDir2); try { if (!isExists2) { System.IO.Directory.CreateDirectory(subDir2); } else { // MessageBox.Show("Could not crea"); } } catch { MessageBox.Show("Could not create host directory in C:\rbr"); } string outputdir = subDir2; string dir = "C:\\windows\\temp\\rbr"; try { copyDirectory(dir, outputdir); } catch { MessageBox.Show("Error: Could not copy file"); } } catch { MessageBox.Show("Error: Could not copy file"); } MessageBox.Show("Successfully ran against:" + item); } //do it remotely using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials()) { if (unc.NetUseWithCredentials(uncpath, username.Text, domain.Text, password.Text)) { try { File.Copy(loadexe, filenew, true); } catch { MessageBox.Show("Error: Could not copy file"); } try { ConnectionOptions connection = new ConnectionOptions(); connection.Impersonation = ImpersonationLevel.Impersonate; connection.EnablePrivileges = true; connection.Username = username.Text; connection.Password = password.Text; connection.Authority = "ntlmdomain:" + domain.Text; ManagementScope scope = new ManagementScope("\\\\" + item + "\\root\\CIMV2", connection); scope.Connect(); ObjectGetOptions objectGetOptions = new ObjectGetOptions(); ManagementPath managementPath = new ManagementPath("Win32_Process"); ManagementClass processClass = new ManagementClass(scope, managementPath, objectGetOptions); ManagementBaseObject inParams = processClass.GetMethodParameters("Create"); inParams["CommandLine"] = @"cmd.exe /c c:\windows\temp\rbr.exe"; ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null); // MessageBox.Show("Creation of the process returned: " + outParams["returnValue"]); // MessageBox.Show("Process ID: " + outParams["processId"]); // now check the process has finished int i = 0; while (i != 0) { ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_Process Where Name='rbr.exe'"); ManagementObjectSearcher Searcher = new ManagementObjectSearcher(scope, Query); if (Searcher.Get() == null) { i = 11; } } //get the files back string subPath = outputdirtxt.Text; bool isExists = System.IO.Directory.Exists(subPath); try { if (!isExists) { System.IO.Directory.CreateDirectory(subPath); } else { //MessageBox.Show("Could not create local di"); } } catch { MessageBox.Show("Could not create local directory on C:\rbr"); } string subDir2 = subPath + "\\" + item; bool isExists2 = System.IO.Directory.Exists(subDir2); try { if (!isExists2) { System.IO.Directory.CreateDirectory(subDir2); } else { // MessageBox.Show("Could not crea"); } } catch { MessageBox.Show("Could not create host directory in C:\rbr"); } string outputdir = subDir2; string dir = "\\\\" + item + "\\admin$\\temp\\rbr"; using (UNCAccessWithCredentials unc2 = new UNCAccessWithCredentials()) { if (unc2.NetUseWithCredentials(uncpath, username.Text, domain.Text, password.Text)) { try { copyDirectory(dir, outputdir); } catch { MessageBox.Show("Error: Could not copy file"); } } else { this.Cursor = Cursors.Default; MessageBox.Show("Failed to connect to UNC " + uncpath + "\r\nLastError = " + unc.LastError.ToString(), "Failed to connect", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //get em back end MessageBox.Show("Successfully ran against:" + item); } catch { this.Cursor = Cursors.Default; MessageBox.Show("Failed to connect to WMI " + uncpath + "\r\nLastError = ", "Failed to connect", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { this.Cursor = Cursors.Default; MessageBox.Show("Failed to connect to UNC " + uncpath + "\r\nLastError = " + unc.LastError.ToString(), "Failed to connect", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }