Learn Python & Ethical Hacking From Scratch

01 - Introduction

001 Teaser

002 Course Introduction





003 Introduction to Python Programming & Ethical Hacking

004 Lab Overview


005 Initial Preparation



kali

006 Installing Kali Linux as a VM on Windows



  • username: root
  • password: toor

010 Basic Overview of Kali Linux

kali可以有多个工作区workspace, ctrl+alt+↓可以切换

011 The Terminal & Linux Commands

012 Python 2 VS Python 3 & Writing Our First Program

#!/usr/bin/env python3 
# 如果是python2就把python3改为python

print("hello world")

013 Installing & Using a Python IDE

移动到/opt
文件地址栏 ctrl+l 可以输入路径

02 - Writing a MAC Address Changer - Python Basics

001 What is MAC Address & How To Change it

# 禁用网络接口
ifconfig eth0 down
# 修改mac地址, 至少12个字符
ifconfig eth0 hw ether 00:11:22:33:44:55
# 启用网络接口
ifconfig eth0 up

002 Using Python Modules & Executing System Commands

import subprocess

subprocess.call("ifconfig", shell=True)

003 Implementing a Very Basic MAC Changer

import subprocess

subprocess.call("ifconfig eth0 down", shell=True)
subprocess.call("ifconfig eth0 hw ether 00:11:22:33:44:55", shell=True)
subprocess.call("ifconfig eth0 up", shell=True)

004 Variables & Strings

interface = "eth0"
new_mac = "00:11:22:33:44:55"

print("[+] Changing MAC address for " + interface + " to " + new_mac) 

005 Using Variables in MAC Changer

import subprocess

interface = "eth0"
new_mac = "00:11:22:33:44:66"

print("[+] Changing MAC address for " + interface + " to " + new_mac)

subprocess.call("ifconfig " + interface + " down", shell=True)
subprocess.call("ifconfig " + interface + " hw ether " + new_mac, shell=True)
subprocess.call("ifconfig " + interface + " up", shell=True)
subprocess.call("ifconfig", shell=True)

006 Getting Input From The User

import subprocess

# interface = "eth0"
# interface = raw_input("interface > ") # python2
interface = input("interface > ")

# new_mac = "00:11:22:33:44:66"
# new_mac = raw_input("new MAC > ") # python2
new_mac = input("new MAC > ")

print("[+] Changing MAC address for " + interface + " to " + new_mac)

subprocess.call("ifconfig " + interface + " down", shell=True)
subprocess.call("ifconfig " + interface + " hw ether " + new_mac, shell=True)
subprocess.call("ifconfig " + interface + " up", shell=True)
subprocess.call("ifconfig", shell=True)

007 Handling User Input

我们的程序可能被用来执行其他命令, 比如;ls;被注入了我们的代码, 而subprocess执行了我们本不希望执行的命令

import subprocess

# interface = "eth0"
# interface = raw_input("interface > ") # python2
interface = input("interface > ")

# new_mac = "00:11:22:33:44:66"
# new_mac = raw_input("new MAC > ") # python2
new_mac = input("new MAC > ")

print("[+] Changing MAC address for " + interface + " to " + new_mac)

# unsecurity
# subprocess.call("ifconfig " + interface + " down", shell=True)
# subprocess.call("ifconfig " + interface + " hw ether " + new_mac, shell=True)
# subprocess.call("ifconfig " + interface + " up", shell=True)
# subprocess.call("ifconfig", shell=True)

subprocess.call(['ifconfig', interface, 'down'])
subprocess.call(['ifconfig', interface, "hw", "ether", new_mac])
subprocess.call(['ifconfig', interface, 'up'])
subprocess.call(["ifconfig"])

008 Handling Command-line Arguments

import optparse

parser = optparse.OptionParser()

parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC address")

parser.parse_args()

009 Initialising Variables Based on Command-line Arguments

import optparse

parser = optparse.OptionParser()

parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC address")
parser.add_option("-m", "--mac", dest="new_mac", help="New MAC address")

(options, arguments) = parser.parse_args()

# interface = "eth0"
# interface = raw_input("interface > ") # python2
# interface = input("interface > ")
interface = options.interface

# new_mac = "00:11:22:33:44:66"
# new_mac = raw_input("new MAC > ") # python2
# new_mac = input("new MAC > ")
new_mac = options.new_mac

print("[+] Changing MAC address for " + interface + " to " + new_mac)

import subprocess

# unsecurity
# subprocess.call("ifconfig " + interface + " down", shell=True)
# subprocess.call("ifconfig " + interface + " hw ether " + new_mac, shell=True)
# subprocess.call("ifconfig " + interface + " up", shell=True)
# subprocess.call("ifconfig", shell=True)

subprocess.call(['ifconfig', interface, 'down'])
subprocess.call(['ifconfig', interface, "hw", "ether", new_mac])
subprocess.call(['ifconfig', interface, 'up'])
subprocess.call(["ifconfig"])

010 Python Functions

import optparse

parser = optparse.OptionParser()

parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC address")
parser.add_option("-m", "--mac", dest="new_mac", help="New MAC address")

(options, arguments) = parser.parse_args()

# interface = "eth0"
# interface = raw_input("interface > ") # python2
# interface = input("interface > ")
interface = options.interface

# new_mac = "00:11:22:33:44:66"
# new_mac = raw_input("new MAC > ") # python2
# new_mac = input("new MAC > ")
new_mac = options.new_mac

import subprocess


def change_mac(interface, new_mac):
    print("[+] Changing MAC address for " + interface + " to " + new_mac)

    # unsecurity
    # subprocess.call("ifconfig " + interface + " down", shell=True)
    # subprocess.call("ifconfig " + interface + " hw ether " + new_mac, shell=True)
    # subprocess.call("ifconfig " + interface + " up", shell=True)
    # subprocess.call("ifconfig", shell=True)

    subprocess.call(['ifconfig', interface, 'down'])
    subprocess.call(['ifconfig', interface, "hw", "ether", new_mac])
    subprocess.call(['ifconfig', interface, 'up'])
    subprocess.call(["ifconfig"])


change_mac(interface, new_mac)

011 Returning Values From Functions

import optparse


def get_arguments():
    parser = optparse.OptionParser()

    parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC address")
    parser.add_option("-m", "--mac", dest="new_mac", help="New MAC address")

    return parser.parse_args()


import subprocess


def change_mac(interface, new_mac):
    print("[+] Changing MAC address for " + interface + " to " + new_mac)

    subprocess.call(['ifconfig', interface, 'down'])
    subprocess.call(['ifconfig', interface, "hw", "ether", new_mac])
    subprocess.call(['ifconfig', interface, 'up'])
    subprocess.call(["ifconfig"])


(options, arguments) = get_arguments()
change_mac(options.interface, options.new_mac)

012 Decision Making in Python

013 Using Conditional Statements in MAC Changer

import optparse


def get_arguments():
    parser = optparse.OptionParser()

    parser.add_option("-i", "--interface", dest="interface", help="Interface to change its MAC address")
    parser.add_option("-m", "--mac", dest="new_mac", help="New MAC address")

    (options, arguments) = parser.parse_args()

    if not options.interface:
        parser.error("[-] Please specify an interface, use --help for more info")
    elif not options.new_mac:
        parser.error("[-] Please specify an new mac, use --help for more info")
    return options


import subprocess


def change_mac(interface, new_mac):
    print("[+] Changing MAC address for " + interface + " to " + new_mac)

    subprocess.call(['ifconfig', interface, 'down'])
    subprocess.call(['ifconfig', interface, "hw", "ether", new_mac])
    subprocess.call(['ifconfig', interface, 'up'])
    subprocess.call(["ifconfig"])


options = get_arguments()
change_mac(options.interface, options.new_mac)

03 - MAC Changer - Algorithm Design

001 Introduction to Algorithms

002 Reading Output Returned By System Commands

ifconfig_result = subprocess.check_output(['ifconfig', options.interface])
print(ifconfig_result)

003 Introduction to Regular Expressions (Regex)

004 Extracting a Substring Using Regex

ifconfig_result = subprocess.check_output(['ifconfig', options.interface])
print(ifconfig_result)

# TypeError: cannot use a string pattern on a bytes-like object
# mac_addr_search_res = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
mac_addr_search_res = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result.decode('utf8'))

if mac_addr_search_res:
    print(mac_addr_search_res.group(0))
else:
    print("[-] Could not read MAC address.")

005 Refactoring & Housekeeping

def get_current_mac(interface):
    ifconfig_result = subprocess.check_output(['ifconfig', interface])

    # TypeError: cannot use a string pattern on a bytes-like object
    # mac_addr_search_res = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
    mac_addr_search_res = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result.decode('utf8'))

    if mac_addr_search_res:
        return mac_addr_search_res.group(0)
    else:
        print("[-] Could not read MAC address.")


current_mac = get_current_mac(options.interface)
print("Current MAC: " + str(current_mac))

006 Implementing The Validation Algorithm

###

options = get_arguments()


def get_current_mac(interface):
    ifconfig_result = subprocess.check_output(['ifconfig', interface])

    # TypeError: cannot use a string pattern on a bytes-like object
    # mac_addr_search_res = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result)
    # mac_addr_search_res = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", ifconfig_result.decode('utf8'))
    mac_addr_search_res = re.search(r"\w\w:\w\w:\w\w:\w\w:\w\w:\w\w", str(ifconfig_result))

    if mac_addr_search_res:
        return mac_addr_search_res.group(0)
    else:
        print("[-] Could not read MAC address.")


current_mac = get_current_mac(options.interface)
print("Current MAC: " + str(current_mac))

change_mac(options.interface, options.new_mac)

current_mac = get_current_mac(options.interface)
if current_mac == options.new_mac:
    print("[+] MAC address was successfully changed to " + current_mac)
else:
    print("[-] MAC address did not get changed")

007 Python 3 Compatibility

04 - Programming a Network Scanner

001 Introduction & Teaser

002 Installing Windows as a Virtual Machine

003 Introduction to ARP

import scapy.all as scapy

def scan(ip):
    scapy.arping(ip)

# route -n
scan("192.168.92.2/24")

004 Designing an Algorithm To Discover Clients on The Same Network


  目录