2016年10月8日 星期六

一些手記


Linux

可以對介面產生UUID:uuidgen eth0
查詢某個未安裝指令的軟體(red hat):yum provides \*/bin/<command>
查看網路介面上的封包:tcpdump -nni eth0 -vvv
產生公鑰、私鑰:ssh-keygen -t rsa -b 4096

用linux轉發封包

查詢是否能轉發
sysctl net.ipv4.ip_forward
or
cat /proc/sys/net/ipv4/ip_forward

改成可轉發
sysctl -w net.ipv4.ip_forward=1
or
echo 1 > /proc/sys/net/ipv4/ip_forward

修改開機後可轉發
在/etc/sysctl.conf裡,修改net.ipv4.ip_forward = 1
sysctl -p /etc/sysctl.conf
重啟服務/etc/init.d/procps.sh restart

Shell Script


[ 0 -eq 0 ] && ${echo 'yes'; echo 'yes2’;} || echo 'no'; #bad substitution
[ 0 -eq 0 ] && `echo 'yes'; echo 'yes2';` || echo 'no'; #持續輸出yes2

[ 0 -eq 0 ] && (echo 'yes'; echo 'yes2';) || echo 'no'; #輸出yes和yes2

Juniper

讓管理權限使用者perry可以用public key做ssh connection(不需密碼)
set system login user perry uid 2000
set system login user perry class super-user
set system login user perry authentication ssh-rsa "ssh-rsa AAAAB3NzaC1yc2EAAAADAQGJIAABAQDAdZ/VeeKUqAvwmopVNy1murzqeoObxcNtSnwMR3jWXZyiW3O29rBPZqYuj5u4VuutceW/ETirDmo7UKlIKFgploeyhFfyfTStNfdZL4QqwadsUFePMeCe41jW+ez2nV1UFAiGcvyhzfJSVAGm2i80cKeAHdO3vjpYR+aBib3VFYxl2B/y4oJijEOCmmWnwZFTNBkI2xX0EmZYiVOgs9G1ztplPdGq+1Km03TB/g5C+2Fm7cYhs9igbZmD5Pr5dhvX10CHV6FFw3tLGDtJJhYs/bvYVaRTJAArh+9erRkECvIvBCywPSdcm9K3dT2fAaTqNhyb3LW4Wi9Yk3nd7Evb test@example.com;”

2016年9月16日 星期五

ssh tunnel

修改20160916

目的

示範使用linux與windows的putty製作ssh tunnel連線

tolopogy


local host需要在forward host上建立ssh tunnel並連到remote host
local host必須要能夠在forward host上建立ssh連線
local host在自己身上選定port當作通道
選定remote host的ip與port

完成後local host使用自己的port連到remote host的ip和port,remote host只會看到是forward host與他建立連線


windows/linux(unix)實作

使用windows 10, putty, ip 10.10.10.10

1.putty->category->connection->SSH->Turrenls
Source port 22222是在local host建立port
Destination 10.10.9.2:22是建立連到remote host的ip和port

2.local host與forward host 10.10.10.22建立連線

3.建立ssh tunnel完成

4.利用ssh tunnel與remote host做ssh連線

5.驗證,在remote host上可以看到登入的ip是10.10.9.1

使用macbook, terminal, ip 10.10.10.1

1.-N 不會執行對方的命令,就是不需要操作forward host
-f 背景執行
-L 後面接ip和port設定
完整指令 ssh -N -f -L [local port]:[remote ip]:[remote port]  [forward host]

驗證



2016年9月12日 星期一

chattr lsattr

更新20160912
來源man chattr
chattr [ -RVf ] [+-=[aAcCdDeijsStTu]] file

-R     Recursively change attributes of directories and their contents.

-V     Be verbose with chattr's output and print the program version.

-f     Suppress most error messages.

屬性
a:檔案只能增加資料,不能覆蓋或刪除. 


A:讀取檔案不會增加atime,減少disk存取次數.



c:the file is automatically compressed on the disk by the kernel.
C:the file will not be subject to copy-on-write updates.
d:the file is not candidate for backup.
D:the file changeling are written synchronously on the disk.
e:the file is using extents for mapping the blocks on disk. 
E:the experimental encryption patches to indicate that the file has been encrypted.

The 'h' attribute indicates the file is storing its blocks in units of the filesystem blocksize instead of in units of sectors, and means that the file is (or at one time was) larger than 2TB.
A file with the 'i' attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file.
A file with the 'j' attribute has all of its data written to the ext3 or ext4 journal before being written to the file itself
A file with the 'N' attribute set indicates that the file has data stored inline, within the inode itself.
A directory with the 'P' attribute set will enforce a hierarchical structure for project id's.
When a file with the 's' attribute set is deleted, its blocks are zeroed and written back to the disk. 
When a file with the 'S' attribute set is modified, the changes are written synchronously on the disk;
A file with the 't' attribute will not have a partial block fragment at the end of the file merged with other files (for those filesystems which support tail-merging). 
A directory with the 'T' attribute will be deemed to be the top of directory hierarchies for the purposes of the Orlov block allocator.
When a file with the 'u' attribute set is deleted, its contents are saved. 
The 'X' attribute is used by the experimental compression patches to indicate that the raw contents of a compressed file can be accessed directly.
The 'Z' attribute is used by the experimental compression patches to indicate a compressed file is dirty.




2016年8月11日 星期四

Rsyslog

修改20161007

產生hostname/年/月/日.log 檔案



參考:rsyslog-configuration
版本:Red Hat 4.4.7-17, rsyslog-5.8.10-10.el6_6.x86_64

目的

1.rsyslog server已運作.

2.產生log檔在 /var/log/rsyslog/${hostname}/${yesr}/${month}/${hostname}-${day}.log
外面傳來的log依照log的hostname在 /var/log/rsyslog下分開,並在分成年月資料夾,最後產生 hostname-日期.log,每天存一份.

3.使用舊式的設定,在/etc/rsyslog.d/test.conf設定兩個tamplate,一個tamplate是定義動態log檔案名稱,另一個是定義收到log message轉成文字檔時的格式.

設定

/etc/rsyslog.d/test.conf
$template DynFile,"/var/log/rsyslog/%HOSTNAME%/%$YEAR%/%$MONTH%/%HOSTNAME%-%$DAY%.log"
$template TraditionalFormat,"%timegenerated% %HOSTNAME% from ip:%fromhost-ip% %syslogtag%%msg:::drop-last-lf%\n"



*.* ?DynFile;TraditionalFormat
$template {templatename},"{format}"
templatename定義template名稱,format定義路徑檔名和輸出文字檔格式.

%HOSTNAME% %$YEAR% %timegenerated%
兩個%之間使用rsyslog Properties,如果多$是指rsyslog server系統上的參數.

from ip:%fromhost-ip%
我是使用原本舊有的範本,再加入遠端丟log的設備ip

*.* ?template1;template2
*.*第一個*是指所有服務,第二個*是指所有log的等級,?後面接template,也可以?前面加 - 代表輸出的log與rsyslog server時間同步,template1指存檔的路徑和檔名,template2只輸出的文字檔格式

寫完記得重啟服務 service rsyslog restart


驗證

可以看到有四台設備,其中一台已有兩天log,並記錄log來源的ip.


Rsyslog Listen

目的

預設rsyslog不會監聽port,修改config產生監聽。

實作


vim /etc/rsyslog.conf 

#$ModLoad imudp
#$UDPServerRun 514
#$ModLoad imtcp
#$InputTCPServerRun 514
把註解#去掉

重新啟動 service rsyslog restart

驗證




Filtering And Forwarding

目的

篩選和轉發rsyslog的資料.

Topology


juniper 的 hostname是R6,送出syslog給centos6.7
centos6.7收到R6封包,刪掉只有下指令的log,轉發給other linux

實作

Filtering


位置:centos6.7
centos6.7上已經可以接收R6的syslog

位置:centos6.7
: rawmsg, regex, ".*mgd.*" ~
這行代表,在rawmsg裡使用正規表示法搜尋有mgd的內容,然後刪掉

rawmsg代表整個封包接收到的內容
regex表示選擇使用正規表示法
".*mgd.*"表示正規表示法,有出現mgd都match
~表示match的都移除(如果沒有~,則只有math的會留下)


位置:juniper R6
在R6上,2016-10-07-17:33:37下show user perry指令

位置:centos6.7
可以看到時間上,沒有出現那個syslog,如果會出現就會像最後一行那樣


Forwarding

位置:centos6.7
*.* @192.168.20.199
表示轉寄給192.168.20.199
*.* 表示所有資訊所有等級
@表示使用utp,@@表示使用tcp
192.168.20.199表示轉發的ip,預設port是514,可以用192.168.20.199[:port number]


位置:other linux
可以找到R6的syslog

2016年8月7日 星期日

logrotate:使用有日期的資料夾

修改20160807
參考man logrotate(8)

介紹

logrotate是linux用來控制log檔案數量`大小`存放地點和檔案名稱.

logrotate不是一個daemon,他是靠cron每天執行的,可以參考下面設定檔順序

版本


Red Hat 4.4.7-17
logrotate-3.7.8-26.el6_7.x86_64

目的

把syslog檔案用年和月資料夾分開.


logrotate configation

預設log設定檔案
/etc/logrotate.conf


# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}


# system-specific logs may be also be configured here.
weekly:每週執行一次rotate.
其他還有dailymonthly, or yearly

rotate:rotate的次數
create:將會創造新的空白檔案放入新的log.

dateext:設定log檔名稱是YYYYMMDD.
另一個dateformat format_string 可以自訂格式.

#compress:ZIP壓縮檔案
include /etc/logrotate.d:指定後面要執行的logrotate

其他還有像minsize size 可以指定要被log檔案必須至少多少size,等等....

logrotate.conf是所有logrotate檔預設設定,在logrotate.conf定義後所有的logrotate檔都會執行,但如果個別的logrotate檔有定義參數,將使用個別的logrotate檔的設定.

個別log檔案設定
/etc/logrotate.d/syslog

/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
        daily
        olddir /var/log/syslog
        dateext
        rotate 4
    firstaction
       /bin/mkdir -p /var/log/syslog
    endscript
    lastaction
        /bin/mkdir -p /var/log/syslog/$(/bin/date '+%Y')/$(/bin/date '+%m');
        cd /var/log/syslog;
        /bin/cp $(/bin/ls /var/log/syslog|/bin/grep $(/bin/date '+%Y%m%d')) /var/log/syslog/$(/bin/date '+%Y')/$(/bin/date '+%m');
        cd -;
    endscript

    postrotate
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript

}
因為後面只想要驗證syslog的關係,所以也把預設有設定的東西也放在這裡.

以下參數跟endscript之間可以寫類似script的內容
firstaction:執行時間是執行所有log之前
lastaction:執行時間是執行所有log之後
prerotate:執行時間是執行單個log之前
postrotate:執行時間是執行單個log之後
注意:無法使用萬用字元,如果有更輕鬆的用法請告訴我


1./bin/mkdir -p /var/log/syslog 創一個專屬的資料夾
2.olddir /var/log/syslog 指定log檔的資料夾
3./bin/mkdir -p /var/log/syslog/$(/bin/date '+%Y')/$(/bin/date '+%m'); 創日期資料夾
4./bin/cp $(/bin/ls /var/log/syslog|/bin/grep $(/bin/date '+%Y%m%d')) /var/log/syslog/$(/bin/date '+%Y')/$(/bin/date '+%m'); copy已rotate的檔案過去

驗證

驗證要執行 logrotate -v /etc/logrotate.d/syslog,因為不知道logrotate的流程,我只要rotate一次他就不會再產生資料,就算我加入參數-v或刪掉/var/lib/logrotate.status裡面的資料也是一樣,這就會只要rotate錯一次就要等至少一天,我不太想這樣,所以只好用虛擬機的快照還原去測試.


可以看到原本/var/log/syslog沒有東西,執行完logrotate /etc/logrotate.d/syslog 後/var/log/syslog裡面就會出現東西,因為我是copy所以/var/log/syslog裡的log檔案可以保留最多四個,但是/var/log/syslog/2016/08裡的log檔案就不會被刪掉.

