Esempio n. 1
0
 def prepare_answer(self, status=SOCKS5_RESP_SUCCESS, addrType=SOCKS5_ADDR_IP4, addr="0.0.0.0", port=0):
   """ prepare socks5 answer packet """
   answer = extStringIO("")
   answer.writeFmt('BBBB', self.version, status, 0, addrType)
   if addrType == SOCKS5_ADDR_IP4:
     answer.writeFmt('BBBB', *(map(int, addr.split('.'))))
   elif addrType == SOCKS5_ADDR_IP6:
     answer.write('0' * 16)
   elif addrType == SOCKS5_ADDR_DOMAIN:
     answer.writeFmt('B', len(addr))
     answer.write(addr)
   answer.writeFmt('>H', port)
   return answer.dump()
Esempio n. 2
0
 def prepare_answer(self,
                    status=SOCKS5_RESP_SUCCESS,
                    addrType=SOCKS5_ADDR_IP4,
                    addr="0.0.0.0",
                    port=0):
     """ prepare socks5 answer packet """
     answer = extStringIO("")
     answer.writeFmt('BBBB', self.version, status, 0, addrType)
     if addrType == SOCKS5_ADDR_IP4:
         answer.writeFmt('BBBB', *(map(int, addr.split('.'))))
     elif addrType == SOCKS5_ADDR_IP6:
         answer.write('0' * 16)
     elif addrType == SOCKS5_ADDR_DOMAIN:
         answer.writeFmt('B', len(addr))
         answer.write(addr)
     answer.writeFmt('>H', port)
     return answer.dump()
Esempio n. 3
0
 def auth_02(self):
   """ auth: user/password """
   logging.info("Auth method 0x02 (user/password")
   stream = extStringIO(self.client_socket.recv(1024))
   #print stream.hexDump()
   ver, uLen = stream.readFmt('BB')
   usr = ""
   if uLen > 0:
     usr = stream.readFmt_single(str(uLen) + "s")
   pLen = stream.readFmt_single('B')
   pwd = ""
   if pLen > 0:
     pwd = stream.readFmt_single(str(pLen) + "s")
   logging.info("provided version/user/password: {0}/{0}/{0}".format(ver, usr, pwd))
   logging.debug(" <-- here we should check suser/password ")
   self.client_socket.send(struct.pack('BB', 1, 0))
   return True
Esempio n. 4
0
 def auth_02(self):
     """ auth: user/password """
     logging.info("Auth method 0x02 (user/password")
     stream = extStringIO(self.client_socket.recv(1024))
     #print stream.hexDump()
     ver, uLen = stream.readFmt('BB')
     usr = ""
     if uLen > 0:
         usr = stream.readFmt_single(str(uLen) + "s")
     pLen = stream.readFmt_single('B')
     pwd = ""
     if pLen > 0:
         pwd = stream.readFmt_single(str(pLen) + "s")
     logging.info("provided version/user/password: {0}/{0}/{0}".format(
         ver, usr, pwd))
     logging.debug(" <-- here we should check suser/password ")
     self.client_socket.send(struct.pack('BB', 1, 0))
     return True
Esempio n. 5
0
    def run(self):
        """ run service """
        data = self.client_socket.recv(1024)
        if not data:
            raise Socks4Exception('Fail to read from client !')

        stream = extStringIO(data)
        ver, cmd, port, binIp = stream.readFmt('>BBH4s')

        self.cmd = cmd
        if ver != self.version:
            raise Socks4Exception(
                "Version mismatch : [ {0:d} != 4 ]".format(ver))

        logging.info(
            " >> Got request [ ver:{0:d}, cmd:0x{1:02X}, port:{2:d} ]".format(
                ver, cmd, port))

        numIp = struct.unpack('>i', binIp)[0]
        strIp = '.'.join(map(str, struct.unpack('BBBB', binIp)))
        if 1 == 2:
            print
            print "+----+----+-------+-----------------+"
            print "| VN | CM | port  |     dst.ip.addr |"
            print "+----+----+-------+-----------------+"
            print "| %02X | %02X | %05d | %s |" % (ver, cmd, port,
                                                   strIp.rjust(15))
            print "+----+----+-------+-----------------+"
            print

        userData = stream.read_rest()
        user, extra = userData.split('\x00', 1)

        logging.info("  Provided user: [{0:s}] ".format(user))
        self.user = user

        if numIp < 257:
            if extra[-1] != '\x00':
                raise Socks4Exception(
                    "Additional (hostname) data should be Null-term ! (is:{0:s}"
                    .format( ` data[:-1] `))
            host = extra[:-1]
            logging.info("** SOCKS-4a")
        else:
            host = strIp
            logging.info("** SOCKS-4")
            if len(extra) > 0:
                logging.info("WARNING: extra data : {0:s}".format( ` extra `))

        self.target = (host, port)
        logging.info(">> Target-host : {0:s}".format( ` self.target `))

        if not self.verify_access():
            logging.info(" !! client rejected !!")
            reply = self.prepare_server_reply(RESPONSE_CODE_REJECTED)
            self.client_socket.send(reply)
            self.terminate()
            return
        #else:
        reply = self.prepare_server_reply(RESPONSE_CODE_GRANTED)
        self.client_socket.send(reply)

        if self.cmd == SOCKS4_CMD_CONNECT:
            remote_socket = self.connect_to(self.target)
            if remote_socket is None:
                reply = self.prepare_server_reply(RESPONSE_CODE_CONERR)
                self.client_socket.send(reply)
                self.terminate()
                logging.info("!! Fail to connect to target !!")
                return
            self.tcp_forward(self.client_socket, remote_socket)
            self.terminate()
Esempio n. 6
0
    def run(self):
        """ run service """
        data = self.client_socket.recv(1024)
        stream = extStringIO(data)

        ver, nMethods = stream.readFmt('BB')
        if ver != self.version:
            raise Exception("Version mismatch [ {0} != 5]".format(ver))

        methods = stream.readFmt('B' * nMethods)
        logging.info(
            "Received available auth methodds: (n={0}) [ {1} ]".format(
                nMethods, ` methods `))

        m = self.select_auth_method(methods)  # return 0xFF if none
        logging.debug("Selected auth method : {0:#02x}".format(m))
        self.client_socket.send(struct.pack('BB', self.version, m))

        if m == SOCKS5_METHOD_FAIL:
            self.terminate()
            return

        if not self.process_auth_method(m):
            self.terminate()
            return

        # not terminated? ->auth ok ;)

        data = self.client_socket.recv(1024)
        stream = extStringIO(data)

        ver, cmd, _, addrType = stream.readFmt('BBBB')

        if ver != self.version:
            raise Exception("Version mismatch [{0:d} != {1:d}]".format(
                ver, self.version))

        logging.info("Client send command {0:#02x}".format(cmd))

        host = None
        port = -1
        if addrType == SOCKS5_ADDR_IP4:
            bin_ip, port = stream.readFmt('>4sH')
            host = '.'.join(map(str, struct.unpack('BBBB', bin_ip)))
        elif addrType == SOCKS5_ADDR_IP6:
            bin_ip, port = stream.readFmt('>16sH')
            raise Exception("Implement me lol ( ipv6 <3 )")
        elif addrType == SOCKS5_ADDR_DOMAIN:
            size = stream.readFmt_single('B')
            host = stream.readFmt_single(str(size) + 's')
            port = int(stream.readFmt_single('>H'))
        else:
            self.client_socket.send(
                self.prepare_answer(SOCKS5_RESP_ADDRNOSUPP))
            self.terminate()
            raise Exception(
                "Unsupported address type ({0:#02x})".format(addrType))
            #return False
        logging.info(" Target host:port ( {0} : {1:d} ) ".format(host, port))
        self.target = (host, port)
        if self.process_command(cmd):
            self.terminate()
            return True
        else:  # command not implemented !
            self.client_socket.send(self.prepare_answer(SOCKS5_RESP_CMDNOSUPP))
            self.terminate()
            raise Socks5Exception(
                "Command not implemented [{0:d}]".format(cmd))
