2013年6月25日 星期二

Shell Scripts Sample



Generate ssh key 


#!/bin/bash

cd ~

#ssh-keygen -t rsa1
ssh-keygen -t rsa
#ssh-keygen -t dsa

HOST=(1 2 3 4)

for ip in "${HOST[@]}"
do
        echo "puttings files to site $ip, please enter password"

        cat ~/.ssh/id_rsa.pub | ssh migi@192.168.1.$ip "cat >> ~/.ssh/authorized_keys ; chmod 600 ~/.ssh/authorized_keys"
done

Copying Files


#!/bin/bash

cd ~

HOST=(1 2 3 4)

for ip in "${HOST[@]"
do
        scp -r settings 192.168.1.$ip:~/
done


Apache Testing


http://www.cyberciti.biz/tips/howto-performance-benchmarks-a-web-server.html
ab -n 1000 -c 5 http://202.54.200.1/snkpage.html

get unique ip in a specific date from apache log


grep "09/Jan/2014" event-access_log | grep "rid=65" | awk '{print $1}' | sort | uniq | wc -l


check iostat


su root
lvdisplay|awk  '/LV Name/{n=$3} /Block device/{d=$3; sub(".*:","dm-",d); print d,n;}' ; iostat 1

Get specific line from a file

sed -n lineA,lineBp FILE


SED

[root@xxsj-web1 game]# more change_cp.sh
#!/bin/bash
# db config
addr=10.10.68.242:3306
user=game
pwd= pwd
db_main=hotgame_s1
db_logs=logDB_s1
db_stat=statisticData_s1
# get pwd base64
pwdbase64=`echo -n $pwd | base64`

# update cp.cf
find . -name cp.cfg | xargs -n 1 sed -i -e"3c\JDBCConnectionURL=jdbc:mysql://${addr}/${db_main}"
find . -name cp.cfg | xargs -n 1 sed -i -e"11c\User=${user}"
find . -name cp.cfg | xargs -n 1 sed -i -e"12c\Password=${pwdbase64}"
# update logdbconfig.cfg
find . -name logdbconfig.cfg | xargs -n 1 sed -i -e"1c\url:jdbc:mysql://${addr}/${db_logs}?useUnicode=true&characterEncoding=UTF-8"
find . -name logdbconfig.cfg | xargs -n 1 sed -i -e"2c\uname:${user}"
find . -name logdbconfig.cfg | xargs -n 1 sed -i -e"3c\pwd:${pwd}"
# update DBConfig.cfg
find . -name DBConfig.cfg | xargs -n 1 sed -i -e"1c\url:jdbc:mysql://${addr}/${db_stat}?useUnicode=true&characterEncoding=UTF-8"
find . -name DBConfig.cfg | xargs -n 1 sed -i -e"2c\uname:${user}"
find . -name DBConfig.cfg | xargs -n 1 sed -i -e"3c\pwd:${pwd}"
# update log4j.properties
find . -name log4j.properties | xargs -n 1 sed -i -e"1c\  log4j.rootLogger=error, stdout, R"


Get A Fields in Files and sum the number

ls Daily-2014-03* | xargs -n 1 sed -n 6,6p | awk '{n+=$1} END { print n;}'

 

Get Nestat count

netstat -tn | awk '{n[$6]++} END { for(k in n) { print k, n[k]; }}'
watch -tdn1 cat /proc/interrupts
http://www.server-world.info/en/note?os=CentOS_6&p=inotify 
  

Split a String

timeResult = "a b c"
#split the timeReulst with ' ' into timeArray
IFS=' ' read -ra timeArray <<< "$timeResult"
#echo "${timeArray[@]}"


2013年6月11日 星期二

2013年6月7日 星期五

Drupal 7 Note

Drush

https://drupal.org/project/drush
執行drush 要在drupal目錄下

 

Customize Views templates


administrator -> structure -> views -> viewsname
Advanced -> Theme: Information

look for the templates name :

ex : views-view--myvewsname.tpl.php

