Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move a5 files to main folder #22

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions a5/advanced_ninja_port.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import socket
import time
from scenarios.lib.util import run, decode

SECRET_GREETING = "found you"

if __name__ == "__main__":
greeting_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
greeting_socket.bind(('', 0))
port_num = greeting_socket.getsockname()[1]
run(decode("vcgnoyrf -V VACHG -c gpc --qcbeg {cbeg_ahz} -w QEBC").format(
port_num=port_num
))
greeting_socket.listen(1)
while True:
connection, addr = greeting_socket.accept()
data = connection.recv(len(SECRET_GREETING))
data.rstrip()
data.lower()
if SECRET_GREETING in str(data).lower():
winner = "Aw man how did you know I was hiding on port {0}\n".format(port_num)
try:
connection.sendall(winner.encode())
except:
print(winner.strip())
break;
greeting_socket.close()
11 changes: 11 additions & 0 deletions a5/scenarios/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import lib.util as util


command = "ifdown {iface}"
routing_entries = util.get_default_routing_information()
default_entry = next(
(e for e in routing_entries if util.is_default_gateway(e)),
None
)

util.run(command.format(iface=default_entry.iface))
18 changes: 18 additions & 0 deletions a5/scenarios/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import lib.util as util


routing_entries = util.get_default_routing_information()
default_entry = next((e for e in routing_entries if util.is_default_gateway(e)), None)
default_iface_entry = next(
(e for e in routing_entries if not util.is_default_gateway(e) and e.iface == default_entry.iface),
None
)

command = "ip route delete default"
util.run(command)

command = "ip route add default via {gateway}".format(
gateway=util.get_iface_ip_address(default_iface_entry.iface)
)

util.run(command)
7 changes: 7 additions & 0 deletions a5/scenarios/3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import lib.util as util

HOSTS_FILE = "/etc/hosts"
ENTRY = "72.66.115.13 google.com\n"

with open(HOSTS_FILE, "a") as host_file:
host_file.write(ENTRY)
11 changes: 11 additions & 0 deletions a5/scenarios/4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import lib.util as util


RESOLV_FILE = "/etc/resolv.conf"

lines = []
with open(RESOLV_FILE, "r+") as resolv_file:
for line in resolv_file:
lines.append("#" + line)
resolv_file.seek(0, 0)
resolv_file.write(''.join(lines))
32 changes: 32 additions & 0 deletions a5/scenarios/5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import lib.util as util
from shutil import copyfile


RESOLV_FILE = "/etc/resolv.conf"
ENTRY = "nameserver {local_ns}\n"
DNSMASQ_HOST_FILE_SRC = "config/dnsmasq.hosts"
DNSMASQ_HOST_FILE_DST = "/etc/dnsmasq.hosts"
DNSMASQ_CONFIG_FILE_SRC = "config/dnsmasq.conf"
DNSMASQ_CONFIG_FILE_DST = "/etc/dnsmasq.conf"

routing_entries = util.get_default_routing_information()
default_entry = next((e for e in routing_entries if util.is_default_gateway(e)), None)
default_iface_entry = next(
(e for e in routing_entries if not util.is_default_gateway(e) and e.iface == default_entry.iface),
None
)

ENTRY = ENTRY.format(local_ns=util.get_iface_ip_address(default_iface_entry.iface))

copyfile(DNSMASQ_HOST_FILE_SRC, DNSMASQ_HOST_FILE_DST)
copyfile(DNSMASQ_CONFIG_FILE_SRC, DNSMASQ_CONFIG_FILE_DST)

with open(RESOLV_FILE, "r+") as resolv_file:
original = resolv_file.read()
resolv_file.seek(0, 0)
resolv_file.write(ENTRY + original)

command = "killall -9 dnsmasq"
util.run(command)
command = "dnsmasq"
util.run(command)
21 changes: 21 additions & 0 deletions a5/scenarios/6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import lib.util as util


INTERFACES_FILE = "/etc/network/interfaces"

routing_entries = util.get_default_routing_information()
default_entry = next((e for e in routing_entries if util.is_default_gateway(e)), None)
default_iface = default_entry.iface

lines = []
with open(INTERFACES_FILE, "r+") as iface_file:
for line in iface_file:
if default_iface in line and "dhcp" in line:
lines.append(line.replace("dhcp", "manual"))
else:
lines.append(line)
iface_file.seek(0, 0)
iface_file.write(''.join(lines))

command = "ip addr flush {iface}".format(iface=default_iface)
util.run(command)
Empty file added a5/scenarios/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions a5/scenarios/config/dnsmasq.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
no-dhcp-interface=
server=8.8.8.8

no-hosts
addn-hosts=/etc/dnsmasq.hosts
address=/com/72.66.115.13
1 change: 1 addition & 0 deletions a5/scenarios/config/dnsmasq.hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
72.66.115.13 www.facebook.com facebook.com
Empty file added a5/scenarios/lib/__init__.py
Empty file.
55 changes: 55 additions & 0 deletions a5/scenarios/lib/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import socket, struct, fcntl
from collections import namedtuple
import subprocess
from shlex import split
from codecs import decode as decipher

ROUTE = "/proc/net/route"

RoutingEntry = namedtuple(
'RoutingEntry',
['iface', 'gateway', 'mask', 'dst', 'flags']
)

def is_default_gateway(routing_entry):
return routing_entry.dst == '00000000' and int(routing_entry.flags, 16) & 2

def get_default_routing_information():
routing_entries = []
with open(ROUTE) as route_file:
for line in route_file:
fields = line.strip().split()
try:
routing_entries.append(RoutingEntry(
iface=fields[0],
gateway=hex2address(fields[2]),
mask=hex2address(fields[7]),
dst=fields[1],
flags=fields[3]
))
except ValueError:
continue
return routing_entries

def get_iface_ip_address(iface):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', bytes(iface[:15], 'utf-8'))
)[20:24])

def hex2address(val):
return socket.inet_ntoa(struct.pack("<L", int(val, 16)))

def run(command):
args = split(command)
return subprocess.run(
args,
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT
)

def decode(cipher):
return decipher(cipher, "rot-13")