Exemplo n.º 1
0
Arquivo: pcre.c Projeto: niubl/ironbee
static ib_status_t modpcre_init(ib_engine_t *ib,
                                ib_module_t *m,
                                void        *cbdata)
{
    assert(ib != NULL);
    assert(m != NULL);

    ib_status_t rc;

    /* Register operators. */
    rc = ib_operator_create_and_register(
        NULL,
        ib,
        "pcre",
        (IB_OP_CAPABILITY_NON_STREAM | IB_OP_CAPABILITY_CAPTURE),
        pcre_operator_create, NULL,
        NULL, NULL,
        pcre_operator_execute, m
    );
    if (rc != IB_OK) {
        return rc;
    }

    /* An alias of pcre. The same callbacks are registered. */
    rc = ib_operator_create_and_register(
        NULL,
        ib,
        "rx",
        (IB_OP_CAPABILITY_NON_STREAM | IB_OP_CAPABILITY_CAPTURE),
        pcre_operator_create, NULL,
        NULL, NULL,
        pcre_operator_execute, m
    );
    if (rc != IB_OK) {
        return rc;
    }

    /* Register a pcre operator that uses pcre_dfa_exec to match streams. */
    rc = ib_operator_create_and_register(
        NULL,
        ib,
        "dfa",
        (IB_OP_CAPABILITY_NON_STREAM | IB_OP_CAPABILITY_STREAM | IB_OP_CAPABILITY_CAPTURE),
        dfa_operator_create, NULL,
        NULL, NULL,
        dfa_operator_execute, m
    );
    if (rc != IB_OK) {
        return rc;
    }

    return IB_OK;
}
Exemplo n.º 2
0
/* Called to initialize a module (on load). */
static ib_status_t sqli_init(ib_engine_t *ib, ib_module_t *m, void *cbdata)
{
    assert(ib != NULL);
    assert(m  != NULL);

    ib_status_t rc;

    /* Register normalizeSqli transformation. */
    rc = ib_transformation_create_and_register(
        NULL,
        ib,
        "normalizeSqli",
        false,
        NULL, NULL,
        NULL, NULL,
        sqli_normalize_tfn, NULL
    );
    if (rc != IB_OK) {
        return rc;
    }

    /* Register is_sqli operator. */
    rc = ib_operator_create_and_register(
        NULL,
        ib,
        "is_sqli",
        IB_OP_CAPABILITY_CAPTURE,
        sqli_op_create, m,
        NULL, NULL,
        sqli_op_execute, NULL
    );
    if (rc != IB_OK) {
        return rc;
    }

    /* Register is_xss operator. */
    rc = ib_operator_create_and_register(
        NULL,
        ib,
        "is_xss",
        IB_OP_CAPABILITY_NONE,
        NULL, NULL,
        NULL, NULL,
        xss_op_execute, NULL
    );
    if (rc != IB_OK) {
        return rc;
    }

    return IB_OK;
}
Exemplo n.º 3
0
TEST_F(OperatorTest, OperatorCallTest)
{
    ib_status_t status;
    ib_num_t call_result;
    void *instance_data;
    ib_operator_t *op;
    const ib_operator_t *cop;

    status = ib_operator_create_and_register(
        &op,
        ib_engine,
        "test_op",
        IB_OP_CAPABILITY_NON_STREAM,
        test_create_fn, NULL,
        NULL, NULL,
        test_execute_fn, NULL
    );
    ASSERT_EQ(IB_OK, status);

    status = ib_operator_lookup(ib_engine, "test_op", &cop);

    ASSERT_EQ(IB_OK, status);

    status = ib_operator_inst_create(op,
                                     ib_context_main(ib_engine),
                                     IB_OP_CAPABILITY_NON_STREAM,
                                     "INVALID",
                                     &instance_data);
    ASSERT_EQ(IB_EINVAL, status);

    status = ib_operator_inst_create(op,
                                     ib_context_main(ib_engine),
                                     IB_OP_CAPABILITY_NON_STREAM,
                                     "data",
                                     &instance_data);
    ASSERT_EQ(IB_OK, status);


    ib_field_t *field;
    const char *matching = "data matching string";
    const char *nonmatching = "non matching string";
    ib_field_create(
        &field,
        ib_engine_pool_main_get(ib_engine),
        IB_FIELD_NAME("testfield"),
        IB_FTYPE_NULSTR,
        NULL
    );

    ib_field_setv(field, ib_ftype_nulstr_in(matching));
    status = ib_operator_inst_execute(op, instance_data, ib_tx, field, NULL, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(1, call_result);

    ib_field_setv(field, ib_ftype_nulstr_in(nonmatching));
    status = ib_operator_inst_execute(op, instance_data, ib_tx, field, NULL, &call_result);
    ASSERT_EQ(IB_OK, status);
    EXPECT_EQ(0, call_result);

    status = ib_operator_inst_destroy(op, instance_data);
    ASSERT_EQ(IB_OK, status);
}
Exemplo n.º 4
0
/**
 * Initialize the eudoxus operator module.
 *
 * Registers the operators and the hash for storing the eudoxus engine
 * instances created by the LoadEudoxus directive.
 *
 * @param[in] ib Ironbee engine.
 * @param[in] m Module instance.
 * @param[in] cbdata Not used.
 */
static
ib_status_t ee_module_init(ib_engine_t *ib,
                           ib_module_t *m,
                           void        *cbdata)
{
    ib_status_t rc;
    ib_mm_t mm;
    ee_config_t *config;

    mm = ib_engine_mm_main_get(ib);
    config = ee_get_config(ib);
    assert(config != NULL);

    if (config->eudoxus_pattern_hash == NULL) {
        rc = ib_hash_create_nocase(&(config->eudoxus_pattern_hash), mm);
        if (rc != IB_OK ) {
            return rc;
        }
    }

    rc = ib_operator_create_and_register(
        NULL,
        ib,
        "ee",
        IB_OP_CAPABILITY_CAPTURE,
        &ee_operator_create, NULL,
        NULL, NULL,
        &ee_operator_execute, m
    );
    if (rc != IB_OK) {
        ib_log_error(
            ib,
            "Error registering ee operator: %s",
            ib_status_to_string(rc));
        return rc;
    }
    rc = ib_operator_stream_create_and_register(
        NULL,
        ib,
        "ee",
        IB_OP_CAPABILITY_CAPTURE,
        &ee_operator_create, NULL,
        NULL, NULL,
        &ee_operator_execute_stream, m
    );
    if (rc != IB_OK) {
        ib_log_error(
            ib,
            "Error registering ee stream operator: %s",
            ib_status_to_string(rc));
        return rc;
    }

    rc = ib_operator_create_and_register(
        NULL,
        ib,
        "ee_match",
        IB_OP_CAPABILITY_CAPTURE,
        &ee_operator_create, NULL,
        NULL, NULL,
        &ee_match_operator_execute, m
    );
    if (rc != IB_OK) {
        ib_log_error(
            ib,
            "Error registering ee operator: %s",
            ib_status_to_string(rc));
        return rc;
    }

    rc = ib_hook_tx_register(ib,
                             tx_finished_state,
                             ee_tx_finished_handler,
                             m);

    if (rc != IB_OK) {
        ib_log_error(
            ib,
            "Error registering transaction finished state for ee operator: %s",
            ib_status_to_string(rc));
        return rc;
    }

    return IB_OK;
}
Exemplo n.º 5
0
/**
 * Called by RuleExt lua:.
 *
 * @param[in] cp       Configuration parser.
 * @param[in] rule     Rule under construction.
 * @param[in] tag      Should be "lua".
 * @param[in] location What comes after "lua:".
 * @param[in] cbdata   Callback data; unused.
 * @return
 * - IB_OK on success.
 * - IB_EINVAL if Lua not available or not called for "lua" tag.
 * - Other error code if loading or registration fails.
 */
static ib_status_t modlua_rule_driver(
    ib_cfgparser_t *cp,
    ib_rule_t *rule,
    const char *tag,
    const char *location,
    void *cbdata
)
{
    assert(cp != NULL);
    assert(cp->ib != NULL);
    assert(tag != NULL);
    assert(location != NULL);

    ib_status_t            rc;
    const char            *slash;
    const char            *name;
    ib_operator_t         *op;
    ib_operator_inst_t    *opinst;
    ib_engine_t           *ib                 = cp->ib;
    modlua_cfg_t          *cfg                = NULL;
    ib_context_t          *ctx                = NULL;
    modlua_rules_cbdata_t *modlua_rules_cbdata =
        (modlua_rules_cbdata_t *)cbdata;

    /* This cbdata is passed on to the implementation. Validate it here. */
    assert(modlua_rules_cbdata != NULL);
    assert(modlua_rules_cbdata->module != NULL);

    if (strncmp(tag, "lua", 3) != 0) {
        ib_cfg_log_error(cp, "Lua rule driver called for non-lua tag.");
        return IB_EINVAL;
    }

    rc = ib_cfgparser_context_current(cp, &ctx);
    if (rc != IB_OK) {
        ib_cfg_log_error(cp, "Failed to retrieve current context.");
        return rc;
    }

    rc = modlua_cfg_get(ib, ctx, &cfg);
    if (rc != IB_OK) {
        return rc;
    }

    rc = ib_lua_load_func(
        cp->ib,
        cfg->L,
        location,
        ib_rule_id(rule));
    if (rc != IB_OK) {
        ib_cfg_log_error(cp, "Failed to load lua file \"%s\"", location);
        return rc;
    }

    /* Record that we need to reload this rule in each TX. */
    rc = modlua_record_reload(
        cp->ib,
        cfg,
        MODLUA_RELOAD_RULE,
        NULL,
        ib_rule_id(rule),
        location);
    if (rc != IB_OK) {
        ib_cfg_log_error(
            cp,
            "Failed to record lua file \"%s\" to reload",
            location);
        return rc;
    }

    slash = strrchr(location, '/');
    if (slash == NULL) {
        name = location;
    }
    else {
        name = slash + 1;
    }

    rc = ib_operator_create_and_register(
        &op,
        cp->ib,
        name,
        IB_OP_CAPABILITY_NONE,
        &lua_operator_create,
        modlua_rules_cbdata,
        NULL,
        NULL,
        &lua_operator_execute,
        modlua_rules_cbdata
    );
    if (rc != IB_OK) {
        ib_cfg_log_error(cp, "Error registering lua operator \"%s\": %s",
                         name, ib_status_to_string(rc));
        return rc;
    }

    rc = ib_operator_inst_create(&opinst,
                                 ib_engine_mm_main_get(cp->ib),
                                 ctx,
                                 op,
                                 ib_rule_required_op_flags(rule),
                                 ib_rule_id(rule)); /* becomes instance_data */

    if (rc != IB_OK) {
        ib_cfg_log_error(cp,
                         "Error instantiating lua operator "
                         "for rule \"%s\": %s",
                         name, ib_status_to_string(rc));
        return rc;
    }

    rc = ib_rule_set_operator(cp->ib, rule, opinst);

    if (rc != IB_OK) {
        ib_cfg_log_error(cp,
                         "Error associating lua operator \"%s\" "
                         "with rule \"%s\": %s",
                         name, ib_rule_id(rule), ib_status_to_string(rc));
        return rc;
    }

    return IB_OK;
}
Exemplo n.º 6
0
/**
 * Initialize the eudoxus operator module.
 *
 * Registers the operators and the hash for storing the eudoxus engine
 * instances created by the LoadEudoxus directive.
 *
 * @param[in] ib Ironbee engine.
 * @param[in] m Module instance.
 * @param[in] cbdata Not used.
 */
static
ib_status_t ee_module_init(ib_engine_t *ib,
                           ib_module_t *m,
                           void        *cbdata)
{
    ib_status_t rc;
    ib_mpool_t *main_mp;
    ib_mpool_t *mod_mp;
    ee_config_t *config;

    main_mp = ib_engine_pool_main_get(ib);
    config = ee_get_config(ib);
    assert(config != NULL);

    rc = ib_mpool_create(&mod_mp, "ee_module", main_mp);
    if (rc != IB_OK ) {
        ib_log_error(ib, MODULE_NAME_STR ": Error allocating module mpool.");
        return rc;
    }
    if (config->eudoxus_pattern_hash == NULL) {
        rc = ib_hash_create_nocase(&(config->eudoxus_pattern_hash), mod_mp);
        if (rc != IB_OK ) {
            ib_log_error(ib, MODULE_NAME_STR ": Error initializing module.");
            return rc;
        }
    }

    rc = ib_operator_create_and_register(
        NULL,
        ib,
        "ee_match_any",
        ( IB_OP_CAPABILITY_NON_STREAM |
          IB_OP_CAPABILITY_STREAM |
          IB_OP_CAPABILITY_CAPTURE ),
        &ee_match_any_operator_create, NULL,
        NULL, NULL,
        &ee_match_any_operator_execute, m
    );

    if (rc != IB_OK) {
        ib_log_error(
            ib,
            "Failed to register ee_match_any operator: %s",
            ib_status_to_string(rc));
        return rc;
    }

    rc = ib_hook_tx_register(ib,
                             tx_finished_event,
                             ee_tx_finished_handler,
                             m);

    if (rc != IB_OK) {
        ib_log_error(
            ib,
            "Failed to register transaction finished event for ee_match_any operator: %s",
            ib_status_to_string(rc));
        return rc;
    }

    return IB_OK;
}