|
| 1 | +package carisma.check.mltop10checks.inputmanipulation; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import org.eclipse.emf.ecore.resource.Resource; |
| 8 | +import org.eclipse.uml2.uml.Artifact; |
| 9 | +import org.eclipse.uml2.uml.Package; |
| 10 | + |
| 11 | +import carisma.core.analysis.AnalysisHost; |
| 12 | +import carisma.core.analysis.DummyHost; |
| 13 | +import carisma.core.analysis.result.AnalysisResultMessage; |
| 14 | +import carisma.core.analysis.result.StatusType; |
| 15 | +import carisma.core.checks.CarismaCheckWithID; |
| 16 | +import carisma.core.checks.CheckParameter; |
| 17 | +import carisma.modeltype.uml2.UMLHelper; |
| 18 | +import carisma.profile.umlsec.mltop10.MLTop10; |
| 19 | +import carisma.profile.umlsec.mltop10.MLTop10Util; |
| 20 | + |
| 21 | +/** |
| 22 | + * analyzes an deployment diagram with respect to input manipulation rules. |
| 23 | + * |
| 24 | + * @author Alexander Peikert |
| 25 | + * |
| 26 | + */ |
| 27 | + |
| 28 | +public class InputManipulationCheck implements CarismaCheckWithID { |
| 29 | + |
| 30 | + public static final String CHECK_ID = "carisma.check.mltop10checks.inputmanipulation"; |
| 31 | + public static final String CHECK_NAME = "MLTop10 Input Manipulation Check"; |
| 32 | + |
| 33 | + /** |
| 34 | + * the model to check. |
| 35 | + */ |
| 36 | + private Package model = null; |
| 37 | + |
| 38 | + /** |
| 39 | + * AnalysisHost for report. |
| 40 | + */ |
| 41 | + private AnalysisHost analysisHost; |
| 42 | + private ArrayList<Artifact> aiapplicationList; |
| 43 | + private ArrayList<Artifact> mlmodelList; |
| 44 | + |
| 45 | + public InputManipulationCheck() { |
| 46 | + // TODO Auto-generated constructor stub |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public final boolean perform(final Map<String, CheckParameter> parameters, final AnalysisHost newHost) { |
| 51 | + if (newHost != null) { |
| 52 | + this.analysisHost = newHost; |
| 53 | + } else { |
| 54 | + this.analysisHost = new DummyHost(true); |
| 55 | + } |
| 56 | + Resource currentModel = this.analysisHost.getAnalyzedModel(); |
| 57 | + if (currentModel.getContents().isEmpty()) { |
| 58 | + this.analysisHost.addResultMessage(new AnalysisResultMessage(StatusType.WARNING, "Empty model")); |
| 59 | + this.analysisHost.appendLineToReport("Empty model"); |
| 60 | + return false; |
| 61 | + } |
| 62 | + if (currentModel.getContents().get(0) instanceof Package) { |
| 63 | + this.model = (Package) currentModel.getContents().get(0); |
| 64 | + return startCheck(); |
| 65 | + } |
| 66 | + this.analysisHost.addResultMessage(new AnalysisResultMessage(StatusType.WARNING, "Content is not a model!")); |
| 67 | + this.analysisHost.appendLineToReport("Content is not a model!"); |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * main function that starts the check. |
| 73 | + * |
| 74 | + * @return true if the model is correct according to data access control rules, |
| 75 | + * false otherwise |
| 76 | + */ |
| 77 | + |
| 78 | + public boolean startCheck() { |
| 79 | + |
| 80 | + boolean checkSuccessful = true; |
| 81 | + boolean warningPotentialAttack = false; |
| 82 | + int numMlModels = 0; |
| 83 | + int numApplication = 0; |
| 84 | + |
| 85 | + ArrayList<Artifact> artifactList = (ArrayList<Artifact>) UMLHelper.getAllElementsOfType(model, Artifact.class); |
| 86 | + ArrayList<Artifact> mlmodelList = new ArrayList<Artifact>(); |
| 87 | + ArrayList<Artifact> aiapplicationList = new ArrayList<Artifact>(); |
| 88 | + |
| 89 | + // ---------------------------------------------------------------------------------------------- |
| 90 | + // Check if there are more / less than one ML Model and AI Application |
| 91 | + |
| 92 | + for (int i = 0; i < artifactList.size(); i++) { |
| 93 | + if (MLTop10Util.hasStereotype(artifactList.get(i), MLTop10.MLModel)) { |
| 94 | + numMlModels++; |
| 95 | + mlmodelList.add(artifactList.get(i)); |
| 96 | + |
| 97 | + } |
| 98 | + if (MLTop10Util.hasStereotype(artifactList.get(i), MLTop10.AIApplication)) { |
| 99 | + numApplication++; |
| 100 | + aiapplicationList.add(artifactList.get(i)); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + if (numMlModels < 1 || numApplication < 1) { |
| 106 | + this.analysisHost.addResultMessage(new AnalysisResultMessage(StatusType.INFO, |
| 107 | + "Your model does not contain a ML Model or AI Application")); |
| 108 | + return checkSuccessful; |
| 109 | + } else if (numMlModels > 1 || numApplication > 1) { |
| 110 | + this.analysisHost.addResultMessage(new AnalysisResultMessage(StatusType.INFO, |
| 111 | + "Your model contains more than one ML Model or AI Application")); |
| 112 | + checkSuccessful = false; |
| 113 | + return checkSuccessful; |
| 114 | + } else { |
| 115 | + this.analysisHost.addResultMessage( |
| 116 | + new AnalysisResultMessage(StatusType.INFO, "Your model contains a ML Model and an AI Application")); |
| 117 | + Artifact mlmodel = mlmodelList.get(0); |
| 118 | + List<Object> taggedValueAdversialTraining = null; |
| 119 | + List<Object> taggedValueDefenseMechanism = null; |
| 120 | + taggedValueAdversialTraining = MLTop10Util.getTaggedValues("AdversarialTraining", MLTop10.MLModel, mlmodel); |
| 121 | + taggedValueDefenseMechanism = MLTop10Util.getTaggedValues("DefenseMechanism", MLTop10.MLModel, mlmodel); |
| 122 | + |
| 123 | + if (taggedValueAdversialTraining.get(0).toString() == "false") { |
| 124 | + this.analysisHost.addResultMessage(new AnalysisResultMessage(StatusType.WARNING, |
| 125 | + "Your model ML Model is not secured against adversarial training")); |
| 126 | + warningPotentialAttack = true; |
| 127 | + |
| 128 | + } |
| 129 | + if (taggedValueDefenseMechanism.get(0).toString() == "false") { |
| 130 | + this.analysisHost.addResultMessage(new AnalysisResultMessage(StatusType.WARNING, |
| 131 | + "Your model ML Model has no robust defense mechanism against manipulative attacks")); |
| 132 | + warningPotentialAttack = true; |
| 133 | + } |
| 134 | + |
| 135 | + Artifact aiapplication = aiapplicationList.get(0); |
| 136 | + List<Object> taggedValueInputValidation = null; |
| 137 | + taggedValueInputValidation = MLTop10Util.getTaggedValues("InputValidation", MLTop10.AIApplication, |
| 138 | + aiapplication); |
| 139 | + |
| 140 | + if (taggedValueInputValidation.get(0).toString() == "false") { |
| 141 | + this.analysisHost.addResultMessage(new AnalysisResultMessage(StatusType.WARNING, |
| 142 | + "Your model AI Application does not perform input validation")); |
| 143 | + warningPotentialAttack = true; |
| 144 | + } |
| 145 | + |
| 146 | + if (warningPotentialAttack == true) { |
| 147 | + this.analysisHost.addResultMessage( |
| 148 | + new AnalysisResultMessage(StatusType.WARNING, "An Input Mainpulation Attack is possible!")); |
| 149 | + } |
| 150 | + |
| 151 | + } |
| 152 | + return checkSuccessful; |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public String getCheckID() { |
| 158 | + // TODO Auto-generated method stub |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String getName() { |
| 164 | + // TODO Auto-generated method stub |
| 165 | + return null; |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments