mirror of
https://github.com/autistic-symposium/web3-starter-py.git
synced 2025-07-22 06:29:04 -04:00
rename
This commit is contained in:
parent
dac26e1abb
commit
90049c80af
15 changed files with 0 additions and 0 deletions
8
Security_examples/README.md
Normal file
8
Security_examples/README.md
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Medium Examples
|
||||
|
||||
This directory holds any code and snippet that I have published in Medium:
|
||||
|
||||
|
||||
* [Learn Networking with Python’s Socket and Threading Module 🚀](https://medium.com/python-for-the-utopian/learning-networking-with-pythons-socket-and-threading-module-30dc77e1fc59).
|
||||
* [Understand the SSH Protocol with Python 🐍](https://medium.com/python-for-the-utopian/writing-ssh-client-and-server-in-python-b5b330c983d3).
|
||||
* [Building a Snifffffing Scanner in Python 💣](https://medium.com/python-for-the-utopian/building-a-udp-scanner-in-python-84e62947aab7).
|
17
Security_examples/python_sniff_scanner/icmp_example.py
Normal file
17
Security_examples/python_sniff_scanner/icmp_example.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
import ctypes
|
||||
|
||||
|
||||
class ICMP(ctypes.Structure):
|
||||
_fields_ = [
|
||||
('type', ctypes.c_ubyte),
|
||||
('code', ctypes.c_ubyte),
|
||||
('checksum', ctypes.c_ushort),
|
||||
('unused', ctypes.c_ushort),
|
||||
('next_hop_mtu',ctypes.c_ushort)
|
||||
]
|
||||
|
||||
def __new__(self, socket_buffer):
|
||||
return self.from_buffer_copy(socket_buffer)
|
||||
|
||||
def __init__(self, socket_buffer):
|
||||
pass
|
44
Security_examples/python_sniff_scanner/main_example.py
Normal file
44
Security_examples/python_sniff_scanner/main_example.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
import socket
|
||||
import os
|
||||
import struct
|
||||
import ctypes
|
||||
|
||||
from ICMPHeader import ICMP
|
||||
|
||||
|
||||
# host to listen on
|
||||
HOST = '192.168.1.114'
|
||||
|
||||
|
||||
def main():
|
||||
socket_protocol = socket.IPPROTO_ICMP
|
||||
sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
|
||||
sniffer.bind(( HOST, 0 ))
|
||||
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
|
||||
|
||||
while True:
|
||||
raw_buffer = sniffer.recvfrom(65565)[0]
|
||||
ip_header = raw_buffer[0:20]
|
||||
iph = struct.unpack('!BBHHHBBH4s4s' , ip_header)
|
||||
|
||||
# Create our IP structure
|
||||
version_ihl = iph[0]
|
||||
version = version_ihl >> 4
|
||||
ihl = version_ihl & 0xF
|
||||
iph_length = ihl * 4
|
||||
ttl = iph[5]
|
||||
protocol = iph[6]
|
||||
s_addr = socket.inet_ntoa(iph[8]);
|
||||
d_addr = socket.inet_ntoa(iph[9]);
|
||||
print('IP -> Version:' + str(version) + ', Header Length:' + str(ihl) + \
|
||||
', TTL:' + str(ttl) + ', Protocol:' + str(protocol) + ', Source:'\
|
||||
+ str(s_addr) + ', Destination:' + str(d_addr))
|
||||
|
||||
# Create our ICMP structure
|
||||
buf = raw_buffer[iph_length:iph_length + ctypes.sizeof(ICMP)]
|
||||
icmp_header = ICMP(buf)
|
||||
print('ICMP -> Type:{0}, Code:{1}'.format((icmp_header.type, icmp_header.code)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
33
Security_examples/python_sniff_scanner/sniff_example.py
Normal file
33
Security_examples/python_sniff_scanner/sniff_example.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
import os
|
||||
import socket
|
||||
|
||||
|
||||
# host to listen
|
||||
HOST = '192.168.1.114'
|
||||
|
||||
|
||||
def sniffing(host, win, socket_prot):
|
||||
|
||||
while True:
|
||||
sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_prot)
|
||||
sniffer.bind((host, 0))
|
||||
|
||||
# include the IP headers in the captured packets
|
||||
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
|
||||
|
||||
if win == 1:
|
||||
sniffer.ioctl(socket.SIO_RCVALL, socket_RCVALL_ON)
|
||||
|
||||
# read in a single packet
|
||||
print(sniffer.recvfrom(65565))
|
||||
|
||||
|
||||
def main(host):
|
||||
if os.name == 'nt':
|
||||
sniffing(host, 1, socket.IPPROTO_IP)
|
||||
else:
|
||||
sniffing(host, 0, socket.IPPROTO_ICMP)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(HOST)
|
69
Security_examples/python_sniff_scanner/udp_sender_example.py
Normal file
69
Security_examples/python_sniff_scanner/udp_sender_example.py
Normal file
|
@ -0,0 +1,69 @@
|
|||
import os
|
||||
import time
|
||||
import socket
|
||||
import struct
|
||||
import ctypes
|
||||
import threading
|
||||
|
||||
from netaddr import IPNetwork, IPAddress
|
||||
from ICMPHeader import ICMP
|
||||
|
||||
|
||||
# host to listen on
|
||||
HOST = '192.168.1.114'
|
||||
|
||||
# subnet to target (iterates through all IP address in this subnet)
|
||||
SUBNET = '192.168.1.0/24'
|
||||
|
||||
# string signature
|
||||
MESSAGE = 'hellooooo'
|
||||
|
||||
|
||||
|
||||
def udp_sender(SUBNET, MESSAGE):
|
||||
''' Sprays out the udp datagram'''
|
||||
time.sleep(5)
|
||||
sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
for ip in IPNetwork(SUBNET):
|
||||
try:
|
||||
sender.sendto(MESSAGE, (str(ip), 65212))
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
t = threading.Thread(target=udp_sender, args=(SUBNET, MESSAGE))
|
||||
t.start()
|
||||
socket_protocol = socket.IPPROTO_ICMP
|
||||
|
||||
sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
|
||||
sniffer.bind(( HOST, 0 ))
|
||||
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
|
||||
|
||||
# continually read in packets and parse their information
|
||||
while True:
|
||||
raw_buffer = sniffer.recvfrom(65565)[0]
|
||||
ip_header = raw_buffer[0:20]
|
||||
iph = struct.unpack('!BBHHHBBH4s4s' , ip_header)
|
||||
|
||||
# Create our IP structure
|
||||
version_ihl = iph[0]
|
||||
ihl = version_ihl & 0xF
|
||||
iph_length = ihl * 4
|
||||
src_addr = socket.inet_ntoa(iph[8]);
|
||||
|
||||
# Create our ICMP structure
|
||||
buf = raw_buffer[iph_length:iph_length + ctypes.sizeof(ICMP)]
|
||||
icmp_header = ICMP(buf)
|
||||
|
||||
# check for the type 3 and code and within our target subnet
|
||||
if icmp_header.code == 3 and icmp_header.type == 3:
|
||||
if IPAddress(src_addr) in IPNetwork(SUBNET):
|
||||
if raw_buffer[len(raw_buffer) - len(MESSAGE):] == MESSAGE:
|
||||
print(f'Host up: {src_addr}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
6
Security_examples/python_sockets/first_example.py
Normal file
6
Security_examples/python_sockets/first_example.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
import socket
|
||||
|
||||
HOST = 'www.github.com'
|
||||
PORT = 80
|
||||
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
client.connect((HOST, PORT))
|
26
Security_examples/python_sockets/netcat_example.py
Normal file
26
Security_examples/python_sockets/netcat_example.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
import socket
|
||||
|
||||
PORT = 12345
|
||||
HOSTNAME = '54.209.5.48'
|
||||
|
||||
|
||||
def netcat(text_to_send):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.connect((HOSTNAME, PORT))
|
||||
s.sendall(text_to_send)
|
||||
s.shutdown(socket.SHUT_WR)
|
||||
rec_data = []
|
||||
|
||||
while 1:
|
||||
data = s.recv(1024)
|
||||
if not data:
|
||||
break
|
||||
rec_data.append(data)
|
||||
s.close()
|
||||
return rec_data
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
text_to_send = ''
|
||||
text_recved = netcat( text_to_send)
|
||||
print(text_recved[1])
|
11
Security_examples/python_sockets/second_example.py
Normal file
11
Security_examples/python_sockets/second_example.py
Normal file
|
@ -0,0 +1,11 @@
|
|||
DATA = 'GET / HTTP/1.1\r\nHost: google.com\r\n\r\n'
|
||||
|
||||
def tcp_client():
|
||||
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
client.connect((HOST, PORT))
|
||||
client.send(DATA)
|
||||
response = client.recv(4096)
|
||||
print(response)
|
||||
|
||||
if __name__ == '__main__':
|
||||
tcp_client()
|
33
Security_examples/python_sockets/tcp_server.py
Normal file
33
Security_examples/python_sockets/tcp_server.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
import socket
|
||||
import threading
|
||||
|
||||
|
||||
BIND_IP = '0.0.0.0'
|
||||
BIND_PORT = 9090
|
||||
|
||||
|
||||
def handle_client(client_socket):
|
||||
request = client_socket.recv(1024)
|
||||
|
||||
print(f'[*] Received: {request}')
|
||||
|
||||
client_socket.send('ACK')
|
||||
client_socket.close()
|
||||
|
||||
def tcp_server():
|
||||
|
||||
server = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
|
||||
server.bind(( BIND_IP, BIND_PORT))
|
||||
server.listen(5)
|
||||
|
||||
print(f'[*] Listening on {BIND_IP}, {BIND_PORT}')
|
||||
while 1:
|
||||
client, addr = server.accept()
|
||||
print(f'[*] Accepted connection: {addr[0]}:{addr[1]}')
|
||||
|
||||
client_handler = threading.Thread(target=handle_client, args= (client,))
|
||||
client_handler.start()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
tcp_server()
|
16
Security_examples/python_sockets/udp_client.py
Normal file
16
Security_examples/python_sockets/udp_client.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
import socket
|
||||
|
||||
HOST = '127.0.0.1'
|
||||
PORT = 9000
|
||||
DATA = 'AAAAAAAAAA'
|
||||
|
||||
def udp_client():
|
||||
|
||||
client = socket.socket( socket.AF_INET, socket.SOCK_DGRAM)
|
||||
client.sendto(DATA, ( HOST, PORT ))
|
||||
data, addr = client.recvfrom(4096)
|
||||
print(data, adr)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
udp_client()
|
18
Security_examples/python_sockets/udp_server.py
Normal file
18
Security_examples/python_sockets/udp_server.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
import socket
|
||||
|
||||
BIND_IP = '0.0.0.0'
|
||||
BIND_PORT = 9000
|
||||
|
||||
def udp_server():
|
||||
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
server.bind(( BIND_IP, BIND_PORT))
|
||||
|
||||
print(f'Waiting on port: {str(BIND_PORT)}')
|
||||
while 1:
|
||||
data, addr = server.recvfrom(1024)
|
||||
print(data)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
udp_server()
|
63
Security_examples/python_ssh_modules/paramiko_example.py
Normal file
63
Security_examples/python_ssh_modules/paramiko_example.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
import getopt
|
||||
import paramiko
|
||||
import socket
|
||||
import threading
|
||||
|
||||
def main():
|
||||
if not len(sys.argv[1:]):
|
||||
print('Usage: ssh_server.py <SERVER> <PORT>')
|
||||
return
|
||||
|
||||
# Create a socket object.
|
||||
server = sys.argv[1]
|
||||
ssh_port = int(sys.argv[2])
|
||||
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
sock.bind((server, ssh_port))
|
||||
sock.listen(100)
|
||||
print('[+] Listening for connection ...')
|
||||
client, addr = sock.accept()
|
||||
except Exception, e:
|
||||
print(f'[-] Connection Failed: {str(e)}')
|
||||
return
|
||||
print('[+] Connection Established!')
|
||||
|
||||
# Creating a paramiko object.
|
||||
try:
|
||||
Session = paramiko.Transport(client)
|
||||
Session.add_server_key(HOST_KEY)
|
||||
paramiko.util.log_to_file('filename.log')
|
||||
server = Server()
|
||||
try:
|
||||
Session.start_server(server=server)
|
||||
except paramiko.SSHException, x:
|
||||
print('[-] SSH negotiation failed.')
|
||||
return
|
||||
chan = Session.accept(10)
|
||||
print('[+] Authenticated!')
|
||||
chan.send('Welcome to Buffy's SSH')
|
||||
while 1:
|
||||
try:
|
||||
command = raw_input('Enter command: ').strip('\n')
|
||||
if command != 'exit':
|
||||
chan.send(command)
|
||||
print chan.recv(1024) + '\n'
|
||||
else:
|
||||
chan.send('exit')
|
||||
print('[*] Exiting ...')
|
||||
session.close()
|
||||
raise Exception('exit')
|
||||
except KeyboardInterrupt:
|
||||
session.close()
|
||||
except Exception, e:
|
||||
print(f'[-] Caught exception: {str(e)}')
|
||||
try:
|
||||
session.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
17
Security_examples/python_ssh_modules/server_example.py
Normal file
17
Security_examples/python_ssh_modules/server_example.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
HOST_KEY = paramiko.RSAKey(filename='test_rsa.key')
|
||||
USERNAME = 'buffy'
|
||||
PASSWORD = 'killvampires'
|
||||
|
||||
class Server(paramiko.ServerInterface):
|
||||
def __init__(self):
|
||||
self.event = threading.Event()
|
||||
|
||||
def check_channel_request(self, kind, chanid):
|
||||
if kind == 'session':
|
||||
return paramiko.OPEN_SUCCEEDED
|
||||
return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
|
||||
|
||||
def check_auth_password(self, username, password):
|
||||
if (username == USERNAME) and (password == PASSWORD):
|
||||
return paramiko.AUTH_SUCCESSFUL
|
||||
return paramiko.AUTH_FAILED
|
16
Security_examples/python_ssh_modules/ssh_client.py
Normal file
16
Security_examples/python_ssh_modules/ssh_client.py
Normal file
|
@ -0,0 +1,16 @@
|
|||
def ssh_client(ip, port, user, passwd):
|
||||
|
||||
client = paramiko.SSHClient()
|
||||
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
client.connect(ip, port=port, username=user, password=passwd)
|
||||
ssh_session = client.get_transport().open_session()
|
||||
if ssh_session.active:
|
||||
print(ssh_session.recv(1024))
|
||||
while 1:
|
||||
command = ssh_session.recv(1024)
|
||||
try:
|
||||
cmd_output = subprocess.check_output(command, shell=True)
|
||||
ssh_session.send(cmd_output)
|
||||
except Exception, e:
|
||||
ssh_session.send(str(e))
|
||||
client.close()
|
59
Security_examples/python_ssh_modules/usage_example.py
Normal file
59
Security_examples/python_ssh_modules/usage_example.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
import paramiko
|
||||
import sys
|
||||
import getopt
|
||||
|
||||
|
||||
def main():
|
||||
if not len(sys.argv[1:]):
|
||||
usage()
|
||||
IP = '0.0.0.0'
|
||||
USER = ''
|
||||
PASSWORD = ''
|
||||
KEY = ''
|
||||
COMMAND = ''
|
||||
PORT = 0
|
||||
try:
|
||||
opts = getopt.getopt(sys.argv[2:],"p:u:a:i:c:", \
|
||||
['PORT', 'USER', 'PASSWORD', 'KEY', 'COMMAND'])[0]
|
||||
except getopt.GetoptError as err:
|
||||
print str(err)
|
||||
usage()
|
||||
IP = sys.argv[1]
|
||||
print(f'[*] Initializing connection to {IP}')
|
||||
# Handle the options and arguments.
|
||||
# TODO: add KeyError error handler.
|
||||
for t in opts:
|
||||
if t[0] in ('-a'):
|
||||
PASSWORD = t[1]
|
||||
elif t[0] in ('-i'):
|
||||
KEY = t[1]
|
||||
elif t[0] in ('-c'):
|
||||
COMMAND = t[1]
|
||||
elif t[0] in ('-p'):
|
||||
PORT = int(t[1])
|
||||
elif t[0] in ('-u'):
|
||||
USER = t[1]
|
||||
else:
|
||||
print('This option does not exist!')
|
||||
usage()
|
||||
|
||||
|
||||
if USER:
|
||||
print(f'[*] User set to {USER}')
|
||||
if PORT:
|
||||
print(f'[*] The port to be used is PORT}')
|
||||
if PASSWORD:
|
||||
print(f'[*] Password length {len(PASSWORD)} was submitted.')
|
||||
if KEY:
|
||||
print(f'[*] The key at {KEY} will be used.')
|
||||
if COMMAND:
|
||||
print(f'[*] Executing the command {COMMAND} in the host...')
|
||||
else:
|
||||
print('You need to specify the command to the host.')
|
||||
usage()
|
||||
# Start the client.
|
||||
ssh_client(IP, PORT, USER, PASSWORD, KEY, COMMAND)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue