@@ -26,25 +26,25 @@ def skip_the_rule_with_context(rule, enforcers, app_features, app_capabilities):
2626 return False
2727
2828
29- def skip_the_rule_with_model_data (rule , enforcers , armor_profile_model ):
29+ def skip_the_rule_with_behavior_data (rule , enforcers , behavior_data ):
3030 if not has_common_item (enforcers , rule ["enforcers" ]):
3131 return True
3232
3333 if "conflicts" in rule :
3434 if "capabilities" in rule ["conflicts" ]:
35- model_caps = retrieve_capabilities_from_model ( armor_profile_model )
36- return has_common_item (rule ["conflicts" ]["capabilities" ], model_caps )
35+ caps = retrieve_capabilities_from_behavior_data ( behavior_data )
36+ return has_common_item (rule ["conflicts" ]["capabilities" ], caps )
3737
3838 if "syscalls" in rule ["conflicts" ]:
39- syscalls = retrieve_syscalls_from_model ( armor_profile_model )
39+ syscalls = retrieve_syscalls_from_behavior_data ( behavior_data )
4040 return has_common_item (rule ["conflicts" ]["syscalls" ], syscalls )
4141
4242 if "executions" in rule ["conflicts" ]:
43- executions = retrieve_executions_from_model ( armor_profile_model )
43+ executions = retrieve_executions_from_behavior_data ( behavior_data )
4444 return has_common_item (rule ["conflicts" ]["executions" ], executions )
4545
4646 if "files" in rule ["conflicts" ]:
47- files = retrieve_files_from_model ( armor_profile_model )
47+ files = retrieve_files_from_behavior_data ( behavior_data )
4848 return files_conflict_with_rule (rule ["conflicts" ]["files" ], files )
4949
5050 return False
@@ -59,7 +59,7 @@ def set_enforcer(policy, enforcers):
5959 policy ["enforcer" ] += "Seccomp"
6060
6161
62- def generate_policy_template (policy , built_in_rules , enforcers , app_features , app_capabilities , armor_profile_model , debug ):
62+ def generate_policy_template (policy , built_in_rules , enforcers , app_features , app_capabilities , behavior_data , debug ):
6363 if "privileged-container" in app_features :
6464 policy ["enhanceProtect" ]["privileged" ] = True
6565
@@ -69,8 +69,8 @@ def generate_policy_template(policy, built_in_rules, enforcers, app_features, ap
6969 if skip_the_rule_with_context (rule , enforcers , app_features , app_capabilities ):
7070 continue
7171
72- # Filter out the rule with behavior model data
73- if skip_the_rule_with_model_data (rule , enforcers , armor_profile_model ):
72+ # Filter out the rule with behavior data
73+ if skip_the_rule_with_behavior_data (rule , enforcers , behavior_data ):
7474 continue
7575
7676 set_enforcer (policy , enforcers )
@@ -85,8 +85,8 @@ def generate_policy_template(policy, built_in_rules, enforcers, app_features, ap
8585 if skip_the_rule_with_context (rule , enforcers , app_features , app_capabilities ):
8686 continue
8787
88- # Filter out the rule with behavior model data
89- if skip_the_rule_with_model_data (rule , enforcers , armor_profile_model ):
88+ # Filter out the rule with behavior data
89+ if skip_the_rule_with_behavior_data (rule , enforcers , behavior_data ):
9090 continue
9191
9292 set_enforcer (policy , enforcers )
@@ -101,8 +101,8 @@ def generate_policy_template(policy, built_in_rules, enforcers, app_features, ap
101101 if skip_the_rule_with_context (rule , enforcers , app_features , app_capabilities ):
102102 continue
103103
104- # Filter out the rule with behavior model data
105- if skip_the_rule_with_model_data (rule , enforcers , armor_profile_model ):
104+ # Filter out the rule with behavior data
105+ if skip_the_rule_with_behavior_data (rule , enforcers , behavior_data ):
106106 continue
107107
108108 set_enforcer (policy , enforcers )
@@ -115,8 +115,8 @@ def generate_policy_template(policy, built_in_rules, enforcers, app_features, ap
115115 if skip_the_rule_with_context (rule , enforcers , app_features , app_capabilities ):
116116 continue
117117
118- # Filter out the rule with behavior model data
119- if skip_the_rule_with_model_data (rule , enforcers , armor_profile_model ):
118+ # Filter out the rule with behavior data
119+ if skip_the_rule_with_behavior_data (rule , enforcers , behavior_data ):
120120 continue
121121
122122 set_enforcer (policy , enforcers )
@@ -129,8 +129,8 @@ def generate_policy_template(policy, built_in_rules, enforcers, app_features, ap
129129 if skip_the_rule_with_context (rule , enforcers , app_features , app_capabilities ):
130130 continue
131131
132- # Filter out the rule with behavior model data
133- if skip_the_rule_with_model_data (rule , enforcers , armor_profile_model ):
132+ # Filter out the rule with behavior data
133+ if skip_the_rule_with_behavior_data (rule , enforcers , behavior_data ):
134134 continue
135135
136136 set_enforcer (policy , enforcers )
@@ -140,15 +140,15 @@ def generate_policy_template(policy, built_in_rules, enforcers, app_features, ap
140140 # ========= Attack Protection - Disable Sensitive Operations =========
141141 # Note:
142142 # We use the built-in rules of the sensitive operation category
143- # only if the behavior model is provided.
144- if armor_profile_model :
143+ # only if the behavior data is provided.
144+ if behavior_data :
145145 for rule in built_in_rules ["sensitive_operations" ]:
146146 # Filter out the rule with context
147147 if skip_the_rule_with_context (rule , enforcers , app_features , app_capabilities ):
148148 continue
149149
150- # Filter out the rule with behavior model data
151- if skip_the_rule_with_model_data (rule , enforcers , armor_profile_model ):
150+ # Filter out the rule with behavior data
151+ if skip_the_rule_with_behavior_data (rule , enforcers , behavior_data ):
152152 continue
153153
154154 set_enforcer (policy , enforcers )
@@ -161,21 +161,23 @@ def generate_policy_template(policy, built_in_rules, enforcers, app_features, ap
161161 if skip_the_rule_with_context (rule , enforcers , app_features , app_capabilities ):
162162 continue
163163
164- # Filter out the rule with behavior model data
165- if skip_the_rule_with_model_data (rule , enforcers , armor_profile_model ):
164+ # Filter out the rule with behavior data
165+ if skip_the_rule_with_behavior_data (rule , enforcers , behavior_data ):
166166 continue
167167
168168 set_enforcer (policy , enforcers )
169169 policy ["enhanceProtect" ]["vulMitigationRules" ].append (rule ["id" ])
170170 debug_print (rule , debug )
171171
172172
173- def built_in_rules_advisor (built_in_rules , enforcers , app_features = [], app_capabilities = [], armor_profile_model = {}, debug = False ):
173+ def built_in_rules_advisor (built_in_rules , enforcers , app_features = [], app_capabilities = [], behavior_data = {}, debug = False ):
174174 policy = {
175175 "enforcer" : "" ,
176176 "mode" : "EnhanceProtect" ,
177177 "enhanceProtect" : {
178178 "privileged" : False ,
179+ "auditViolations" : True ,
180+ "allowViolations" : False ,
179181 "hardeningRules" : [],
180182 "attackProtectionRules" : [
181183 {
@@ -187,7 +189,7 @@ def built_in_rules_advisor(built_in_rules, enforcers, app_features=[], app_capab
187189 }
188190 }
189191
190- generate_policy_template (policy , built_in_rules , enforcers , app_features , app_capabilities , armor_profile_model , debug )
192+ generate_policy_template (policy , built_in_rules , enforcers , app_features , app_capabilities , behavior_data , debug )
191193
192194 print ('''
193195Please take note of the following tips about the template:
@@ -208,15 +210,15 @@ def built_in_rules_advisor(built_in_rules, enforcers, app_features=[], app_capab
208210
209211if __name__ == "__main__" :
210212 parser = argparse .ArgumentParser (formatter_class = RawTextHelpFormatter ,
211- description = '''This program can help users generate a `.spec.policy` template with built-in rules or the behavior model data.
213+ description = '''This program can help users generate a `.spec.policy` template with built-in rules or the behavior data.
212214The template can be a good start to craft the final policy. Please use the -f and -c command-line arguments to specify the context.
213215
214216use cases:
2152171). Generate a policy template that runs in EnhanceProtect mode with built-in rules supported by AppArmor and BPF enforcers.
216218 policy-advisor.py AppArmor,BPF -f share-containers-pid-ns -c sys_admin,net_admin,kill
217219
218- 2). Filter out the conflicted built-in rules with behavior model data to make the policy template more precise.
219- policy-advisor.py AppArmor,BPF -f share-containers-pid-ns -c sys_admin,net_admin,kill -m model_data .json
220+ 2). Filter out the conflicted built-in rules with behavior data to make the policy template more precise.
221+ policy-advisor.py AppArmor,BPF -f share-containers-pid-ns -c sys_admin,net_admin,kill -m data .json
220222''' )
221223
222224 parser .add_argument ("enforcers" , type = str ,
@@ -250,10 +252,10 @@ def built_in_rules_advisor(built_in_rules, enforcers, app_features=[], app_capab
250252Available Values: CAPABILITIES(7) without 'CAP_' prefix (they should be combined with commas).
251253For Example: "sys_admin,net_admin,sys_module"\n \n ''' )
252254
253- parser .add_argument ("-m" , dest = "behavior_model " , type = str , default = "" ,
254- help = '''The behavior model data is an ArmorProfileModel object that is generated by vArmor.
255- The input file must be in JSON format. You can export the data with kubectl command, such as:
256- kubectl get ArmorProfileModel -n {NAMESPACE} {NAME} -o json > model .json\n \n ''' )
255+ parser .add_argument ("-m" , dest = "behavior_data " , type = str , default = "" ,
256+ help = '''The behavior data is a JSON file that includes an ArmorProfileModel object.
257+ You can export the behavior data with kubectl command, such as:
258+ kubectl get ArmorProfileModel -n {NAMESPACE} {NAME} -o json > data .json\n \n ''' )
257259
258260 parser .add_argument ("-d" , dest = "debug" , action = "store_true" , default = False , help = "Print debug information." )
259261
@@ -266,16 +268,16 @@ def built_in_rules_advisor(built_in_rules, enforcers, app_features=[], app_capab
266268 if len (capabilities ) == 1 and '' in capabilities :
267269 capabilities = []
268270
269- if args .behavior_model and not os .path .exists (args .behavior_model ):
270- print ("[!] The model file isn't exist." )
271+ if args .behavior_data and not os .path .exists (args .behavior_data ):
272+ print ("[!] The behavior data file isn't exist." )
271273 sys .exit (1 )
272274
273275 with open (os .path .join (current_dir , "./built-in-rules.json" ), "r" ) as f :
274276 built_in_rules = json .load (f )
275277
276- if args .behavior_model :
277- with open (args .behavior_model , "r" ) as model_f :
278- armor_profile_model = json .load (model_f )
279- built_in_rules_advisor (built_in_rules , enforcers , features , capabilities , armor_profile_model , args .debug )
278+ if args .behavior_data :
279+ with open (args .behavior_data , "r" ) as model_f :
280+ behavior_data = json .load (model_f )
281+ built_in_rules_advisor (built_in_rules , enforcers , features , capabilities , behavior_data , args .debug )
280282 else :
281283 built_in_rules_advisor (built_in_rules , enforcers , features , capabilities , {}, args .debug )
0 commit comments