Skip to content

Instantly share code, notes, and snippets.

@p0pr0ck5
Last active April 4, 2018 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p0pr0ck5/9b2c414641c9b03d527679d0c8cb7d86 to your computer and use it in GitHub Desktop.
Save p0pr0ck5/9b2c414641c9b03d527679d0c8cb7d86 to your computer and use it in GitHub Desktop.
poprocks@mini-vm:~/src/ModSecurity/examples/simple_example_using_c$ cat test.c
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact Trustwave Holdings, Inc.
* directly using the email address security@modsecurity.org.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
#include "modsecurity/modsecurity.h"
#include "modsecurity/rules.h"
char main_rule_uri[] = "basic_rules.conf";
uint64_t n, bail;
void
alrm_handler(int sig) {
printf("Did %lu\n", n);
bail = 1;
}
void cb(void *log, const void *data)
{
// swallow it
return;
}
int main (int argc, char **argv)
{
int ret;
const char *error = NULL;
ModSecurity *modsec;
Transaction *transaction = NULL;
Rules *rules;
n = 0;
signal(SIGALRM, alrm_handler);
modsec = msc_init();
msc_set_connector_info(modsec, "ModSecurity-test v0.0.1-alpha (Simple " \
"example on how to use ModSecurity API");
rules = msc_create_rules_set();
ret = msc_rules_add_file(rules, main_rule_uri, &error);
if (ret < 0) {
fprintf(stderr, "Problems loading the rules --\n");
fprintf(stderr, "%s\n", error);
goto end;
}
msc_rules_dump(rules);
msc_set_log_cb(modsec, cb);
alarm(1);
for (;;) {
if (bail) {
goto end;
}
transaction = msc_new_transaction(modsec, rules, NULL);
msc_process_connection(transaction, "127.0.0.1", 12345, "127.0.0.1", 80);
msc_process_uri(transaction,
"http://www.modsecurity.org/test?key1=value1&key2=value2&key3=value3",
"GET", "1.1");
msc_process_request_headers(transaction);
msc_process_request_body(transaction);
msc_process_response_headers(transaction, 200, "HTTP 1.1");
msc_process_response_body(transaction);
msc_process_logging(transaction);
++n;
}
end:
msc_rules_cleanup(rules);
msc_cleanup(modsec);
printf("Done!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment