kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

sql中exists和except用法介绍,代替in

https://www.jb51.net/article/230899.htm

一、exists

相对于 inner join,exists 性能要好一些,当它找到第一个符合条件的记录时,就会立即停止搜索返回 TRUE。

EXISTS = IN,意思相同不过语法上有点点区别,好像使用 IN 效率要差点,应该是不会执行索引的原因。

1.1 说明

EXISTS(包括 NOT EXISTS)子句的返回值是一个 BOOL 值。EXISTS 内部有一个子查询语句(SELECT … FROM…),我将其称为 EXIST 的内查询语句。其内查询语句返回一个结果集。

EXISTS 子句根据其内查询语句的结果集空或者非空,返回一个布尔值。

exists:强调的是是否返回结果集,不要求知道返回什么,

比如:

1
select name from student where sex = 'm' and mark exists(select 1 from grade where ...)

只要 exists 引导的子句有结果集返回,那么 exists 这个条件就算成立了,大家注意返回的字段始终为 1,如果改成

1
select 2 from grade where ...

那么返回的字段就是 2,这个数字没有意义。

所以 exists 子句不在乎返回什么,而是在乎是不是有结果集返回。

1.2 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
--EXISTS
select name from family_member where group_level > 0
	and exists(select 1 from family_grade where family_member.name = family_grade.name and grade > 90)

--result:
name
cherrie

--NOT EXISTS
select name from family_member where group_level > 0
	and not exists(select 1 from family_grade where family_member.name = family_grade.name and grade > 90)

--result:
name
mazey
rabbit

1.3 intersect/2017-07-21

intersect 的作用与 exists 类似。

1
2
3
4
5
6
7
--intersect
  select name from family_member where group_level > 0
      intersect select name from family_grade where grade > 90

  --result:
  name
  cherrie

二、except

2.1 说明

查询结果上 EXCEPT = NOT EXISTS,INTERSECT = EXISTS,但是 EXCEPT/INTERSECT 的「查询开销」会比 NOT EXISTS/EXISTS 大很多。

except 自动去重复,not in/not exists不会。

2.2 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
--except
select name from family_member where group_level > 0
	except(select name from family_grade)

--result:
name
rabbit

--NOT EXISTS
select name from family_member where group_level > 0
	and not exists(select name from family_grade where family_member.name = family_grade.name)

--result:
name
rabbit
rabbit

三、测试数据

其中验证 except 去重复功能时在 family_member 中新增一个 rabbit。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
-- ----------------------------
-- Table structure for family_grade
-- ----------------------------
DROP TABLE [mazeytop].[family_grade]
GO

CREATE TABLE [mazeytop].[family_grade] (
	[id] int NOT NULL ,
	[name] varchar(20) NULL ,
	[grade] int NULL
)
GO

-- ----------------------------
-- Records of family_grade
-- ----------------------------
INSERT INTO [mazeytop].[family_grade] ([id], [name], [grade]) VALUES (N'1', N'mazey', N'70')
GO
GO

INSERT INTO [mazeytop].[family_grade] ([id], [name], [grade]) VALUES (N'2', N'cherrie', N'93')
GO
GO

-- ----------------------------
-- Table structure for family_member
-- ----------------------------
DROP TABLE [mazeytop].[family_member]
GO

CREATE TABLE [mazeytop].[family_member] (
	[id] int NOT NULL ,
	[name] varchar(20) NULL ,
	[sex] varchar(20) NULL ,
	[age] int NULL ,
	[group_level] int NULL
)
GO

-- ----------------------------
-- Records of family_member
-- ----------------------------
INSERT INTO [mazeytop].[family_member] ([id], [name], [sex], [age], [group_level]) VALUES (N'1', N'mazey', N'male', N'23', N'1')
GO
GO
INSERT INTO [mazeytop].[family_member] ([id], [name], [sex], [age], [group_level]) VALUES (N'2', N'cherrie', N'female', N'22', N'2')
GO
GO
INSERT INTO [mazeytop].[family_member] ([id], [name], [sex], [age], [group_level]) VALUES (N'3', N'rabbit', N'female', N'15', N'3')
GO
GO
INSERT INTO [mazeytop].[family_member] ([id], [name], [sex], [age], [group_level]) VALUES (N'4', N'rabbit', N'female', N'15', N'3')
GO
GO

