summaryrefslogtreecommitdiff
path: root/src/analysis/scan/exprs
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2023-10-11 23:16:21 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2023-10-11 23:16:21 (GMT)
commitab6b87b7309e2d00926615f6557016bee6ab0b71 (patch)
treebf69b93d2f0548fc845e68ae1e8519bd33d1da24 /src/analysis/scan/exprs
parent404097b7b07b336b427b3682ec2bc48d831cb2e9 (diff)
Add two new functions to ROST grammar: modpath and maxcommon.
Diffstat (limited to 'src/analysis/scan/exprs')
-rw-r--r--src/analysis/scan/exprs/handler-int.h6
-rw-r--r--src/analysis/scan/exprs/handler.c132
-rw-r--r--src/analysis/scan/exprs/handler.h5
-rw-r--r--src/analysis/scan/exprs/literal.c52
4 files changed, 172 insertions, 23 deletions
diff --git a/src/analysis/scan/exprs/handler-int.h b/src/analysis/scan/exprs/handler-int.h
index f707fdb..a38dd03 100644
--- a/src/analysis/scan/exprs/handler-int.h
+++ b/src/analysis/scan/exprs/handler-int.h
@@ -37,7 +37,9 @@ struct _GScanPatternHandler
{
GScanExpression parent; /* A laisser en premier */
- GSearchPattern *pattern; /* Motif associé */
+ GSearchPattern **patterns; /* Motifs associés */
+ size_t count; /* Nombre de ces motifs */
+
ScanHandlerType type; /* Manipulation attendue */
};
@@ -51,7 +53,7 @@ struct _GScanPatternHandlerClass
/* Met en place une manipulation de correspondances établies. */
-bool g_scan_pattern_handler_create(GScanPatternHandler *, GSearchPattern *, ScanHandlerType);
+bool g_scan_pattern_handler_create(GScanPatternHandler *, GSearchPattern ** const, size_t, ScanHandlerType);
diff --git a/src/analysis/scan/exprs/handler.c b/src/analysis/scan/exprs/handler.c
index 1676522..1b6416e 100644
--- a/src/analysis/scan/exprs/handler.c
+++ b/src/analysis/scan/exprs/handler.c
@@ -122,7 +122,9 @@ static void g_scan_pattern_handler_class_init(GScanPatternHandlerClass *klass)
static void g_scan_pattern_handler_init(GScanPatternHandler *handler)
{
- handler->pattern = NULL;
+ handler->patterns = NULL;
+ handler->count = 0;
+
handler->type = SHT_RAW;
}
@@ -142,7 +144,10 @@ static void g_scan_pattern_handler_init(GScanPatternHandler *handler)
static void g_scan_pattern_handler_dispose(GScanPatternHandler *handler)
{
- g_clear_object(&handler->pattern);
+ size_t i; /* Boucle de parcours */
+
+ for (i = 0; i < handler->count; i++)
+ g_clear_object(&handler->patterns[i]);
G_OBJECT_CLASS(g_scan_pattern_handler_parent_class)->dispose(G_OBJECT(handler));
@@ -170,8 +175,9 @@ static void g_scan_pattern_handler_finalize(GScanPatternHandler *handler)
/******************************************************************************
* *
-* Paramètres : pattern = motif à impliquer. *
-* type = type de manipulation attendue. *
+* Paramètres : patterns = motifs à impliquer. *
+* count = quantité de ces motifs. *
+* type = type de manipulation attendue. *
* *
* Description : Met en place une manipulation de correspondances établies. *
* *
@@ -181,13 +187,13 @@ static void g_scan_pattern_handler_finalize(GScanPatternHandler *handler)
* *
******************************************************************************/
-GScanExpression *g_scan_pattern_handler_new(GSearchPattern *pattern, ScanHandlerType type)
+GScanExpression *g_scan_pattern_handler_new(GSearchPattern ** const patterns, size_t count, ScanHandlerType type)
{
GScanExpression *result; /* Structure à retourner */
result = g_object_new(G_TYPE_SCAN_PATTERN_HANDLER, NULL);
- if (!g_scan_pattern_handler_create(G_SCAN_PATTERN_HANDLER(result), pattern, type))
+ if (!g_scan_pattern_handler_create(G_SCAN_PATTERN_HANDLER(result), patterns, count, type))
g_clear_object(&result);
return result;
@@ -197,9 +203,10 @@ GScanExpression *g_scan_pattern_handler_new(GSearchPattern *pattern, ScanHandler
/******************************************************************************
* *
-* Paramètres : handler = instance à initialiser pleinement. *
-* pattern = motif à impliquer. *
-* type = type de manipulation attendue. *
+* Paramètres : handler = instance à initialiser pleinement. *
+* patterns = motifs à impliquer. *
+* count = quantité de ces motifs. *
+* type = type de manipulation attendue. *
* *
* Description : Met en place une manipulation de correspondances établies. *
* *
@@ -209,15 +216,22 @@ GScanExpression *g_scan_pattern_handler_new(GSearchPattern *pattern, ScanHandler
* *
******************************************************************************/
-bool g_scan_pattern_handler_create(GScanPatternHandler *handler, GSearchPattern *pattern, ScanHandlerType type)
+bool g_scan_pattern_handler_create(GScanPatternHandler *handler, GSearchPattern ** const patterns, size_t count, ScanHandlerType type)
{
bool result; /* Bilan à retourner */
+ size_t i; /* Boucle de parcours */
result = g_scan_expression_create(G_SCAN_EXPRESSION(handler), SRS_WAIT_FOR_SCAN);
if (!result) goto exit;
- handler->pattern = pattern;
- g_object_ref(G_OBJECT(pattern));
+ handler->patterns = malloc(count * sizeof(GSearchPattern *));
+ handler->count = count;
+
+ for (i = 0; i < count; i++)
+ {
+ handler->patterns[i] = patterns[i];
+ g_object_ref(G_OBJECT(patterns[i]));
+ }
handler->type = type;
@@ -228,6 +242,63 @@ bool g_scan_pattern_handler_create(GScanPatternHandler *handler, GSearchPattern
}
+/******************************************************************************
+* *
+* Paramètres : handler = instance à initialiser pleinement. *
+* ctx = contexte de suivi de l'analyse courante. *
+* count = quantité de correspondances enregistrées. [OUT] *
+* *
+* Description : Fournit la liste de toutes les correspondances représentées. *
+* *
+* Retour : Liste courante de correspondances établies. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+GScanMatch **g_scan_pattern_handler_get_all_matches(const GScanPatternHandler *handler, GScanContext *ctx, size_t *count)
+{
+ GScanMatch **result; /* Liste à retourner */
+ size_t used; /* Indice pour le stockage */
+ size_t i; /* Boucle de parcours #1 */
+ size_t partial; /* Décompte partiel */
+ const GScanMatch **matches; /* Correspondances en place */
+ size_t k; /* Boucle de parcours #2 */
+
+ result = NULL;
+
+ if (!g_scan_pattern_handler_count_items(handler, ctx, count))
+ {
+ *count = 0;
+ goto exit;
+ }
+
+ if (*count == 0)
+ goto exit;
+
+ result = malloc(*count * sizeof(GScanMatch *));
+
+ used = 0;
+
+ for (i = 0; i < handler->count; i++)
+ {
+ matches = g_scan_context_get_full_matches(ctx, handler->patterns[i], &partial);
+
+ for (k = 0; k < partial; k++)
+ {
+ result[used++] = matches[k];
+ g_object_ref(G_OBJECT(matches[k]));
+ }
+
+ }
+
+ exit:
+
+ return result;
+
+}
+
+
/* ---------------------------------------------------------------------------------- */
/* IMPLEMENTATION DES FONCTIONS DE CLASSE */
@@ -284,9 +355,7 @@ static bool g_scan_pattern_handler_reduce_to_boolean(const GScanPatternHandler *
bool result; /* Bilan à retourner */
size_t count; /* Quantité de correspondances */
- result = true;
-
- g_scan_context_get_full_matches(ctx, expr->pattern, &count);
+ result = g_scan_pattern_handler_count_items(expr, ctx, &count);
*out = g_scan_literal_expression_new(LVT_BOOLEAN, (bool []){ count > 0 });
@@ -312,12 +381,20 @@ static bool g_scan_pattern_handler_reduce_to_boolean(const GScanPatternHandler *
static bool g_scan_pattern_handler_count_items(const GScanPatternHandler *expr, GScanContext *ctx, size_t *count)
{
bool result; /* Bilan à retourner */
+ size_t partial; /* Décompte partiel */
+ size_t i; /* Boucle de parcours */
result = true;
assert(g_scan_context_is_scan_done(ctx));
- g_scan_context_get_full_matches(ctx, expr->pattern, count);
+ *count = 0;
+
+ for (i = 0; i < expr->count; i++)
+ {
+ g_scan_context_get_full_matches(ctx, expr->patterns[i], &partial);
+ *count += partial;
+ }
return result;
@@ -342,6 +419,7 @@ static bool g_scan_pattern_handler_count_items(const GScanPatternHandler *expr,
static bool g_scan_pattern_handler_get_item(const GScanPatternHandler *expr, size_t index, GScanContext *ctx, GScanExpression **out)
{
bool result; /* Bilan à retourner */
+ size_t i; /* Boucle de parcours */
size_t count; /* Quantité de correspondances */
const GScanMatch **matches; /* Correspondances en place */
const GScanBytesMatch *match; /* Correspondance ciblée */
@@ -353,18 +431,32 @@ static bool g_scan_pattern_handler_get_item(const GScanPatternHandler *expr, siz
const bin_t *data; /* Accès aux données brutes */
sized_string_t binary; /* Conversion de formats */
+ result = false;
+
assert(g_scan_context_is_scan_done(ctx));
- matches = g_scan_context_get_full_matches(ctx, expr->pattern, &count);
+ /* Identification du motif concerné */
- result = (index < count);
- if (!result) goto done;
+ for (i = 0; i < expr->count; i++)
+ {
+ matches = g_scan_context_get_full_matches(ctx, expr->patterns[i], &count);
+
+ if (index < count)
+ break;
+ else
+ index -= count;
+
+ }
+
+ if (i == expr->count) goto done;
result = G_IS_SCAN_BYTES_MATCH(matches[index]);
if (!result) goto done;
match = G_SCAN_BYTES_MATCH(matches[index]);
+ /* Traitement adapté de la requête */
+
len = g_scan_bytes_match_get_location(match, &start, &end);
switch (expr->type)
@@ -376,7 +468,7 @@ static bool g_scan_pattern_handler_get_item(const GScanPatternHandler *expr, siz
data = g_binary_content_get_raw_access(content, &pos, len);
- binary.data = data;
+ binary.static_bin_data = data;
binary.len = len;
*out = g_scan_literal_expression_new(LVT_STRING, &binary);
diff --git a/src/analysis/scan/exprs/handler.h b/src/analysis/scan/exprs/handler.h
index 8ad700a..36985e7 100644
--- a/src/analysis/scan/exprs/handler.h
+++ b/src/analysis/scan/exprs/handler.h
@@ -59,7 +59,10 @@ typedef enum _ScanHandlerType
GType g_scan_pattern_handler_get_type(void);
/* Met en place une manipulation de correspondances établies. */
-GScanExpression *g_scan_pattern_handler_new(GSearchPattern *, ScanHandlerType);
+GScanExpression *g_scan_pattern_handler_new(GSearchPattern ** const, size_t, ScanHandlerType);
+
+/* Fournit la liste de toutes les correspondances représentées. */
+GScanMatch **g_scan_pattern_handler_get_all_matches(const GScanPatternHandler *, GScanContext *, size_t *);
diff --git a/src/analysis/scan/exprs/literal.c b/src/analysis/scan/exprs/literal.c
index de7e32a..b1a094c 100644
--- a/src/analysis/scan/exprs/literal.c
+++ b/src/analysis/scan/exprs/literal.c
@@ -62,6 +62,9 @@ static bool g_scan_literal_expression_reduce_to_boolean(const GScanLiteralExpres
/* Dénombre les éléments portés par une expression. */
static bool g_scan_literal_expression_count(const GScanLiteralExpression *, GScanContext *, size_t *);
+/* Fournit un élément donné issu d'un ensemble constitué. */
+static bool g_scan_literal_expression_get_item(const GScanLiteralExpression *, size_t, GScanContext *, GScanExpression **);
+
/* ---------------------------------------------------------------------------------- */
@@ -100,6 +103,7 @@ static void g_scan_literal_expression_class_init(GScanLiteralExpressionClass *kl
expr->cmp_rich = (compare_expr_rich_fc)g_scan_literal_expression_compare_rich;
expr->reduce_to_bool = (reduce_expr_to_bool_fc)g_scan_literal_expression_reduce_to_boolean;
expr->count = (count_scan_expr_fc)g_scan_literal_expression_count;
+ expr->get = (get_scan_expr_fc)g_scan_literal_expression_get_item;
}
@@ -659,3 +663,51 @@ static bool g_scan_literal_expression_count(const GScanLiteralExpression *expr,
return result;
}
+
+
+/******************************************************************************
+* *
+* Paramètres : expr = expression à consulter. *
+* index = indice de l'élément à transférer. *
+* ctx = contexte de suivi de l'analyse courante. *
+* out = zone d'enregistrement de la réduction opérée. [OUT] *
+* *
+* Description : Fournit un élément donné issu d'un ensemble constitué. *
+* *
+* Retour : Bilan de l'opération : false en cas d'erreur irrécupérable. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static bool g_scan_literal_expression_get_item(const GScanLiteralExpression *expr, size_t index, GScanContext *ctx, GScanExpression **out)
+{
+ bool result; /* Bilan à retourner */
+ sized_string_t ch; /* Caractère extrait */
+
+ switch (expr->value_type)
+ {
+ case LVT_STRING:
+
+ result = (index < expr->value.string.len);
+
+ if (result)
+ {
+ ch.data = expr->value.string.data + index;
+ ch.len = 1;
+
+ *out = g_scan_literal_expression_new(LVT_STRING, &ch);
+
+ }
+
+ break;
+
+ default:
+ result = false;
+ break;
+
+ }
+
+ return result;
+
+}