コード例 #1
0
ファイル: module.py プロジェクト: vinceyxl/LuWu
    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()
コード例 #2
0
ファイル: module.py プロジェクト: vinceyxl/LuWu
    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')
コード例 #3
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
コード例 #4
0
    def gen_site_docker_deploy_config(cls,
                                      *,
                                      docker_host: str = DEFAULT_DOCKER_HOST,
                                      site_name: str = None,
                                      template_tar_bytes: bytes = None,
                                      script: str = None,
                                      ssh_user: str = DEFAULT_SSH_USER,
                                      ssh_private_key: str,
                                      ssh_host: str,
                                      ssh_port: int = DEFAULT_SSH_PORT):
        config = Terrascript()
        docker_provider = provider.docker(host=docker_host,
                                          connection=cls.gen_ssh_conn_config(
                                              ssh_user=ssh_user,
                                              ssh_private_key=ssh_private_key,
                                              ssh_host=ssh_host,
                                              ssh_port=ssh_port))
        docker_image_resource = resource.docker_image(
            'nginx_image',
            name=cls.DEFAULT_NGINX_DOCKER_IMAGE,
        )
        docker_container_resource = resource.docker_container(
            'nginx_container',
            name=f"{site_name}-container-${{random_pet.docker_pet_name.id}}",
            image="${docker_image.nginx_image.latest}",
            restart="always",
            start=True,
            ports={'internal': 80},
            upload=[])
        docker_name_resource = resource.random_pet(
            'docker_pet_name',
            length=1,
        )

        if template_tar_bytes:
            template_tar_file = f"{site_name}-tar-${{random_pet.docker_pet_name.id}}.tar.gz",
            template_tar_file_content = base64.b64encode(
                template_tar_bytes).decode('utf8')
            template_tar_path = f"{cls.DEFAULT_NGINX_DOCKER_CONTAINER_HTML_PATH}/${template_tar_file}"
            # self.upload_file(
            #     content='conf/myapp.conf',
            #     destination=f"{self.DEFAULT_UPLOAD_PATH}/${template_tar_file}",
            #     ssh_user=ssh_user,
            #     ssh_private_key=ssh_private_key,
            #     ssh_host=ssh_host,
            #     ssh_port=ssh_port
            # )
            docker_container_resource['upload'].append({
                'content_base64':
                template_tar_file_content,
                'file':
                template_tar_path
            })

        if script:
            entrypoint_sh_content = TemplateRender().render(
                cls.DEFAULT_NGINX_DOCKER_ENTRYPOINT_PATH,
                init_script_path=cls.DEFAULT_DOCKER_ENTRYPOINT_PATH,
                html_path=cls.DEFAULT_NGINX_DOCKER_CONTAINER_HTML_PATH)
            docker_container_resource['upload'].append({
                'content':
                entrypoint_sh_content,
                'file':
                cls.DEFAULT_DOCKER_ENTRYPOINT_PATH
            })

        config += docker_provider
        config += docker_image_resource
        config += docker_container_resource
        config += docker_name_resource

        return config