-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpync.py
More file actions
executable file
·150 lines (134 loc) · 3.6 KB
/
pync.py
File metadata and controls
executable file
·150 lines (134 loc) · 3.6 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python
import socket
import sys
import os
import select
import argparse
import subprocess
import shlex
"""
Ugly and patchy netcat-like python utility
NOTE: won't probably work on Windows (it does not allow to select anything other than sockets)
TODO:
- buffering on listen side in UDP but not in TCP, must pick one
- UDP listen mode always behave like -k, find out why
- (try to) fix -e functionality for UDP
- clean up/refactor the *fine* everything (e.g. setblocking not needed for -e, code duplication)
- maybe more/better comments?
"""
def exec_handle(s, binary):
p = subprocess.Popen(shlex.split(binary), stdin=s.fileno(), stdout=s.fileno())#, stderr=subprocess.STDOUT)
p.wait()
s.close()
def tcp_handle(s):
while 1:
try:
inready, outready, error = select.select([s, sys.stdin],[],[])
for ir in inready:
if ir == s:
res = s.recv(2048)
if res != '':
#sys.stdout.write(res)
os.write(sys.stdout.fileno(), res)
else:
s.close()
return
else:
res = os.read(sys.stdin.fileno(), 2048)
s.sendall(res)
except (KeyboardInterrupt):
break
def udp_handle(s, addr=0):
buf = ''
while 1:
try:
inready, outready, error = select.select([s, sys.stdin],[],[])
for ir in inready:
if ir == s:
res, addr = s.recvfrom(2048)
if res != '':
#sys.stdout.write(res)
os.write(sys.stdout.fileno(),res)
else:
s.close()
return
else:
buf += os.read(sys.stdin.fileno(), 2048)
#print buf
if addr:
#print 'now sending to ' + str(addr)
s.sendto(buf, addr)
buf = ''
except (KeyboardInterrupt):
break
if __name__ == '__main__':
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("-l", action='store_true', help='listening mode')
group.add_argument("destination", type=str, help="destination address", nargs='?')
parser.add_argument("-p", type=int, help='listening port')
parser.add_argument("-k", action='store_true', help="Keep inbound socket open")
parser.add_argument("-u", action='store_true', help="UDP mode")
parser.add_argument("-e", type=str, help="program to exec after connect [dangerous!!]")
#parser.add_argument("destination", type=str, help="destination address", nargs='?')
parser.add_argument("port", type=int, help="destination port", nargs='?')
args = parser.parse_args()
#print args
if args.l and args.p is None:
parser.print_help()
print '\n-p option required with -l'
sys.exit(1)
elif not args.l and (args.destination is None or args.port is None):
parser.print_help()
print '\ndestination and port needed'
sys.exit(1)
if args.u:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
else:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if args.l:
try:
s.bind(('0.0.0.0', args.p))
except:
print >>sys.stderr, 'Cannot bind to port'
sys.exit(1)
if not args.u:
s.listen(0)
try:
abort = 0
while not abort:
c, a = s.accept()
try:
c.setblocking(0)
except:
pass
if args.e is None:
tcp_handle(c)
else:
exec_handle(c, args.e)
if not args.k:
abort = 1
except (KeyboardInterrupt):
s.close()
sys.exit(0)
else:
s.setblocking(0)
if args.e is None:
udp_handle(s)
else:
exec_handle(s, args.e)
else:
if not args.u:
try:
s.connect((args.destination, args.port))
except:
print >>sys.stderr, 'Cannot connect to destination/port'
sys.exit(1)
try:
s.setblocking(0)
except:
pass
tcp_handle(s)
else:
s.setblocking(0)
udp_handle(s, (args.destination, args.port))