الوصول إلى ملف مشترك (UNC) من مجال بعيد غير موثوق به باستخدام بيانات الاعتماد
-
20-08-2019 - |
سؤال
لقد واجهنا موقفًا مثيرًا للاهتمام يحتاج إلى حل، ولم تصل عمليات البحث التي أجريتها إلى أي شيء.لذلك أناشد مجتمع SO للحصول على المساعدة.
القضية هي:نحتاج إلى الوصول برمجيًا إلى ملف مشترك غير موجود في مجالنا، وليس ضمن مجال خارجي موثوق به عبر مشاركة الملفات عن بعد / UNC.وبطبيعة الحال، نحتاج إلى توفير بيانات الاعتماد للجهاز البعيد.
عادةً ما يتم حل هذه المشكلة بإحدى طريقتين:
- قم بتعيين مشاركة الملف كمحرك أقراص وقم بتوفير بيانات الاعتماد في ذلك الوقت.ويتم ذلك عادة باستخدام
NET USE
الأمر أو وظائف Win32 المكررةNET USE
. - قم بالوصول إلى الملف باستخدام مسار UNC كما لو كان الكمبيوتر البعيد موجودًا في المجال وتأكد من تكرار الحساب الذي يتم تشغيل البرنامج من خلاله (بما في ذلك كلمة المرور) على الجهاز البعيد كمستخدم محلي.الاستفادة بشكل أساسي من حقيقة أن Windows سيقوم تلقائيًا بتوفير بيانات اعتماد المستخدم الحالي عندما يحاول المستخدم الوصول إلى ملف مشترك.
- لا تستخدم مشاركة الملفات عن بعد.استخدم FTP (أو أي وسيلة أخرى) لنقل الملف، والعمل عليه محليًا، ثم نقله مرة أخرى.
لأسباب مختلفة ومتنوعة، رفض مهندسو الأمن/الشبكات لدينا النهجين الأولين.من الواضح أن النهج الثاني هو ثغرة أمنية؛إذا تم اختراق الكمبيوتر البعيد، فإن الكمبيوتر المحلي الآن في خطر.الطريقة الأولى غير مرضية لأن محرك الأقراص المثبت حديثًا هو مورد مشترك متاح للبرامج الأخرى على الكمبيوتر المحلي أثناء وصول البرنامج إلى الملفات.على الرغم من أنه من الممكن تمامًا جعل هذا الأمر مؤقتًا، إلا أنه لا يزال هناك فجوة في رأيهم.
إنهم منفتحون على الخيار الثالث، لكن مسؤولي الشبكة البعيدة يصرون على SFTP بدلاً من FTPS، ولا يدعم FtpWebRequest سوى FTPS.سفتب يكون الخيار الأكثر ملاءمة لجدار الحماية وهناك مكتبتان يمكنني استخدامهما لهذا النهج، لكنني أفضل تقليل تبعياتي إذا استطعت.
لقد بحثت في MSDN عن وسيلة مُدارة أو Win32 لاستخدام مشاركة الملفات عن بُعد، لكنني فشلت في التوصل إلى أي شيء مفيد.
ولذا أسأل:هل هناك طريقة أخرى؟هل فاتني وظيفة win32 فائقة السرية التي تفعل ما أريد؟أو هل يجب أن أتبع نوعًا ما من الخيار 3؟
المحلول
طريقة حل مشكلتك هي استخدام Win32 API يسمى WNetUseConnection.
استخدم هذه الوظيفة للاتصال بمسار UNC باستخدام المصادقة، وليس لتعيين محرك أقراص.
سيسمح لك هذا بالاتصال بجهاز بعيد، حتى لو لم يكن موجودًا في نفس المجال، وحتى لو كان له اسم مستخدم وكلمة مرور مختلفين.
بمجرد استخدام WNetUseConnection، ستتمكن من الوصول إلى الملف عبر مسار UNC كما لو كنت في نفس المجال.ربما تكون أفضل طريقة هي من خلال المشاركات الإدارية المضمنة.
مثال:\\اسم الكمبيوتر\c$\ملفات البرنامج\مجلد\ملف.txt
فيما يلي بعض نماذج تعليمات برمجية C# التي تستخدم WNetUseConnection.
ملاحظة، بالنسبة لـ NetResource، يجب عليك تمرير قيمة فارغة لـ lpLocalName وlpProvider.يجب أن يكون dwType RESOURCETYPE_DISK.يجب أن يكون lpRemoteName \\ComputerName.
نصائح أخرى
للأشخاص الذين يبحثون عن حل سريع، يمكنك استخدام NetworkShareAccesser
لقد كتبت مؤخرًا (استنادًا إلى هذه الإجابة (ًشكراً جزيلا!)):
الاستخدام:
using (NetworkShareAccesser.Access(REMOTE_COMPUTER_NAME, DOMAIN, USER_NAME, PASSWORD))
{
File.Copy(@"C:\Some\File\To\copy.txt", @"\\REMOTE-COMPUTER\My\Shared\Target\file.txt");
}
تحذير: يرجى التأكد تماما من ذلك Dispose
التابع NetworkShareAccesser
يتم استدعاؤه (حتى لو تعطل تطبيقك!)، وإلا فسيظل الاتصال مفتوحًا على Windows.يمكنك رؤية جميع الاتصالات المفتوحة عن طريق فتح الملف cmd
موجه وأدخل net use
.
الرمز:
/// <summary>
/// Provides access to a network share.
/// </summary>
public class NetworkShareAccesser : IDisposable
{
private string _remoteUncName;
private string _remoteComputerName;
public string RemoteComputerName
{
get
{
return this._remoteComputerName;
}
set
{
this._remoteComputerName = value;
this._remoteUncName = @"\\" + this._remoteComputerName;
}
}
public string UserName
{
get;
set;
}
public string Password
{
get;
set;
}
#region Consts
private const int RESOURCE_CONNECTED = 0x00000001;
private const int RESOURCE_GLOBALNET = 0x00000002;
private const int RESOURCE_REMEMBERED = 0x00000003;
private const int RESOURCETYPE_ANY = 0x00000000;
private const int RESOURCETYPE_DISK = 0x00000001;
private const int RESOURCETYPE_PRINT = 0x00000002;
private const int RESOURCEDISPLAYTYPE_GENERIC = 0x00000000;
private const int RESOURCEDISPLAYTYPE_DOMAIN = 0x00000001;
private const int RESOURCEDISPLAYTYPE_SERVER = 0x00000002;
private const int RESOURCEDISPLAYTYPE_SHARE = 0x00000003;
private const int RESOURCEDISPLAYTYPE_FILE = 0x00000004;
private const int RESOURCEDISPLAYTYPE_GROUP = 0x00000005;
private const int RESOURCEUSAGE_CONNECTABLE = 0x00000001;
private const int RESOURCEUSAGE_CONTAINER = 0x00000002;
private const int CONNECT_INTERACTIVE = 0x00000008;
private const int CONNECT_PROMPT = 0x00000010;
private const int CONNECT_REDIRECT = 0x00000080;
private const int CONNECT_UPDATE_PROFILE = 0x00000001;
private const int CONNECT_COMMANDLINE = 0x00000800;
private const int CONNECT_CMD_SAVECRED = 0x00001000;
private const int CONNECT_LOCALDRIVE = 0x00000100;
#endregion
#region Errors
private const int NO_ERROR = 0;
private const int ERROR_ACCESS_DENIED = 5;
private const int ERROR_ALREADY_ASSIGNED = 85;
private const int ERROR_BAD_DEVICE = 1200;
private const int ERROR_BAD_NET_NAME = 67;
private const int ERROR_BAD_PROVIDER = 1204;
private const int ERROR_CANCELLED = 1223;
private const int ERROR_EXTENDED_ERROR = 1208;
private const int ERROR_INVALID_ADDRESS = 487;
private const int ERROR_INVALID_PARAMETER = 87;
private const int ERROR_INVALID_PASSWORD = 1216;
private const int ERROR_MORE_DATA = 234;
private const int ERROR_NO_MORE_ITEMS = 259;
private const int ERROR_NO_NET_OR_BAD_PATH = 1203;
private const int ERROR_NO_NETWORK = 1222;
private const int ERROR_BAD_PROFILE = 1206;
private const int ERROR_CANNOT_OPEN_PROFILE = 1205;
private const int ERROR_DEVICE_IN_USE = 2404;
private const int ERROR_NOT_CONNECTED = 2250;
private const int ERROR_OPEN_FILES = 2401;
#endregion
#region PInvoke Signatures
[DllImport("Mpr.dll")]
private static extern int WNetUseConnection(
IntPtr hwndOwner,
NETRESOURCE lpNetResource,
string lpPassword,
string lpUserID,
int dwFlags,
string lpAccessName,
string lpBufferSize,
string lpResult
);
[DllImport("Mpr.dll")]
private static extern int WNetCancelConnection2(
string lpName,
int dwFlags,
bool fForce
);
[StructLayout(LayoutKind.Sequential)]
private class NETRESOURCE
{
public int dwScope = 0;
public int dwType = 0;
public int dwDisplayType = 0;
public int dwUsage = 0;
public string lpLocalName = "";
public string lpRemoteName = "";
public string lpComment = "";
public string lpProvider = "";
}
#endregion
/// <summary>
/// Creates a NetworkShareAccesser for the given computer name. The user will be promted to enter credentials
/// </summary>
/// <param name="remoteComputerName"></param>
/// <returns></returns>
public static NetworkShareAccesser Access(string remoteComputerName)
{
return new NetworkShareAccesser(remoteComputerName);
}
/// <summary>
/// Creates a NetworkShareAccesser for the given computer name using the given domain/computer name, username and password
/// </summary>
/// <param name="remoteComputerName"></param>
/// <param name="domainOrComuterName"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
public static NetworkShareAccesser Access(string remoteComputerName, string domainOrComuterName, string userName, string password)
{
return new NetworkShareAccesser(remoteComputerName,
domainOrComuterName + @"\" + userName,
password);
}
/// <summary>
/// Creates a NetworkShareAccesser for the given computer name using the given username (format: domainOrComputername\Username) and password
/// </summary>
/// <param name="remoteComputerName"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
public static NetworkShareAccesser Access(string remoteComputerName, string userName, string password)
{
return new NetworkShareAccesser(remoteComputerName,
userName,
password);
}
private NetworkShareAccesser(string remoteComputerName)
{
RemoteComputerName = remoteComputerName;
this.ConnectToShare(this._remoteUncName, null, null, true);
}
private NetworkShareAccesser(string remoteComputerName, string userName, string password)
{
RemoteComputerName = remoteComputerName;
UserName = userName;
Password = password;
this.ConnectToShare(this._remoteUncName, this.UserName, this.Password, false);
}
private void ConnectToShare(string remoteUnc, string username, string password, bool promptUser)
{
NETRESOURCE nr = new NETRESOURCE
{
dwType = RESOURCETYPE_DISK,
lpRemoteName = remoteUnc
};
int result;
if (promptUser)
{
result = WNetUseConnection(IntPtr.Zero, nr, "", "", CONNECT_INTERACTIVE | CONNECT_PROMPT, null, null, null);
}
else
{
result = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null);
}
if (result != NO_ERROR)
{
throw new Win32Exception(result);
}
}
private void DisconnectFromShare(string remoteUnc)
{
int result = WNetCancelConnection2(remoteUnc, CONNECT_UPDATE_PROFILE, false);
if (result != NO_ERROR)
{
throw new Win32Exception(result);
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
this.DisconnectFromShare(this._remoteUncName);
}
}
AFAIK، لا تحتاج إلى ذلك خريطة مسار UNC إلى حرف محرك الأقراص لتأسيس بيانات اعتماد للخادم.لقد استخدمت بانتظام البرامج النصية الدفعية مثل:
net use \\myserver /user:username password
:: do something with \\myserver\the\file\i\want.xml
net use /delete \\my.server.com
ومع ذلك، فإن أي برنامج يعمل على نفس الحساب الذي يستخدمه برنامجك سيظل قادرًا على الوصول إلى كل ما هو موجود فيه username:password
لديه حق الوصول إلى.قد يكون الحل المحتمل هو عزل البرنامج الخاص بك في حساب المستخدم المحلي الخاص به (يكون الوصول إلى UNC محليًا للحساب الذي تم الاتصال به NET USE
).
ملحوظة: إن استخدام الشركات الصغيرة والمتوسطة عبر المجالات لا يعد استخدامًا جيدًا لتقنية المنظمة البحرية الدولية (IMO).إذا كان الأمان بهذه الأهمية، فإن حقيقة افتقار الشركات الصغيرة والمتوسطة إلى التشفير يعد أمرًا مثبطًا في حد ذاته.
بدلاً من WNetUseConnection، أوصي به NetUseAdd.WNetUseConnection هي وظيفة قديمة حلت محلها WNetUseConnection2 وWNetUseConnection3، ولكن كل هذه الوظائف تنشئ جهاز شبكة يمكن رؤيته في Windows Explorer.NetUseAdd يعادل استدعاء net use في موجه DOS للمصادقة على كمبيوتر بعيد.
إذا قمت باستدعاء NetUseAdd، فيجب أن تنجح المحاولات اللاحقة للوصول إلى الدليل.
على الرغم من أنني لا أعرف نفسي، إلا أنني آمل بالتأكيد أن يكون الرقم 2 غير صحيح...أود أن أعتقد أن Windows لن يقوم تلقائيًا بإعطاء معلومات تسجيل الدخول الخاصة بي (على الأقل كلمة المرور الخاصة بي!) إلى أي جهاز ناهيك عن الشخص الذي ليس جزءًا من ثقتي.
بغض النظر، هل قمت باستكشاف بنية انتحال الشخصية؟سيبدو الكود الخاص بك مشابهًا لهذا:
using (System.Security.Principal.WindowsImpersonationContext context = System.Security.Principal.WindowsIdentity.Impersonate(token))
{
// Do network operations here
context.Undo();
}
في هذه الحالة، token
المتغير هو IntPtr.للحصول على قيمة لهذا المتغير، سيتعين عليك استدعاء وظيفة LogonUser Windows API غير المُدارة.رحلة سريعة إلى pinvoc.net يعطينا التوقيع التالي:
[System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
out IntPtr phToken
);
يجب أن يبدو اسم المستخدم والمجال وكلمة المرور واضحين إلى حد ما.قم بإلقاء نظرة على القيم المختلفة التي يمكن تمريرها إلى dwLogonType وdwLogonProvider لتحديد القيمة التي تناسب احتياجاتك.
لم يتم اختبار هذا الرمز، حيث ليس لدي نطاق ثانٍ هنا حيث يمكنني التحقق منه، ولكن نأمل أن يضعك هذا على المسار الصحيح.
تدعم معظم خوادم SFTP SCP أيضًا، مما قد يكون من الأسهل العثور على مكتبات لها.يمكنك أيضًا الاتصال بعميل حالي من الكود الخاص بك مثل pscp المتضمن المعجون.
إذا كان نوع الملف الذي تتعامل معه بسيطًا مثل ملف نصي أو ملف XML، فيمكنك الذهاب إلى أبعد من ذلك بكتابة تنفيذ العميل/الخادم الخاص بك لمعالجة الملف باستخدام شيء مثل .NET Remoting أو خدمات الويب.
لقد رأيت الخيار 3 مطبقًا مع أدوات جي سكيب بطريقة واضحة جدا.ربما يمكنك تجربتهاإنها ليست مجانية، ولكنها تقوم بعملها.
هنا الحد الأدنى من فئة POC مع إزالة كل الأشياء
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
public class UncShareWithCredentials : IDisposable
{
private string _uncShare;
public UncShareWithCredentials(string uncShare, string userName, string password)
{
var nr = new Native.NETRESOURCE
{
dwType = Native.RESOURCETYPE_DISK,
lpRemoteName = uncShare
};
int result = Native.WNetUseConnection(IntPtr.Zero, nr, password, userName, 0, null, null, null);
if (result != Native.NO_ERROR)
{
throw new Win32Exception(result);
}
_uncShare = uncShare;
}
public void Dispose()
{
if (!string.IsNullOrEmpty(_uncShare))
{
Native.WNetCancelConnection2(_uncShare, Native.CONNECT_UPDATE_PROFILE, false);
_uncShare = null;
}
}
private class Native
{
public const int RESOURCETYPE_DISK = 0x00000001;
public const int CONNECT_UPDATE_PROFILE = 0x00000001;
public const int NO_ERROR = 0;
[DllImport("mpr.dll")]
public static extern int WNetUseConnection(IntPtr hwndOwner, NETRESOURCE lpNetResource, string lpPassword, string lpUserID,
int dwFlags, string lpAccessName, string lpBufferSize, string lpResult);
[DllImport("mpr.dll")]
public static extern int WNetCancelConnection2(string lpName, int dwFlags, bool fForce);
[StructLayout(LayoutKind.Sequential)]
public class NETRESOURCE
{
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;
}
}
}
يمكنك استخدامها مباشرة \\server\share\folder
ث / WNetUseConnection
, ، فلا داعي لتجريده \\server
جزء فقط مسبقا.
ايم إرفاق بلدي كود vb.net مرتكز على بريان مرجع
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Public Class PinvokeWindowsNetworking
Const NO_ERROR As Integer = 0
Private Structure ErrorClass
Public num As Integer
Public message As String
Public Sub New(ByVal num As Integer, ByVal message As String)
Me.num = num
Me.message = message
End Sub
End Structure
Private Shared ERROR_LIST As ErrorClass() = New ErrorClass() {
New ErrorClass(5, "Error: Access Denied"),
New ErrorClass(85, "Error: Already Assigned"),
New ErrorClass(1200, "Error: Bad Device"),
New ErrorClass(67, "Error: Bad Net Name"),
New ErrorClass(1204, "Error: Bad Provider"),
New ErrorClass(1223, "Error: Cancelled"),
New ErrorClass(1208, "Error: Extended Error"),
New ErrorClass(487, "Error: Invalid Address"),
New ErrorClass(87, "Error: Invalid Parameter"),
New ErrorClass(1216, "Error: Invalid Password"),
New ErrorClass(234, "Error: More Data"),
New ErrorClass(259, "Error: No More Items"),
New ErrorClass(1203, "Error: No Net Or Bad Path"),
New ErrorClass(1222, "Error: No Network"),
New ErrorClass(1206, "Error: Bad Profile"),
New ErrorClass(1205, "Error: Cannot Open Profile"),
New ErrorClass(2404, "Error: Device In Use"),
New ErrorClass(2250, "Error: Not Connected"),
New ErrorClass(2401, "Error: Open Files")}
Private Shared Function getErrorForNumber(ByVal errNum As Integer) As String
For Each er As ErrorClass In ERROR_LIST
If er.num = errNum Then Return er.message
Next
Try
Throw New Win32Exception(errNum)
Catch ex As Exception
Return "Error: Unknown, " & errNum & " " & ex.Message
End Try
Return "Error: Unknown, " & errNum
End Function
<DllImport("Mpr.dll")>
Private Shared Function WNetUseConnection(ByVal hwndOwner As IntPtr, ByVal lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserID As String, ByVal dwFlags As Integer, ByVal lpAccessName As String, ByVal lpBufferSize As String, ByVal lpResult As String) As Integer
End Function
<DllImport("Mpr.dll")>
Private Shared Function WNetCancelConnection2(ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Boolean) As Integer
End Function
<StructLayout(LayoutKind.Sequential)>
Private Class NETRESOURCE
Public dwScope As Integer = 0
Public dwType As Integer = 0
Public dwDisplayType As Integer = 0
Public dwUsage As Integer = 0
Public lpLocalName As String = ""
Public lpRemoteName As String = ""
Public lpComment As String = ""
Public lpProvider As String = ""
End Class
Public Shared Function connectToRemote(ByVal remoteUNC As String, ByVal username As String, ByVal password As String) As String
Return connectToRemote(remoteUNC, username, password, False)
End Function
Public Shared Function connectToRemote(ByVal remoteUNC As String, ByVal username As String, ByVal password As String, ByVal promptUser As Boolean) As String
Dim nr As NETRESOURCE = New NETRESOURCE()
nr.dwType = ResourceTypes.Disk
nr.lpRemoteName = remoteUNC
Dim ret As Integer
If promptUser Then
ret = WNetUseConnection(IntPtr.Zero, nr, "", "", Connects.Interactive Or Connects.Prompt, Nothing, Nothing, Nothing)
Else
ret = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, Nothing, Nothing, Nothing)
End If
If ret = NO_ERROR Then Return Nothing
Return getErrorForNumber(ret)
End Function
Public Shared Function disconnectRemote(ByVal remoteUNC As String) As String
Dim ret As Integer = WNetCancelConnection2(remoteUNC, Connects.UpdateProfile, False)
If ret = NO_ERROR Then Return Nothing
Return getErrorForNumber(ret)
End Function
Enum Resources As Integer
Connected = &H1
GlobalNet = &H2
Remembered = &H3
End Enum
Enum ResourceTypes As Integer
Any = &H0
Disk = &H1
Print = &H2
End Enum
Enum ResourceDisplayTypes As Integer
Generic = &H0
Domain = &H1
Server = &H2
Share = &H3
File = &H4
Group = &H5
End Enum
Enum ResourceUsages As Integer
Connectable = &H1
Container = &H2
End Enum
Enum Connects As Integer
Interactive = &H8
Prompt = &H10
Redirect = &H80
UpdateProfile = &H1
CommandLine = &H800
CmdSaveCred = &H1000
LocalDrive = &H100
End Enum
End Class
كيفية استخدامها
Dim login = PinvokeWindowsNetworking.connectToRemote("\\ComputerName", "ComputerName\UserName", "Password")
If IsNothing(login) Then
'do your thing on the shared folder
PinvokeWindowsNetworking.disconnectRemote("\\ComputerName")
End If
نظرت إلى MS للعثور على الإجابات.يفترض الحل الأول أن حساب المستخدم الذي يقوم بتشغيل عملية التطبيق لديه حق الوصول إلى المجلد المشترك أو محرك الأقراص (نفس المجال).تأكد من حل DNS الخاص بك أو حاول استخدام عنوان IP.ببساطة قم بما يلي:
DirectoryInfo di = new DirectoryInfo(PATH);
var files = di.EnumerateFiles("*.*", SearchOption.AllDirectories);
إذا كنت تريد استخدام نطاقات مختلفة .NET 2.0 ببيانات الاعتماد، فاتبع هذا النموذج:
WebRequest req = FileWebRequest.Create(new Uri(@"\\<server Name>\Dir\test.txt"));
req.Credentials = new NetworkCredential(@"<Domain>\<User>", "<Password>");
req.PreAuthenticate = true;
WebResponse d = req.GetResponse();
FileStream fs = File.Create("test.txt");
// here you can check that the cast was successful if you want.
fs = d.GetResponseStream() as FileStream;
fs.Close();