Try   HackMD

信安之路第一周-环境搭建

tags: 信安之路

要求:第一周:学籍备案以及环境准备

个人资料

知识星球中的编号:243

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

常用名:Lhaihai

联系方式:

目前职业:

所在地区:

熟悉的编程语言:php/python

自我介绍:

Ubuntu 搭建

Ubuntu 18.04 TLS 下载地址:

https://ubuntu.com/download/desktop

默认没有ifconfig,安装net-tools

sudo apt install net-tools vim openssh-server curl 

启动ssh服务

sudo /etc/init.d/ssh start或sudo service ssh start 

启动ssh服务后连接不上,怀疑防火墙没有关闭,用下面命令查看。输出inactive是关闭,active是开启着。 https://www.opencli.com/linux/ubuntu-18-04-disable-enable-firewall

sudo ufw status

配置ssh:https://www.jianshu.com/p/b294e9da09ad

更换为阿里源

https://zhuanlan.zhihu.com/p/61228593

启动Software & Updates, 点other选择阿里源

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

然后点右下角的close,开始更新源
查看配置文件已经更改为阿里源了

less /etc/apt/sources.list

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

sudo apt update

安装vmtools:https://kb.vmware.com/articleview?docid=1022525&lang=zh_CN

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

解压到桌面,然后安装

cd ~/Desktop/vmware-tools-distrib
sudo ./vmware-install.pl -d
reboot

LEMP 安装

https://www.howtoing.com/how-to-install-linux-nginx-mysql-php-lemp-stack-ubuntu-18-04

https://wiki.ubuntu.org.cn/Nginx

启动服务

sudo systemctl restart php7.2-fpm
sudo systemctl reload nginx
sudo systemctl start mysql

安装 Nginx

sudo apt install nginx

Ubuntu安装之后的文件结构大致为:

  • 所有的配置文件都在/etc/nginx下,并且每个虚拟主机已经安排在了/etc/nginx/sites-available下
  • 程序文件在/usr/sbin/nginx
  • 日志放在了/var/log/nginx中
  • 并已经在/etc/init.d/下创建了启动脚本nginx
  • 默认的虚拟主机的目录设置在了/var/www/nginx-default (有的版本 默认的虚拟主机的目录设置在了/var/www, 请参考/etc/nginx/sites-available里的配置)

启动ngnix

sudo /etc/init.d/nginx start

然后就可以访问了,http://localhost/ , 一切正常!如果不能访问,先不要继续,看看是什么原因,解决之后再继续。 启动时候若显示端口80被占用: Starting nginx: [emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use) 修改文件:/etc/nginx/sites-available/default,去掉 listen 前面的 # 号 , # 号在该文件里是注释的意思 , 并且把 listen 后面的 80 端口号改为自己的端口,访问是需要添加端口号。

(安装完后如出现403错误,那可能是nginx配置文件里的网站路径不正确)

出现下图说明安装好Ngnix了

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Mysql

https://linuxize.com/post/how-to-install-mysql-on-ubuntu-18-04/

sudo apt install mysql-server-5.7

允许脚本配置Mysql

sudo mysql_secure_installation

启动mysql

sudo systemctl start mysql

root 密码是 20190801

连接mysql

mysql -p -uctf

root 权限启动mysql

sudo mysql

如果想要phpmyadmin使用mysql root登陆

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'very_strong_password';
FLUSH PRIVILEGES;

创建新用户

可以访问所有数据库
GRANT ALL PRIVILEGES ON *.* TO 'ctf'@'localhost' IDENTIFIED BY 'ctf2019@';

只可以访问ctf数据库
GRANT ALL PRIVILEGES ON ctf.* TO xoops_root@localhost IDENTIFIED BY "654321";

PHP

sudo apt install php-fpm php-mysql

您现在已经安装了PHP组件,但您需要进行轻微的配置更改以使您的设置更安全。

以root权限打开主要的php-fpm配置文件:

sudo vim /etc/php/7.2/fpm/php.ini

在这个文件中,找到设置cgi.fix_pathinfo的参数。 这将用分号(;)注释掉,默认设置为“1”。

这是一个非常不安全的设置,因为它告诉PHP尝试执行最近的文件,如果找不到请求的PHP文件,它可以找到它。 这可以允许用户以一种允许他们执行不应允许执行的脚本的方式制作PHP请求。

通过取消注释并将其设置为“0”来更改这两个条件,如下所示:

/etc/php/7.2/fpm/php.ini
cgi.fix_pathinfo=0

完成后保存并关闭文件。 然后,输入以下命令重新启动您的PHP处理器

sudo systemctl restart php7.2-fpm

这将实施您所做的更改。

配置Nginx使用PHP处理器

/etc/nginx/sites-available/default

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name your_server_domain_or_IP;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

编写代码

create database ctf;

连接数据库

<?php
$username = "ctf";
$password = "ctf2019@";
$db = "ctf";

try {
    $conn = new PDO("mysql:host=$server;dbname=$db",$username,$password);
    echo "Connected successfully";
} catch (PDOException $e){
    echo $e->getMessage();
}

?>

安装vscode

在 ubuntu software 搜索下载vscode

以root权限启动,但权限太高了有点危险

sudo code . --user-data-dir="~/.vscode-root"

将文件的权限降为普通用户

sudo chown -R lhaihai:lhaihai(用户名) ./wayne(文件名)

nginx 权限报错

lhaihai@ubuntu:~$ nginx -t
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2019/08/02 08:50:10 [warn] 2894#2894: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2019/08/02 08:50:10 [emerg] 2894#2894: open() "/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /etc/nginx/nginx.conf test failed
sudo chmod 622 /var/log/nginx/error.log
sudo chmod 622 /var/log/nginx/access.log
sudo chmod 777 /run/nginx.pid

加固

https://klionsec.github.io/2017/11/20/nginx-sec/

创建nginx运行账户

创建nginx运行账户,尽量以伪用户身份来运行nginx服务,切记,千万不要直接用root权限来运行nginx,否则别人拿到的webshell权限很可能直接是root权限的 [ 如果你后面的的fastcgi服务端 [php-fpm] 和 nginx使用的是同一用户身份来运行,就很容造成这种情况 ]

# useradd -s /sbin/nologin -M nginx

屏蔽ip

server {
    ...
    deny 192.168.3.0/24;
    ...
}

方便起见创建一个 blockip.conf 文件

sudo vim /etc/nginx/blockip.conf
deny 127.0.0.1;

然后在配置文件加上

include blockip.conf;

后续可以通过日志查看ip 量,自动加爬虫ip到封禁文件

http://www.nginx.cn/2487.html

拒绝各种危险请求方法

下面的意思就是除了GET,POST请求方法之外,别的方法一律禁止

server {
    ...
    if ($request_method !~ ^(GET|POST)$ ) {
	return    403;
    }
    ...
}

nginx_lua_waf

ngx_lua_waf是一个基于lua-nginx-module(openresty)的web应用防火墙

https://www.jianshu.com/p/630fd428c950
https://www.centos.bz/2017/10/nginxnginx_lua实现waf防护功能/

github:https://github.com/loveshell/ngx_lua_waf

X-WAF

X-WAF是一款适用中、小企业的云WAF系统,让中、小企业也可以非常方便地拥有自己的免费云WAF。

官网:https://waf.xsec.io

github源码:https://github.com/xsec-lab/x-waf