Esempio n. 1
0
def cli_command_field_list(session_obj, pkt_data, code):
    table_name, wildcard = pkt_data.split('\x00')[:2]
    if not re.match(r'^[a-zA-Z0-9_]+', table_name):
        session_obj.send_payload(ERRPacket(
            session_obj.client_capabilities, 1049,
            u'Invalid table name', seq_id=1))
        return True

    if not re.match(r'^[a-zA-Z0-9_%]+', table_name):
        session_obj.send_payload(ERRPacket(
            session_obj.client_capabilities, 1049,
            u'Invalid wildcard', seq_id=1))
        return True

    cli_con = session_obj.proxy_obj.client_conn
    field_list = cli_con.get_field_list(table_name, wildcard)
    results = ResultSetText(session_obj.client_capabilities,
        flags=session_obj.server_status)
    for colname, coltype, col_max_len, \
            field_len, field_max_len, _, _ in field_list:
        results.add_column(unicode(colname), coltype, field_len)
    # TODO: server status negoatiation
    tx_packets = results.columns
    for i in range(0, len(tx_packets)):
        tx_packets[i].seq_id = i+1
    tx_eof = EOFPacket(
        session_obj.client_capabilities,
        status_flags=session_obj.server_status,
        seq_id=len(tx_packets)+1)
    tx_packets.append(tx_eof)
    return True
Esempio n. 2
0
def cli_command_field_list(session_obj, pkt_data, code):
    table_name, wildcard = pkt_data.split('\x00')[:2]
    if not re.match(r'^[a-zA-Z0-9_]+', table_name):
        session_obj.send_payload(
            ERRPacket(session_obj.client_capabilities,
                      1049,
                      u'Invalid table name',
                      seq_id=1))
        return True

    if not re.match(r'^[a-zA-Z0-9_%]+', table_name):
        session_obj.send_payload(
            ERRPacket(session_obj.client_capabilities,
                      1049,
                      u'Invalid wildcard',
                      seq_id=1))
        return True

    cli_con = session_obj.proxy_obj.client_conn
    field_list = cli_con.get_field_list(table_name, wildcard)
    results = ResultSetText(session_obj.client_capabilities,
                            flags=session_obj.server_status)
    for colname, coltype, col_max_len, \
            field_len, field_max_len, _, _ in field_list:
        results.add_column(unicode(colname), coltype, field_len)
    # TODO: server status negoatiation
    tx_packets = results.columns
    for i in range(0, len(tx_packets)):
        tx_packets[i].seq_id = i + 1
    tx_eof = EOFPacket(session_obj.client_capabilities,
                       status_flags=session_obj.server_status,
                       seq_id=len(tx_packets) + 1)
    tx_packets.append(tx_eof)
    return True
Esempio n. 3
0
 def build_response_from_query(self, query):
     """
     Do the actual query on the target MySQL host.
     Returns a packet type of either OK, ERR, or a ResultSetText
     """
     cursor = self.client_conn.cursor()
     num_rows = cursor.execute(query)
     results = cursor.fetchall()
     if not results or len(results) == 0:
         cursor.close()
         return OKPacket(self.session.client_capabilities,
             affected_rows=num_rows,
             last_insert_id=cursor.lastrowid,
             seq_id=1
             )
     col_types = cursor.description
     cursor.close()
     response = ResultSetText(self.session.client_capabilities,
         flags=self.session.server_status)
     for colname, coltype, col_max_len, \
             field_len, field_max_len, _, _ in col_types:
         response.add_column(unicode(colname), coltype, field_len)
     for row in results:
         lvals = list(row)
         response.add_row(lvals)
     return response
Esempio n. 4
0
def cli_command_query(session_obj, pkt_data, code):
    query = pkt_data
    _LOG.debug('Got query command: %s' % query)
    if query.lower() == 'select @@version_comment limit 1':
        # intercept the MySQL client getting version info, replace with our own
        response = ResultSetText(session_obj.client_capabilities,
            flags=session_obj.server_status)
        col_name = u'@@version_comment'
        row_val = u'mysqlproxy-0.1'
        response.add_column(col_name, column_types.VAR_STRING, len(row_val))
        response.add_row([row_val])
    else:
        proxy = session_obj.proxy_obj
        plugin_continue, plugin_ret = proxy.plugins.call_hooks('com_query',
            query, session_obj)
        if plugin_continue:
            response = proxy.build_response_from_query(query)
        else:
            response = plugin_ret
    session_obj.send_payload(response)
    return True
Esempio n. 5
0
 def build_response_from_query(self, query):
     """
     Do the actual query on the target MySQL host.
     Returns a packet type of either OK, ERR, or a ResultSetText
     """
     cursor = self.client_conn.cursor()
     num_rows = cursor.execute(query)
     results = cursor.fetchall()
     if not results or len(results) == 0:
         cursor.close()
         return OKPacket(self.session.client_capabilities,
                         affected_rows=num_rows,
                         last_insert_id=cursor.lastrowid,
                         seq_id=1)
     col_types = cursor.description
     cursor.close()
     response = ResultSetText(self.session.client_capabilities,
                              flags=self.session.server_status)
     for colname, coltype, col_max_len, \
             field_len, field_max_len, _, _ in col_types:
         response.add_column(unicode(colname), coltype, field_len)
     for row in results:
         lvals = list(row)
         response.add_row(lvals)
     return response
Esempio n. 6
0
def cli_command_query(session_obj, pkt_data, code):
    query = pkt_data
    _LOG.debug('Got query command: %s' % query)
    if query.lower() == 'select @@version_comment limit 1':
        # intercept the MySQL client getting version info, replace with our own
        response = ResultSetText(session_obj.client_capabilities,
                                 flags=session_obj.server_status)
        col_name = u'@@version_comment'
        row_val = u'mysqlproxy-0.1'
        response.add_column(col_name, column_types.VAR_STRING, len(row_val))
        response.add_row([row_val])
    else:
        proxy = session_obj.proxy_obj
        plugin_continue, plugin_ret = proxy.plugins.call_hooks(
            'com_query', query, session_obj)
        if plugin_continue:
            response = proxy.build_response_from_query(query)
        else:
            response = plugin_ret
    session_obj.send_payload(response)
    return True