cp   sites/all/modules/views/theme/views-view.tpl.php   /sites/all/themes/mysubtheme1/templates/views-view--myvewsname.tpl.php

Rescan template files

check if the templates name bolded















相關連結



http://evolvingweb.ca/story/theming-views-drupal-templates-and-preprocess-functions

http://www.interworks.com/blogs/carmstrong/2013/02/27/quick-tutorial-overriding-drupal-views-using-templates


CSS:

http://www.w3schools.com/cssref/css_selectors.asp
http://www.w3schools.com/cssref/css_colornames.asp
http://www.w3schools.com/cssref/css3_browsersupport.asp



http://yunol.com.tw/phpbb3/viewtopic.php?f=24&t=25548


Floating Block
QuickTab

Node view count

2013年6月2日 星期日

CentOS 6.4 設定 Dovecot (SMTP 認證 / smtp authentication / saslauthd )


參考 http://linux.vbird.org/linux_server/0380mail.php#adv_auth

yum install cyrus-sasl, cyrus-sasl-plain, cyrus-sasl-md5

vim /etc/sysconfig/saslauthd
MECH=pam   #use PAM

service saslauthd start


vim /etc/sasl2/smtpd.conf
log_level: 3                #log_level
pwcheck_method: saslauthd   #use saslauthd
mech_list: plain login      #support plain / login

vim /etc/postfix/main.cf


smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions =       #依照順序 by order
     permit_mynetworks,              #信任內部網路(可以關閉) trust lan 
     permit_sasl_authenticated,      #預設啟用 sasl_authenticated
     reject_unknown_sender_domain,
     reject_unknown_recipient_domain,
     reject_unauth_destination,
     reject_rbl_client cbl.abuseat.org,
     reject_rbl_client bl.spamcop.net,
     reject_rbl_client cblless.anti-spam.org.cn,
     reject_rbl_client sbl-xbl.spamhaus.org,
     check_policy_service unix:/var/spool/postfix/postgrey/socket 

postfix check
service postfix restart


Testing ....

 telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 CentOS.localdomain ESMTP Postfix
ehlo localhost
250-CentOS.localdomain
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-AUTH LOGIN PLAIN            #要出現 LOGIN PLAIN 
250-AUTH=LOGIN PLAIN            #要出現 LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

Thunderbird,
SMTP 寄件伺服器

不安全傳輸的密碼
使用者名稱


嘗試寄信, 會跳出需要密碼的畫面
more /var/log/maillog

postfix/smtpd[5091]: E362C1A0128: client=unknown[192.168.226.1], sasl_method=PLAIN, sasl_username=myusername

有以上訊息就成功




Install Drupal in CentOS 6.4 with Apache/MySQL/SELinux




1. 要讓httpd 可以存取遠端資料庫(remote database)

     要打開SELinux httpd_can_network_connect
    
   setsebool -P httpd_can_network_connect 1

2.  如果 Enable Clean URL fail


    先確認 RewriteEngine 是否能正常運作
    VirtualHost + RewriteEngine
    A: httpd.conf 要打開  Options  FollowSymLinks


3. httpd.conf 相關與其他安全設定

打開 .htaccess override 讓 apache 去讀drupal的 .htaccess

        AllowOverride All

上面註解了以後,autopath 模組應該就不用另外設定rewrite rule 在httpd.conf裡面了


修改Drupal的.htaccess


        Order deny,allow
        Deny from all
        Allow from 127.0.0.1

讓相關檔案只能從local存取

另外依照 https://drupal.org/node/244924 調整目錄權限
[root@localhost]cd /path_to_drupal_installation
[root@localhost]chown -R webadmin:www-data .
[root@localhost]find . -type d -exec chmod u=rwx,g=rx,o= '{}' \;
[root@localhost]find . -type f -exec chmod u=rw,g=r,o= '{}' \;

[root@localhost]cd /path_to_drupal_installation/sites
[root@localhost]find . -type d -name files -exec chmod ug=rwx,o= '{}' \;
[root@localhost]for d in ./*/files
do
   find $d -type d -exec chmod ug=rwx,o= '{}' \;
   find $d -type f -exec chmod ug=rw,o= '{}' \;
done 



Summarizing the permissions

  • drupal_admin: the user on the server that administrates Drupal, not necessarily is the root.
  • site_admin: the owner of the hosted site (a customer)

Ownership

Core modules/themes files and directories: drupal_admin:www-data
Hosted sites modules/themes/files files and directories: site_admin:www-data

Permissions

Core modules/themes directories: rwxr-x---
Core modules/themes files: rw-r-----
Hosted sites modules/themes directories: rwxr-x---
Hosted sites modules/themes files: rw-r-----
Hosted sites "files" directory: rwxrwx---
Hosted sites files under "files" directories: rw-rw----
Hosted sites subdirectories under "files" directories: rwxrwx---



參考
http://www.madirish.net/242

 

4. Update Manger with SSH


Just ran into same issue on my CentOS 6 box...
Here's the info...
# su -
# php -m | grep ssh2
# rpm -qa | grep libssh2

If one or both queries are empty, then proceed with following...
# yum install libssh2 libssh2-devel
# pecl install ssh2
   *** Note you'll probably get a PECL error, if so then fix with...
   # pecl install "channel://pecl.php.net/ssh2-0.12"

如果pecl 安裝 ssh2 時出現
 ex :
              sh: phpize: command not found




解法 :   yum install php-devel

如果pecl 安裝 ssh2 時出現
ex:
       configure: error: no acceptable C compiler found in $PATH

解法: yum install gcc

Then enable the extension...
# echo "extension=ssh2.so" > /etc/php.d/ssh2.ini

Finally restart Apache
# /etc/init.d/httpd restart

最後要注意上傳目錄
sites/all/modules/views
sites/all/modules/themes
的寫入權限

必須SSH user 有write
(建議新增一個webadministrator 之類的user)






drush dl views ctools panels advanced_help pathauto token wysiwyg entity ds nivo_slider nivo_formatter media admin_menu omega_tools

drush dl delta context jquery_update libraries link drupalforfirebug devel imce

drush dl css_injector workbench


drush install youtube views_slideshow varnish quicktabs fb_social


Relate Modules:

https://drupal.org/project/views
(http://ftp.drupal.org/files/projects/views-7.x-3.7.tar.gz)

https://drupal.org/project/ctools
 (http://ftp.drupal.org/files/projects/ctools-7.x-1.3.tar.gz)
 
https://drupal.org/project/panels
(http://ftp.drupal.org/files/projects/panels-7.x-3.3.tar.gz)

https://drupal.org/project/advanced_help
(http://ftp.drupal.org/files/projects/advanced_help-7.x-1.0.tar.gz)

http://ftp.drupal.org/files/projects/pathauto-7.x-1.2.tar.gz


http://ftp.drupal.org/files/projects/token-7.x-1.5.tar.gz

https://drupal.org/project/wysiwyg
()
裝普通版(先不要裝dev版)
  • http://download.moxiecode.com/tinymce/tinymce_3.5.8.zip
  • http://download.cksource.com/CKEditor/CKEditor/CKEditor%203.6.4/ckeditor_3.6.4.tar.gz

http://ftp.drupal.org/files/projects/entity-7.x-1.1.tar.gz

http://ftp.drupal.org/files/projects/ds-7.x-2.3.tar.gz

https://drupal.org/project/admin_menu
()

Omega:
https://drupal.org/node/1298616

https://drupal.org/project/omega
(http://ftp.drupal.org/files/projects/omega-7.x-3.1.tar.gz)


omegatool
http://ftp.drupal.org/files/projects/omega_tools-7.x-3.0-rc4.tar.gz
delta
http://ftp.drupal.org/files/projects/delta-7.x-3.0-beta11.tar.gz
context
http://ftp.drupal.org/files/projects/context-7.x-3.0-beta6.tar.gz






nivo_slider:
https://drupal.org/project/nivo_slider
https://drupal.org/project/Jquery_update
https://drupal.org/project/libraries
http://ftp.drupal.org/files/projects/nivo_formatter-7.x-1.x-dev.tar.gz
http://dev7studios.com/wp-content/uploads/downloads/2013/01/nivo-slider3.2.zip


views slideshow
https://drupal.org/project/views_slideshow
(http://ftp.drupal.org/files/projects/views_slideshow-7.x-3.0.tar.gz)
https://drupal.org/project/libraries
(http://ftp.drupal.org/files/projects/libraries-7.x-2.1.tar.gz)
https://github.com/malsup/cycle/downloads
(
  1. Locate/download the jQuery cycle plugin from the internet, download jquery.cycle.all.latest.min.js and rename it to jquery.cycle.all.min.js
)
https://drupal.org/project/link
(http://ftp.drupal.org/files/projects/link-7.x-1.1.tar.gz)


responsive :
https://drupal.org/project/views_responsive_grid
https://drupal.org/project/cs_adaptive_image



2013年6月1日 星期六

CentOS 6.4 Secure Installation

參考

http://bkraft.fr/articles/Securing_CentOS_6_installation/

Activate the available shields to protect your server

The default CentOS installation provides many security features that are not enabled by default and that we all should take benefit of. Here are some to consider.

Prerequisites

The media used for this tutorial is the CentOS 6.2 minimal installation CD. There’s no way to make a mistake when choosing packages with this installation media, because you can’t. It just installs the minimal software. Make sure that you install the EPEL project repository.
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm
   Retrieving http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm
   warning: /var/tmp/rpm-tmp.VseyUg: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
   Preparing...                ########################################### [100%]
      1:epel-release           ########################################### [100%]
And make sure that your server is up to date.
yum update -y

Unnecessary services

To limit the available attack surface available on the server, we’ll simply shut down the unnecessary services (well, those that I do think I don’t need for, let’s say, a webserver) and prevent them from starting at next reboot. These are the following services I’m going to disable :
rpcbind
The rpcbind utility is a server that converts RPC program numbers into universal addresses. It must be running on the host to be able to make RPC calls on a server on that machine.
nfslock
Starts up the NFS file locking service
lldpad
Link Layer Discovery Protocol Agent Daemon
fcoe
Open-FCoE Initiator
rpdidmapd
This deamon maps user names and groups to UID and GID numbers on NFSv4 mounts
for i in rpcbind  nfslock  lldpad fcoe rpcidmapd; do service $i stop; chkconfig $i off; done
   Stopping rpcbind:                                          [  OK  ]
   Stopping NFS statd:                                        [  OK  ]
   Stopping lldpad:                                           [  OK  ]
   Stopping FCoE initiator service:                           [  OK  ]
   Stopping RPC idmapd:                                       [  OK  ]

Protecting SSH service

As this is one the core and mostly attacked services, we need to protect ourselves against one of the most common threats : brute force attacks. For this, we are going to set up Fail2ban that will account for us the number of times a server will occur with failed authentications attempts and add the corresponding ip address to iptables reject list (a specific iptables user chain that returns until the ip is listed in it, to be exact). The package file comes from the EPELproject, and I build a configuration file specifically for this simple purpose :
[DEFAULT]
ignoreip = 127.0.0.1
# Block the attacking host for a day
bantime  = 86400
# If it appears in the watched log file for the last hour
findtime  = 3600
# And generated 3 error messages
maxretry = 3
backend = auto

[ssh-iptables]

enabled  = true
filter   = sshd
action   = iptables[name=SSH, port=ssh, protocol=tcp]
# Optionnal : sendmail-whois[name=SSH, dest=root, sender=fail2ban@mail.com]
logpath  = /var/log/secure
maxretry = 3
So now, install it, configure it using the custom jail.conf and make the service start when the system boots.
yum install fail2ban
curl http://bkraft.fr/files/Configurations/fail2ban/jail.conf -o /etc/fail2ban/jail.conf
chkconfig fail2ban on
service fail2ban start
   Starting fail2ban:                                          [  OK  ]
We now can see that there is an iptables chain called fail2ban-SSH as you can see below.
iptables-save 
   # Generated by iptables-save v1.4.7 on Sat Mar 10 22:09:08 2012
   *filter
   :INPUT ACCEPT [0:0]
   :FORWARD ACCEPT [0:0]
   :OUTPUT ACCEPT [17:1492]
   :fail2ban-SSH - [0:0]
   -A INPUT -p tcp -m tcp --dport 22 -j fail2ban-SSH 
   -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
   -A INPUT -p icmp -j ACCEPT 
   -A INPUT -i lo -j ACCEPT 
   -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT 
   -A INPUT -j REJECT --reject-with icmp-host-prohibited 
   -A FORWARD -j REJECT --reject-with icmp-host-prohibited 
   -A fail2ban-SSH -j RETURN 
   COMMIT
   # Completed on Sat Mar 10 22:09:08 2012

Small things that matters

In single user mode, ask for password
perl -i -pe 's/sushell/sulogin/' /etc/sysconfig/init
Change the number of available gettys as there is too much of them available by default.
perl -i -pe 's/1-6/1/' /etc/sysconfig/init
perl -i -pe 's/1-6/1/' /etc/init/start-ttys.conf
Prevent prompting for interactive boot
perl -i -pe 's/PROMPT=yes/PROMPT=no/' /etc/sysconfig/init
Prevent rebooting when Control-Alt-Delete is being pressed on the console.
perl -i -pe 's/exec.*/exec \/bin\/echo "Control-Alt-Delete pressed, but no action will be taken"/' /etc/init/control-alt-delete.conf
Change the minimum password length from 5 to 9
perl -i -pe 's/PASS_MIN_LEN\s+5/PASS_MIN_LEN  9/' /etc/login.defs
Disconnect idle users after 15 minutes
cat > /etc/profile.d/inactive-users-disconnect.sh << EOF
readonly TMOUT=900
readonly HISTFILE
EOF 
chmod +x /etc/profile.d/inactive-users-disconnect.sh
Prevent anybody but root to run cron or at tasks
touch /etc/cron.allow
chmod 600 /etc/cron.allow
awk -F: '{print $1}' /etc/passwd | grep -v root > /etc/cron.deny
touch /etc/at.allow
chmod 600 /etc/at.allow
awk -F: '{print $1}' /etc/passwd | grep -v root > /etc/at.deny
Be careful, consistency of at.deny and cron.deny is not guaranteed over time. you should care about it (rerun the awks by hand or by cron)
Change /etc/issue to something scary
cat >/etc/issue << EOF
USE OF THIS COMPUTER SYSTEM, AUTHORIZED OR UNAUTHORIZED, CONSTITUTES CONSENT TO MONITORING OF THIS SYSTEM.
UNAUTHORIZED USE MAY SUBJECT YOU TO CRIMINAL PROSECUTION.
EVIDENCE OF UNAUTHORIZED USE COLLECTED DURING MONITORING MAY BE USED FOR ADMINISTRATIVE, CRIMINAL, OR OTHER ADVERSE ACTION.
USE OF THIS SYSTEM CONSTITUTES CONSENT TO MONITORING FOR THESE PURPOSES.
EOF

Narrow down rights

Many of the files have by default too wide rights that might led to disclose some important informations.
# Narrow down right on /root
chmod 700 /root

# Audit logs should be available only for root
chmod 700 /var/log/audit

# Remove too wide rights on iptables binary and init scripts
chmod 740 /etc/rc.d/init.d/iptables
chmod 740 /sbin/iptables

# Change the rights of the default user skeleton
chmod -R 700 /etc/skel

# Restrict access to rsyslog configuration to root
chmod 600 /etc/rsyslog.conf

# Locking down LNX00440
chmod 640 /etc/security/access.conf

# Sysctl configuration should only accessible to root
chmod 600 /etc/sysctl.conf

Secure network via sysctl

cat << 'EOF' >> /etc/sysctl.conf
 # disable packet forwarding
 net.ipv4.ip_forward = 0
 # drop icmp redirects
 net.ipv4.conf.all.send_redirects = 0
 net.ipv4.conf.default.send_redirects = 0
 net.ipv4.conf.all.accept_redirects = 0
 net.ipv4.conf.all.secure_redirects = 0
 net.ipv4.conf.default.accept_redirects = 0
 net.ipv4.conf.default.secure_redirects = 0
 # double the syn backlog size
 net.ipv4.tcp_max_syn_backlog = 2048
 # ignore ping broadcasts
 net.ipv4.icmp_echo_ignore_broadcasts = 1
 # drop the source routing ability
 net.ipv4.conf.all.accept_source_route = 0
 net.ipv4.conf.default.accept_source_route = 0
 # log packets destinated to impossible addresses
 net.ipv4.conf.all.log_martians = 1
 # ignore bogus icmp error responses
 net.ipv4.icmp_ignore_bogus_error_responses = 1
 # protect a bit from SYN flood attacks
 net.ipv4.tcp_syncookies = 1
 # drop packets that come in using a bad interface
 # (they will be logged as martian)
 net.ipv4.conf.all.rp_filter = 1
 net.ipv4.conf.default.rp_filter = 1
 # don't send timestamps
 net.ipv4.tcp_timestamps = 0
EOF

Delete unnecessary users

By default many users are present that aren’t necessary, let’s just get rid of them.
/usr/sbin/userdel shutdown
/usr/sbin/userdel halt
/usr/sbin/userdel games
/usr/sbin/userdel operator
/usr/sbin/userdel ftp
/usr/sbin/userdel gopher

Tuning OpenSSH

As per this setup, the SSH service is opened on internet, and we need to make sure that it is secured. By default, it is already well secured, but there is some things we should change.
# Prevent ssh connections from root
perl -i -pe 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
# Set our scary issue message as ssh banner
perl -i -pe 's/#Banner.*/Banner \/etc\/issue/g' /etc/ssh/sshd_config

# Make the server keys a bit bigger
perl -i -pe 's/^#ServerKeyBits 1024/ServerKeyBits 2048/g' /etc/ssh/sshd_config
# We need now to drop previously created 1024 keys and regenerate them.
rm -vf /etc/ssh/ssh_host*
 removed `/etc/ssh/ssh_host_dsa_key'
 removed `/etc/ssh/ssh_host_dsa_key.pub'
 removed `/etc/ssh/ssh_host_key'
 removed `/etc/ssh/ssh_host_key.pub'
 removed `/etc/ssh/ssh_host_rsa_key'
 removed `/etc/ssh/ssh_host_rsa_key.pub'
/etc/init.d/sshd restart
 Stopping sshd:                                             [  OK  ]
 Generating SSH1 RSA host key:                              [  OK  ]
 Generating SSH2 RSA host key:                              [  OK  ]
 Generating SSH2 DSA host key:                              [  OK  ]
 Starting sshd:                                             [  OK  ]

# Restrict max authentications
perl -i -pe 's/^#MaxAuthTries 6/MaxAuthTries 3/g' /etc/ssh/sshd_config

Removing kernel modules

As the machine that I’m running is a virtual one, I don’t need wireless drivers to be loaded and neither do I need fcoe, so let’s just blacklist them.
for i in $(find /lib/modules/`uname -r`/kernel/drivers/net/wireless -name "*.ko" -type f) ; do echo blacklist $i >> /etc/modprobe.d/blacklist-wireless.conf ; done
for i in $(find /lib/modules/`uname -r`/kernel/drivers/scsi/fcoe -name "*.ko" -type f) ; do echo blacklist $i >> /etc/modprobe.d/blacklist-fcoe.conf ; done

Run the whole configuration script

If you want to run all the modifications done here in one shot, it’s possible. The script is available in the file repository.
Be very careful, use this script with caution, because it really changes the security configuration of your server. Benjamin KRAFT is not liable with all impacts the use of this script would have.
If you know that you need to secure your server, but don’t know how to proceed, or what this script will modify, a better idea would be to ask for help as you really risk some downtime if your server is already in production.
curl http://bkraft.fr/files/scripts/Secure_CentOS.sh | sh
Created the 2012-03-18