RSS

Copy file to remote computer using remote admin credentials

06 May

Correct me if I’m wrong, but you can use LogonUser to impersonate a local group also not only domain accounts.
by: Shay Erlichmen

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;
public class Form1
{
	[DllImport("advapi32.DLL", SetLastError = true)]
	public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
	private void Button1_Click(System.Object sender, System.EventArgs e)
	{
		IntPtr admin_token = default(IntPtr);
		WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
		WindowsIdentity wid_admin = null;
		WindowsImpersonationContext wic = null;
		try {
			MessageBox.Show("Copying file...");
			if (LogonUser("Local Admin name", "Local computer name", "pwd", 9, 0, ref admin_token) != 0) {
				wid_admin = new WindowsIdentity(admin_token);
				wic = wid_admin.Impersonate();
				System.IO.File.Copy("C:\\right.bmp", "\\\\157.60.113.28\\testnew\\right.bmp", true);
				MessageBox.Show("Copy succeeded");
			} else {
				MessageBox.Show("Copy Failed");
			}
		} catch (System.Exception se) {
			int ret = Marshal.GetLastWin32Error();
			MessageBox.Show(ret.ToString(), "Error code: " + ret.ToString());
			MessageBox.Show(se.Message);
		} finally {
			if (wic != null) {
				wic.Undo();
			}
		}
	}
}

Referenced by: http://stackoverflow.com/questions/766033/copy-file-to-remote-computer-using-remote-admin-credentials

Advertisements
 
2 Comments

Posted by on May 6, 2011 in C#, Window Application

 

2 responses to “Copy file to remote computer using remote admin credentials

  1. Sreekanth Kolluri

    October 24, 2011 at 6:34 PM

    good work dear……….!!
    Helped me alot

     
  2. Liran Perez

    October 30, 2012 at 3:38 PM

    Thanks!
    is there a way do it with out use of shared folder?

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: