-
Notifications
You must be signed in to change notification settings - Fork 180
/
yara-python.c
3257 lines (2706 loc) · 76.2 KB
/
yara-python.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2007-2022. The YARA Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
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
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* headers */
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h"
#if PY_VERSION_HEX >= 0x02060000
#include "bytesobject.h"
#include "structseq.h"
#elif PY_VERSION_HEX < 0x02060000
#define PyBytes_AsString PyString_AsString
#define PyBytes_Check PyString_Check
#define PyBytes_FromStringAndSize PyString_FromStringAndSize
#endif
#include <time.h>
#include <yara.h>
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif
#ifndef PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
#endif
#if PY_VERSION_HEX < 0x03020000
typedef long Py_hash_t;
#endif
#if PY_MAJOR_VERSION >= 3
#define PY_STRING(x) PyUnicode_DecodeUTF8(x, strlen(x), "ignore" )
#define PY_STRING_FORMAT(...) PyUnicode_FromFormat(__VA_ARGS__)
#define PY_STRING_TO_C(x) PyUnicode_AsUTF8(x)
#define PY_STRING_CHECK(x) PyUnicode_Check(x)
#else
#define PY_STRING(x) PyString_FromString(x)
#define PY_STRING_FORMAT(...) PyString_FromFormat(__VA_ARGS__)
#define PY_STRING_TO_C(x) PyString_AsString(x)
#define PY_STRING_CHECK(x) (PyString_Check(x) || PyUnicode_Check(x))
#endif
#if PY_VERSION_HEX < 0x03020000
#define PyDescr_NAME(x) (((PyDescrObject*)x)->d_name)
#endif
/* Module globals */
static PyObject* YaraError = NULL;
static PyObject* YaraSyntaxError = NULL;
static PyObject* YaraTimeoutError = NULL;
static PyObject* YaraWarningError = NULL;
#define YARA_DOC "\
This module allows you to apply YARA rules to files or strings.\n\
\n\
For complete documentation please visit:\n\
https://yara.readthedocs.io/en/stable/yarapython.html\n"
#if defined(_WIN32) || defined(__CYGWIN__)
#include <string.h>
#define strdup _strdup
#endif
// Match object
typedef struct
{
PyObject_HEAD
PyObject* rule;
PyObject* ns;
PyObject* tags;
PyObject* meta;
PyObject* strings;
} Match;
static PyMemberDef Match_members[] = {
{
"rule",
T_OBJECT_EX,
offsetof(Match, rule),
READONLY,
"Name of the matching rule"
},
{
"namespace",
T_OBJECT_EX,
offsetof(Match, ns),
READONLY,
"Namespace of the matching rule"
},
{
"tags",
T_OBJECT_EX,
offsetof(Match, tags),
READONLY,
"List of tags associated to the rule"
},
{
"meta",
T_OBJECT_EX,
offsetof(Match, meta),
READONLY,
"Dictionary with metadata associated to the rule"
},
{
"strings",
T_OBJECT_EX,
offsetof(Match, strings),
READONLY,
"Tuple with offsets and strings that matched the file"
},
{ NULL } // End marker
};
static PyObject* Match_NEW(
const char* rule,
const char* ns,
PyObject* tags,
PyObject* meta,
PyObject* strings);
static void Match_dealloc(
PyObject* self);
static PyObject* Match_repr(
PyObject* self);
static PyObject* Match_getattro(
PyObject* self,
PyObject* name);
static PyObject* Match_richcompare(
PyObject* self,
PyObject* other,
int op);
static Py_hash_t Match_hash(
PyObject* self);
static PyMethodDef Match_methods[] =
{
{ NULL },
};
static PyTypeObject Match_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"yara.Match", /*tp_name*/
sizeof(Match), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Match_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
Match_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
Match_hash, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
Match_getattro, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"Match class", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
Match_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Match_methods, /* tp_methods */
Match_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
// StringMatch object
typedef struct
{
PyObject_HEAD
PyObject* identifier;
PyObject* instances;
// This is not exposed directly because it contains flags that are internal
// to yara (eg: STRING_FLAGS_FITS_IN_ATOM) along with modifiers
// (eg: STRING_FLAGS_XOR).
uint64_t flags;
} StringMatch;
static PyMemberDef StringMatch_members[] = {
{
"identifier",
T_OBJECT_EX,
offsetof(StringMatch, identifier),
READONLY,
"Name of the matching string"
},
{
"instances",
T_OBJECT_EX,
offsetof(StringMatch, instances),
READONLY,
"StringMatchInstance objects of the matching string"
},
{ NULL } // End marker
};
static PyObject* StringMatch_NEW(
const char* identifier,
uint64_t flags,
PyObject* instance_list);
static void StringMatch_dealloc(
PyObject* self);
static PyObject* StringMatch_repr(
PyObject* self);
static PyObject* StringMatch_getattro(
PyObject* self,
PyObject* name);
static Py_hash_t StringMatch_hash(
PyObject* self);
static PyObject* StringMatch_is_xor(
PyObject* self,
PyObject* args);
static PyMethodDef StringMatch_methods[] =
{
{
"is_xor",
(PyCFunction) StringMatch_is_xor,
METH_NOARGS,
"Return true if a string has the xor modifier"
},
{ NULL },
};
static PyTypeObject StringMatch_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"yara.StringMatch", /*tp_name*/
sizeof(StringMatch), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)StringMatch_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
StringMatch_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
StringMatch_hash, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
StringMatch_getattro, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"StringMatch class", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */ // XXX: Implement richcompare?
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
StringMatch_methods, /* tp_methods */
StringMatch_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
// StringMatchInstance object
typedef struct
{
PyObject_HEAD
PyObject* offset;
PyObject* matched_data;
PyObject* matched_length;
PyObject* xor_key;
} StringMatchInstance;
static PyMemberDef StringMatchInstance_members[] = {
{
"offset",
T_OBJECT_EX,
offsetof(StringMatchInstance, offset),
READONLY,
"Offset of the matched data"
},
{
"matched_data",
T_OBJECT_EX,
offsetof(StringMatchInstance, matched_data),
READONLY,
"Matched data"
},
{
"matched_length",
T_OBJECT_EX,
offsetof(StringMatchInstance, matched_length),
READONLY,
"Length of matched data"
},
{
"xor_key",
T_OBJECT_EX,
offsetof(StringMatchInstance, xor_key),
READONLY,
"XOR key found for xor strings"
},
{ NULL } // End marker
};
static PyObject* StringMatchInstance_NEW(
uint64_t offset,
PyObject* matched_data,
int32_t match_length,
uint8_t xor_key);
static void StringMatchInstance_dealloc(
PyObject* self);
static PyObject* StringMatchInstance_repr(
PyObject* self);
static PyObject* StringMatchInstance_getattro(
PyObject* self,
PyObject* name);
static Py_hash_t StringMatchInstance_hash(
PyObject* self);
static PyObject* StringMatchInstance_plaintext(
PyObject* self,
PyObject* args);
static PyMethodDef StringMatchInstance_methods[] =
{
{
"plaintext",
(PyCFunction) StringMatchInstance_plaintext,
METH_NOARGS,
"Return matched data after xor key applied."
},
{ NULL },
};
static PyTypeObject StringMatchInstance_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"yara.StringMatchInstance", /*tp_name*/
sizeof(StringMatchInstance), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)StringMatchInstance_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
StringMatchInstance_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
StringMatchInstance_hash, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
StringMatchInstance_getattro, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"StringMatchInstance class", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */ // XXX: Implement richcompare?
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
StringMatchInstance_methods, /* tp_methods */
StringMatchInstance_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
// Rule object
typedef struct
{
PyObject_HEAD
PyObject* identifier;
PyObject* tags;
PyObject* meta;
PyObject* global;
PyObject* private;
} Rule;
static void Rule_dealloc(
PyObject* self);
static PyObject* Rule_getattro(
PyObject* self,
PyObject* name);
static PyMemberDef Rule_members[] = {
{
"is_global",
T_OBJECT_EX,
offsetof(Rule, global),
READONLY,
"Rule is global"
},
{
"is_private",
T_OBJECT_EX,
offsetof(Rule, private),
READONLY,
"Rule is private"
},
{
"identifier",
T_OBJECT_EX,
offsetof(Rule, identifier),
READONLY,
"Name of the rule"
},
{
"tags",
T_OBJECT_EX,
offsetof(Rule, tags),
READONLY,
"Tags for the rule"
},
{
"meta",
T_OBJECT_EX,
offsetof(Rule, meta),
READONLY,
"Meta for the rule"
},
{ NULL } // End marker
};
static PyMethodDef Rule_methods[] =
{
{ NULL, NULL }
};
static PyTypeObject Rule_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"yara.Rule", /*tp_name*/
sizeof(Rule), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor) Rule_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
Rule_getattro, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"Rule class", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Rule_methods, /* tp_methods */
Rule_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
// Rules object
typedef struct
{
PyObject_HEAD
PyObject* externals;
PyObject* warnings;
YR_RULES* rules;
YR_RULE* iter_current_rule;
} Rules;
static Rules* Rules_NEW(void);
static void Rules_dealloc(
PyObject* self);
static PyObject* Rules_match(
PyObject* self,
PyObject* args,
PyObject* keywords);
static PyObject* Rules_save(
PyObject* self,
PyObject* args,
PyObject* keywords);
static PyObject* Rules_profiling_info(
PyObject* self,
PyObject* args);
static PyObject* Rules_getattro(
PyObject* self,
PyObject* name);
static PyObject* Rules_next(
PyObject* self);
static PyMemberDef Rules_members[] = {
{
"warnings",
T_OBJECT_EX,
offsetof(Rules, warnings),
READONLY,
"List of compiler warnings"
},
{ NULL } // End marker
};
static PyMethodDef Rules_methods[] =
{
{
"match",
(PyCFunction) Rules_match,
METH_VARARGS | METH_KEYWORDS
},
{
"save",
(PyCFunction) Rules_save,
METH_VARARGS | METH_KEYWORDS
},
{
"profiling_info",
(PyCFunction) Rules_profiling_info,
METH_NOARGS
},
{
NULL,
NULL
}
};
static PyTypeObject Rules_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"yara.Rules", /*tp_name*/
sizeof(Rules), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor) Rules_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
Rules_getattro, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"Rules class", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc) Rules_next, /* tp_iternext */
Rules_methods, /* tp_methods */
Rules_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
typedef struct _CALLBACK_DATA
{
PyObject* matches;
PyObject* callback;
PyObject* modules_data;
PyObject* modules_callback;
PyObject* warnings_callback;
PyObject* console_callback;
int which;
bool allow_duplicate_metadata;
} CALLBACK_DATA;
static PyStructSequence_Field RuleString_Fields[] = {
{"namespace", "Namespace of the rule"},
{"rule", "Identifier of the rule"},
{"string", "Identifier of the string"},
{NULL}
};
static PyStructSequence_Desc RuleString_Desc = {
"RuleString",
"Named tuple tying together rule identifier and string identifier",
RuleString_Fields,
(sizeof(RuleString_Fields) / sizeof(RuleString_Fields[0])) - 1
};
static PyTypeObject RuleString_Type = {0};
// Forward declarations for handling module data.
PyObject* convert_structure_to_python(
YR_OBJECT_STRUCTURE* structure);
PyObject* convert_array_to_python(
YR_OBJECT_ARRAY* array);
PyObject* convert_dictionary_to_python(
YR_OBJECT_DICTIONARY* dictionary);
PyObject* convert_object_to_python(
YR_OBJECT* object)
{
PyObject* result = NULL;
if (object == NULL)
return NULL;
switch(object->type)
{
case OBJECT_TYPE_INTEGER:
if (object->value.i != YR_UNDEFINED)
result = Py_BuildValue("l", object->value.i);
break;
case OBJECT_TYPE_STRING:
if (object->value.ss != NULL)
result = PyBytes_FromStringAndSize(
object->value.ss->c_string,
object->value.ss->length);
break;
case OBJECT_TYPE_STRUCTURE:
result = convert_structure_to_python(object_as_structure(object));
break;
case OBJECT_TYPE_ARRAY:
result = convert_array_to_python(object_as_array(object));
break;
case OBJECT_TYPE_FUNCTION:
// Do nothing with functions...
break;
case OBJECT_TYPE_DICTIONARY:
result = convert_dictionary_to_python(object_as_dictionary(object));
break;
case OBJECT_TYPE_FLOAT:
if (!isnan(object->value.d))
result = Py_BuildValue("d", object->value.d);
break;
default:
break;
}
return result;
}
PyObject* convert_structure_to_python(
YR_OBJECT_STRUCTURE* structure)
{
YR_STRUCTURE_MEMBER* member;
PyObject* py_object;
PyObject* py_dict = PyDict_New();
if (py_dict == NULL)
return py_dict;
member = structure->members;
while (member != NULL)
{
py_object = convert_object_to_python(member->object);
if (py_object != NULL)
{
PyDict_SetItemString(py_dict, member->object->identifier, py_object);
Py_DECREF(py_object);
}
member =member->next;
}
return py_dict;
}
PyObject* convert_array_to_python(
YR_OBJECT_ARRAY* array)
{
PyObject* py_object;
PyObject* py_list = PyList_New(0);
if (py_list == NULL)
return py_list;
// If there is nothing in the list, return an empty Python list
if (array->items == NULL)
return py_list;
for (int i = 0; i < array->items->length; i++)
{
py_object = convert_object_to_python(array->items->objects[i]);
if (py_object != NULL)
{
PyList_Append(py_list, py_object);
Py_DECREF(py_object);
}
}
return py_list;
}
PyObject* convert_dictionary_to_python(
YR_OBJECT_DICTIONARY* dictionary)
{
PyObject* py_object;
PyObject* py_dict = PyDict_New();
if (py_dict == NULL)
return py_dict;
// If there is nothing in the YARA dictionary, return an empty Python dict
if (dictionary->items == NULL)
return py_dict;
for (int i = 0; i < dictionary->items->used; i++)
{
py_object = convert_object_to_python(dictionary->items->objects[i].obj);
if (py_object != NULL)
{
PyDict_SetItemString(
py_dict,
dictionary->items->objects[i].key->c_string,
py_object);
Py_DECREF(py_object);
}
}
return py_dict;
}
static int handle_import_module(
YR_MODULE_IMPORT* module_import,
CALLBACK_DATA* data)
{
if (data->modules_data == NULL)
return CALLBACK_CONTINUE;
PyGILState_STATE gil_state = PyGILState_Ensure();
PyObject* module_data = PyDict_GetItemString(
data->modules_data,
module_import->module_name);
#if PY_MAJOR_VERSION >= 3
if (module_data != NULL && PyBytes_Check(module_data))
#else
if (module_data != NULL && PyString_Check(module_data))
#endif
{
Py_ssize_t data_size;
#if PY_MAJOR_VERSION >= 3
PyBytes_AsStringAndSize(
module_data,
(char**) &module_import->module_data,
&data_size);
#else
PyString_AsStringAndSize(
module_data,
(char**) &module_import->module_data,
&data_size);
#endif
module_import->module_data_size = data_size;
}
PyGILState_Release(gil_state);
return CALLBACK_CONTINUE;
}
static int handle_module_imported(
void* message_data,
CALLBACK_DATA* data)
{
if (data->modules_callback == NULL)
return CALLBACK_CONTINUE;
PyGILState_STATE gil_state = PyGILState_Ensure();
PyObject* module_info_dict = convert_structure_to_python(
object_as_structure(message_data));
if (module_info_dict == NULL)
{
PyGILState_Release(gil_state);
return CALLBACK_CONTINUE;
}
PyObject* object = PY_STRING(object_as_structure(message_data)->identifier);
PyDict_SetItemString(module_info_dict, "module", object);
Py_DECREF(object);
Py_INCREF(data->modules_callback);
PyObject* callback_result = PyObject_CallFunctionObjArgs(
data->modules_callback,
module_info_dict,
NULL);
int result = CALLBACK_CONTINUE;
if (callback_result != NULL)
{
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check(callback_result))
#else
if (PyLong_Check(callback_result) || PyInt_Check(callback_result))
#endif
{
result = (int) PyLong_AsLong(callback_result);
}
}
else
{
result = CALLBACK_ERROR;
}
Py_XDECREF(callback_result);
Py_DECREF(module_info_dict);
Py_DECREF(data->modules_callback);
PyGILState_Release(gil_state);
return result;
}
static int handle_console_log(
void* message_data,
CALLBACK_DATA* data)
{
PyGILState_STATE gil_state = PyGILState_Ensure();
int result = CALLBACK_CONTINUE;
if (data->console_callback == NULL)
{
// If the user does not specify a console callback we dump to stdout.
// If we want to support 3.2 and newer only we can use
// https://docs.python.org/3/c-api/sys.html?highlight=stdout#c.PySys_FormatStdout
// instead of this call with the limit.
PySys_WriteStdout("%.1000s\n", (char*) message_data);
}
else
{
PyObject* log_string = PY_STRING((char*) message_data);
Py_INCREF(data->console_callback);
PyObject* callback_result = PyObject_CallFunctionObjArgs(
data->console_callback,
log_string,
NULL);
if (callback_result != NULL)
{
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check(callback_result))
#else
if (PyLong_Check(callback_result) || PyInt_Check(callback_result))
#endif
{
result = (int) PyLong_AsLong(callback_result);
}
}
else
{
result = CALLBACK_ERROR;
}
Py_DECREF(log_string);
Py_XDECREF(callback_result);
Py_DECREF(data->console_callback);
}
PyGILState_Release(gil_state);
return result;
}
static int handle_too_many_matches(
YR_SCAN_CONTEXT* context,
YR_STRING* string,
CALLBACK_DATA* data)
{
PyGILState_STATE gil_state = PyGILState_Ensure();
PyObject* warning_type = NULL;
PyObject* string_identifier = NULL;
PyObject* rule_identifier = NULL;
PyObject* namespace_identifier = NULL;
PyObject* rule_string = NULL;
YR_RULE* rule = NULL;
int result = CALLBACK_CONTINUE;