1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Apache License, Version 2.0
6 * which accompanies this distribution, and is available at
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *******************************************************************************/
9 package it.polito.verigraph.mcnet.netobjs;
11 import java.util.ArrayList;
12 import java.util.List;
13 import com.microsoft.z3.BoolExpr;
14 import com.microsoft.z3.Context;
15 import com.microsoft.z3.DatatypeExpr;
16 import com.microsoft.z3.Expr;
17 import com.microsoft.z3.FuncDecl;
18 import com.microsoft.z3.IntExpr;
19 import com.microsoft.z3.Solver;
20 import it.polito.verigraph.mcnet.components.NetContext;
21 import it.polito.verigraph.mcnet.components.Network;
22 import it.polito.verigraph.mcnet.components.NetworkObject;
24 public class PolitoIDS extends NetworkObject {
26 public static final int DROGA = 1; //no go
27 public static final int GATTINI = 2; //go
30 List<BoolExpr> constraints = new ArrayList<BoolExpr>();
31 DatatypeExpr politoIDS;
34 FuncDecl isInBlacklist;
37 public PolitoIDS(Context ctx, Object[]...args){
42 public DatatypeExpr getZ3Node() {
47 protected void init(Context ctx, Object[]... args) {
50 this.isEndHost = false;
51 this.politoIDS = this.z3Node = ((NetworkObject)args[0][0]).getZ3Node();
52 this.net = (Network)args[0][1];
53 this.nctx = (NetContext)args[0][2];
58 protected void addConstraints(Solver solver) {
59 BoolExpr[] constr = new BoolExpr[constraints.size()];
60 solver.add(constraints.toArray(constr));
64 public void installIDS(int[] blackList){
65 Expr n_0 = ctx.mkConst(politoIDS + "_n_0", nctx.node);
66 Expr n_1 = ctx.mkConst(politoIDS + "_n_1", nctx.node);
67 Expr p_0 = ctx.mkConst(politoIDS + "_p_0", nctx.packet);
68 IntExpr t_0 = ctx.mkIntConst(politoIDS + "_t_0");
69 IntExpr t_1 = ctx.mkIntConst(politoIDS + "_t_1");
70 Expr b_0 = ctx.mkIntConst(politoIDS + "_b_0");
72 isInBlacklist = ctx.mkFuncDecl(politoIDS + "_isInBlacklist", ctx.mkIntSort(), ctx.mkBoolSort());
75 BoolExpr[] blConstraints = new BoolExpr[blackList.length];
76 if(blackList.length != 0){
78 for(int i = 0; i<blackList.length; i++)
79 blConstraints[i] = ctx.mkEq(b_0, ctx.mkInt(blackList[i]));
81 this.constraints.add(ctx.mkForall(new Expr[]{b_0},
82 ctx.mkIff((BoolExpr)isInBlacklist.apply(b_0), ctx.mkOr(blConstraints)),
84 null, null, null, null));
87 this.constraints.add(ctx.mkForall(new Expr[]{b_0},
88 ctx.mkEq(isInBlacklist.apply(b_0), ctx.mkBool(false)),
90 null, null, null, null));
93 //Constraint2 send(politoIDS, n_0, p, t_0) && (p.proto(HTTP_RESPONSE) || p.proto(HTTP_REQUEST)) ->
94 //(exist n_1,t_1 : (recv(n_1, politoIDS, p, t_1) && t_1 < t_0)) && !isInBlackList(p.body)
96 this.constraints.add(ctx.mkForall(new Expr[]{n_0, p_0, t_0},
97 ctx.mkImplies(ctx.mkAnd((BoolExpr)nctx.send.apply(politoIDS, n_0, p_0, t_0), ctx.mkOr(ctx.mkEq(nctx.pf.get("proto").apply(p_0), ctx.mkInt(nctx.HTTP_RESPONSE)), ctx.mkEq(nctx.pf.get("proto").apply(p_0), ctx.mkInt(nctx.HTTP_REQUEST)))),
98 ctx.mkAnd(ctx.mkExists(new Expr[]{n_1,t_1},
99 ctx.mkAnd((BoolExpr)nctx.recv.apply(n_1,politoIDS,p_0,t_1),ctx.mkLt(t_1, t_0)),
101 null, null, null, null),
102 ctx.mkNot((BoolExpr)isInBlacklist.apply(nctx.pf.get("body").apply(p_0))))),
104 null, null, null, null));
106 //Constraint3 send(politoIDS, n_0, p, t_0) && p.proto(HTTP_REQUEST) ->
107 //(exist n_1,t_1 : (recv(n_1, politoIDS, p, t_1) && t_1 < t_0)) Constraint not needed anymore (included in contr. 2)
108 /* this.constraints.add(ctx.mkForall(new Expr[]{n_0, p_0, t_0},
109 ctx.mkImplies(ctx.mkAnd((BoolExpr)nctx.send.apply(politoIDS, n_0, p_0, t_0), ctx.mkEq(nctx.pf.get("proto").apply(p_0), ctx.mkInt(nctx.HTTP_REQUEST))),
110 ctx.mkAnd(ctx.mkExists(new Expr[]{n_1,t_1},
111 ctx.mkAnd((BoolExpr)nctx.recv.apply(n_1,politoIDS,p_0,t_1),ctx.mkLt(t_1, t_0)),
113 null, null, null, null))),
115 null, null, null, null));
118 //Constraint5send(politoIDS, n_0, p, t_0) -> p.proto == HTTP_REQ || p.protpo == HTTP_RESP
120 this.constraints.add(ctx.mkForall(new Expr[]{n_0, p_0, t_0},
121 ctx.mkImplies((BoolExpr)nctx.send.apply(politoIDS, n_0, p_0, t_0),
122 ctx.mkOr(ctx.mkEq(nctx.pf.get("proto").apply(p_0), ctx.mkInt(nctx.HTTP_REQUEST)),
123 ctx.mkEq(nctx.pf.get("proto").apply(p_0), ctx.mkInt(nctx.HTTP_RESPONSE)))),
125 null,null,null,null));
127 //Constraint6send(politoIDS, n_0, p, t_0) -> nodeHasAddr(politoIDS,p.src)
129 this.constraints.add(ctx.mkForall(new Expr[]{n_0, p_0, t_0},
130 ctx.mkImplies((BoolExpr)nctx.send.apply(politoIDS, n_0, p_0, t_0),
131 ctx.mkNot((BoolExpr)nctx.nodeHasAddr.apply(politoIDS,nctx.pf.get("src").apply(p_0)))),
133 null,null,null,null));