【笔记】Windows上通过篡改文件实现提权

前言

Windows上通过篡改文件实现提权

替换程序的DLL文件

  • 在某个程序的根目录下,查找可能会被调用的DLL文件,替换为反弹Shell的DLL文件
  • 当程序被启动时,反弹Shell会被执行,此时得到的权限为启动程序时的权限

通过ChkDllHijack检测DLL文件是否被指定程序调用

下载项目

1
2
3
certutil -urlcache -split -f https://raw.githubusercontent.com/anhkgg/anhkgg-tools/refs/heads/master/ChkDllHijack.zip
"C:\Program Files\7-Zip\7z.exe" x ChkDllHijack.zip -oChkDllHijack
cd ChkDllHijack

篡改服务路径包含空格的服务

  • 在服务器管理器中,找到启动服务的命令的目录路径中包含空格且不包含引号的服务,比如c:\program files\xxx
  • 截取第一个空格之前的路径,伪造可执行文件为反弹Shell的EXE文件c:\program.exe
  • 当服务被启动时,反弹Shell会被执行,此时得到的权限为系统权限

通过wmic命令检测可以被篡改的服务

1
wmic service get name,displayname,pathname,startmode | findstr /i "Auto" | findstr /i /v "C:\Windows\\" | findstr /i /v """

通过JAWS检测可以被篡改的服务

下载项目

1
2
git clone https://github.com/411Hall/JAWS.git
cd JAWS
源代码
jaws-enum.ps1
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<#
.SYNOPSIS
Windows enumeration script
.DESCRIPTION
This script is designed to be used in a penetration test or CTF
enviroment. It will enumerate useful information from the host
for privilege escalation.
.EXAMPLE
PS > .\jaws-enum.ps1
will write results out to screen.
.EXAMPLE
PS > .\jaws-enum.ps1 -OutputFileName Jaws-Enum.txt
Writes out results to Jaws-Enum.txt in current directory.
.LINK
https://github.com/411Hall/JAWS
#>
Param(
[String]$OutputFilename = ""
)

function JAWS-ENUM {
write-output "`nRunning J.A.W.S. Enumeration"
$output = ""
$output = $output + "############################################################`r`n"
$output = $output + "## J.A.W.S. (Just Another Windows Enum Script) ##`r`n"
$output = $output + "## ##`r`n"
$output = $output + "## https://github.com/411Hall/JAWS ##`r`n"
$output = $output + "## ##`r`n"
$output = $output + "############################################################`r`n"
$output = $output + "`r`n"
$win_version = (Get-WmiObject -class Win32_OperatingSystem)
$output = $output + "Windows Version: " + (($win_version.caption -join $win_version.version) + "`r`n")
$output = $output + "Architecture: " + (($env:processor_architecture) + "`r`n")
$output = $output + "Hostname: " + (($env:ComputerName) + "`r`n")
$output = $output + "Current User: " + (($env:username) + "`r`n")
$output = $output + "Current Time\Date: " + (get-date)
$output = $output + "`r`n"
$output = $output + "`r`n"
write-output " - Gathering User Information"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Users`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME"
$adsi.Children | where {$_.SchemaClassName -eq 'user'} | Foreach-Object {
$groups = $_.Groups() | Foreach-Object {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
$output = $output + "----------`r`n"
$output = $output + "Username: " + $_.Name + "`r`n"
$output = $output + "Groups: " + $groups + "`r`n"
}
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Network Information`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + (ipconfig | out-string)
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Arp`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + (arp -a | out-string)
$output = $output + "`r`n"
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " NetStat`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + (netstat -ano | out-string)
$output = $output + "`r`n"
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Firewall Status`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + "`r`n"
$Firewall = New-Object -com HNetCfg.FwMgr
$FireProfile = $Firewall.LocalPolicy.CurrentProfile
if ($FireProfile.FirewallEnabled -eq $False) {
$output = $output + ("Firewall is Disabled" + "`r`n")
} else {
$output = $output + ("Firwall is Enabled" + "`r`n")
}
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " FireWall Rules`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
Function Get-FireWallRule
{Param ($Name, $Direction, $Enabled, $Protocol, $profile, $action, $grouping)
$Rules=(New-object -comObject HNetCfg.FwPolicy2).rules
If ($name) {$rules= $rules | where-object {$_.name -like $name}}
If ($direction) {$rules= $rules | where-object {$_.direction -eq $direction}}
If ($Enabled) {$rules= $rules | where-object {$_.Enabled -eq $Enabled}}
If ($protocol) {$rules= $rules | where-object {$_.protocol -eq $protocol}}
If ($profile) {$rules= $rules | where-object {$_.Profiles -bAND $profile}}
If ($Action) {$rules= $rules | where-object {$_.Action -eq $Action}}
If ($Grouping) {$rules= $rules | where-object {$_.Grouping -like $Grouping}}
$rules}
$output = $output + (Get-firewallRule -enabled $true | sort direction,applicationName,name | format-table -property Name , localPorts,applicationname | out-string)
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Hosts File Content`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + "`r`n"
$output = $output + ((get-content $env:windir\System32\drivers\etc\hosts | out-string) + "`r`n")
$output = $output + "`r`n"
write-output " - Gathering Processes, Services and Scheduled Tasks"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Processes`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + ((Get-WmiObject win32_process | Select-Object Name,ProcessID,@{n='Owner';e={$_.GetOwner().User}},CommandLine | sort name | format-table -wrap -autosize | out-string) + "`r`n")
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Scheduled Tasks`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + "Current System Time: " + (get-date)
$output = $output + (schtasks /query /FO CSV /v | convertfrom-csv | where { $_.TaskName -ne "TaskName" } | select "TaskName","Run As User", "Task to Run" | fl | out-string)
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Services`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + (get-service | Select Name,DisplayName,Status | sort status | Format-Table -Property * -AutoSize | Out-String -Width 4096)
$output = $output + "`r`n"
write-output " - Gathering Installed Software"
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Installed Programs`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + (get-wmiobject -Class win32_product | select Name, Version, Caption | ft -hidetableheaders -autosize| out-string -Width 4096)
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Installed Patches`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + (Get-Wmiobject -class Win32_QuickFixEngineering -namespace "root\cimv2" | select HotFixID, InstalledOn| ft -autosize | out-string )
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Program Folders`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + "`n`rC:\Program Files`r`n"
$output = $output + "-------------"
$output = $output + (get-childitem "C:\Program Files" -EA SilentlyContinue | select Name | ft -hidetableheaders -autosize| out-string)
$output = $output + "C:\Program Files (x86)`r`n"
$output = $output + "-------------------"
$output = $output + (get-childitem "C:\Program Files (x86)" -EA SilentlyContinue | select Name | ft -hidetableheaders -autosize| out-string)
$output = $output + "`r`n"
write-output " - Gathering File System Information"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Files with Full Control and Modify Access`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$files = get-childitem C:\
foreach ($file in $files){
try {
$output = $output + (get-childitem "C:\$file" -include *.ps1,*.bat,*.com,*.vbs,*.txt,*.html,*.conf,*.rdp,.*inf,*.ini -recurse -EA SilentlyContinue | get-acl -EA SilentlyContinue | select path -expand access |
where {$_.identityreference -notmatch "BUILTIN|NT AUTHORITY|EVERYONE|CREATOR OWNER|NT SERVICE"} | where {$_.filesystemrights -match "FullControl|Modify"} |
ft @{Label="";Expression={Convert-Path $_.Path}} -hidetableheaders -autosize | out-string -Width 4096)
}
catch {
$output = $output + "`nFailed to read more files`r`n"
}
}

$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Folders with Full Control and Modify Access`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$folders = get-childitem C:\
foreach ($folder in $folders){
try {
$output = $output + (Get-ChildItem -Recurse "C:\$folder" -EA SilentlyContinue | ?{ $_.PSIsContainer} | get-acl | select path -expand access |
where {$_.identityreference -notmatch "BUILTIN|NT AUTHORITY|CREATOR OWNER|NT SERVICE"} | where {$_.filesystemrights -match "FullControl|Modify"} |
select path,filesystemrights,IdentityReference | ft @{Label="";Expression={Convert-Path $_.Path}} -hidetableheaders -autosize | out-string -Width 4096)
}
catch {
$output = $output + "`nFailed to read more folders`r`n"
}
}
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Mapped Drives`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + (Get-WmiObject -Class Win32_LogicalDisk | select DeviceID, VolumeName | ft -hidetableheaders -autosize | out-string -Width 4096)
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Unquoted Service Paths`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + (cmd /c 'wmic service get name,displayname,pathname,startmode |findstr /i "auto" |findstr /i /v "c:\windows\\" |findstr /i /v """')
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Recent Documents`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + (get-childitem "C:\Users\$env:username\AppData\Roaming\Microsoft\Windows\Recent" -EA SilentlyContinue | select Name | ft -hidetableheaders | out-string )
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Potentially Interesting Files in Users Directory `r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + (get-childitem "C:\Users\" -recurse -Include *.zip,*.rar,*.7z,*.gz,*.conf,*.rdp,*.kdbx,*.crt,*.pem,*.ppk,*.txt,*.xml,*.vnc.*.ini,*.vbs,*.bat,*.ps1,*.cmd -EA SilentlyContinue | %{$_.FullName } | out-string)
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " 10 Last Modified Files in C:\User`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + (Get-ChildItem 'C:\Users' -recurse -EA SilentlyContinue | Sort {$_.LastWriteTime} | %{$_.FullName } | select -last 10 | ft -hidetableheaders | out-string)
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " MUICache Files`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
get-childitem "HKCU:\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\" -EA SilentlyContinue |
foreach { $CurrentKey = (Get-ItemProperty -Path $_.PsPath)
if ($CurrentKey -match "C:\\") {
$output = $output + ($_.Property -join "`r`n")
}
}
$output = $output + "`r`n"
$output = $output + "`r`n"
write-output " - Looking for Simple Priv Esc Methods"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " System Files with Passwords`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$files = ("unattended.xml", "sysprep.xml", "autounattended.xml","unattended.inf", "sysprep.inf", "autounattended.inf","unattended.txt", "sysprep.txt", "autounattended.txt")
$output = $output + (get-childitem C:\ -recurse -include $files -EA SilentlyContinue | Select-String -pattern "<Value>" | out-string)
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " AlwaysInstalledElevated Registry Key`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$HKLM = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Installer"
$HKCU = "HKCU:\SOFTWARE\Policies\Microsoft\Windows\Installer"
if (($HKLM | test-path) -eq "True")
{
if (((Get-ItemProperty -Path $HKLM -Name AlwaysInstallElevated).AlwaysInstallElevated) -eq 1)
{
$output = $output + "AlwaysInstallElevated enabled on this host!"
}
}
if (($HKCU | test-path) -eq "True")
{
if (((Get-ItemProperty -Path $HKCU -Name AlwaysInstallElevated).AlwaysInstallElevated) -eq 1)
{
$output = $output + "AlwaysInstallElevated enabled on this host!"
}
}
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Stored Credentials`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + (cmdkey /list | out-string)
$output = $output + "`r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$output = $output + " Checking for AutoAdminLogon `r`n"
$output = $output + "-----------------------------------------------------------`r`n"
$Winlogon = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
if (get-itemproperty -path $Winlogon -Name AutoAdminLogon -ErrorAction SilentlyContinue)
{
if ((get-itemproperty -path $Winlogon -Name AutoAdminLogon).AutoAdminLogon -eq 1)
{
$Username = (get-itemproperty -path $Winlogon -Name DefaultUserName).DefaultUsername
$output = $output + "The default username is $Username `r`n"
$Password = (get-itemproperty -path $Winlogon -Name DefaultPassword).DefaultPassword
$output = $output + "The default password is $Password `r`n"
$DefaultDomainName = (get-itemproperty -path $Winlogon -Name DefaultDomainName).DefaultDomainName
$output = $output + "The default domainname is $DefaultDomainName `r`n"
}
}
$output = $output + "`r`n"
if ($OutputFilename.length -gt 0)
{
$output | Out-File -FilePath $OutputFileName -encoding utf8
}
else
{
clear-host
write-output $output
}
}

if ($OutputFilename.length -gt 0)
{
Try
{
[io.file]::OpenWrite($OutputFilename).close()
JAWS-ENUM
}
Catch
{
Write-Warning "`nUnable to write to output file $OutputFilename, Check path and permissions"
}
}
else
{
JAWS-ENUM
}

检测可以被篡改的服务

-OutputFileName <file_name>.txt:将结果输出到文件

1
.\jaws-enum.ps1

篡改可以被任意修改的服务

  • 将可以被任意修改的服务的可执行文件改为反弹Shell的EXE文件

通过accesschk命令检测可以被篡改的服务

  • 篡改权限为SERVICE_ALL_ACCESS的服务
1
.\accesschk.exe -uwcqv "administrators" *

通过JAWS检测可以被篡改的服务

完成