-- ----------------------------
-- Table structure for family_part
-- ----------------------------
DROP TABLE [mazeytop].[family_part]
GO

CREATE TABLE [mazeytop].[family_part] (
	[id] int NOT NULL ,
	[group] int NULL ,
	[group_name] varchar(20) NULL
)
GO

-- ----------------------------
-- Records of family_part
-- ----------------------------
INSERT INTO [mazeytop].[family_part] ([id], [group], [group_name]) VALUES (N'1', N'1', N'父亲')
GO
GO
INSERT INTO [mazeytop].[family_part] ([id], [group], [group_name]) VALUES (N'2', N'2', N'母亲')
GO
GO
INSERT INTO [mazeytop].[family_part] ([id], [group], [group_name]) VALUES (N'3', N'3', N'女儿')
GO
GO

-- ----------------------------
-- Indexes structure for table family_grade
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table family_grade
-- ----------------------------
ALTER TABLE [mazeytop].[family_grade] ADD PRIMARY KEY ([id])
GO

-- ----------------------------
-- Indexes structure for table family_member
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table family_member
-- ----------------------------
ALTER TABLE [mazeytop].[family_member] ADD PRIMARY KEY ([id])
GO

-- ----------------------------
-- Indexes structure for table family_part
-- ----------------------------

-- ----------------------------
-- Primary Key structure for table family_part
-- ----------------------------
ALTER TABLE [mazeytop].[family_part] ADD PRIMARY KEY ([id])
GO

php字符串压缩, MySQL中的BLOB类型

压缩后的字符串是bin类型, mysql不能用char、varchar、text存储


https://blog.51cto.com/u_15470226/5185727

在PHP中偶尔遇到字符串的压缩,比如一个长字符串,数据库开始设计的字段存不下,但是又不想改数据库字段存储长度,就可以用压缩的方式降低数据字段字符串的长度数量级,把几百个字符的字符串压缩到几十个字符。总结下来有以下几个:

压缩函数:gzcompress gzdeflate gzencode

与之对应的解压函数如下:

解压函数:gzuncompress gzinflate gzdecode

特别注意:gzdecode是PHP 5.4.0之后才加入的,使用的时候要注意兼容性问题。

gzcompress gzdeflate gzencode函数的区别在于它们压缩的数据格式不同:

gzcompress使用的是ZLIB格式;

gzdeflate使用的是纯粹的DEFLATE格式;

gzencode使用的是GZIP格式;

1
2
3
4
5
6
7
8
9
10
11
<?php
$test_string="123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789123456789";
var_dump($test_string);//原字符串长度

var_dump(gzencode($test_string));//gzencode压缩后
var_dump(gzcompress($test_string));//gzcompress压缩后
var_dump(gzdeflate($test_string));//gzdeflate压缩后

var_dump(gzdecode(gzencode($test_string)));//gzencode压缩后再解压缩回来
var_dump(gzuncompress(gzcompress($test_string)));//gzcompress压缩后再解压缩回来
var_dump(gzinflate(gzdeflate($test_string)));//gzdeflate压缩后再解压缩回来

https://blog.csdn.net/weixin_42408447/article/details/117412778

一.BLOB介绍

  BLOB (binary large object),二进制大对象,是一个可以存储二进制文件的容器。在计算机中,BLOB常常是数据库中用来存储二进制文件的字段类型。BLOB是一个大文件,典型的BLOB是一张图片或一个声音文件,由于它们的尺寸,必须使用特殊的方式来处理(例如:上传、下载或者存放到一个数据库)。根据Eric Raymond的说法,处理BLOB的主要思想就是让文件处理器(如数据库管理器)不去理会文件是什么,而是关心如何去处理它。但也有专家强调,这种处理大数据对象的方法是把双刃剑,它有可能引发一些问题,如存储的二进制文件过大,会使数据库的性能下降。在数据库中存放体积较大的多媒体对象就是应用程序处理BLOB的典型例子。

