Created
September 7, 2022 01:34
-
-
Save preetampvp/e0cb16cd4b88e243e371b8b297d35db6 to your computer and use it in GitHub Desktop.
SMB Local
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net; | |
using SMBLibrary; | |
using SMBLibrary.Client; | |
using FileAttributes = SMBLibrary.FileAttributes; | |
var client = new SMB2Client(); | |
var isConnected = client.Connect( | |
IPAddress.Loopback, | |
SMBTransportType.DirectTCPTransport | |
); | |
if (!isConnected) | |
{ | |
Console.WriteLine("Cannot connect"); | |
return; | |
} | |
var status = client.Login("localhost/Shared", "guest", ""); | |
if (status != NTStatus.STATUS_SUCCESS) | |
{ | |
Console.WriteLine("Unable to login", status); | |
return; | |
} | |
Console.WriteLine($"Successfully connected {status}"); | |
var shares = client.ListShares(out status); | |
if (status == NTStatus.STATUS_SUCCESS) | |
{ | |
foreach (var share in shares) | |
{ | |
Console.WriteLine(share); | |
} | |
} | |
var filestore = client.TreeConnect("guest", out status); | |
if (status != NTStatus.STATUS_SUCCESS) | |
{ | |
Console.WriteLine($"unable to tree connect to share {status}"); | |
} | |
status = filestore.CreateFile(out var directoryHandle, out var fileStatus, "", AccessMask.GENERIC_READ, FileAttributes.Directory, | |
ShareAccess.Read | ShareAccess.Write, | |
CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null); | |
if (status == NTStatus.STATUS_SUCCESS) | |
{ | |
status = filestore.QueryDirectory(out var fileList, directoryHandle, "*", FileInformationClass.FileDirectoryInformation); | |
status = filestore.CloseFile(directoryHandle); | |
Console.WriteLine(fileList.Count); | |
foreach (var file in fileList) | |
{ | |
Console.WriteLine(file.ToString()); | |
} | |
} | |
else | |
{ | |
Console.WriteLine($"unable to create directory handle {status}"); | |
} | |
client.Logoff(); | |
client.Disconnect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment