import
select
import
socket
import
struct
import
hexdump
import
binascii
import
random
from
socketserver
import
StreamRequestHandler, ThreadingTCPServer
PLC_IP
=
'10.65.60.81'
PLC_PORT
=
502
def
modify_pkt(data):
return
data
class
SocksProxy(StreamRequestHandler):
def
handle(
self
):
print
(
'Accepting connection from {}'
.
format
(
self
.client_address))
port
=
PLC_PORT
address
=
PLC_IP
try
:
remote
=
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote.connect((address, port))
bind_address
=
remote.getsockname()
print
(
'Connected to {} {}'
.
format
(address, port))
except
Exception as err:
logging.error(err)
self
.exchange_loop(
self
.connection, remote)
self
.server.close_request(
self
.request)
def
exchange_loop(
self
, client, remote):
while
True
:
r, w, e
=
select.select([client, remote], [], [])
if
client
in
r:
data
=
client.recv(
4096
)
if
remote.send(data) <
=
0
:
break
if
remote
in
r:
data
=
remote.recv(
4096
)
data
=
modify_pkt(data)
hexdump.hexdump(data)
if
client.send(data) <
=
0
:
break
if
__name__
=
=
'__main__'
:
server
=
ThreadingTCPServer((
'0.0.0.0'
, PLC_PORT), SocksProxy)
server.serve_forever()