...
 
Commits (2)
import socket
import time
host = 'amm042.bucknell.edu'
port = 7891
dest = (host,port)
packet_max = 7 # bytes
# first 3: 1 byte digits
# last 4: 32 bit checksum, one's complement on sum of the first 3 digits
def build_packet(byte_str):
s = sum( byte_str )
comp = (~s) & 0xffff # one's complement on the sum
checksum = comp.to_bytes(4, byteorder='big')
return byte_str + checksum
def build_num(a, b, c):
return a.to_bytes(1, byteorder='big') + b.to_bytes(1, byteorder='big') + c.to_bytes(1, byteorder='big')
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect(dest)
for i in range(256):
p = build_num(251,19,97)
packet = build_packet(p)
sock.send(packet)
resp = sock.recv(1048)
if ord(resp) == 3:
print("Sending {}\tResponse Code: {}".format(i, ord(resp)))
break
#time.sleep(1)
'''
pin_code = build_num(10,0,0)
print("pin_code: {}".format(pin_code))
p = build_packet(pin_code)
sock.send(p)
resp = sock.recv(1048)
print("Respond Code: {}".format(ord(resp)))
'''
"""Simple network trace printer
Format:
Switch ID (1 byte)
Link ID (1 byte)
Destination MAC (6 bytes)
Source MAC (6 bytes)
Alan Marchiori 2019
"""
import os
import struct
import math
import sys
import re
from tabulate import tabulate as tbl
def print_trace(filename):
wd = 2*17+10+9
wd -= len(filename)
wd -= 2 # spaces
print(math.floor(wd/2)*"*",
filename,
"*"*math.ceil(wd/2))
print("{:>5} | {:>5} | {:>17} | {:>17}".format(
"SWID",
"LnkID",
" DESTMAC ",
" SRCMAC "))
print("------|-------|-------------------|------------------")
with open(filename, 'rb') as f:
raw = f.read(14)
while raw:
sw_id, link_id = struct.unpack("BB", raw[0:2])
destmac = bytes(raw[2:8])
srcmac = bytes(raw[8:14])
print("{:5x} | {:5x} | {:17} | {:17}".format(
sw_id,
link_id,
"-".join(["{:02x}".format(x) for x in destmac]),
"-".join(["{:02x}".format(x) for x in srcmac])
))
raw = f.read(14)
def create_packet_list(filename):
wd = 2*17+10+9
wd -= len(filename)
wd -= 2 # spaces
packet_list = []
with open(filename, 'rb') as f:
raw = f.read(14)
while raw:
sw_id, link_id = struct.unpack("BB", raw[0:2])
destmac = bytes(raw[2:8])
srcmac = bytes(raw[8:14])
packet = "{:5x} | {:5x} | {:17} | {:17}".format(
sw_id,
link_id,
"-".join(["{:02x}".format(x) for x in destmac]),
"-".join(["{:02x}".format(x) for x in srcmac])
)
packet = packet.replace(" ", "")
pList = re.split(r'\|', packet)
packet_list.append(pList)
raw = f.read(14)
return packet_list
# 0 -ID, 1 - LinkID, 2 - DESTMAX, 3 - SRCMAC, 4 - outlnk
def build_table(binFile):
packet_list = create_packet_list(binFile)
destDic = {}
table = []
for p in packet_list:
if p[2] not in destDic:
destDic[p[3]] = p[1]
p.append('ff')
table.append(p)
else:
# its in the table
incLink = p[1]
outLink = destDic[p[2]]
if incLink != outLink:
p.append(outLink)
table.append(p)
srcMac = p[3]
if srcMac not in destDic:
destDic[srcMac] = p[1]
return table, destDic
def get_unique_elemets(aList):
unique = []
for i in aList:
if i not in unique:
unique.append(i)
return unique
def get_stats(table):
RX = []
TX = []
for packet in table:
RX.append(packet[1])
TX.append(packet[-1])
total = len(table)
links = get_unique_elemets(RX)
stats = {}
tx_sum = 0
for link in links:
recv = RX.count(link)
tran = TX.count(link) + TX.count('ff')
stats[link] = [recv, tran]
tx_sum += tran
return stats, tx_sum
if __name__=="__main__":
if len(sys.argv) == 1:
print("Usage: ptrace [trace file]")
for f in sys.argv[1:]:
if f.endswith(".out"):
#print_trace(f)
#pList = create_packet_list(f)
table, dest_dic = build_table(f)
stats, tx_total = get_stats(table)
rx_total = len(table)
print(' link | RX | TX ')
print("------|---------------|---------------")
for l in stats:
rx_count = stats[l][0]
tx_count = stats[l][1]
rx_percent = str(round(rx_count/rx_total*100,1))
tx_percent = str(round(tx_count/tx_total*100,1))
print('{:5s} | {:5d} ({:3s}%) | {:5d} ({:3s}%)'.format(l, rx_count, rx_percent, tx_count, tx_percent) )
import socket
from Cryto.PublicKey import RSA
dest = 'eg.bucknell.edu'
port = 6997
destAddr = (dest,port)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(destAddr)
s.send('HELLO wx002')
recv = client.recv(2048)
print(recv)
import socket
import time
import random
host = 'amm042.bucknell.edu'
port = 8888
dest = (host, port)
def play():
#choices = [0,1,2]
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(dest)
sock.send(b'playben3')
resp = sock.recv(1048)
print(resp)
sock.send(b'gues0')
resp = sock.recv(1048)
print(resp)
open = int(resp.decode()[-2])
#choices.remove(open)
#index = random.choices(choices)[0]
if open == 1:
index = 2
else:
index = 1
open_str = 'open' + str(index)
print(open_str)
sock.send(open_str.encode('utf-8'))
resp = sock.recv(1048)
print(resp)
sock.send(b'done')
sock.close()
if __name__ == '__main__':
for i in range(100):
play()
time.sleep(1.1)