Article Title: some security tips for linuxSSH. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
I. Preface
I don't need to talk about the benefits of ssh?
In short, the previous rpc command and telnet are all replaced by ssh.
--------------------------------------------------
To the client:
$ Ssh-keygen-t rsa
(Press enter to complete? No Password is required unless you use ssh-agent .)
$ Scp ~ /. Ssh/id_rsa.pub user1@server.machine: id_rsa.pub
(For windows client, puttygen.exe can be used to generate a public key,
Copy it to the server and modify it to make the content a single line .)
---------------------------------------------------
Return to the server:
$ Cat ~ /Id_rsa.pub> ~ /. Ssh/authorized_keys
$ Rm ~ /Id_rsa.pub
$ Exit
3) restrict the su/sudo list:
# Vi/etc/pam. d/su
Auth required/lib/security/$ ISA/pam_wheel.so use_uid
# Mongodo
% Wheel ALL = (ALL) ALL
# Gpasswd-a user1 wheel
4) restrict the ssh user list
# Vi/etc/pam. d/sshd
Auth required pam_listfile.so item = user sense = allow file =/etc/ssh_users onerr = fail
# Echo user1>/etc/ssh_users
5) block ssh connection and switch to web Control List
(It is better to set up SSL, or connect to https only. I have skipped SSL settings here. Please complete the settings yourself .)
(If you need to control the online source, add the Allow/Deny project again, and the reader needs to make up for it .)
# Cat>/var/www/html/ssh_open/ssh_open.php <END
<?
// Set dir path for ip list
$ Dir_path = ".";
// Set filename for ip list
$ Ip_list = "ssh_open.txt ";
// Get client ip
$ User_ip = $ _ SERVER ['remote _ ADDR '];
// Allow specifying ip if needed
If (@ $ _ GET ['myip']) {
$ User_ip = $ _ GET ['myip'];
}
// Checking IP format
If ($ user_ip = long2ip (ip2long ($ user_ip ))){
// Put client ip to a file
If (@! ($ File = fopen ("$ dir_path/$ ip_list", "w + ")))
{
Echo "Permission denied !! <Br> ";
Echo "Pls Check your rights to dir $ dir_path or file $ ip_list ";
}
Else
{
Fputs ($ file, "$ user_ip ");
Fclose ($ file );
Echo "client ip ($ user_ip) has put into $ dir_path/$ ip_list ";
}
} Else {
Echo "Invalid IP format !! <Br> ssh_open.txt was not changed .";
}
?>
END
# Touch/var/www/html/ssh_open/ssh_open.txt
# Chmod 640/var/www/html/ssh_open /*
# Chgrp apache/var/www/html/ssh_open /*
# Chmod g + w/var/www/html/ssh_open/ssh_open.txt
# Chmod o + t/var/www/html/ssh_open
# Service httpd restart
# Mkdir/etc/iptables
# Cat>/etc/iptables/sshopen. sh <END
#! /Bin/bash
# Do nothing while list is empty
[-S $ list_file] | exit 1
# Add rule
Iptables-A $ chain_name-p tcp -- dport 22-s $ (<$ list_file)-j ACCEPT & echo "ssh opened to $ (<$ list_file) on $ (date) "| mail-s" sshopen "$ mail_to
END
# Chmod + x/etc/iptables/sshopen. sh
# Echo-e 'sshopen \ t \ t1234/tcp '>/etc/services
# Cat>/etc/xinetd. d/sshopen <END
Service sshopen
{
Disable = no
Socket_type = stream
Protocol = tcp
Wait = no
User = root
Server =/etc/iptables/sshopen. sh
}
# Iptables-I INPUT-p tcp -- dport 1234-j ACCEPT
# Cat>/etc/cron. d/sshopen <END
*/5 * root/etc/iptables/sshopen. sh clear
END
---------------------------
To the client
In the browser URL, enter:
Http://server.machine/ssh_open/ssh_open.php? Myip = 1.2.3.4
(If not specified? Myip = 1.2.3.4 is based on the client's current IP address. If no proxy is used .)
In this way, the ssh_open.txt file on the server only has a single record and is written each time.
Next:
$ Telnet server. machine 1234
Then you can use ssh to connect to the server for up to 5 minutes!
---------------------------
The basic concept of this step is as follows:
5.1) block all firewall of sshd online.
5.2) then set a directory in httpd, which can be set to ssl + htpasswd + allow/deny control,
Write a php file in the directory and record the browser ip in A. txt text file.
Depending on your transcription capability, You can automatically capture the IP address of the browser, or let the browser input parameters to specify.
A text file only has a single record and is written each time.
5.3) Modify/etc/services, add a new project (such as xxx), and specify a new port (such as 1234)
5.4) use xinetd to listen to the port, start to make a script, set iptables, get the IP address from the list in step 2, and open ssh online for it.
5.5) set crontab to clean up iptables ssh online rules in each score. This does not affect the existing connection. If it is re-connected during compaction, repeat the above.
6) if the previous step is not set, you may worry too many people will try your ssh service:
# Cat>/etc/iptables/sshblock. sh <END
#! /Bin/bash
For I in $ (echo-e "$ {bad_list // \ n}" | sort-u)
Do
Hit = $ (echo $ bad_list | egrep-o "$ I" | wc-l)
["$ Hit"-ge "$ LIMIT"] & {
Echo "$ IPT_SAV" | grep-q "$ I. *-j DROP" | {
Echo-e "\ n $ I was dropped on $ (date) \ n" | mail-s "DROP by ${0 # */}: $ I" $ MAIL_TO
Iptables-I input-s $ I-j DROP
}
Egrep-q "^ $ I $" $ PERM_LIST | echo $ I >>$ PERM_LIST
}
Done
END
# Chmod + x/etc/firewall/sshblock. sh
# Cat>/etc/hosts. allow <END
Sshd: ALL: spawn (/etc/firewall/sshblock. sh) &: ALLOW
END
In this way, the guys who try SSH can try five times at the top (the LIMIT can be adjusted), and then BLOCK it.
In addition, the ip address in PERM_LIST can also be provided to the initial script of iptables for permanent closure:
For I in $ (<$ PERM_LIST)
Do
/Sbin/iptables-I input-s $ I-j DROP
Done
7) also, if you want to know who is doing full range port scan for you:
# Iptables-I INPUT-p tcp -- dport 79-j ACCEPT
Cat>/etc/xinetd. d/finger <END
Service finger
{
Socket_type = stream
Wait = no
User = nobody
Server =/usr/sbin/in. fingerd
Disable = no
}
END
# Cat>/etc/hosts. allow <END
In. fingerd: ALL: spawn (echo-e "\ nWARNING % a was trying finger. \ n $ (date) "| mail-s" finger from % a "root) &: DENY
END
Here, I just set it to send a mail to the root.
In fact, you can change the value returned by % a to ban by starting firewall.
However, if the other party chooses port scan and does not scan finger, then of course it will be useless...
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service