目录
帐号权限安全
需求:限制用户权限,仅提供一些linux常用命令,用户监控linux系统于网络运行情况,不允许用户ssh登录后随意运行linux命令
使用mgmt替代bash
#!/bin/bash
TITLE="Client"
#USER=$(whiptail --inputbox "User:" 8 60 --title "$TITLE" 3>&1 1>&2 2>&3)
#PASSWD=$(whiptail --title "$TITLE" --passwordbox "Passsword:" 8 60 3>&1 1>&2 2>&3)
COMMAND=$(whiptail --title "$TITLE" --menu "Administrator Tools" 22 50 10 \
"ping" "ping" \
"tracepath" "tracepath" \
"top" "top" \
"free" "free" \
"ps" "ps" \
"netstat" "netstat" \
"lsof" "lsof" \
"iftop" "iftop" \
"log" "log" \
3>&1 1>&2 2>&3)
function option(){
OPTION=$(whiptail --inputbox "COMMAND-LINE Options: " 8 60 --title "$TITLE" 3>&1 1>&2 2>&3)
}
function weblog(){
LOG=$(whiptail --title "$TITLE" --menu "Logs" 22 50 8 \
"/var/log/messages" "message" \
"/var/log/syslog" "syslog" \
"/var/log/nginx/access.log" "access.log" \
"/var/log/nginx/error.log" "error.log" \
3>&1 1>&2 2>&3)
}
case $COMMAND in
ping)
option
$COMMAND $OPTION
;;
tracepath)
option
$COMMAND $OPTION
;;
free)
$COMMAND -m
read
;;
top|iftop)
$COMMAND
;;
log)
weblog
tail -f $LOG
;;
ps|lsof)
option
$COMMAND $OPTION
read
;;
netstat)
option
$COMMAND $OPTION
read
;;
*)
exit $?
esac
Shell 启动文件,主要用户隐藏 /srv/sbin/mgmt 文件(针对菜鸟)
$ cat shell.c
#include <stdlib.h>
main()
{
for (;;){
system("/srv/sbin/mgmt");
}
}
编译.c文件
gcc shell.c -o /bin/nsh
添加Shell到/etc/shells
echo /bin/nsh >> /etc/shells
将用户shell更改为我们刚刚创建的nsh
$ vim /etc/passwd
www:x:33:33:www:/var/www:/bin/nsh
现在来作一个测试,如果正确应该现在为下面的TUI界面
ssh www@example.com
┌───────────────────┤ Client ├───────────────────┐
│ Administrator Tools │
│ │
│ ping ping │
│ tracepath tracepath │
│ top top │
│ free free │
│ ps ps │
│ netstat netstat │
│ lsof lsof │
│ iftop iftop │
│ log log │
│ │
│ │
│ <Ok> <Cancel> │
│ │
└────────────────────────────────────────────────┘
这里采用的方式是给用户提供一个界面的方式,另外还有更好的方案,你可以些一个Shell的外壳,你需要实现
流程
用户输入 -> 关键字过滤 -> 放行
例如用户输入 cd / 经过过滤器后, cd /home/usr/
例如用户输入 cd /aaa 经过过滤器后, cd /home/usr/aaa
rm -rf /usr/local 提示拒绝等等
我已经使用python实现上面的大部分功能(因为python受到很多限制)如果使用C可以100%实现,需要你的想想力了
SA的操作记录问题
通过~/.bash_history文件记录系统管理员的操作记录,定制.bash_history格式
HISTSIZE=1000
HISTFILESIZE=2000
HISTTIMEFORMAT="%Y-%m-%d-%H:%M:%S "
export HISTTIMEFORMAT
看看实际效果
$ history | head
1 2012-02-27-09:10:45 do-release-upgrade
2 2012-02-27-09:10:45 vim /etc/network/interfaces
3 2012-02-27-09:10:45 vi /etc/network/interfaces
4 2012-02-27-09:10:45 ping www.163.com
临时文件不应该有执行权限
/tmp
/dev/sda3 /tmp ext4 nosuid,noexec,nodev,rw 0 0
同时使用符号连接将/var/tmp 指向 /tmp
/dev/shm
none /dev/shm tmpfs defaults,nosuid,noexec,rw 0 0
/etc/sudoers
Cmnd_Alias WEBMASTER = /usr/local/webserver/nginx/sbin/nginx, /usr/local/webserver/php/sbin/php-fpm, !/usr/local/webserver/mysql/bin/*
www localhost = NETWORKING, SERVICES, DELEGATING, PROCESSES, WEBMASTER
Cmnd_Alias Database = /usr/bin/mysqldump, /srv/mysql/bin/mysql, /u01/oracle/10.x.x/bin/sqlplus
oralce localhost = NETWORKING, SERVICES, DELEGATING, PROCESSES, WEBMASTER, Database
使用www用户测试登录,无误后修改SSH配置文件,禁止root登录。
vim /etc/ssh/sshd_config
PermitRootLogin no
然后在测试从www su 到root
封锁22等端口,避免相互跳转
lokkit --enabled
iptables -F
iptables -A OUTPUT -p tcp -m multiport --dports 22,21,2049 -j REJECT
/etc/init.d/iptables save
iptables -L -n
web 服务器禁止使用ssh,作为跳板机
用户将不能使用ssh命令登陆到其他电脑
我们以MySQL为例,讲解怎样控制DBA权限。稍加修改即可用于oracle等服务器
rpm, deb 等等包安装mysql后默认权限是 755
$ ll /usr/bin/mysql*
-rwxr-xr-x 1 root root 132132 2012-02-28 01:33 /usr/bin/mysql*
-rwxr-xr-x 1 root root 111572 2012-02-28 01:31 /usr/bin/mysqlaccess*
-rwxr-xr-x 1 root root 32468 2012-02-28 01:33 /usr/bin/mysqladmin*
-rwxr-xr-x 1 root root 2030768 2011-09-14 23:04 /usr/bin/mysql-admin*
lrwxrwxrwx 1 root root 10 2012-02-28 01:33 /usr/bin/mysqlanalyze -> mysqlcheck*
-rwxr-xr-x 1 root root 147288 2012-02-28 01:33 /usr/bin/mysqlbinlog*
-rwxr-xr-x 1 root root 12006 2012-02-28 01:31 /usr/bin/mysqlbug*
-rwxr-xr-x 1 root root 24940 2012-02-28 01:33 /usr/bin/mysqlcheck*
-rwxr-xr-x 1 root root 451016 2012-02-28 01:33 /usr/bin/mysql_client_test*
-rwxr-xr-x 1 root root 7246484 2012-02-28 01:33 /usr/bin/mysql_client_test_embedded*
-rwxr-xr-x 1 root root 4245 2012-02-28 01:31 /usr/bin/mysql_convert_table_format*
-rwxr-xr-x 1 root root 23943 2012-02-28 01:31 /usr/bin/mysqld_multi*
-rwxr-xr-x 1 root root 16642 2012-02-28 01:32 /usr/bin/mysqld_safe*
-rwxr-xr-x 1 root root 101636 2012-02-28 01:33 /usr/bin/mysqldump*
-rwxr-xr-x 1 root root 7402 2012-02-28 01:31 /usr/bin/mysqldumpslow*
-rwxr-xr-x 1 root root 3315 2012-02-28 01:31 /usr/bin/mysql_find_rows*
-rwxr-xr-x 1 root root 1261 2012-02-28 01:31 /usr/bin/mysql_fix_extensions*
-rwxr-xr-x 1 root root 5834 2012-02-28 01:31 /usr/bin/mysql_fix_privilege_tables*
-rwxr-xr-x 1 root root 32477 2012-02-28 01:31 /usr/bin/mysqlhotcopy*
-rwxr-xr-x 1 root root 24584 2012-02-28 01:33 /usr/bin/mysqlimport*
-rwxr-xr-x 1 root root 14657 2012-02-28 01:31 /usr/bin/mysql_install_db*
lrwxrwxrwx 1 root root 10 2012-02-28 01:33 /usr/bin/mysqloptimize -> mysqlcheck*
-rwxr-xr-x 1 root root 2006884 2011-09-14 23:04 /usr/bin/mysql-query-browser*
lrwxrwxrwx 1 root root 10 2012-02-28 01:33 /usr/bin/mysqlrepair -> mysqlcheck*
-rwxr-xr-x 1 root root 39016 2012-02-28 01:32 /usr/bin/mysqlreport*
-rwxr-xr-x 1 root root 8066 2012-02-28 01:31 /usr/bin/mysql_secure_installation*
-rwxr-xr-x 1 root root 17473 2012-02-28 01:31 /usr/bin/mysql_setpermission*
-rwxr-xr-x 1 root root 23716 2012-02-28 01:33 /usr/bin/mysqlshow*
-rwxr-xr-x 1 root root 45884 2012-02-28 01:33 /usr/bin/mysqlslap*
-rwxr-xr-x 1 root root 208148 2012-02-28 01:33 /usr/bin/mysqltest*
-rwxr-xr-x 1 root root 6960852 2012-02-28 01:33 /usr/bin/mysqltest_embedded*
-rwxr-xr-x 1 root root 1334028 2012-02-28 01:33 /usr/bin/mysql_tzinfo_to_sql*
-rwxr-xr-x 1 root root 64728 2012-02-28 01:33 /usr/bin/mysql_upgrade*
-rwxr-xr-x 1 root root 149836 2012-02-28 01:33 /usr/bin/mysql_waitpid*
-rwxr-xr-x 1 root root 2108 2012-02-22 01:28 /usr/bin/mysql-workbench*
-rwxr-xr-x 1 root root 9885312 2012-02-22 01:29 /usr/bin/mysql-workbench-bin*
-rwxr-xr-x 1 root root 3888 2012-02-28 01:31 /usr/bin/mysql_zap*
从安全角度考虑我们需要如下更改
chown mysql:mysql /usr/bin/mysql*
chmod 700 /usr/bin/mysql*
mysql用户是DBA专用用户
DBA不需要通过SSH登录数据库服务器,然后运行mysql/sqlplus在登录数据库
如果web与database 在一台机器上
bind-address = 127.0.0.1
$ cat ../database/mysqltui
#!/bin/bash
TITLE="MySQL Client"
HOST=$(whiptail --title "$TITLE" --menu "MySQL Host" 22 50 8 \
"127.0.0.1" "localhost" \
"172.16.0.1" "MySQL Master" \
"172.16.0.2" "MySQL Slave 1" \
"172.16.0.3" "MySQL Slave 2" \
3>&1 1>&2 2>&3)
USER=$(whiptail --inputbox "MySQL User:" 8 60 --title "$TITLE" 3>&1 1>&2 2>&3)
PASSWD=$(whiptail --title "$TITLE" --passwordbox "MySQL Password:" 8 60 3>&1 1>&2 2>&3)
#DATABASE=$(mysqlshow -h$HOST -u$USER | egrep -o "|\w(.*)\w|" | grep -v "Databases" |awk '{print "\""$1"\" \""$1"\""}')
#DATABASE=$(mysqlshow -h$HOST -u$USER | egrep -o "|\w(.*)\w|" | grep -v "Databases" |awk "{print \"$1\" \"$1\"}")
#DB=$(whiptail --title "$TITLE" --menu "MySQL DATABASE" 22 50 8 $DATABASE 3>&1 1>&2 2>&3)
DATABASE=$(whiptail --inputbox "MySQL Database:" 8 60 --title "$TITLE" 3>&1 1>&2 2>&3)
echo $HOST $USER $PASSWD $DATABASE
mysql -h$HOST -u$USER -p$PASSWD $DATABASE
┌───┤ MySQL Adminstrator ├───┐
│ Menu │
│ │
│ 1 MySQL Manager │
│ 2 MySQL Backup │
│ 2 MySQL Restore │
│ │
│ │
│ <Ok> <Cancel> │
│ │
└────────────────────────────┘
┌────────┤ MySQL Adminstrator ├────────┐
│ Database Host │
│ │
│ 127.0.0.1 localhost │
│ 172.16.0.1 mysql master │
│ 172.16.0.2 mysql slave │
│ │
│ <Ok> <Cancel> │
│ │
└──────────────────────────────────────┘
/etc/php5/fpm/pool.d/www.conf
┌────────┤ MySQL Adminstrator ├────────┐
│ User │
│ │
│ root________________________________ │
│ │
│ <Ok> <Cancel> │
│ │
└──────────────────────────────────────┘
┌────────┤ MySQL Adminstrator ├────────┐
│ Password │
│ │
│ ****________________________________ │
│ │
│ <Ok> <Cancel> │
│ │
└──────────────────────────────────────┘
进入mysql客户端
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 503
Server version: 5.1.58-1ubuntu1 (Ubuntu)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
通过~/.mysql_history文件记录DBA操作记录
插入时间点,在~/.bashrc中加入下面命令
cat >> ~/.bashrc <<EOD
echo `date` >> ~/.mysql_history
EOD
$ tail ~/.bashrc
echo `date` >> ~/.mysql_history
查看实际效果
$ tail ~/.mysql_history
EXPLAIN SELECT * FROM stuff where id=3 \G
EXPLAIN SELECT * FROM stuff where id='3' \G
EXPLAIN SELECT * FROM stuff where id='2' \G
Mon Feb 27 09:15:18 CST 2012
EXPLAIN SELECT * FROM stuff where id='2' and created = '2012-02-01' \G
EXPLAIN SELECT * FROM stuff where id='1' and created = '2012-02-01' \G
EXPLAIN SELECT * FROM stuff where id='3' and created = '2012-02-01' \G
EXPLAIN SELECT * FROM stuff where id='2' and created = '2012-02-01' \G
EXPLAIN SELECT * FROM stuff where id='2' or created = '2012-02-01' \G
EXPLAIN SELECT * FROM stuff where id='2' and created = '2012-02-01' \G
Mon Feb 27 11:48:37 CST 2012