forked from pypxe/PyPXE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtftpd.py
executable file
·46 lines (42 loc) · 1.58 KB
/
tftpd.py
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
#!/usr/bin/env python
import socket, IN, binascii, os
from math import ceil
from sys import exit
iface_listen = 'eth1.101'
host = ''
port = 69
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, iface_listen+'\0')
s.bind((host, port))
def group(s, n): return [s[i:i+n] for i in xrange(0, len(s), n)]
last=()
while 1:
try:
message, address = s.recvfrom(8192)
if last == address: continue
if message.startswith('\x00\x01'): #WRQ
message=message.split('\x00')
filename=message[1][1:]
print address,"wants",repr(filename)
if not os.path.exists('.'+filename):
s.sendto('\x00\x05\x00\x01no such file exists',address)
continue
fsize=os.path.getsize('.'+filename)
s.sendto('\x00\x06blksize\x00512\x00tsize\x00%s\x00' % fsize,address)
elif message.startswith('\x00\x04'): #OptACK
last=address
f=open('.'+filename,'r')
data=f.read()
dataset=group(data,512)
if len(dataset) > 65534: print "Won't work, too large... >64MB"
for index,chunk in enumerate(dataset):
try:
s.sendto('\x00\x03'+binascii.unhexlify(hex(index+1)[2:].rjust(4,'0'))+chunk,address)
except TypeError:
break
s.recvfrom(128)
s.sendto('\x00\x03'+binascii.unhexlify(hex(index+2)[2:].rjust(4,'0')),address)
f.close()
except KeyboardInterrupt:
exit()