CSharp学习--sharpshares代码分析
GitHub地址:SharpShares
使用Visual Studio编译,运行效果(域环境)如下,列出了域内所有机器的共享,分为Unreadable Shares(不可读的)和Listable Shares(可读的):
原理流程图
前置知识
在我们看到不会的方法的时候,可以去参考官方文档:dotnetapi查询
先来看看几段关键代码
测试代码11
2
3
4
5
6
7
8
9
10
11
12
13public static void Main(string[] args)
{
#获取当前域
Console.WriteLine("Current DC :" + Domain.GetCurrentDomain());
#获取本机用户名
Console.WriteLine("Current User:" + WindowsIdentity.GetCurrent().Name.Split('\\')[1]);
#判断当前是否是System权限
Console.WriteLine("IsSystem:" + WindowsIdentity.GetCurrent().IsSystem);
#获取当前token id
Console.WriteLine("Current Token:" + WindowsIdentity.GetCurrent().Token);
#获取当前主机名
Console.WriteLine("Host Name:" + System.Net.Dns.GetHostName());
}
效果如下:
测试代码21
2
3
4
5
6
7
8
9
10
11
12public static void Main(string[] args)
{
#获取当前机器的ip,首先利用GetHostName方法获取主机名,将主机名作为参数传入GetHostAddresses中
IPAddress[] ips = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());
foreach (IPAddress ip in ips)
{
if (!ip.ToString().Contains(":"))
{
Console.WriteLine("{0}: {1}", System.Net.Dns.GetHostName(), ip);
}
}
}
效果如下:
测试代码31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102using System;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Security.Principal;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ConsoleApp2
{
class Program
{
#region External Calls
[DllImport("Netapi32.dll", SetLastError = true)]
static extern int NetApiBufferFree(IntPtr Buffer);
[DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
private static extern int NetShareEnum(
StringBuilder ServerName,
int level,
ref IntPtr bufPtr,
uint prefmaxlen,
ref int entriesread,
ref int totalentries,
ref int resume_handle
);
#endregion
#region External Structures
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SHARE_INFO_1
{
public string shi1_netname;
public uint shi1_type;
public string shi1_remark;
public SHARE_INFO_1(string sharename, uint sharetype, string remark)
{
this.shi1_netname = sharename;
this.shi1_type = sharetype;
this.shi1_remark = remark;
}
public override string ToString()
{
return shi1_netname;
}
}
#endregion
const uint MAX_PREFERRED_LENGTH = 0xFFFFFFFF;
const int NERR_Success = 0;
private enum NetError : uint
{
NERR_Success = 0,
NERR_BASE = 2100,
NERR_UnknownDevDir = (NERR_BASE + 16),
NERR_DuplicateShare = (NERR_BASE + 18),
NERR_BufTooSmall = (NERR_BASE + 23),
}
private enum SHARE_TYPE : uint
{
STYPE_DISKTREE = 0,
STYPE_PRINTQ = 1,
STYPE_DEVICE = 2,
STYPE_IPC = 3,
STYPE_SPECIAL = 0x80000000,
}
public static SHARE_INFO_1[] EnumNetShares(string Server)
{
List<SHARE_INFO_1> ShareInfos = new List<SHARE_INFO_1>();
int entriesread = 0;
int totalentries = 0;
int resume_handle = 0;
int nStructSize = Marshal.SizeOf(typeof(SHARE_INFO_1));
IntPtr bufPtr = IntPtr.Zero;
StringBuilder server = new StringBuilder(Server);
int ret = NetShareEnum(server, 1, ref bufPtr, MAX_PREFERRED_LENGTH, ref entriesread, ref totalentries, ref resume_handle);
if (ret == NERR_Success)
{
IntPtr currentPtr = bufPtr;
for (int i = 0; i < entriesread; i++)
{
SHARE_INFO_1 shi1 = (SHARE_INFO_1)Marshal.PtrToStructure(currentPtr, typeof(SHARE_INFO_1));
ShareInfos.Add(shi1);
currentPtr += nStructSize;
}
NetApiBufferFree(bufPtr);
return ShareInfos.ToArray();
}
else
{
ShareInfos.Add(new SHARE_INFO_1("ERROR=" + ret.ToString(), 10, string.Empty));
return ShareInfos.ToArray();
}
}
static void Main(string[] args)
{
SHARE_INFO_1[] computerShares = EnumNetShares(System.Net.Dns.GetHostName());
foreach (SHARE_INFO_1 computershare in computerShares) {
Console.WriteLine(computershare);
}
}
}
}
这段代码其实是别人造好的轮子(这里面涉及到很多知识点,不一一展开讲了),输出共享路径,不过方法要添加static属性,不然会报错,参考
效果如下:
看过这些之后,再看看源码调用的逻辑就行了
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 sher10cksec@foxmail.com
文章标题:CSharp学习--sharpshares代码分析
本文作者:sher10ck
发布时间:2020-12-22, 15:43:03
最后更新:2020-12-22, 18:10:09
原始链接:http://sherlocz.github.io/2020/12/22/sharpshares-code/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。