EXP入门 - requests & regex

KaldX

1. requests 库的使用

requests 库是一个用于发送HTTP请求的Python库。常用的操作:

1.1 发送GET请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import requests

url = "http://example.com"
response = requests.get(url)

# 打印响应对象
print(response)

# 打印状态码
print("Status Code:", response.status_code)

# 打印响应头
print("Headers:", response.headers)

# 打印响应体内容(字符串形式)
print("Text:", response.text)

1.2 发送POST请求

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

url = "http://example.com"
data = {"key": "value"}
response = requests.post(url, data=data)

# 打印响应对象
print(response)

# 打印状态码
print("Status Code:", response.status_code)

# 打印响应头
print("Headers:", response.headers)

# 打印响应体内容(字符串形式)
print("Text:", response.text)

1.3 常用属性和方法

  • response.status_code:返回HTTP状态码。
  • response.text:以字符串形式返回响应体内容。
  • response.content:以字节形式返回响应体内容。
  • response.json():如果响应内容是JSON格式,可以用这个方法将其解析为Python字典。
  • response.headers:以字典形式返回响应头。
  • response.url:返回最终的URL(考虑到重定向)。
  • response.history:返回重定向历史记录。
  • response.cookies:返回响应中的cookies。
  • response.encoding:返回或设置响应的文本编码。
  • response.elapsed:返回请求与响应之间的时间差。
  • response.raise_for_status():如果响应状态码不是200,抛出异常。

2. Python中正则表达式的书写

  • 正则表达式(Regular Expression,简称regex)用来匹配字符串的模式。

2.1 基本语法

  • .:匹配任意单个字符(除了换行符)。
  • *:匹配前面的字符0次或多次。
  • +:匹配前面的字符1次或多次。
  • ?:匹配前面的字符0次或1次。
  • {n}:匹配前面的字符恰好n次。
  • {n,}:匹配前面的字符至少n次。
  • {n,m}:匹配前面的字符至少n次,至多m次。
  • []:匹配括号内的任意一个字符。
  • |:匹配左边或右边的字符。
  • ^:匹配字符串的开头。
  • $:匹配字符串的结尾。
  • \:转义字符,用于匹配特殊字符。

2.2 使用原始字符串

在Python中,使用 r 前缀定义原始字符串可以避免反斜杠 \ 被解释为转义字符。

1
pattern = r"\d{3}-\d{2}-\d{4}"

2.3 非贪婪匹配

非贪婪匹配会尽可能少地匹配字符,通过在量词后面加上 ? 实现。

1
2
3
4
5
6
import re

pattern = r"a.*?b"
string = "a123b456b"
match = re.search(pattern, string)
print(match.group(0)) # 输出: a123b

2.4 示例:提取flag

1
2
3
4
5
6
7
8
9
10
11
import re

def flag(res):
# 使用正则表达式提取flag,匹配"FCTF{任意字符串}"
flag_pattern = r"FCTF\{.*?\}"
match = re.search(flag_pattern, res)
if match:
flag = match.group(0)
print(f"Flag: {flag}")
else:
print("Flag not found")

3. 综合示例

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
import requests
import re

def CTF_1():
url = "http://example.com/?year=2025"
headers = {
"User-Agent": "HappyNewYear",
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": "9",
"Referer": "http://example.com",
"Authorization": "Basic Uk9JUzpST0lT"
}
data = {
"team": "ROIS"
}
response = requests.post(url, headers=headers, data=data)
flag(response.text)

def CTF_2():
url = "http://example.com"
response = requests.post(url, allow_redirects=False)
flag(str(response.headers))

def flag(res):
flag_pattern = r"FCTF\{.*?\}"
match = re.search(flag_pattern, res)
if match:
flag = match.group(0)
print(f"Flag: {flag}")
else:
print("Flag not found")

if __name__ == "__main__":
print("CTF_1:")
CTF_1()
print("CTF_2:")
CTF_2()
  • Title: EXP入门 - requests & regex
  • Author: KaldX
  • Created at : 2025-03-20 09:00:00
  • Updated at : 2025-03-20 09:00:00
  • Link: https://blog.kaldx.com/2025/03/20/EXP入门 - requests & regex/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments