Skip to content

Commit c06e9dd

Browse files
authored
Merge pull request #106 from lostsnow/feature/concat-hook
Feature/concat hook
2 parents 6e39ab5 + fe9c028 commit c06e9dd

File tree

6 files changed

+123
-8
lines changed

6 files changed

+123
-8
lines changed

dongtai_agent_python/assess/c_api_hook.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
'builtins.bytes.__new__': 'callback_bytes_cast',
1010
'builtins.bytearray.__init__': 'callback_bytearray_cast',
1111
'builtins.str.__new__': 'callback_unicode_cast',
12+
'builtins.bytes.__add__': 'callback_bytes_concat',
13+
'builtins.bytearray.__add__': 'callback_bytearray_concat',
14+
'builtins.str.__add__': 'callback_unicode_concat',
1215
}
1316

1417

dongtai_agent_python/assess_ext/include/patch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ PyObject *str_origin(PyObject *self, PyObject *args);
2121
int apply_cformat_patch(funchook_t *funchook);
2222
int apply_fstring_patch(funchook_t *funchook);
2323
int apply_cast_patch(funchook_t *funchook);
24+
int apply_concat_patch(funchook_t *funchook);
2425

2526
#define BUILD_NEW_BINARYFUNC(NAME) \
2627
static PyObject *NAME##_new(PyObject *self, PyObject *args) { \

dongtai_agent_python/assess_ext/patch.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ PyObject *enable_patches(PyObject *self, PyObject *arg) {
6969
apply_patch(apply_cformat_patch, funchook);
7070
apply_patch(apply_fstring_patch, funchook);
7171
apply_patch(apply_cast_patch, funchook);
72+
apply_patch(apply_concat_patch, funchook);
7273

7374
Py_RETURN_NONE;
7475
}
@@ -90,6 +91,10 @@ PyObject *install(PyObject *self, PyObject *arg) {
9091
}
9192

9293
void patch_string_callback(char *prop_method_name, PyObject *source, PyObject *target, PyObject *hook_args, PyObject *hook_kwargs) {
94+
if (!PyObject_HasAttrString(patch_module, prop_method_name)) {
95+
return;
96+
}
97+
9398
PyObject *result;
9499
PyObject *prop_hook_args;
95100
int free_hook_args = 0;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#define PY_SSIZE_T_CLEAN
2+
#include <Python.h>
3+
4+
#include <funchook.h>
5+
#include <logger.h>
6+
#include <utils.h>
7+
#include <patch.h>
8+
9+
binaryfunc bytes_concat_origin;
10+
binaryfunc bytearray_concat_origin;
11+
binaryfunc bytearray_inplace_concat_origin;
12+
binaryfunc unicode_concat_origin;
13+
void (*unicode_append_origin)(PyObject **l, PyObject *r);
14+
15+
PyObject *bytes_concat_new(PyObject *l, PyObject *r) {
16+
PyObject *result = bytes_concat_origin(l, r);
17+
18+
if (result == NULL) {
19+
return result;
20+
}
21+
22+
patch_string_callback("callback_bytes_concat", l, result, r, NULL);
23+
24+
return result;
25+
}
26+
27+
PyObject *bytearray_concat_new(PyObject *l, PyObject *r) {
28+
PyObject *result = bytearray_concat_origin(l, r);
29+
30+
if (result == NULL) {
31+
return result;
32+
}
33+
34+
patch_string_callback("callback_bytearray_concat", l, result, r, NULL);
35+
36+
return result;
37+
}
38+
39+
PyObject *bytearray_inplace_concat_new(PyObject *l, PyObject *r) {
40+
PyObject *result = bytearray_inplace_concat_origin(l, r);
41+
42+
if (result == NULL) {
43+
return result;
44+
}
45+
46+
patch_string_callback("callback_bytearray_concat", l, result, r, NULL);
47+
48+
return result;
49+
}
50+
51+
PyObject *unicode_concat_new(PyObject *l, PyObject *r) {
52+
PyObject *result = unicode_concat_origin(l, r);
53+
54+
if (result == NULL) {
55+
return result;
56+
}
57+
58+
patch_string_callback("callback_unicode_concat", l, result, r, NULL);
59+
60+
return result;
61+
}
62+
63+
void unicode_append_new(PyObject **l, PyObject *r) {
64+
PyObject *origin_l = *l;
65+
Py_XINCREF(origin_l);
66+
unicode_append_origin(l, r);
67+
68+
if (*l == NULL) {
69+
Py_XDECREF(origin_l);
70+
return;
71+
}
72+
73+
patch_string_callback("callback_unicode_concat", origin_l, *l, r, NULL);
74+
Py_XDECREF(origin_l);
75+
}
76+
77+
int apply_concat_patch(funchook_t *funchook) {
78+
bytes_concat_origin = PyBytes_Type.tp_as_sequence->sq_concat;
79+
funchook_prepare_wrapper(funchook, &bytes_concat_origin, bytes_concat_new);
80+
81+
bytearray_concat_origin = PyByteArray_Concat;
82+
funchook_prepare_wrapper(funchook, &bytearray_concat_origin, bytearray_concat_new);
83+
84+
bytearray_inplace_concat_origin = PyByteArray_Type.tp_as_sequence->sq_inplace_concat;
85+
funchook_prepare_wrapper(funchook, &bytearray_inplace_concat_origin, bytearray_inplace_concat_new);
86+
87+
unicode_concat_origin = PyUnicode_Concat;
88+
funchook_prepare_wrapper(funchook, &unicode_concat_origin, unicode_concat_new);
89+
90+
unicode_append_origin = PyUnicode_Append;
91+
funchook_prepare_wrapper(funchook, &unicode_append_origin, unicode_append_new);
92+
93+
log_debug("------c_patch------------------ concat");
94+
95+
return 0;
96+
}

dongtai_agent_python/policy_api.json

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@
543543
"source": "P",
544544
"track": "true",
545545
"target": "R",
546-
"value": "builtins.str.__format__",
546+
"value": "builtins.str.__add__",
547547
"inherit": "false"
548548
},
549549
{
@@ -581,13 +581,6 @@
581581
"value": "builtins.str.cformat",
582582
"inherit": "false"
583583
},
584-
{
585-
"source": "P",
586-
"track": "true",
587-
"target": "R",
588-
"value": "builtins.str.concat",
589-
"inherit": "false"
590-
},
591584
{
592585
"source": "P1",
593586
"track": "true",
@@ -756,6 +749,13 @@
756749
"value": "builtins.str.zfill",
757750
"inherit": "false"
758751
},
752+
{
753+
"source": "P",
754+
"track": "true",
755+
"target": "R",
756+
"value": "builtins.bytes.__add__",
757+
"inherit": "false"
758+
},
759759
{
760760
"source": "P1,source",
761761
"track": "true",
@@ -777,6 +777,13 @@
777777
"value": "builtins.bytes.decode",
778778
"inherit": "false"
779779
},
780+
{
781+
"source": "P",
782+
"track": "true",
783+
"target": "R",
784+
"value": "builtins.bytearray.__add__",
785+
"inherit": "false"
786+
},
780787
{
781788
"source": "P1,source",
782789
"track": "true",

dongtai_agent_python/setting/const.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
'builtins.str.__new__',
2929
'builtins.bytes.__new__',
3030
'builtins.bytearray.__init__',
31+
'builtins.str.__add__',
32+
'builtins.bytes.__add__',
33+
'builtins.bytearray.__add__',
3134
]
3235

3336
CRYPTO_BAD_CIPHER_NEW = [

0 commit comments

Comments
 (0)