CTFd搭建CTF平台

作者: 分类: CTF,*nix,Python 时间: 2016-09-20 评论: 暂无评论

CTFd搭建CTF平台

0x00 前言

搭个CTF平台,看能不能带动一下学校的CTF参与度。

一个下午都在搭这个平台:O 抓瞎摸索,最后成功用Apache+mod_wsgi也算是功德圆满了。

进入正题:

  • 系统: CentOS release 6.8 (Final)
  • Apache: Apache/2.2.15 (Unix)
  • Mysql: Ver 14.14 Distrib 5.1.73
  • Python: 2.7.10

0x01 下载项目&环境配置

git clone https://github.com/isislab/CTFd.git

根据文档4个大步骤:

Install:

  1. ./prepare.sh to install dependencies using apt.
  2. Modify CTFd/config.py to your liking.
  3. Use python serve.py in a terminal to drop into debug mode.
  4. Here are some deployment options

apache和mysql的搭建我不再赘述,python系统自带是2.6,我升到2.7,可参见这里,记得还要装下PyMySQL

0x02 安装&排错

环境配置好了之后运行(root权限)CTFd目录下的./prepare.sh

之后修改CTFd/config.py文件,我的配置文件如下:

config.py.png

进mysql里新建一个数据库:

CREATE DATABASE CTFd;
exit;

然后

# python serve.py
 * Running on http://0.0.0.0:4000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 286-009-634
 

这里先别进网站,再次进入mysql数据库,输入下面的命令:

USE CTFd;
show tables;
ALTER TABLE awards CONVERT TO CHARACTER SET utf8;
ALTER TABLE challenges CONVERT TO CHARACTER SET utf8;
ALTER TABLE config CONVERT TO CHARACTER SET utf8;
ALTER TABLE containers CONVERT TO CHARACTER SET utf8;
ALTER TABLE files CONVERT TO CHARACTER SET utf8;
ALTER TABLE keys CONVERT TO CHARACTER SET utf8;
ALTER TABLE pages CONVERT TO CHARACTER SET utf8;
ALTER TABLE solves CONVERT TO CHARACTER SET utf8;
ALTER TABLE tags CONVERT TO CHARACTER SET utf8;
ALTER TABLE teams CONVERT TO CHARACTER SET utf8;
ALTER TABLE tracking CONVERT TO CHARACTER SET utf8;
ALTER TABLE wrong_keys CONVERT TO CHARACTER SET utf8;

把编码改成utf8,之后再访问http://IPorDomain:4000/进行配置,这样中文就不会变成??????了

如果一切都OK的话,就开始转移到Apache上去,先安装下mod_wsgi,Centos需要自己编译安装,具体可以看这里在 Centos 下安裝 mod_wsgi

安装好mod_wsgi后还要配置下CTFd目录下的wsgi.py文件,改成如下:

import sys
sys.path.insert(0, '/var/www/html/CTFd')

from CTFd import create_app
application = create_app()

装好之后配置下Apache

vim /etc/httpd/conf/httpd.conf

httpd.conf这个文件篇幅相当长,具体内容可以参见这里,写的很详细:

Web服务基础二之Apache主配置文件httpd.conf

linux下httpd.conf配置详解

主要有三部分(Section),

  1. Global Environment
  2. Main server configuration
  3. Virtual Hosts

我们只需要在Section 1 中加入一句

LoadModule auth_basic_module modules/mod_auth_basic.so
...
LoadModule version_module modules/mod_version.so
#在这个位置加入下面这句,就是加载wsgi模块
LoadModule wsgi_module modules/mod_wsgi.so

Section 2中加入这句:

WSGISocketPrefix /var/run/wsgi

然后在Section 3 中编辑虚拟主机:

<VirtualHost *:80>
    ServerName ctf.ifuryst.com

    WSGIDaemonProcess CTFd user=apache group=apache threads=5
    WSGIScriptAlias / /var/www/html/CTFd/wsgi.py

    <Directory /var/www/html/CTFd>
        WSGIProcessGroup CTFd
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        AllowOverride All
        #Require all granted
        Allow from all
    </Directory>
</VirtualHost>

之后重启Apache

/etc/init.d/httpd restart

之后访问http://ctf.ifuryst.com/先后出现了500503的错误,纳闷了半天,去查看错误日志

tail -n 20 /etc/httpd/logs/error_log

我遇到的错误分别是下面几个:

[Tue Sep 20 06:11:10 2016] [error] [client 58.23.13.241] IOError: [Errno 13] Permission denied: '.ctfd_secret_key'
[Tue Sep 20 06:12:26 2016] [error] [client 58.23.13.241] IOError: [Errno 13] Permission denied: '/var/www/html/CTFd/CTFd/logs/keys.log'
[Tue Sep 20 06:13:39 2016] [error] [client 58.23.13.241] IOError: [Errno 13] Permission denied: '/var/www/html/CTFd/CTFd/logs/logins.log'

CTFd/config.py里的

with open('.ctfd_secret_key', 'a+') as secret:
#改为绝对路径
with open('/var/www/html/CTFd/.ctfd_secret_key', 'a+') as secret:

CTFd/logs目录下的三个日志文件全部给足权限(读写权限),可以直接:

chmod -R 777 CTFd/logs

不出意外的话,这样就搞定了。

参考资料:

CTFd-Deployment

Python Web(mainly Flask)环境折腾小记

mod_wsgi(Apache)

标签: CTF

声明:文章基本原创,允许转载,但转载时必须以超链接的形式标明文章原始出处及作者信息。

暂无评论

  1. laf
    laf

    为什么我运行python serve.py后,CTFd数据库里仍然没有表?

    时间: 2018-09-21 at 20:32 回复

添加新评论