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

Ports Cyris to Ubuntu 20.04 and Python 3, fixes several/potential bugs #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 8 additions & 32 deletions HOST-PREPARE.sh
Original file line number Diff line number Diff line change
@@ -1,42 +1,18 @@
#!/bin/bash

# This script is for preparing host Ubuntu Server 16.04 ready for CyRIS.
# This script is for preparing host Ubuntu Server 20.04 ready for CyRIS.

sudo apt-get update

# 1. Install kvm.
sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils
# Install depedencies
sudo apt-get install qemu-kvm nstall qemu-kvm libvirt-clients libvirt-daemon-system ubuntu-vm-builder bridge-utils virt-manager python3-pip python3-paramiko tcpreplay wireshark sshpass pssh python3-yaml python3-scapy sendemail ifupdown -y

# 2. Install virt-manager.
sudo apt-get install virt-manager

# 3. Install pip.
sudo apt-get install python-pip

# 4. Install python-paramiko.
sudo apt-get install python-paramiko

# 5. Install tcpreplay.
sudo apt-get install tcpreplay

# 6. Install wireshark.
sudo apt-get install wireshark

# 7. Install sshpass.
sudo apt-get install sshpass

# 8. Install pssh.
sudo apt-get install pssh

# 9. Install yaml for python.
sudo apt-get install python-yaml

# 10. Install scapy for python.
sudo apt-get install python-scapy
# ssh-copy-id to itself and other hosts.
ssh-copy-id localhost

# 11. Install sendemail
sudo apt-get install sendemail
# https://askubuntu.com/a/944787
echo "Edit `/etc/NetworkManager/NetworkManager.conf` with `managed=true`"

# 12. ssh-copy-id to itself and other hosts.
ssh-copy-id localhost

sudo apt-get install bridge-utils
16 changes: 10 additions & 6 deletions cleanup/downbridges.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#!/usr/bin/python

# this script is to bring down cyber range 's bridges. It gets the job done by open and see how many bridges are there in the create_bridges.sh file
# this script is to bring down cyber range 's bridges. It gets the job
# done by open and see how many bridges are there in the create_bridges.sh
# file

import sys
import fcntl

filename = sys.argv[1]
clone_id = sys.argv[2]


def down_bridges():
number = 0
count = 0
Expand All @@ -28,18 +31,19 @@ def down_bridges():
gap = 11 * count
else:
gap = 10
print "starting line: ", number - 1
print "ending line: ", number + gap + 1
first_part = file_lines[:(number-1)]
second_part = file_lines[(number+gap):]
print("starting line: ", number - 1)
print("ending line: ", number + gap + 1)
first_part = file_lines[:(number - 1)]
second_part = file_lines[(number + gap):]
my_file.seek(0)
if first_part[-1] != "\n" and second_part and second_part[0] != "\n":
my_file.writelines(first_part)
my_file.writelines("\n")
my_file.writelines(second_part)
else:
my_file.writelines(first_part+second_part)
my_file.writelines(first_part + second_part)
my_file.truncate()
fcntl.flock(my_file, fcntl.LOCK_UN)


down_bridges()
4 changes: 2 additions & 2 deletions examples/basic.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
- host_settings:
- id: host_1
mgmt_addr: 172.16.1.7
mgmt_addr: 10.0.2.15
virbr_addr: 192.168.122.1
account: cyuser

- guest_settings:
- id: desktop
basevm_host: host_1
basevm_config_file: /home/cyuser/images/basevm.xml
basevm_config_file: /home/cyuser/images/basevm_x.xml
basevm_type: kvm

- clone_settings:
Expand Down
35 changes: 22 additions & 13 deletions instantiation/attacks_emulation/attack_paramiko_ssh.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/python
import paramiko, sys, os, socket
import paramiko
import sys
import os
import socket
import threading
import subprocess

Expand All @@ -8,6 +11,7 @@
number = sys.argv[3]
time = sys.argv[4]


class myThread (threading.Thread):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
Expand All @@ -16,23 +20,24 @@ def __init__(self, threadID, name):
self.assign_number = 0

def run(self):
print "Starting " + self.name
print("Starting " + self.name)
if(self.threadID != 5):
self.assign_number = int(number)/5
self.assign_number = int(number) / 5
else:
self.assign_number = int(number) - (int(number)/5)*4
self.assign_number = int(number) - (int(number) / 5) * 4

