# 服务

考虑到很多服务也是开机启动的，因此服务也可以用来实现持久化。我们可以通过 SharPersist 工具的命令来实现

```
SharPersist -t service -c "C:\Windows\Tasks\Beacon.exe"  -n "Update Services" -m add 
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-03/scaled-1680-/R6PoCmcCHsDyU7Wr-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-03/R6PoCmcCHsDyU7Wr-image.png)

我们也可以手动创建后门服务。

```powershell
sc.exe create Service2 binPath= "C:\Windows\System32\calc.exe" start=auto

sc.exe start Service2
```

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-03/scaled-1680-/jcZjqV1UWIiXygVB-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-03/jcZjqV1UWIiXygVB-image.png)

我们可以直接将 **binPath** 指向我们想要的可执行文件，例如 exe 载荷。但是需要注意的是，服务所用的 exe 可执行文件与一般的可执行文件有所不同，如果我们直接指向一般可执行文件，例如 exe 版的 CS 或 Metasploit 载荷，我们会遇到 **The service did not respond to the start or control request in a timely fashion** 报错，或者程序会在运行后很快退出。作为一个 workaround，我们可以使用具有**进程注入**功能的 Shellcode 加载器，即在程序退出之前迁移到其他进程中实现持久化。我们也可以直接生成服务二进制文件。

[![image.png](https://raven-medicine.com/uploads/images/gallery/2023-03/scaled-1680-/N3N1SCnzXMweum04-image.png)](https://raven-medicine.com/uploads/images/gallery/2023-03/N3N1SCnzXMweum04-image.png)