-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcline.py
More file actions
115 lines (92 loc) · 3 KB
/
cline.py
File metadata and controls
115 lines (92 loc) · 3 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import socket
import os
import sys
import struct
import os
import time
import threading
from scapy.all import *
HOST = 'localhost' # use '' to expose to all networks
PORT = 12345
PASSWORD = "12345"
filepath = "C:\\Users\\Air-wang\\Desktop\\pcap.pcap"
pkts = []
count = 0
pcapnum = 0
pack = 0
LOCK = threading.Lock()
def socket_client(request):
global filepath
#request.send(b'1')
while os.path.exists(filepath) ==False:
print("bucunzai")
time.sleep(0.2)
while 1:
# filepath = raw_input('please input file path: ')
print(os.path.isfile(filepath))
if os.path.isfile(filepath):
# 定义定义文件信息。128s表示文件名为128bytes长,l表示一个int或log文件类型,在此为文件大小
fileinfo_size = struct.calcsize('128sl')
# 定义文件头信息,包含文件名和文件大小
fhead = struct.pack('128sl', bytes(os.path.basename(
filepath), 'utf-8'), os.stat(filepath).st_size)
request.send(fhead)
print('client filepath: {0}'.format(filepath))
fp = open(filepath, 'rb')
while 1:
data = fp.read(1024)
if not data:
print('{0} file send over...'.format(filepath))
break
request.send(data)
break
def mainthread():
global pack
try:
"""Open specified port and return file-like object"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# set SOL_SOCKET.SO_REUSEADDR=1 to reuse the socket if
# needed later without waiting for timeout (after it is
# closed, for example)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((HOST, PORT))
sock.listen(0) # do not queue connections
request, addr = sock.accept()
# print (request.recv(1024), addr)
if request.recv(1024).decode('utf-8') == PASSWORD:
request.send(b'1')
LOCK.acquire()
pack = 1
LOCK.release()
socket_client(request)
if request.recv(1024).decode('utf-8') == "succes":
os.remove(filepath)
request.close()
sock.close()
else:
request.close()
sock.close()
except Exception as e:
sock.close()
pass
def write_cap(x):
global pkts
global pack
global pcapnum
pkts.append(x)
if pack == 1 : #300 packets save one time
pname = "C:\\Users\\Air-wang\\Desktop\\pcap.pcap"
t = threading.Thread(target=wrpcap, args=(pname, pkts))
t.start()
pkts = []
LOCK.acquire()
pack = 0
LOCK.release()
def begin_packet():
sniff(prn=write_cap, filter="tcp port 8080 or tcp port 80")
if __name__ == '__main__':
t = threading.Thread(target=begin_packet, args=())
t.start()
while True:
mainthread()
print("h")