for i in range(0, self.assign_number):
try:
response = ssh_connect()
if response == 1:
print "{}: {}".format(self.name, i)
print("{}: {}".format(self.name, i))
elif response == 2:
print "socket error"
except Exception, e:
print e
print("socket error")
except Exception as e:
print(e)
pass
print "Exiting " + self.name
print("Exiting " + self.name)


def ssh_connect():
ssh = paramiko.SSHClient()
Expand All @@ -48,6 +53,7 @@ def ssh_connect():
ssh.close()
return response


# Set system date as the same as input
if time != "none":
os.system("ssh root@{0} date +%Y%m%d -s {1}".format(attacked_addr, time))
Expand Down Expand Up @@ -77,6 +83,9 @@ def ssh_connect():
if time != "none":
correct_date = subprocess.check_output("date +%Y%m%d", shell=True)
correct_time = subprocess.check_output("date +%T", shell=True)
os.system("ssh root@{0} date +%Y%m%d -s {1}".format(attacked_addr, correct_date))
os.system("ssh root@{0} date +%T -s {1}".format(attacked_addr, correct_time))
os.system("ssh root@{0} sort --stable --reverse --key=1,2 /var/log/secure -o /var/log/secure".format(attacked_addr))
os.system(
"ssh root@{0} date +%Y%m%d -s {1}".format(attacked_addr, correct_date))
os.system(
"ssh root@{0} date +%T -s {1}".format(attacked_addr, correct_time))
os.system(
"ssh root@{0} sort --stable --reverse --key=1,2 /var/log/secure -o /var/log/secure".format(attacked_addr))
25 changes: 18 additions & 7 deletions instantiation/content_copy_program_run/copy_and_run_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@
LOG_FILE = sys.argv[6]

# this program is for executing outside program on cyber range


class RunProgram():
# this def allows program to run shell commands in python
def execute_command(self, command):
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
with open(LOG_FILE, "a") as myfile:
for line in p.stdout.readlines():
myfile.write(line,)

# get name of the program from the string PROGRAM provided by users from cyber range definition file
# get name of the program from the string PROGRAM provided by users from
# cyber range definition file
def getProgramName(self):
list_elements = PROGRAM.split("/")
return list_elements[-1]
Expand All @@ -40,16 +47,20 @@ def runProgram(self):
else:
program_args = ARGS
# copy program to /bin/cyberrange of virtual machine
command = "sshpass -p {0} scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no {1} root@{2}:/bin/cyberrange".format(IMAGE_PASSWD, PROGRAM, IMAGE_ADDR)
command = "sshpass -p {0} scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no {1} root@{2}:/bin/cyberrange".format(
IMAGE_PASSWD, PROGRAM, IMAGE_ADDR)
self.execute_command(command)
print command
print(command)
# execute program on virtual machine
command = "sshpass -p {0} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@{1} {2} /bin/cyberrange/{3} {4}".format(IMAGE_PASSWD, IMAGE_ADDR, program_compiler, program_name, program_args)
command = "sshpass -p {0} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@{1} {2} /bin/cyberrange/{3} {4}".format(
IMAGE_PASSWD, IMAGE_ADDR, program_compiler, program_name, program_args)
self.execute_command(command)
print command
print(command)
# delete program to /bin/cyberrange
command = "sshpass -p {0} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@{1} \"rm -f /bin/cyberrange/{2}\"".format(IMAGE_PASSWD, IMAGE_ADDR, program_name)
command = "sshpass -p {0} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@{1} \"rm -f /bin/cyberrange/{2}\"".format(
IMAGE_PASSWD, IMAGE_ADDR, program_name)
self.execute_command(command)


runProgram = RunProgram()
runProgram.runProgram()
30 changes: 21 additions & 9 deletions instantiation/content_copy_program_run/run_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@
#import os
import sys
import subprocess
import urllib
import urllib.request
import urllib.parse
import urllib.error

PROGRAM = sys.argv[1]
COMPILER = sys.argv[2]
#ARGS = sys.argv[3]
ARGS = urllib.unquote(sys.argv[3])
ARGS = urllib.parse.unquote(sys.argv[3])
IMAGE_ADDR = sys.argv[4]
IMAGE_PASSWD = sys.argv[5]
LOG_FILE = sys.argv[6]
OS_TYPE=sys.argv[7]
OS_TYPE = sys.argv[7]

# this program is for executing outside program on cyber range


class RunProgram():
# this def allows program to run shell commands in python
def execute_command(self, command):
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
with open(LOG_FILE, "a") as myfile:
for line in p.stdout.readlines():
myfile.write(line,)

