2015年8月28日 星期五

Block SSH Brute Force Attacks with IPTables

From : http://www.rackaid.com/blog/how-to-block-ssh-brute-force-attacks/

Detecting a SSH Brute Force Attack
If you are under a SSH brute force attack, you will likely see something like this in your logs.
1
2
3
4
5
6
7
8
9
10
Jan 26 03:46:02 host sshd[22731]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=125.39.22.154
Jan 26 03:46:02 host sshd[22731]: pam_succeed_if(sshd:auth): error retrieving information about user seymour
Jan 26 03:46:02 host sshd[22734]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.147.103.185 user=root
Jan 26 03:46:02 host sshd[22722]: Failed password for root from 61.147.103.185 port 16563 ssh2
Jan 26 03:46:02 host sshd[22723]: Received disconnect from 61.147.103.185: 11: Normal Shutdown, Thank you for playing
Jan 26 03:46:03 host sshd[22705]: Received disconnect from 61.147.103.185: 11: Normal Shutdown, Thank you for playing
Jan 26 03:46:03 host sshd[22726]: Failed password for invalid user madonna from 125.39.22.154 port 51706 ssh2
Jan 26 03:46:03 host sshd[22917]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=61.147.103.185 user=root
Jan 26 03:46:03 host sshd[22727]: Failed password for root from 61.147.103.185 port 16723 ssh2
Jan 26 03:46:03 host sshd[22729]: Failed password for invalid user florin from 125.39.22.154 port 54958 ssh2

This is a bot scanning your server trying to guess passwords.

 Methods to Stop SSH Brute Force Attacks

There are basically four approaches to dealing with SSH brute force attacks:
  • Restrict SSH access by IP address
  • Change SSH to another Port
  • Use intrusion prevention tools to dynamically block access
  • Rates limit SSH sessions using IPTables
All of these approaches have theirs benefits and drawbacks.
While restricting SSH access by IP address is the most secure method, such restrictions are often not possible when dealing with web hosting services as you have multiple users with constantly changing IP addresses.
Changing the SSH port may defeat bot scans but does little against targeted attacks.  Also, this usually just frustrates your users.
Intrusion prevention tools like fail2ban and denyhosts have their place but they are subject to log based attacks.  These tools essential analyze logs using regular expressions.  Hackers have found ways around both of these tools in the past.
Lastly, you have a great tool to block ssh brute force attacks right on your server: IPtables.

Using IPtables to Stop SSH Brute Force Attacks

I like to think of this approach similar to flow rates with pipes.  Bigger pipes allow more water to flow.  Smaller pipes can handle less water.
control ssh access with iptables
To block a SSH brute force attack, we just need to slow down the flow of requests. We can do this by rate-limiting requests to SSH with iptables.
Essentially, we create a smaller pipe for new SSH sessions.  This slows brute force attacks to a point where they become ineffective.
The iptables rules are relatively simple.
1
2
/usr/sbin/iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --set
/usr/sbin/iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent  --update --seconds 60 --hitcount 4 -j DROP
This rule will block an IP if it attempts more than 3 connections per minute to SSH. Notice that the state is set to NEW. This means only new connections not established ones are impacted. Established connections are the result of a successful SSHauthentication, so users who authenticate properly will not be blocked.
If you need to see what’s being done, you may want to log these drops. You can do so by setting up a log rule and then using these rules instead.
1
2
3
4
5
/sbin/iptables -N LOGDROP
/sbin/iptables -A LOGDROP -j LOG
/sbin/iptables -A LOGDROP -j DROP
iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --set
iptables -I INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent  --update --seconds 60 --hitcount 4 -j LOGDROP
Notice that I’ve changed the rule from DROP to LOGDROP. This way your drops will get logged and you can see the results in your logs:
1
2
3
4
Jan 27 08:22:29 server kernel: IN=eth1 OUT= MAC=00:30:48:94:fb:21:00:1b:00:00:00:00:00:00 SRC=118.103.140.3 DST=208.43.148.64 LEN=60 TOS=0x00 PREC=0x00 TTL=51 ID=16406 DF PROTO=TCP SPT=31003 DPT=22 WINDOW=5840 RES=0x00 SYN URGP=0
Jan 27 08:22:29 server kernel: IN=eth1 OUT= MAC=00:30:48:94:fb:21:00:1b:00:00:00:00:00:00 SRC=118.103.140.3 DST=192.168.1.1 LEN=60 TOS=0x00 PREC=0x00 TTL=51 ID=5076 DF PROTO=TCP SPT=45354 DPT=22 WINDOW=5840 RES=0x00 SYN URGP=0
Jan 27 08:22:35 server kernel: IN=eth1 OUT= MAC=00:30:48:94:fb:21:00:1b:00:00:00:00:00:00 SRC=118.103.140.3 DST=208.43.148.66 LEN=60 TOS=0x00 PREC=0x00 TTL=51 ID=37295 DF PROTO=TCP SPT=21077 DPT=22 WINDOW=5840 RES=0x00 SYN URGP=0
Jan 27 08:22:35 server kernel: IN=eth1 OUT= MAC=00:30:48:94:fb:21:00:1b:00:00:00:00:00:00 SRC=118.103.140.3 DST=208.43.148.64 LEN=60 TOS=0x00 PREC=0x00 TTL=51 ID=16408 DF PROTO=TCP SPT=31003 DPT=22 WINDOW=5840 RES=0x00 SYN URGP=0

Effectively Stopping SSH Brute Force Attacks

I always try to get a sense of effectiveness of any tool or configuration we deploy. I find many “security” tools that are popular among the web hosting crowd provide little to no value. In many cases, appropriate configuration of your server or web application could achieve similar results without the hassle of maintaining a third party product.
Are the IPTables rules effective? In short yes.
During a recent attack on a server, the SSH service remained fully accessible with no service interruption.
Previously such aggressive attack would have caused service interruptions. So on the service side, this approach works. When I dug into the logs, I found three failed user attempts against SSH prior to the rate-limiting kicked in. The attack then sent 67 more attempts before it gave up.

Benefits of Using IPtables to Block SSH Attacks

The benefit of this approach is you don’t need any added software. IPtables is likely sitting on your server already, so you can easily and quickly deploy this solution.
Also, there are no “ban lists” to maintain.  People forget passwords or incorrectly setup their SSH/SFTP programs.   As a result, they trigger a block and get locked out.  You then have to manually edit some ban list to remove them or whitelist IPs.   Over time or with multiple servers, this is a time-consuming server management tasks.  By using iptables, there’s no list to maintain — leaving you time to work on more important things.
One of the drawbacks is that this approach does not lock accounts. A slow, distributed attack could fall under the radar. If it was a directed attack against a specific user account, the attacker could churn away for days or weeks without detection. For that, you would need something that can lock user accounts after failures. PAM includes a module called pam_tally that does just this. If you fail too many times, an account is locked.

2015年5月4日 星期一

漫談802.11ac 新協定

From : http://www.cc.ntu.edu.tw/chinese/epaper/0024/20130320_2409.html
802.11ac提供下列的技術來提升網路頻寬與更好的使用者體驗:
1.支援更寬的頻寬(RF Bandwidth): 最高160 MHz(802.11n上限是 40 MHz)
2.支援最多 8空間串流(MIMO Spatial Streams)(802.11n僅支援4個)
3.多使用者的 MIMO (Multi-user MIMO) (802.11n 無此功能)
4.傳送波束成型正式納入標準(Beam forming) (802.11n 非標準功能)
5.支援高密度的解調變(Modulation): 256 QAM (802.11n 最高 64-QAM)
1. 支援更寬的頻寬(RF Bandwidth): 最高160 MHz
802.11ac Draft預計使用5 GHz RF頻帶(4.9 ~ 6.0 GHz),主要原因在於802.11ac有較寬的頻寬(RF Bandwidth)需求。
以美國地區為例, 2.4GHz 能用的範圍僅有2.4~ 2.462 GHz, 以5MHz 區分一個 Channel,共有 11 個Channels 如下:

Channel
1
2
3
4
5
6
7
8
9
10
11
Frequency
(MHz)
2412
2417
2422
2427
2432
2437
2442
2447
2452
2457
2462
雖然有 11個Channels可用,若以 802.11b 為例,所需頻寬RF Bandwidth: 22MHz,因此僅有三個不會互相干擾之 Channel 存在。

