Example #1
0
    def run(self, team_server_id: int):
        with session_manager() as db_session:
            team_server_obj = crud_team_server.get(db_session=db_session, id=team_server_id)
            ssh_obj = crud_ssh_config.get_config(db_session)
            if not ssh_obj:
                return

            tmp_dir = TemporaryDirectory()
            ssh_conn = self.gen_ssh_conn(
                addr=f"root@{team_server_obj.ip}",
                private_key=ssh_obj.private_key,
                tmp_dir=tmp_dir.name
            )
            # 1. install requirement
            install_lib_script = "apt-get install -y wget unzip"
            self.exec_remote_cmd(
                conn=ssh_conn,
                command=install_lib_script
            )

            # 2. download c2 profile , teamserver, cs
            template_render = TemplateRender()
            c2_content = team_server_obj.c2_profile.profile_content
            team_server_content = template_render.render(
                'scripts/team_server.sh', **{'port': team_server_obj.port}
            )
            cs_content = template_render.render(
                'scripts/cs_install.sh',
                cs_url=team_server_obj.cs_download_url,
                zip_pwd=team_server_obj.zip_password,
                cs_pwd=team_server_obj.password,
                kill_date=team_server_obj.kill_date
            )
            c2_tmp_file = self.gen_tmp_file(
                content=c2_content, dir_path=tmp_dir.name
            )
            team_server_file = self.gen_tmp_file(
                content=team_server_content, dir_path=tmp_dir.name
            )
            cs_server_file = self.gen_tmp_file(
                content=cs_content, dir_path=tmp_dir.name
            )
            self.upload_remote_file(
                conn=ssh_conn, source_file=c2_tmp_file.name, remote_file='ok.profile'
            )
            self.upload_remote_file(
                conn=ssh_conn, source_file=team_server_file.name, remote_file='teamserver'
            )
            self.upload_remote_file(
                conn=ssh_conn, source_file=cs_server_file.name, remote_file='cs.sh'
            )

            # 3. exec cs.sh
            self.exec_remote_cmd(conn=ssh_conn, command='chmod +x cs.sh && bash cs.sh')
            return self.set_result()
Example #2
0
File: vps.py Project: vinceyxl/LuWu
 def run(self, vps_profile: VpsCreateSchema, vps_id: int, *args, **kwargs) -> dict:
     with session_manager() as db_session:
         vps_isp_obj = crud_isp.get(db_session=db_session, id=vps_profile['isp_id'])
         ssh_key_obj = crud_ssh_config.get_config(db_session)
         extra_server_data = vps_isp_obj.isp_instance.create_server(vps_profile, ssh_key_obj.public_key)
         vps_data = dict(
             status=VpsStatus.running,
             **extra_server_data
         )
         crud_vps.update(db_session=db_session, obj_id=vps_id, obj_in=vps_data)
     return self.set_result()
Example #3
0
    def run(self, redirector_id: int):
        with session_manager() as db_session:
            redirector_obj = crud_redirector.get(db_session=db_session, id=redirector_id)
            ssh_obj = crud_ssh_config.get_config(db_session)
            if not ssh_obj:
                return

            tmp_dir = TemporaryDirectory()

            ssh_conn = self.gen_ssh_conn(
                addr=f"root@{redirector_obj.ip}",
                private_key=ssh_obj.private_key,
                tmp_dir=tmp_dir.name
            )

            template_render = TemplateRender()
            c2_content = redirector_obj.team_server.c2_profile.profile_content
            c2_tmp_file = self.gen_tmp_file(
                content=c2_content, dir_path=tmp_dir.name
            )
            self.upload_remote_file(
                conn=ssh_conn, source_file=c2_tmp_file.name, remote_file='c2.profile'
            )
            redirector_content = template_render.render(
                'scripts/c2_redirectors.sh',
                domain=redirector_obj.domain_name,
                ssl=1,
                c2_profile='~/c2.profile',
                cs2_server_ip=redirector_obj.team_server.cs_conn_url,
                redirect=redirector_obj.redirect_domain
            )
            redirector_bash_file = self.gen_tmp_file(
                content=redirector_content,
                dir_path=tmp_dir.name
            )
            self.upload_remote_file(
                conn=ssh_conn, source_file=redirector_bash_file.name, remote_file='redirector.sh'
            )
            self.exec_remote_cmd(conn=ssh_conn, command='chmod +x redirector.sh && bash redirector.sh')
Example #4
0
    def run(self, grow_domain_id: int):
        with session_manager() as db_session:
            grow_domain_obj = crud_domain_grow.get(db_session=db_session,
                                                   id=grow_domain_id)
            if not grow_domain_obj:
                return

            ip_address = grow_domain_obj.vps.ip
            if not ip_address:
                return

            tmp_dir = TemporaryDirectory()
            ssh_obj = crud_ssh_config.get_config(db_session)

            ssh_conn = self.gen_ssh_conn(addr=f"root@{ip_address}",
                                         tmp_dir=tmp_dir.name,
                                         private_key=ssh_obj.private_key)
            self.exec_remote_cmd(
                conn=ssh_conn,
                command=(
                    "ps -aux | grep 'nginx:' | awk '{print $2}'| xargs kill"),
                warn=True)
            return self.set_result()
Example #5
0
    def run(self, grow_domain_id: int):
        with session_manager() as db_session:
            grow_domain_obj = crud_domain_grow.get(db_session=db_session,
                                                   id=grow_domain_id)
            if not grow_domain_obj:
                return

            ip_address = grow_domain_obj.vps.ip
            tmp_dir = TemporaryDirectory()
            ssh_obj = crud_ssh_config.get_config(db_session)
            site_work_dir = f"/opt/{PROJECT_NAME}/site"
            site_data_dir = f"/opt/{PROJECT_NAME}/data"

            # 1. install nginx
            ssh_conn = self.gen_ssh_conn(addr=f"root@{ip_address}",
                                         tmp_dir=tmp_dir.name,
                                         private_key=ssh_obj.private_key)
            install_nginx_command = (
                "command -v yum && yum install -y epel-release && yum install -y nginx unzip;"
                "command -v apt-get && apt-get update -y && apt-get install -y nginx unzip;"
                f"mkdir -p {site_work_dir} {site_data_dir}")

            self.exec_remote_cmd(conn=ssh_conn, command=install_nginx_command)

            # 2. upload template file
            site_template_file_name = grow_domain_obj.template.zip_file_name
            site_template_content = BytesIO(
                grow_domain_obj.template.zip_file_content).read()

            site_template_tmp_file = self.gen_tmp_file(
                content=site_template_content, dir_path=tmp_dir.name)
            self.upload_remote_file(
                conn=ssh_conn,
                source_file=site_template_tmp_file.name,
                remote_file=f"{site_data_dir}/{site_template_file_name}")

            # 3. update nginx conf and configure nginx
            self.exec_remote_cmd(
                conn=ssh_conn,
                command=
                (f"rm -rf {site_work_dir} &&"
                 f"unzip -o -d {site_work_dir} {site_data_dir}/{site_template_file_name};"
                 ))
            nginx_config_content = TemplateRender().render_nginx_conf(
                nginx_site_work_dir=site_work_dir)
            nginx_config_tmp_file = self.gen_tmp_file(
                content=nginx_config_content, dir_path=tmp_dir.name)
            nginx_conf_deploy_path = f"{site_data_dir}/{TemplateRender.NGINX_TEMPLATE_CONF}"
            self.upload_remote_file(conn=ssh_conn,
                                    source_file=nginx_config_tmp_file.name,
                                    remote_file=nginx_conf_deploy_path)
            self.exec_remote_cmd(
                conn=ssh_conn,
                command=(
                    "ps -aux | grep 'nginx:' | awk '{print $2}'| xargs kill"),
                warn=True)
            self.exec_remote_cmd(
                conn=ssh_conn, command=(f"nginx -c {nginx_conf_deploy_path}"))

            # 4. set dns record
            grow_domain_obj.isp.isp_instance.set_dns_a_record(
                grow_domain_obj.domain_name, ip_address)
            return
Example #6
0
def get_ssh_config(db: Session = Depends(get_db)):
    ssh_config = crud_ssh_config.get_config(db)
    return dict(result=ssh_config)