二.mysql BLOB类型

  MySQL中,BLOB是个类型系列,包括:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的唯一区别是在存储文件的最大大小上不同。

三.MySQL的四种BLOB类型

1
2
3
4
5
  类型         大小(单位:字节)
  1.TinyBlob   最大  255
  2.Blob       最大  65K
  3.MediumBlob 最大  16M
  4.LongBlob   最大  4G

四.配置修改

在BLOB中存储大型文件,MYSQL提供了很强的灵活性!允许的最大文件大小,可以在配置文件中设置。

通过etc/my.cnf

1
2
[mysqld]
max_allowed_packet = 10M

nginx解决sql注入

https://blog.csdn.net/A_Apprentice/article/details/125861741

  1. get请求好处理

  2. post请求 由于需要拿到请求体,需要安装lua插件支持

errlog

1
2
3
ngx.log(ngx.ERR, "error: ", body)

tail -f /var/log/nginx/error.log

当前方案 :

get在server级别处理

post在lication级别处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
	# 文件上传的限制
	client_max_body_size 100m;

	if ($query_string ~* ".*('|--|union|insert|drop|truncate|update|(%20)from|grant|(%20)where|(%20)select|(%20)and|(%20)chr|(%20)mid|like|(%20)iframe|(%20)script|alert|webscan|dbappsecurity|style|WAITFOR|confirm|innerhtml|innertext|class).*") { return 403; }
        #if ($uri ~* (.*)(insert|select|delete|update|count|master|truncate|declare|\*|%|\')(.*)$ ) { return 403; }
        if ($http_user_agent ~ ApacheBench|WebBench|Jmeter|JoeDog|Havij|GetRight|TurnitinBot|GrabNet|masscan|mail2000|github|wget|curl) { return 444; }
        if ($http_user_agent ~ "Go-Ahead-Got-It") { return 444; }
        if ($http_user_agent ~ "GetWeb!") { return 444; }
        if ($http_user_agent ~ "Go!Zilla") { return 444; }
        if ($http_user_agent ~ "Download Demon") { return 444; }
        if ($http_user_agent ~ "Indy Library") { return 444; }
        if ($http_user_agent ~ "libwww-perl") { return 444; }
        if ($http_user_agent ~ "Nmap Scripting Engine") { return 444; }
        if ($http_user_agent ~ "Load Impact") { return 444; }
        if ($http_user_agent ~ "~17ce.com") { return 444; }
        if ($http_user_agent ~ "WebBench*") { return 444; }
        if ($http_referer ~* 17ce.com) { return 444; }
        if ($http_user_agent ~* qiyunce) { return 444; }
        if ($http_user_agent ~* YunGuanCe) { return 403; }
        if ($http_referer ~* WebBench*") { return 444; }
        if ($http_user_agent ~ "BLEXBot") { return 403; }
        if ($http_user_agent ~ "MJ12bot") { return 403; }
        if ($http_user_agent ~ "semalt.com") { return 403; }
        if ($http_user_agent ~ "sqlmap") { return 403; }

        #自动防护
	if ($request_uri ~* \.(htm|do)\?(.*)$) {
            set $req $2;
        }
        if ($req ~* "(cost\()|(concat\()") {
            return 503;
        }
        if ($req ~* "union[+|(%20)]") {
            return 503;
        }
        if ($req ~* "and[+|(%20)]") {
            return 503;
        }
        if ($req ~* "select[+|(%20)]") {
            return 503;
        }


        #溢出过滤
        if ($query_string ~ "(<|%3C).*script.*(>|%3E)") { return 403; }
        if ($query_string ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") { return 403; }
        if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") { return 403; }
        if ($query_string ~ "proc/self/environ") { return 403; }
        if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3D)") { return 403; }
        if ($query_string ~ "base64_(en|de)code\(.*\)") { return 403; }

        #文件注入禁止
        if ($query_string ~ "[a-zA-Z0-9_]=http://") { return 403; }
        if ($query_string ~ "[a-zA-Z0-9_]=(\.\.//?)+") { return 403; }
        if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") { return 403; }

        location / {

            lua_need_request_body on;
            access_by_lua_block {
                local body = ngx.var.request_body
                if ngx.var.request_method == "POST" and body ~= nil then
                    local regex0 = "(.*?((sqlwhere)|(c0-param0)).*?){1,}"
                    local m0 = ngx.re.match(body, regex0, "i")
                    local regex1 = "(.*?((insert)|(drop)|(truncate)|(update)|(grant)|(chr)|(webscan)|(dbappsecurity)|(WAITFOR)).*?){1,}"
                    local m1 = ngx.re.match(body, regex1, "i")
                    local regex2 = "(.*?((union)|(insert)|(drop)|(truncate)|(grant)|(chr)|(iframe)|(alert)|(webscan)|(dbappsecurity)|(style)|(WAITFOR)|(confirm)|(innerhtml)|(innertext)|(class)).*?){1,}"
                    local m2 = ngx.re.match(body, regex2, "i")
                    if (m0 and m1) or (not m0 and m2) then
                        ngx.log(ngx.ERR, "error: ", body)
                        ngx.status = 403
                        ngx.say('{"code": 403, "msg": "非法参数","ok": false,"runningTime": "0ms"}')
                    end
                end

                ngx.req.read_body()
                local args, err = ngx.req.get_post_args()
                if args then
                    for k, v in pairs(args) do
                       if k == "j_username" or k == "j_password" then
                           local regex = "(.*?((union)|(insert)|(drop)|(truncate)|(update)|(from)|(grant)|(where)|(select)|(chr)|(mid)|(like)|(iframe)|(script)|(alert)|(webscan)|(dbappsecurity)|(style)|(WAITFOR)|(confirm)|(innerhtml)|(innertext)|(class)).*?){1,}"
                           local m = ngx.re.match(v, regex, "i")
                           if m then
                              ngx.log(ngx.ERR, "error: ", v)
                              ngx.status = 403
                              ngx.say('{"code": 403, "msg": "非法参数","ok": false,"runningTime": "0ms"}')
                           end
                       end
                   end
                end
            }


            proxy_http_version 1.1;
            proxy_set_header Connection "";

            proxy_next_upstream http_502 error timeout invalid_header;
            proxy_pass http://192.168.100.199:8888;
            proxy_set_header Host $http_host;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        #自动防护
	if ($request_uri ~* \.(htm|do)\?(.*)$) {
            set $req $2;
        }
        if ($req ~* "(cost\()|(concat\()") {
            return 503;
        }
        if ($req ~* "union[+|(%20)]") {
            return 503;
        }
        if ($req ~* "and[+|(%20)]") {
            return 503;
        }
        if ($req ~* "select[+|(%20)]") {
            return 503;
        }

1、这里之所以使用$request_uri而未使用$query_string变量,因为通过$request_uri进行rewrite分割更精准。

2、%20代表的是空格,同上文不的是,我这里把上面的空格匹配进行了取消。这样像www.361way.com/aaa.do?select * from test之样的也可以进行匹配。

3、上面的htm是伪静态,实际上同.do一样,也是动态文件。为了便于和静态文件进行区分,这里选择了htm而不是html。

4、注意,最上面的url里面的\? ,这个也分重要。如果没有的话,www.361way.com/aaa.htm select * from test不会被过滤,而www.361way.com/aaa.htm?select * from test会被过滤。如果想将前面的也过滤,只需要把\? 取消即可。

https://blog.csdn.net/remotesupport/article/details/11967851

https://blog.csdn.net/qq_34777982/article/details/125390989

http://www.3qphp.com/linux/centos/2581.html