MySQL无法从注入过渡到参数化

时间:2017-08-01 01:42:04

标签: c++ mysql

我在这里执行以下代码来执行查询。最初,我使用SQL Injection返回行结果。听到我应该使用参数化,我重新安排了我的代码,并阅读MySQL文档如何这样做。我在C ++应用程序中使用MySQL的C库。

但是,它没有返回结果。

我知道我的SQL语句是100%罚款。它已经过测试。我唯一改变的是将%d(注入)更改为?,它接受玩家的ID。

返回-1。虽然它是一个SELECT语句,所以它可能是正常的吗?

    // Get the number of affected rows
    affected_rows = mysql_stmt_affected_rows(m_stmt);

返回2.这是正确的。我有两个字段被退回。

    // Store the field count
    m_fieldCount = mysql_field_count(&m_conn);

返回0(成功)

    if (mysql_stmt_store_result(m_stmt)) 

最后,返回null。

    m_result = mysql_store_result(&m_conn);

我需要m_result所以我可以读取行。 "了mysql_stmt_affected_rows"听起来很相似,但没有返回MYSQL_RESULT。

  m_result = mysql_store_result(&m_conn);

/// <summary>
/// Executes a query.
/// </summary>
/// <param name="query">The query to execute.</param>
/// <returns>Returns true on success, else false.</returns>
bool SQLConnection::executeQuery_New(const char *query)
{
    int param_count = 0;
    int affected_rows = 0;

    // Validate connection.
    if (!m_connected)
        return false; 

    // Initialize the statement
    m_stmt = mysql_stmt_init(&m_conn);
    if (!m_stmt) {
        fprintf(stderr, " mysql_stmt_init(), out of memory\n");
        return false;
    }

    // Prepare the statement
    if (mysql_stmt_prepare(m_stmt, query, strlen(query))) {
        fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(m_stmt));
        return false;
    }

    // Get the parameter count from the statement
    param_count = mysql_stmt_param_count(m_stmt);
    if (param_count != m_bind.size()) {
        fprintf(stderr, " invalid parameter count returned by MySQL\n");
        return false;
    }

    // Bind buffers 
    // The parameter binds are stored in std::vector<MYSQL_BIND>
    // I need to convert std::vector<MYSQL_BIND> m_bind to MYSQL_BIND *bnd
    MYSQL_BIND *bind = new MYSQL_BIND[m_bind.size()  + 1];

    memset(bind, 0, sizeof(bind) * m_bind.size()); 

    for (int i = 0; i < param_count; i++)
        bind[i] = m_bind[i]; 

    if (mysql_stmt_bind_param(m_stmt, &bind[0]))
    {
        fprintf(stderr, " mysql_stmt_bind_param() failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(m_stmt));
        return false;
    }

    // Execute the query
    if (mysql_stmt_execute(m_stmt)) {
        fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
        fprintf(stderr, " %s\n", mysql_stmt_error(m_stmt));
        return false;
    }

    // Get the number of affected rows
    affected_rows = mysql_stmt_affected_rows(m_stmt);
    //if (affected_rows == -1) {
    //  fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
    //  fprintf(stderr, " %s\n", mysql_stmt_error(m_stmt));
    //  return false;
    //}

    // Store the field count
    m_fieldCount = mysql_field_count(&m_conn);


    // Store the result
    if (mysql_stmt_store_result(m_stmt)) 
    {
        fprintf(stderr, " failed retrieving result\n");
        fprintf(stderr, " %s\n", mysql_error(&m_conn));
        int d = mysql_errno(&m_conn);
        return false;
    }

    // This looks similar to the last above statement, but I need m_result. I used mysql_store_result earlier when using injection and it worked fine, but here in this case it returns null. 
    m_result = mysql_store_result(&m_conn);


    // Close the statement
    if (mysql_stmt_close(m_stmt)) {
        /* mysql_stmt_close() invalidates stmt, so call          */
        /* mysql_error(mysql) rather than mysql_stmt_error(stmt) */
        fprintf(stderr, " failed while closing the statement\n");
        fprintf(stderr, " %s\n", mysql_error(&m_conn));
        return false; 
    }

    // Delete bind array
    if (bind) {
        delete[] bind;
        bind = NULL;
    }

    return true;
}

我如何添加一个int参数(播放器的ID):

void SQLConnection::addParam(int buffer, enum_field_types type, unsigned long length)
{
    MYSQL_BIND bind; 

    memset(&bind, 0, sizeof(bind));

    bind.buffer = (char *)&buffer;
    bind.buffer_type = type;
    bind.is_null = 0;
    bind.length = &length;

    m_bind.push_back(bind);
}

我的变量及其类型:

class SQLConnection
{
private: 
    MYSQL m_conn;
    MYSQL_ROW m_row;
    MYSQL_RES *m_result;  
    char m_errorMessage[ERROR_MSG_MAX];
    bool m_connected;
    MYSQL_STMT *m_stmt;
    std::vector<MYSQL_BIND> m_bind;
    int m_fieldCount;
    // ...

最后它在SQL语句末尾的调用函数:

...WHERE player_id = ?;"); 

conn.addParam(m_id, MYSQL_TYPE_LONGLONG, 0);

    if (!conn.executeQuery_New(buffer)) {
        conn.close();
        return "";
    }

    // Close the connection.
    conn.close();

    std::string s = conn.getField("max_value_column_name");

我得到的错误代码是2014年: https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html

为了感兴趣,这是我使用的先前功能。这适用于注射。使用上面带参数化的新功能是引发问题的功能。

bool SQLConnection::executeQuery(const char *query)
{ 
    // Validate connection.
    if (!m_connected)
        return false;  

    // Execute the query
    int status = mysql_query(&m_conn, query);

    if (status != 0) {
        sprintf(m_errorMessage, "Error: %s", mysql_error(&m_conn)); 
        return false;
    } 

    // Store the result
    m_result = mysql_store_result(&m_conn);

    return true;
}  

在我开始使用C#over C ++进行语言宗教战争之后,我想我会在这里做最后一次尝试。任何帮助表示赞赏。

编辑: 这是我在参数化之前读取列名的方法(可能在调用mysql_stmt_store_result(m_stmt)之后需要更新此代码?

std::string SQLConnection::getField(const char *fieldName)
{
    MYSQL_FIELD *field = NULL;
    unsigned int name_field = 0;

     mysql_stmt_data_seek(m_stmt, 0);
     mysql_stmt_fetch_column(m_stmt, &bind, 0, 0);
     //mysql_data_seek(m_result, 0);
     //mysql_field_seek(m_result, 0);

     const unsigned int num_fields = mysql_stmt_field_count(m_stmt); 
     // const unsigned int num_fields = mysql_num_fields(m_result);

    std::vector<char *> headers(num_fields);

    for (unsigned int i = 0; (field = mysql_fetch_field(m_result)); i++)
    {
        headers[i] = field->name;

        if (strcmp(fieldName, headers[i]) == 0)
            name_field = i;
    }  

    while ((m_row = mysql_fetch_row(m_result))) {
        return std::string(m_row[name_field]);
    }

    return "";
}

编辑: 我发现在最后一个函数中,语句有相同的函数,比如mysql_num_fields()是mysql_stmt_field_count()。我认为这些需要更新,因为它不再使用m_stmt而不是m_result,因此有理由更新函数以便使用m_stmt。但是,如何更新函数的后半部分并不是很明显。

1 个答案:

答案 0 :(得分:0)

您可能需要更好地了解stmt的工作原理。当您使用mysql_store_result()时,您无法通过stmt获得最终结果。

  1. 您应该为您接受结果集的语句绑定几个缓冲区。您可以mysql_stmt_bind_result()完成此操作,就像mysql_stmt_bind_param()

  2. 一样
  3. 然后,您可以使用mysql_stmt_bind_result()绑定的缓冲区来返回行数据。您可以通过mysql_stmt_fetch()完成此操作。

  4. 重复执行fetch方法,这样就可以逐行获取整个结果集。

  5. 电话的基本顺序:

    • mysql_stmt_init
    • mysql_stmt_prepare
    • mysql_stmt_bind_param
    • 在mysql_stmt_execute
    • mysql_stmt_bind_result
    • 了mysql_stmt_affected_rows
    • mysql_stmt_fetch(重复,逐行)
    • mysql_stmt_free_result

    它对我有用。

    自从我完成项目的这一部分很长一段时间后,您最好仔细阅读本手册并找到更多stmt的示例。

    抱歉我的英语不好。祝你好运!