(圖片來源:http://en.wikipedia.org/wiki/List_of_WLAN_channels)
這也就是一般在無線網路建置中,在一個空間中,若無線 AP 僅支援 2.4GHz 802.11 b/g/n,則建議最多佈建三台,且三台 AP 各設定使用 Channel 1/6/11,才能有互不干擾之最佳效果。除了無線網路使用於 2.4 GHz頻帶,藍芽、家用無線電話都在使用,甚至連微波爐都可能會在這個頻帶內。而5GHz在美國地區能用的範圍有 5.180~5.850GHz,以5MHz 區分一個 Channel,可用的 Channels 有 36~165,因此才能容納 802.11ac 最高 160 MHz 之頻寬要求。但5GHz也不是完全沒有缺點,因為頻率越高,波長越短,繞射(diffraction)程度越低,也就是遇到障礙不容易繞過,因此在相同功率上之有效傳輸距離會較 2.4GHz 來的短。
802.11ac 所需160 MHz 之頻寬可利用通道集成技術 (Channel Bonding)來達成,也就是可使用連續Contiguous 80+80 MHz 或非連續 Discontinuous 80+80 MHz,使總頻寬達到160 MHz。下表為在單一空間流使用不同頻寬 (Bandwidth) 在 802.11n 與 802.11ac 之理論傳輸速率:
Protocol
Bandwidth
(MHz)
Data rate per stream
(Mbps)
802.11n
(64-QAM)
20
72.2
40
802.11ac
(256-QAM)
20
40
80
160

2. 支援最多 8空間流(MIMO Spatial Streams)MIMO 是 Multi-input Multi-output 之縮寫,可用此法表示:
T x R:S
發射天線數量 x 接收天線數量:空間流數
例如:3x3:3 MIMO
表示有三個發射天線與三個接收天線,共提供三個空間流(Spatial Streams)。
在企業方案所提供之無線解決方案,也會看到如 MIMO: 4x4:3,表示有四個發射天線與四個接收天線,卻僅提供三個空間流數量,其優點在於使用N+1 的冗餘收發器,可針對信號衰減和硬體故障提供有效保護,使三個空間流之性能和覆蓋範圍更大且更穩定。
802.11n 40 MHz Bandwidth (64-QAM),使用多個空間流之理論傳輸速率:
空間流
Spatial Streams
1
2
3
4
傳輸速率Mbps
150
300
450
600
802.11ac 40 MHz Bandwidth (256-QAM),使用多個空間流之理論傳輸速率:
空間流
Spatial Streams
1
2
3
4
5
6
7
8
傳輸速率Mbps
200
400
600
800
1000
1200
1400
1600

3.多使用者的 MIMO (Multi-user MIMO) Multi-user MIMO 是一種新的技術,其優點在於多個終端設備同時連上 AP 時,每個裝置可獨立使用不同的空間流(Spatial Streams)傳輸資料,進而減少競爭,此種技術又稱為 SDMA (Space Division Multiple Access)。
例如一個無線 AP 使用 4 x 4: 4 Mu-MiMo可對2個連上的終端設備同時單獨進行通訊。而現行的 802.11n MiMo裝置只能在同一時間服務一個終端設備之多重天線,無線 AP必須以時間多工服務多個終端設備。
4.傳送波束成型(Beam forming) 正式納入標準Beam forming (波束成型)技術已經正式納入 802.11ac 之標準,雖然在 802.11n 已有多家廠商提供此技術,但因為當時為非標準規格,因此各廠商在實作上可能存在相容性問題。
所謂的Beam forming技術在於使用單一聲測(Single sounding)與反饋格式(相較於802.11n的多重聲測與反饋格式),而在特定方向集中射頻(RF)能量,以改善到個別終端設備之傳輸效率。
5.支援高密度的解調變(Modulation): 256-QAM 802.11ac使用與802.11n 相同之 OFDM(正交分頻多工)作為調變與編碼技術,也相同要求裝置能夠支援BPSK、QPSK、16-QAM與64-QAM,但802.11ac額外增加256-QAM(3/4 or 5/6 Coding Rate)之調變方式,256-QAM的好處在於提供比64-QAM更大33% 之傳輸流量。不過256-QAM僅允許較低的位元錯誤容許誤差,因此較適用於無干擾之通訊環境中。
下表是使用各種調變(Modulation)技術在頻寬 40 MHz with 400 ns GI 使用單一空間流之理論傳輸速率:
Modulation
Coding rate
傳輸速率Mbps
BPSK
1/2
15
QPSK
1/2
30
QPSK
3/4
45
16-QAM
1/2
60
16-QAM
3/4
90
64-QAM
2/3
120
64-QAM
3/4
135
64-QAM
5/6
150
*256-QAM
3/4
180
*256-QAM
5/6
200
另一個與802.11n 之差異在於 802.11n 支援「不同」調變,例如一位使用者可能在一空間流上接收BPSK調變信號,及在另一空間流上接收16QAM 調變信號。但802.11ac只支援「相同」調變,因為此特性證明在市場中不會成功(很少802.11n 裝置實際支援此功能),所以IEEE決定放棄支援「不同」調變。
總結
下表整理 802.11ac 在單一空間流中使用不同頻寬 Bandwidth與不同調變 Modulation 之理論傳輸速率 Mbps:
Modulation
Coding rate
20 MHz channels
40 MHz channels
80 MHz channels
160 MHz channels
800 ns GI
400 ns GI
800 ns GI
400 ns GI
800 ns GI
400 ns GI
800 ns GI
400 ns GI
BPSK
1/2
6.5
7.2
13.5
15
29.3
32.5
58.5
65
QPSK
1/2
13
14.4
27
30
58.5
65
117
130
QPSK
3/4
19.5
21.7
40.5
45
87.8
97.5
175.5
195
16-QAM
1/2
26
28.9
54
60
117
130
234
260
16-QAM
3/4
39
43.3
81
90
175.5
195
351
390
64-QAM
2/3
52
57.8
108
120
234
260
468
520
64-QAM
3/4
58.5
65
121.5
135
263.3
292.5
526.5
585
64-QAM
5/6
65
72.2
135
150
292.5
325
585
650
256-QAM
3/4
78
86.7
162
180
351
390
702
780
256-QAM
5/6
N/AN/A
180
200
390
433.3
780
866.7
(資料來源: http://en.wikipedia.org/wiki/IEEE_802.11ac)
因此若802.11ac 使用最高 160 MHz Bandwidth,與最佳之調變 256-QAM,在8個空間流之情況下,最高可達 6.93 Gbps之理論傳輸速率
802.11ac協定除了上述五個新的特性之外,能與現有802.11n相容也是十分重要的,因此 802.11ac提供與802.11a和802.11n裝置在5 GHz頻帶之相容性。表示802.11ac能與支援802.11a和802.11n技術的裝置互動;802.11ac訊框結構可容納與802.11 a和802.11n裝置的傳輸。
參考資料1.IEEE 802.11: Wireless LAN Medium Access Control (MAC) and Physical Layer (PHY) Specifications
2."Wi-Fi (wireless networking technology)". Encyclopadia Britannica.
3.DIGITIMES中文網: 即將邁入802.11ac的世代所面臨的測試挑戰
4.http://en.wikipedia.org/wiki/Guard_interval
5.http://en.wikipedia.org/wiki/IEEE_802.11n-2009
6.http://en.wikipedia.org/wiki/IEEE_802.11ac
7.http://zh.wikipedia.org/wiki/IEEE_802.11
8.Cisco無線網路解決方案 http://www.cisco.com/web/TW/products/wireless/index.html

2015年4月21日 星期二

在Windows中建立任意大小的檔案

要產生任意大小的檔案在 Linux 下面有 dd 可以用
如果在 Windows 下面則可以用 fsutil 這個程式達成
這個工具 Windows XP 本身就有附帶了
開始 -> 執行 -> cmd
到文字介面模式後就可以使用了
如果要在 C:\ 產生的一個 1MB 的檔案就使用下面這行指令
fsutil file createnew C:\aa.txx 1024000
from:http://www.minitw.com/archives/pcskill/in-windows-create-any-size-file.htm?variant=zh-tw

2014年8月20日 星期三

Band Steering

當2.4GHz 與 5GHz 的網路同時存在時,自動使支援 5GHz 的用戶端連線至 5GHz 的網路以提升效能。






IEEE 802.11s

*  http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11s

*  https://github.com/cozybit/open80211s/wiki

* http://www.ieee802.org/802_tutorials/06-November/802.11s_Tutorial_r5.pdf

How to repair and clone disk with ddrescue

  ddrescue  is a tool that can be used to repair and clone disks on a  Linux system . This includes hard drives, partitions, DVD discs, flas...