#!/usr/bin/python
from pwn import *
import json

HOST = '2018shell2.picoctf.com' 
PORT = 40952

conn = remote(HOST, PORT)

with open("incidents.json", "r") as data_file :
    data = json.load(data_file)
tickets_list = data["tickets"]

# Q1: What is the most common src ip
print conn.read()
tmp = { }
for t in tickets_list :
    try : 
        tmp[t['src_ip']] += 1
    except :
        tmp[t['src_ip']] = 0

    if tmp[t['src_ip']] > 1 :
        conn.sendline(t['src_ip'])
        break

# Q2: How many unique dst ip targeted by 9.94.163.45
print conn.readuntil("address ")
src_addr = conn.read()[:-2]
print src_addr
tmp = [ ]
for t in tickets_list :
    if t['src_ip'] == "9.94.163.45" and t['dst_ip'] not in tmp :
        tmp.append(t["dst_ip"])

conn.sendline(str(len(tmp)))

# Q3: What is the average number of unique destination IP addresses that were sent a file with the same hash? Your answer needs to be correct to 2 decimal places.
tmp = { }
for t in tickets_list :
    try :
        if t['dst_ip'] not in tmp[t['file_hash']] :
            tmp[t['file_hash']].append(t['dst_ip'])
    except :
        tmp[t['file_hash']] = [t['dst_ip']]

sum_of_count = 0
for t in tmp.values() :
    sum_of_count += len(t)

conn.sendline(str(round(float(sum_of_count) / float(len(tmp)), 3)))
print str(round(float(sum_of_count) / float(len(tmp)), 2))
print sum_of_count, len(tmp)

conn.interactive()
8a382de03da1ee4fe99c13883a1d068ea6039ec3
#!/usr/bin/python
from pwn import *
import json

HOST = '2018shell2.picoctf.com' 
PORT = 40952

conn = remote(HOST, PORT)

with open("incidents.json", "r") as data_file :
    data = json.load(data_file)
tickets_list = data["tickets"]

# Q1: What is the most common src ip
print conn.read()
tmp = { }
for t in tickets_list :
    try : 
        tmp[t['src_ip']] += 1
    except :
        tmp[t['src_ip']] = 0

    if tmp[t['src_ip']] > 1 :
        conn.sendline(t['src_ip'])
        break

# Q2: How many unique dst ip targeted by 9.94.163.45
print conn.readuntil("address ")
src_addr = conn.read()[:-2]
print src_addr
tmp = [ ]
for t in tickets_list :
    if t['src_ip'] == "9.94.163.45" and t['dst_ip'] not in tmp :
        tmp.append(t["dst_ip"])

conn.sendline(str(len(tmp)))

# Q3: What is the average number of unique destination IP addresses that were sent a file with the same hash? Your answer needs to be correct to 2 decimal places.
tmp = { }
for t in tickets_list :
    try :
        if t['dst_ip'] not in tmp[t['file_hash']] :
            tmp[t['file_hash']].append(t['dst_ip'])
    except :
        tmp[t['file_hash']] = [t['dst_ip']]

sum_of_count = 0
for t in tmp.values() :
    sum_of_count += len(t)

conn.sendline(str(round(float(sum_of_count) / float(len(tmp)), 3)))
print str(round(float(sum_of_count) / float(len(tmp)), 2))
print sum_of_count, len(tmp)

conn.interactive()