kingshard SQL黑名單功能介紹

2021-09-19 10:41:48 字數 3151 閱讀 5290

在kingshard開源之後,有使用者多次提到能不能在kingshard中加入sql黑名單機制,讓kingshard能夠根據特定的規則來攔截在黑名單中的sql。有幾個比較典型的應用場景:

dba定義一些比較危險的sql,放在sql黑名單檔案中。可以避免前端應用發過來的sql對資料庫造成危害。這種sql有可能是開發者粗心編寫的,也有可能是被sql注入生成的sql。例如:delete from mytable,這種不帶where條件的sql,會把整個表刪除。

在kingshard專案上線後,通過log發現存在大量某條sql給db造成了很大的壓力。這時候可以動態地將這條sql加入黑名單,阻止該sql的執行,從而使資料庫壓力降低。例如:select count(*) from mytable where ***x,這類sql如果沒有優化得當,是很容易造成系統的io過高的。

在kingshard如果想使用sql黑名單功能,只需要在配置中新增:

blacklist_sql_file: /users/flike/blacklist
然後我們在blacklist定義sql黑名單,這樣kingshard在**的時候,就會阻止黑名單中sql的**。

黑名單sql以正規表示式的形式定義。對於sql中的值用??+代替。為保證黑名單有效,最好手動驗證一下,kingshard是否正確攔截了黑名單中的sql。定義規則(上一條是原sql,對應的下一條是黑名單形式的sql)可以參考下列例子:

select c from t where id=1

select c from t where id=?

select * from prices.rt_5min where id=1

select * from prices.rt_5min where id=?

select null, 5.001, 5001. from foo

select ?, ?, ? from foo

select 'hello', '\nhello\n', \"hello\", '\\'' from foo

select ?, ?, ?, ? from foo

select 'hello'\n

select ?

select * from t where (base.nid in ('1412', '1410', '1411'))

select * from t where (base.nid in(?+))

select * from foo where a in (5) and b in (5, 8,9 ,9 , 10)

select * from foo where a in(?+) and b in(?+)

select * from foo limit 5

select * from foo limit ?

select * from foo limit 5, 10

select * from foo limit ?, ?

select * from foo limit 5 offset 10

select * from foo limit ? offset ?

insert into t (ts) values (now())

insert into t (ts) values(?+)

insert into foo(a, b, c) values(2, 4, 5)

insert into foo(a, b, c) values(?+)

call foo(1, 2, 3)

call foo

load data infile '/tmp/foo.txt' into db.tbl

load data infile ? into db.tbl

administrator command: init db

administrator command: init db

use `foo`

use ?

在blacklist加入如下sql:

select count(*) from test_shard_hash where id > ?

select count(*) from test_shard_range

select * from world

delete from world

連線kingshard,執行sql顯示如下:

mysql> select * from world;

error 1105 (hy000): sql in blacklist.

mysql> select * from world where a > 0;

+------+------+

| a | b |

+------+------+

| 10 | 23 |

| 45 | 565 |

+------+------+

2 rows in set (0.00 sec)

mysql> delete from world;

error 1105 (hy000): sql in blacklist.

mysql> delete from world where a =10;

query ok, 1 row affected (0.00 sec)

#注意在sql黑名單中該sql是大於後面有個空格,必須要嚴格匹配,否則#kingshard不會認為是黑名單sql

mysql> select count(*) from test_shard_hash where id >1;

+----------+

| count(*) |

+----------+

| 24 |

+----------+

1 row in set (0.02 sec)

mysql> select count(*) from test_shard_hash where id > 1;

error 1105 (hy000): sql in blacklist.

用sysbench測試了一下存在blacklist時kingshad的效能,發現效能並沒有明顯下降,所以可以放心使用該功能。

asp 檢查黑名單 黑名單驗證的突破

黑名單驗證的突破 作業系統特性 windows下對大小寫不敏感,php和php對windows來說都是一樣的。windows下,上傳的檔名中帶有test.php data會在伺服器生產乙個test.php的檔案並可以被解析。及 test.php test.php data windows下檔名結尾加...

aws 黑名單功能

如果你想授權某段ip訪問,那麼可以用安全組的白名單功能,但是安全組不提供黑名單功能。如果我們的某個服務受某個ip攻擊,那麼此時可以選擇網路acl對其進行黑名單限制,操作步驟如下 1.登入aws,選擇vpc 2.vpc裡有個網路acl,當然事先你要找到你服務對應的vpc 3.選擇編輯入站規則,入站規則...

Linux nginx 黑名單配置

進入nginx配置資料夾下,建立blacklist.conf黑名單配置檔案,編輯blacklist.conf cd usr local nginx conf touch blacklist.conf vi blacklist.conf 寫入要攔截的ip位址,儲存並退出 編輯當前目錄下nginx.co...