Retrieve processes information remote computer using C# - c#

I'm developing a desktop application using C# language. I'm trying to retrieve all the current active processes on the remote computer with the following code:
else if (infocomboBox.SelectedIndex == 2)
{
string compname = compnameLabel.Text;
Process[] remoteAll = Process.GetProcesses(compnameLabel.Text);
foreach (Process process in remoteAll)
{
ListViewItem item = new ListViewItem(process.ProcessName);
item.Tag = process;
listProcessView.Items.Add(item);
}
}
I put the code inside the button_click function.
I can't seem to retrieve it with an error saying "InvalidOperationException was unhandled"
"Couldn't connect to remote machine"
Log in username and password is disabled on the remote computer. Firewall and antivirus are off also.
Refer below for the error screenshot
Screenshot of the error
What is exactly the problem? Can anybody give solution?
Thank you very much in advance!

I would try to verify from a command line that the hostname is resolvable.
ping theOtherComputerName
If that resolves to an IP address and responds then you've eliminated one possible issue. Next up would have to be connection issues.
I believe the GetProcesses command tries to login to the remote machine using the current username and password, so you would need the same username and password on the other machine,
OR you'll need to impersonate an admin user on the remote machine doing something like this impersonate user in c#

Related

C# run executable from remote server on remote client

I have the following function for connecting to a remote client machine and running an executable on it.
I copied it from a response on this site, but don't have the link anymore, so I'm not sure who to give credit to.
public static void ConnectToRemoteClient(string client_machine, string user, string password, string target_exe )
{
var connection = new ConnectionOptions();
// The '.\' is for a local user on the remote machine
// or 'mydomain\user' for a domain user
connection.Username = user;
connection.Password = password;
object[] theProcessToRun = { target_exe };
var wmiScope = new ManagementScope($#"\\{client_machine}\root\cimv2", connection);
wmiScope.Connect();
using (var managementClass = new ManagementClass(wmiScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()))
{
managementClass.InvokeMethod("Create", theProcessToRun );
}
}
It is called using the following syntax:
string exe = string.Format("taskkill.exe {0} {1}", #"/F", #"/PID 8704");
ConnectToRemoteClient("ClientMachine", #"Domain\Username", #"password", exe);
It works just fine for executables that exist on the remote client machine.
However, I want to call an executable from a server, and run it on that remote client machine.
Not sure how best to approach this. I tried feeding it the following:
ConnectToRemoteClient("ClientMachine", #"Domain\User", #"password", #"\\ServerName\MyDir\Myfile.exe");
But it never initiated the executable on the machine. No error messages.
The reason I want to do this, is to save me from having to copy the large executable and supporting files to each client, but rather just run it from the server depot on each remote client.
Do I have to call a CMD window and feed it the \\ServerName\MyDir\Myfile.exe in order to get it to work properly? or is there a way I can make this work?
You are connecting to the remote machine using the passed credentials, but that only establishes your rights to open WMI. The command you then pass to WMI to execute is not running as the credentials you pass in, but under the LocalSystem account credentials.
LocalSystem does not have access to the network share.
To do this you need to remotely execute PSEXEC (https://ss64.com/nt/psexec.html) which allows you to pass the parameters to launch the application as. PSEXEC runs as LocalSystem but allows you to pass credentials to use when it launches your designated application. The launched program will then impersonate the user you pass in, and will have access to the network share.

How to access a remote desktops active directory? (ASP.NET MVC)

I'm trying to connect to a remote desktops active directory.
When I ping the remote desktop I get a response back, so is it possible to connect to it?
Currently, when I connect locally while on the remote desktop, I use the following code: (It works)
PrincipalContext context = new PrincipalContext(ContextType.ApplicationDirectory, "doris-dev", "cn=" + SupplierName + ",cn=suppliers,o=doris");
UserPrincipal user = UserPrincipal.FindByIdentity(context, Username);
I use the same code when I try to remotely connect to the machine, but then the exception message says it needs a username and password. Which username and password is this? Where does it come from?

C# Impersonation technique

I wanted to remove a config file from a remote machine having IP as : sj1slm612. Now the problem is i do not have full modification rights to that remote machine, so I'm using impersonation technique to do this. Normally when I'm connected to this remote machine via putty, I use 'sudo'. So my question is will the following code be able to solve my problem ? Thanks.
My Code :
AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
using (WindowsIdentity Authorized_user = new WindowsIdentity("sj1slm612\\wtsnqa", "password"))
{
using (WindowsImpersonationContext context = Authorized_user.Impersonate())
{
File.Delete(#"/apps/instances/express_13000/configuration/standalone-full.xml");
File.Delete(#"/apps/instances/query_13100/configuration/standalone-full.xml");
File.Delete(#"/apps/instances/wppapi_13200/configuration/standalone-full.xml");
File.Delete(#"/apps/instances/wppgui_13300/configuration/standalone-full.xml");
Console.WriteLine("All config files removed from sj1slm612");
Console.ReadLine();
There are 2 problems with your approach:
You are trying to impersonate a remote machine account on a local machine; this won't work. The credentials of a machine account can only be validated by that machine. In addition, that account has no rights on the local machine, so it doesn't really make sense to impersonate it. You need to impersonate a domain account. When you use a tool like putty, the credentials are sent to the remote machine and not validated by the local machine. This is why you can use a machine account of the remote machine.
You need to give proper paths for the files. Nowhere do you indicate that these files are on the remote machine. Use something like "\\machine\c$\path\to\file".
The details on what are going to work or not will depend on your network and OS, which you didn't specify, though it sounds Linux-ish. There may be a different syntax for referring to remote files that you need to use.

Call Process.GetProcesses by specifying/impersonating another account?

By using Process.GetProcesses, we can list running processes in a remote computer, e.g.
Process [] allRemoteProcesses = Process.GetProcesses("myRemoteComputer");
However, this seems to depend on the permission of my account. It throws exception "Couldn't connect to remote machine." if the running user does not have access to the remote machine. Can I specify another account to be used by .NET when checking the processes, similar to the -u and -p switches in PsList?
What I've done before is use something similar to what's described in this article.
using (new Impersonator("user", "domain", "pass"))
{
Process[] allRemoteProcesses = Process.GetProcesses("myRemoteComputer");
// Rest of code...
}
An alternative is use to use WMI and query for the processes like what's described here.
For the same problem, turning on the remote registry service in the remote registry worked for me!

Streamwriter issue with remote machine

I am trying to create a file on the remote machine but I am getting The "Network name cannot be found". I checked the network path and I was able to access the path from my machine. Could you please let me know what could be wrong?
Here is my code.
using (StreamWriter sw = new StreamWriter("\\\\servername\\TEST1\\TEST\\NEWFILE.csv", true))
{
sw.WriteLine(sw);
}
Go to \servername\TEST1 and give write permission to the user or aspnet (if you have a web application) on test folder and then re-run your program. It will work.
To give write permissions, just refer to this article:
How to share a folder/File
In case it still does not work, replace servername with server IP address and do the same as stated above.
Give the access rights to the user under which this application runs either it is a IIS pool or windows service etc
it is surely a security isssue. you need to give Write access to the remote machine

Categories

Resources