Esempio n. 7
0
  def run(self):
    """ run service """
    data = self.client_socket.recv(1024)
    if not data:
      raise Socks4Exception('Fail to read from client !')

    stream = extStringIO(data)
    ver, cmd, port, binIp = stream.readFmt('>BBH4s')

    self.cmd = cmd
    if ver != self.version:
      raise Socks4Exception("Version mismatch : [ {0:d} != 4 ]".format(ver))

    logging.info(" >> Got request [ ver:{0:d}, cmd:0x{1:02X}, port:{2:d} ]".format(ver, cmd, port))

    numIp = struct.unpack('>i', binIp)[0]
    strIp = '.'.join(map(str, struct.unpack('BBBB', binIp)))
    if 1==2:
      print
      print "+----+----+-------+-----------------+"
      print "| VN | CM | port  |     dst.ip.addr |"
      print "+----+----+-------+-----------------+"
      print "| %02X | %02X | %05d | %s |" % (ver, cmd, port, strIp.rjust(15))
      print "+----+----+-------+-----------------+"
      print

    userData = stream.read_rest()
    user, extra = userData.split('\x00', 1)

    logging.info("  Provided user: [{0:s}] ".format(user))
    self.user = user

    if numIp < 257:
      if extra[-1] != '\x00':
        raise Socks4Exception("Additional (hostname) data should be Null-term ! (is:{0:s}".format(`data[:-1]`))
      host = extra[:-1]
      logging.info("** SOCKS-4a")
    else:
      host = strIp
      logging.info("** SOCKS-4")
      if len(extra) > 0:
        logging.info("WARNING: extra data : {0:s}".format(`extra`))

    self.target = (host, port)
    logging.info(">> Target-host : {0:s}".format(`self.target`))
  
    
    if not self.verify_access():
      logging.info(" !! client rejected !!")
      reply = self.prepare_server_reply(RESPONSE_CODE_REJECTED)
      self.client_socket.send(reply)
      self.terminate()
      return
    #else:
    reply = self.prepare_server_reply(RESPONSE_CODE_GRANTED)
    self.client_socket.send(reply) 

    if self.cmd == SOCKS4_CMD_CONNECT:
      remote_socket = self.connect_to(self.target)
      if remote_socket is None:
        reply = self.prepare_server_reply(RESPONSE_CODE_CONERR)
        self.client_socket.send(reply)
        self.terminate()
        logging.info("!! Fail to connect to target !!")
        return
      self.tcp_forward(self.client_socket, remote_socket)
      self.terminate()
Esempio n. 8
0
  def run(self):
    """ run service """
    data = self.client_socket.recv(1024)
    stream = extStringIO(data)

    ver, nMethods = stream.readFmt('BB')
    if ver != self.version:
      raise Exception("Version mismatch [ {0} != 5]".format(ver))

    methods = stream.readFmt('B' *nMethods)
    logging.info("Received available auth methodds: (n={0}) [ {1} ]".format(nMethods, `methods`))

    m = self.select_auth_method(methods) # return 0xFF if none
    logging.debug("Selected auth method : {0:#02x}".format(m))
    self.client_socket.send(struct.pack('BB', self.version, m))

    if m == SOCKS5_METHOD_FAIL:
      self.terminate()
      return

    if not self.process_auth_method(m):
      self.terminate()
      return

    # not terminated? ->auth ok ;)

    data = self.client_socket.recv(1024)
    stream = extStringIO(data)

    ver, cmd, _, addrType = stream.readFmt('BBBB')

    if ver != self.version:
      raise Exception("Version mismatch [{0:d} != {1:d}]".format(ver, self.version))

    logging.info("Client send command {0:#02x}".format(cmd))

    host = None
    port = -1
    if addrType == SOCKS5_ADDR_IP4:
      bin_ip, port = stream.readFmt('>4sH')
      host = '.'.join(map(str, struct.unpack('BBBB', bin_ip)))
    elif addrType == SOCKS5_ADDR_IP6:
      bin_ip, port = stream.readFmt('>16sH')
      raise Exception("Implement me lol ( ipv6 <3 )")
    elif addrType == SOCKS5_ADDR_DOMAIN:
      size = stream.readFmt_single('B')
      host = stream.readFmt_single(str(size)+ 's')
      port = int(stream.readFmt_single('>H'))
    else:
      self.client_socket.send(self.prepare_answer(SOCKS5_RESP_ADDRNOSUPP))
      self.terminate()
      raise Exception("Unsupported address type ({0:#02x})".format(addrType))
      #return False
    logging.info(" Target host:port ( {0} : {1:d} ) ".format(host, port))
    self.target = (host, port)
    if self.process_command(cmd):
      self.terminate()
      return True
    else: # command not implemented !
      self.client_socket.send(self.prepare_answer(SOCKS5_RESP_CMDNOSUPP))
      self.terminate()
      raise Socks5Exception("Command not implemented [{0:d}]".format(cmd))