-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsp_shared_folders.py
More file actions
131 lines (111 loc) · 4.16 KB
/
sp_shared_folders.py
File metadata and controls
131 lines (111 loc) · 4.16 KB
1
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# MIT License – Copyright (c) 2025 Menny Levinski
"""
Mapping shared folders on the device (requires admin permissions).
- Third-party:
- pywin32 (win32security, win32con, pythoncom)
- wmi
"""
import ctypes
import win32security
import win32con as con
import pythoncom
import wmi
import os
# --- Get shared folders ---
# --- Check if script runs as admin ---
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
# --- Resolve SID to account name ---
def resolve_sid(sid_str):
try:
sid = win32security.ConvertStringSidToSid(sid_str)
name, domain, _ = win32security.LookupAccountSid(None, sid)
return f"{domain}\\{name}" if domain else name
except Exception:
return sid_str
# --- Get permissions for a folder ---
def get_permissions(folder_path):
perms = []
everyone_detected = False
try:
sd = win32security.GetFileSecurity(folder_path, win32security.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl()
if dacl:
for i in range(dacl.GetAceCount()):
ace = dacl.GetAce(i)
sid = ace[2]
access_mask = ace[1]
sid_str = win32security.ConvertSidToStringSid(sid)
account = resolve_sid(sid_str)
# Check for Everyone
if account.upper() in ("EVERYONE", "BUILTIN\\EVERYONE"):
everyone_detected = True
if account.startswith("NT AUTHORITY\\"):
continue # skip these accounts
rights = []
if access_mask & con.FILE_GENERIC_READ:
rights.append("Read")
if access_mask & con.FILE_GENERIC_WRITE:
rights.append("Write")
if access_mask & con.FILE_GENERIC_EXECUTE:
rights.append("Execute")
if access_mask & con.FILE_ALL_ACCESS:
rights = ["Full Control"]
perms.append(f"{account}: {', '.join(rights) if rights else 'Special Permissions'}")
else:
perms.append("(No DACL)")
except Exception as e:
perms.append(f"(Unable to read ACL: {e})")
return perms, everyone_detected
# --- Get shared folders with Everyone check ---
def get_shared_folders():
output = [""]
shares_data = []
try:
pythoncom.CoInitialize()
c = wmi.WMI()
admin = is_admin()
for s in c.Win32_Share():
if s.Name.upper() in ("ADMIN$", "IPC$"):
continue
if admin and s.Path:
permissions, everyone_flag = get_permissions(s.Path)
else:
permissions = ["(Access denied)"]
everyone_flag = False
shares_data.append({
"Name": s.Name,
"Path": s.Path or "",
"Description": s.Description or "None",
"Permissions": permissions,
"Everyone": everyone_flag
})
except Exception as e:
output.append(f"Error: {e}")
return "\n".join(output)
if not shares_data:
output.append("No shared folders found.")
else:
for i, share in enumerate(shares_data):
output.append(f"Share Name : {share['Name']}")
output.append(f"Path : {share['Path']}")
output.append(f"Description: {share['Description']}")
output.append("Permissions:")
for perm in share["Permissions"]:
output.append(f" {perm}")
if share["Everyone"]:
output.append("⚠️ Warning: Folder accessible by Everyone!")
# Only add the separator if not the last share
if i != len(shares_data) - 1:
output.append("–" * 40)
return "\n".join(output)
# --- Output ---
if __name__ == "__main__":
print("Shared Folders Mapping")
print("–" * len("Shared Folders Mapping"))
print(get_shared_folders())
print("")
os.system("pause")