本人小白第一次参加ctf比赛特在此记录
-
最初是想用python的os.system()或os.popen()来执行nc语句再做处理,后来想到直接用python的socket模块去连接对方服务器来取得数据,并用eval()函数将取得的字符串类型的式子执行得出答案,再发送回去即可。
连接服务器以及接收数据都没有问题,但是我发现怎么send都没反应(程序只会等待在那里,直到30秒时间到,抛出异常),搞了一整天都没想明白(我是真的菜)后来突发奇想会不会是要一个回车才能发送于是我就加一个‘\n’没想到成功了
(服务器发送给我们的一些python语句中会有一些使程序等待或退出的语句,将其替换即可)
import socket
import string
target_host = '202.38.95.46'
target_port = 12009
#建立socket对象,建立包含AF_INET,和SOCK_STREAM参数的socket对象。AF_INET参数锁门使用IPV4地址,SOCK_STREAM TCP客户端
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#连接客户端
client.connect((target_host, target_port))
#send some data
#client.send() #get some data
response = client.recv(1024).decode()
while(True):
response = client.recv(1024).decode()
print(response)
if("exit()"in response):
response = response.replace('exit()','0')
if("__import__('time').sleep(100)"in response):
response = response.replace("__import__('time').sleep(100)",'0')
if("__import__('os').system('find ~')"in response):
response = response.replace("__import__('os').system('find ~')",'0')
if('flag'in response):
break
print('edited',response)
result = str(eval(response))
#print(result)
buffer = result
buffer += '\n'
print('sended',result)
# 发送出去
client.send((buffer).encode())
#flag{'Life_1s_sh0rt_use_PYTH0N'*1000}