I have been learning PHP before, but I did not expect to study python .. I am sorry to have read some basic syntaxes and system programming, and the thread descriptions in Linux.
Question 1: The flag is base64 encoded and then encrypted to obtain the ciphertext: ivubnjmwax5wq29zxteknyde. The displacement of shift encryption is unknown. Compile the script to obtain the flag.
A simple question: Save the result after the shift, and then use the function in the built-in base64 template to directly decode it. It is worth mentioning that python does not support the character (char) type, therefore, a lot of operations are more difficult than the C language (maybe I do not know much about strings in Python =), use for traversal to get every character using ord () the function obtains the ASC code, and then uses the CHR () function to return characters. Complete the shift operation.
The following is the code (Python version is 2.7.6 ):
#usr/bin/env python"8.6 exam1"import base64AIMSTR=‘IVUBNJmwAx5Wq29zXTekNyde‘FILENAME=‘/home/al/Desktop/res.txt‘def movword(n,a):"single letter"temp=ord(a)if temp>47 and temp<58:return chr(temp)i=0while i<n:temp+=1if temp==91:temp =65if temp==123:temp =97i+=1return chr(temp)def movstr(n,str):"for each word in str mov n forward"temp=""for ch in str:ch=movword(n,ch)temp+=chreturn tempfileobj=open(FILENAME,"w")temp=AIMSTRfor i in range(24):temp=movstr(1,temp)temp2=base64.b64decode(temp)print temp2fileobj.writelines(‘%s\n‘%temp2)fileobj.close()print ‘Done!‘
Question 2: design a port scanner controlled by the thread pool and use the socket connect method. After specifying the port, enable thread pool scheduling. After scanning, put the result into the message queue and enable the print thread to print the message. Scanning and printing are required to be parallel.
Multi-thread programming and Message Queue implementation have been discussed in the previous blog. I will not repeat them here. I will mainly summarize web programming, that is, the use of socket templates and sockets.
This section describes how to create a TCP server.
First, create a socket object. The first parameter is the socket type. Here we use af_inet, which is a network-based socket. Bind the socket address using the BIND () built-in function. The address is composed of a host and a port. If the host is set to null, the socket address can be bound to all valid addresses, select an unused port.
Then the socket object will listen to the data sent from this port, and use the built-in Recv () to passively accept the data sent and store it in a variable, or directly put it in the message list.
Next, you just need to keep this Recv () function in the active state and constantly listen for incoming data.
Put the received data into the Message Queue Q, and then read the messages in Q from the thread where the reader () function is located. Emphasize that Q should be given to reader. add a non-zero parameter block to the get () function, so that messages in the queue can be suspended instead of always outputting spaces.
The Code is as follows:
from socket import *from time import *from threading import Threadfrom Queue import QueueHOST=‘‘PORT=9011BUFSIZ=1024ADDR=(HOST,PORT)#the attribute of socketdef listener(queue):tcp=socket(AF_INET,SOCK_STREAM)tcp.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)tcp.bind(ADDR)tcp.listen(0)print ‘waiting for connection....‘tcp,addr=tcp.accept()print ‘...connected from:‘,addrwhile True :data = tcp.recv(BUFSIZ)if not data:breakqueue.put(‘[%s] %s‘%(ctime(),data),1)#print ‘[%s] %s‘%(ctime(),data)tcp.close()def reader(queue):while True:data=queue.get(1)print datafuncs=[listener,reader]nfuncs=range(len(funcs))threads=[]q=Queue(32)for i in nfuncs:t=Thread(target=funcs[i],args=(q,))threads.append(t)for i in nfuncs:threads[i].start()for i in nfuncs:threads[i].join()
Finally, let's talk about the problems encountered during code writing.
First, if the socket object is prematurely closed, an error with no write permission will be generated when the Recv () function is called.
Second, The args type must be a tuples. When there is only one element in the tuples, it seems that an error will occur. However, simply add a comma to the backend.