2016年7月18日 星期一

centos 6, nfdump and nfsen

修改20161007

版本:
Linux 2.6.32-642.3.1.el6.x86_64

Red Hat 4.4.7-17
nfdump-1.6.13
nfsen-1.3.6p1


更新下載檔案:
yum update

yum install -y httpd php wget gcc make rrdtool-devel rrdtool-perl perl-MailTools perl-Socket6 flex byacc

wget https://sourceforge.net/projects/nfdump/files/stable/nfdump-1.6.13/nfdump-1.6.13.tar.gz/download

wget https://sourceforge.net/projects/nfsen/files/stable/nfsen-1.3.6p1/nfsen-1.3.6p1.tar.gz

tar -zxvf /root/nfdump-1.6.13.tar.gz
tar -zxvf /root/nfsen-1.3.6p1.tar.gz

useradd -G apache -s /bin/false netflow
新增使用者,加入apache群組,無法用shell登入

防火牆設定:
iptables -I INPUT -p tcp  --dport 443 -j ACCEPT
iptables -I INPUT -p tcp  --dport 80 -j ACCEPT
iptables -I INPUT -p udp  --dport 9995 -j ACCEPT
iptables -I INPUT -p udp  --dport 9996 -j ACCEPT
service iptables save
開放http,https還有netflow流量預設port

selinux設定:
vim /etc/selinux/config
set SELINUX=disabled

reboot

nfdump安裝:
/root/nfdump-1.6.13/configure --enable-nfprofile --enable-nftrack --enable-sflow
cp /root/nfsen-1.3.6p1/etc/nfsen-dist.conf /root/nfsen-1.3.6p1/etc/nfsen.conf

cd /root/nfdump-1.6.13
make
make install
nfdump功能

nfsen安裝:
vim /root/nfsen-1.3.6p1/etc/nfsen.conf
$BASEDIR = "/home/netflow";  

$USER    = "netflow";

$WWWUSER  = "apache";
$WWWGROUP = "apache";

%sources = (
    'home'    => { 'port' => '9995', 'col' => '#0000ff', 'type' => 'netflow' },
    'internal'    => { 'port' => '9996', 'col' => '#FF0000', 'type' => 'netflow' },
#    'peer1'        => { 'port' => '9996', 'IP' => '172.16.17.18' },
#    'peer2'        => { 'port' => '9996', 'IP' => '172.16.17.19' },
);

/root/nfsen-1.3.6p1/install.pl /root/nfsen-1.3.6p1/etc/nfsen.conf
/home/netflow/bin/nfsen start

vim /etc/init.d/nfsen
#!/bin/bash
#
# chkconfig: - 50 50
# description: nfsen

DAEMON=/home/netflow/bin/nfsen
case "$1" in
start)
$DAEMON start
;;
stop)
$DAEMON stop
;;
status)
$DAEMON status
;;
restart)
$DAEMON stop
sleep 1
$DAEMON start
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
exit 0

chkconfig --add nfsen
chkconfig nfsen on

網頁設定:
vim /etc/httpd/conf.d/nfsen.conf
Alias /nfsen "/var/www/nfsen"

<Directory "/var/www/nfsen">
DirectoryIndex nfsen.php
Options -Indexes
AllowOverride None
Order allow,deny
Allow from all
</Directory>
service httpd restart

權限設定:
群組apache對${BASEDIR}/var/run/nfsen.conf必須有rw權限
我的${BASEDIR}是/home/netflow
所以home, netflow, var 和 run資料夾apache群組必須要有rx權限

使用windows系統用flowalyzer產生netflow流量
Flowalyzer NetFlow Generator2.0


http://{IP or FQDN}/nfsen/



2016年6月17日 星期五

AD 操作主機(operation master)


操作主機(Operation Master)

  • 專門用來變更資料庫內特殊資料,以避免衝突發生
  • 預設五部操作主機是在第一台DC上
  • 樹系範圍操作主機
    • 架構主機(Schema Master)
    • 網域命名主機(Domain Naming Master)
  • 網域範圍操作主機
    • RID主機(relative identifier operations master )
    • PDC模擬主機(The primary domain controller emulator operations master)
    • 基礎結構主機(Infrastructure Master)

PDC模擬主機(PDC Emulator)

  • 降低使用者變更密碼的時間延遲
  • 提供時間同步對時服務
  • 管理及變更GPO
  • 查詢操作主機:dsquery server  –hasfsmo PDC

PDC模擬主機對時服務

  • 主機設定對時伺服器
  • W32tm /config /update /manualpeerlist:”<對時伺服器>,<輪詢間格>”
  • 主機開始同步
  • W32tm /resync
  • 用戶端對時
  • Net time \\DCComputer /set /y

PDC操作主機失敗

RID主機

  • 負責管理RID
  • 每個使用者、電腦、群組會被分配一個SID,而這SID是由Domain SID和RID組成
  • 藉由RID主機從一個Domain移動物件到其他Domain
  • 查詢操作主機:dsquery server  –hasfsmo RID

RID操作主機失敗


基礎結構主機

  • 基礎結構主機主要用來維護本網域物件參考到其他網域物件時,ID的一致性
  • 查詢操作主機:dsquery server  –hasfsmo INFR

基礎結構主機失敗

架構主機(Schema Master)

  • 架構主機定義儲存在AD資料庫內的物件類別和屬性
  • 更新或新增Schema都必須透過架構主機
  • 查詢操作主機:dsquery server –forest –hasfsmo schema

架構主機失敗


網域命名主機(Domain Naming Master)

  • 負責AD樹系網域的新增和刪除
  • 查詢操作主機:dsquery server –forest –hasfsmo name

網域命名主機失敗

一般變更操作主機

PDC主機


PDC主機


基礎結構主機

架構主機

需先變更目前架構主機,選擇變更AD網域控制站

網域命名主機

強制指定操作主機

  • 操作主機損壞,無法回復
  • 無法使用一般轉移操作主機

  • 使用命令列:
    • Ntdsuit
    • Roles
    • Connections
    • Connect to server <需要被指定的server名稱>  
    • Quit
    • <各操作主機的強制指令>

強制指定實作

各操作主機的強制指令


From: https://technet.microsoft.com/en-us/library/cc816779(v=ws.10).aspx