# get name of the program from the string PROGRAM provided by users from cyber range definition file
# get name of the program from the string PROGRAM provided by users from
# cyber range definition file
def getProgramName(self):
list_elements = PROGRAM.split("/")
return list_elements[-1]
Expand All @@ -49,12 +58,15 @@ def runProgram(self):
# self.execute_command(command)
# print command
# execute program on virtual machine
if OS_TYPE=="windows.7":
command = "sshpass -p {0} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@{1} {2} \"{3}\" {4}".format(IMAGE_PASSWD, IMAGE_ADDR, program_compiler, PROGRAM, program_args)
if OS_TYPE == "windows.7":
command = "sshpass -p {0} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@{1} {2} \"{3}\" {4}".format(
IMAGE_PASSWD, IMAGE_ADDR, program_compiler, PROGRAM, program_args)
else:
command = "sshpass -p {0} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@{1} {2} {3} {4}".format(IMAGE_PASSWD, IMAGE_ADDR, program_compiler, PROGRAM, program_args)
command = "sshpass -p {0} ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@{1} {2} {3} {4}".format(
IMAGE_PASSWD, IMAGE_ADDR, program_compiler, PROGRAM, program_args)
self.execute_command(command)
print command
print(command)


runProgram = RunProgram()
runProgram.runProgram()
48 changes: 30 additions & 18 deletions instantiation/logs_preparation/mergePcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
ABSPATH = sys.argv[3]
CR_DIR = sys.argv[4]

INSTANTIATION_DIR="instantiation"
INSTANTIATION_DIR = "instantiation"


class PcapCreation():
def execute_command(self, command):
subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)

def get_pcap_timestamp(self, pcapfile):
try:
Expand All @@ -29,43 +34,50 @@ def get_pcap_timestamp(self, pcapfile):
list_timestamp = list(pcap_content)
start_time = list_timestamp[0].time
end_time = list_timestamp[-1].time
print "{0}: {1} - {2}".format(pcapfile, start_time, end_time)
return (start_time + end_time)/2

print("{0}: {1} - {2}".format(pcapfile, start_time, end_time))
return (start_time + end_time) / 2

def merge_pcap(self):
noise_file = ""

# convert pcapng to pcap
command = "editcap -F libpcap {0}attack.pcapng {0}attack.pcap;\n".format(CR_DIR)
command = "editcap -F libpcap {0}attack.pcapng {0}attack.pcap;\n".format(
CR_DIR)
os.system(command)

path = "{0}attack.pcap".format(CR_DIR)

sleep(0.5)
if os.path.isfile(path):
print "yes"
print("yes")
else:
print "no"
print("no")

time1 = self.get_pcap_timestamp(path)
if NOISE == "low":
noise_file = "{0}{1}/logs_preparation/noise_low.pcap".format(ABSPATH, INSTANTIATION_DIR)
noise_file = "{0}{1}/logs_preparation/noise_low.pcap".format(
ABSPATH, INSTANTIATION_DIR)
elif NOISE == "medium":
noise_file = "{0}{1}/logs_preparation/noise_medium.pcap".format(ABSPATH, INSTANTIATION_DIR)
noise_file = "{0}{1}/logs_preparation/noise_medium.pcap".format(
ABSPATH, INSTANTIATION_DIR)
elif NOISE == "high":
noise_file = "{0}{1}/logs_preparation/noise_high.pcap".format(ABSPATH, INSTANTIATION_DIR)
print "{0} \n".format(noise_file)
noise_file = "{0}{1}/logs_preparation/noise_high.pcap".format(
ABSPATH, INSTANTIATION_DIR)
print("{0} \n".format(noise_file))
time2 = self.get_pcap_timestamp(noise_file)

print "{0}\n".format(FILE_NAME)
print("{0}\n".format(FILE_NAME))
# shift time of noise pcap file
command = "editcap -t {0} {1} {2}noise.pcap;\n".format(time1-time2, noise_file, CR_DIR)
command = "editcap -t {0} {1} {2}noise.pcap;\n".format(
time1 - time2, noise_file, CR_DIR)
# merge pcap files
command += "mergecap {0}noise.pcap {0}attack.pcap -w {0}{1};\n".format(CR_DIR, FILE_NAME)
# clean folder
command += "mergecap {0}noise.pcap {0}attack.pcap -w {0}{1};\n".format(
CR_DIR, FILE_NAME)
# clean folder
command += "rm -f {0}noise.pcap; rm -f {0}attack.*; \n".format(CR_DIR)
os.system(command)


a = PcapCreation()
a.merge_pcap()
Loading