加密,gpg加密,ssh三种转发,openssl,pam 题目

加密,gpg加密,ssh三种转发,openssl,pam

一、仅开放本机两个IP地址中的一个地址172.16.0.X上绑定的sshd和vsftpd服务给172.16.0.0/16网络中除了 172.16.0.0/24网络中的主机之外的所有主机,但允许172.16.0.200访问,每次的用户访问都要记录于日志文件 中,注:其中X为学号

/etc/hosts.allow:
    sshd,vsftpd: 172.16.0.0/16 EXCEPT 172.16.0.0/24 EXCEPT 172.16.0.200 :spawn echo `date` login >> /var/log/ssh.log
/etc/hosts.deny:
    sshd,vsftpd:ALL

2、编写脚本/root/bin/checkip.sh,每5分钟检查一次,如果发现通过ssh登录失败次数超过10次,自动将此远 程IP放入Tcp Wrapper的黑名单中予以禁止防问

[root@localhost ~]# echo '*/5 * * * * /data/checkip.awk /var/log/secure' >> /var/spool/cron/root
[root@localhost ~]# cat /data/checkip.awk
#!/bin/awk -f
/sshd.*Failed password/{ip=$(NF-3); ips[ip]++}
END{
    for (i in ips){
        if(ips[i]>10){
            cmd="echo sshd:"i">>/etc/hosts.deny"; system(cmd)
        }
    }
}

3、gpg加密解密:

交互式加密:会弹出一个框

[root@localhost ~]# gpg -c virtual_machine_reset_5.sh 

交互式解密:

gpg -d virtual_machine_reset_5.sh.gpg

非交互式:

[root@localhost tmp]# echo "cX0BCoWt1+qq9ZeKYCXTtxMQeiI" | gpg -c --no-tty  --batch --passphrase-fd 0 virtual_machine_reset_5.sh
[root@localhost tmp]# gpg --batch --passphrase cX0BCoWt1+qq9ZeKYCXTtxMQeiI virtual_machine_reset_5.sh.gpg 

查看gpg版本:gpg -h

CentOS7的gpg是2.0.22,Ubuntu18.04的gpg是2.2.4

4、非交互式生成密钥

[root@localhost tmp]# ssh-keygen -f ~/.ssh/id_rsa -P '' -C 'admin'

5、ssh_config

1、改端口号
Port  22
2、加主机
[root@localhost tmp]# tail -5 /etc/ssh/ssh_config
Host testbox
    HostName 192.168.38.146
    User root
    Port 2222
    IdentityFile ~/.ssh/id_rsa
就可以直接连了,默认是22端口

6、ssh命令选项

ProxyCommand

    -q      静默模式。大多数警告信息将不输出。

    -W host:port  请求客户端上的标准输入和输出通过安全隧道转发到host:port上,该选项隐含了"-N","-T",ExitOnForwardFailure和ClearAllForwardings选项

    -T      禁止为ssh分配伪终端。
    -N      明确表示不执行远程命令。仅作端口转发时比较有用。

# ProxyCommand ssh -q -W %h:%p gateway.example.com 当跳板机用

ssh当跳板机:

7、本地端口转发:

ssh -L [local_bind_addr:]local_port:remote:remote_port middle_host
root@host3:~# ssh -L 2222:192.168.38.154:80   192.168.10.134 -fNg

通过这条神奇的命令,现在就可以通过访问192.168.10.135:2222来访问192.168.38.154:80了

其中”-L”选项表示本地端口转发,其工作方式为:host3主机监听2222端口;host3将192.168.38.154:80映射为本地2222;当有人访问192.168.10.135:2222时,本地ssh将此端口的数据包转发给192.168.10.134;192.168.10.134将数据包转发给192.168.38.154:80

《加密,gpg加密,ssh三种转发,openssl,pam 题目》

再来一个:ssh -L 2222:127.0.0.1:80 127.0.0.1 -fNg

该命令执行后,就把本机2222端口的流量转发给本机80端口了

可以发现,该命令执行后访问的还是本地IP的某一端口,所以叫本地转发

8、远程端口转发

远程端口转发表示的是将远程端口的数据转发到本地。

这个就牛逼了!将远程端口转发到本地,那我岂不是连接远程的端口就可以连接到本地服务器了!

看下面,远程端口转发使用的是-R,注意公网服务器要开启GatewayPorts

[root@hk-server ~]# sed -i '$a GatewayPorts yes' /etc/ssh/sshd_config && systemctl restart sshd #这个必须得开,不然端口监听在127.0.0.1上
[root@192-168-38-140 ~]# ssh -R 2222:192.168.38.154:22 47.75.136.95 -fNg

其工作方式为:192.168.38.140请求47.75.136.95上的ssh,在47.75.136.95上建立一个套接字监听2222端口,该端口是192.168.38.154:22的映射;当有主机连接47.75.136.95:2222时,此连接中的数据全部通过通过安全隧道转发给192.168.38.154:22

《加密,gpg加密,ssh三种转发,openssl,pam 题目》

可以通过此方法,将内网搭建的博客网站映射出去,然后就可以通过公网访问了

再来一个:ssh -R 12345:127.0.0.1:22 47.75.136.95 -fNg

[root@192-168-38-140 ~]# ssh -R 12345:127.0.0.1:22 47.75.136.95 -fNg

将请求转发给自己,该命令执行后,就可以通过47.75.136.95:12345来访问本地的192.168.38.140了

9、动态端口转发(SOCKS代理)

ssh -D [bind_addr:]port remote

ssh支持动态端口转发,由ssh来判断发起请求的工具使用的是什么应用层协议,然后根据判断出的协议结果决定目标端口

可以实现:让内网不能上网的服务器上网:

[root@192-168-38-140 ~]# ssh -D 1080 127.0.0.1 -fNg

然后192.168.10.135机器将火狐浏览器设置代理到socket代理:192.168.10.134 1080

about:config将network.proxy.socks_remote_dns设置为true,开启远程DNS

工作方式:我是192.168.38.140,我在本地监听1080,所有人都可以把数据转发到我的1080端口,我再把数据通过ssh隧道动态转发出去

《加密,gpg加密,ssh三种转发,openssl,pam 题目》

10、ssh_config常改选项,优化

UseDNS no       #禁用DNS,解决连接慢
GSSAPIAuthentication no     #不开启GSSAPI认证,解决连接慢
Port 9527       #修改默认端口号
PermitRootLogin yes     #不允许root登陆
PermitEmptyPasswords no  #禁止空密码登陆

ssh_config的使用,将常用主机写到ssh_config或者~/.ssh/config里面,例如

Host testbox
    HostName 192.168.38.146
    User root
    Port 2222
    IdentityFile ~/.ssh/id_rsa
ssh-agent管理密钥

​ 生产环境中经常对密钥加密,每次连接都需要输入密码,很麻烦,而且多个私钥时,也不用自己去指定ssh-agent全部管理了

启动ssh-agent

ssh-agent

添加私钥交给ssh-agent管理

ssh-add ~/.ssh/id_rsa

列出ssh-agent管理的密钥

ssh-add -L

注意

​ 使用ssh-add失败,提示Could not open a connection to your authentication agent.
执行:ssh-agent bash 再试
​ 还有一种情况下比如,主机A需要通过主机B才能访问主机C的情况下,我们可能需要在B上保存私钥才可以,但是如果使用ssh-agent的agent forwarding功能后,就可以使用主机A登陆B和C了,而不用在B上保存私钥

11、pam模块-google验证器

比如google身份验证就是通过pam模块实现的

如何使用:epel源安装google-authenticator

apk:https://www.lanzous.com/i5yl8ad 密码:6666

家目录那个隐藏文件里面放着几个临时密码,防止手机丢了,可以自行在里面加几个密码,一次有效,

参考:http://dwz.win/j5v

12、pam模块学习

以pam_limits模块为例

先man看帮助

NAME
       pam_limits - PAM module to limit resources
SYNOPSIS
       pam_limits.so [conf=/path/to/limits.conf] [debug] [set_all] [utmp_early] [noaudit]
DESCRIPTION
       The pam_limits PAM module sets limits on the system resources that can be obtained in a
       user-session. Users of uid=0 are affected by this limits, too.
       By default limits are taken from the /etc/security/limits.conf config file. Then individual
       *.conf files from the /etc/security/limits.d/ directory are read. The files are parsed one
       after another in the order of "C" locale. The effect of the individual files is the same as
       if all the files were concatenated together in the order of parsing. If a config file is
       explicitly specified with a module option then the files in the above directory are not
       parsed.

​ *可以看到,不管哪个用户,哪怕root都受到此限制的影响,且默认情况下,限制文件未/etc/security/limits.conf及/etc/security/limits.conf.d/.conf**

使用limit -a查看当前所有资源限制情况

[root@192-168-38-140 pam.d]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 1779
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1779
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

默认文件打开数太小,不够,生产中必须修改,ubuntu18.04现在默认都改大了,如下:

*   soft nofile 128000
*   hard nofile 256000

root soft nofile 128000
root hard nofile 256000

《加密,gpg加密,ssh三种转发,openssl,pam 题目》

关于fork弹的避免措施其一就是显示每个用户的进程数,比如

*   soft    nproc   1024
*   hard    nproc   1024

ulimit -n 2000只会临时生效,建议写文件

13、自签证书和CA生成

查看证书到期时间:

[root@192-168-38-140 ~]# openssl x509 -in /etc/pki/tls/cert.pem -noout -dates | sed -nr 's/notAfter=//p'
Dec 31 09:37:37 2030 GMT
[root@192-168-38-140 ~]# openssl s_client -host www.taobao.com -port 443 -showcerts </dev/null 2>/dev/null | sed -n '/BEGIN CERTIFICATE/,/END CERT/p' -n | openssl x509 -noout -text | sed -n 's/ *Not After : *//p'
Nov 13 07:36:08 2019 GMT
    原文作者:USCWIFI
    原文地址: https://blog.51cto.com/14012942/2435965
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