def expected_mi_from_undersampling(sigma, Ne, L, copies, n):
    p = mismatch_probability(sigma, Ne, L, copies)
    q = 1 - p
    Hx = -(q * log2(q) + p * log2(p / 3))
    Hxy = -(q**2 * log2(q**2) + 2 * p * q * log2(p * q / 3) +
            p**2 * log2(p**2 / 9))
    return
def expected_ic(L, k):
    L = float(L)
    q = k / L  # prob mismatch
    p = 1 - q
    h_per_col = -((p * log2(p) if p else 0) + (q * log2(q / 3) if q else 0))
    ic_per_col = 2 - h_per_col
    return ic_per_col * L
def expected_mi(sigma, Ne, L, copies):
    """How much MI should you expect due to ROR effect?"""
    ps = ps_from_copies(sigma, Ne, L, copies)
    misX = sum(k * p for k, p in enumerate(ps)) / L
    matX = 1 - misX
    misY = misX
    matY = matX
    L = float(L)
    matYmatX = sum(ps[k] * ((L - k) / L) * (L - k - 1) / (L - 1)
                   for k in range(int(L + 1)))
    matYmisX = sum(ps[k] * ((L - k) / L) * (k) / (L - 1)
                   for k in range(int(L + 1)))
    misYmatX = matYmisX
    misYmisX = sum(ps[k] * (k / L) * (k - 1) / (L - 1)
                   for k in range(int(L + 1)))
    #print "joints sum to:", (matYmatX + matYmisX + misYmatX + misYmisX)
    HX = HY = -(matX * log2(matX) + 3 * (misX / 3) * log2(misX / 3))
    #print "HX:",HX
    MI_ref = (misYmisX * log2(misYmisX / (misY * misX)) +
              matYmisX * log2(matYmisX / (matY * misX)) +
              misYmatX * log2(misYmatX / (misY * matX)) +
              matYmatX * log2(matYmatX / (matY * matX)))
    MI = (9 * (misYmisX / 9) * log2(
        (misYmisX / 9) /
        ((misY / 3) * (misX / 3))) + 3 * (matYmisX / 3) * log2(
            (matYmisX / 3) / (matY * (misX / 3))) + 3 * (misYmatX / 3) * log2(
                (misYmatX / 3) / ((misY / 3) * matX)) +
          matYmatX * log2(matYmatX / (matY * matX)))
    return (MI) * choose(int(L), 2)
def expected_ic(L,k):
    L = float(L)
    q = k/L # prob mismatch
    p = 1 - q
    h_per_col = - ((p*log2(p) if p else 0) + (q*log2(q/3) if q else 0))
    ic_per_col = 2 - h_per_col
    return ic_per_col * L
示例#5
0
文件: mfsce.py 项目: poneill/amic
def mean_field_test(M=10, K=2, sigma=1, plotting=True):
    Vs = [[None for j in range(M)] for jp in range(M)]
    for j in range(M):
        for jp in range(j + 1, M):
            d = {(xj, xjp): random.gauss(0, sigma) for xj in range(K) for xjp in range(K)}
            Vjjp = lambda xj, xjp: d[(xj, xjp)]
            Vs[j][jp] = Vjjp
    states = list(itertools.product(*[range(K) for j in range(M)]))

    def Hp(xs):
        return sum(Vs[j][jp](xj, xjp) for ((j, xj), (jp, xjp)) in itertools.combinations(enumerate(xs), 2))

    mf_hs = mean_field_hs(Vs, K)
    print "computing Zp"
    Zp = sum(exp(-beta * Hp(xs)) for xs in states)

    def P(xs):
        return exp(-beta * Hp(xs)) / Zp

    def Hq(xs):
        return sum(mf_hs[j][xj] for j, xj in enumerate(xs))

    print "computing Zq"
    Zq = sum(exp(-beta * Hq(xs)) for xs in states)

    def Q(xs):
        return exp(-beta * Hq(xs)) / Zq

    # for state in states:
    #     print state,P(state),Q(state)
    ps = [P(state) for state in states]
    qs = [Q(state) for state in states]
    print pearsonr(ps, qs)
    print "Sp (bits):", sum(-p * log2(p) for p in ps)
    print "Sq (bits):", sum(-q * log2(q) for q in qs)
    print "Dkl(P||Q) (bits):", sum(p * log2(p / q) for p, q in zip(ps, qs))

    def rQ(xs):
        """MFA proposal"""
        return [inverse_cdf_sample(range(K), boltzmann(mf_h)) for mf_h in mf_hs]

    def rR(xs):
        """Uniform proposal"""
        return [random.choice(range(K)) for j in range(M)]

    mh(f=P, proposal=rQ, dprop=Q, x0=[0] * M)
    mh(f=P, proposal=rR, x0=[0] * M)

    if plotting:
        plt.scatter(ps, qs)
        plt.xlabel("Ps")
        plt.ylabel("Qs")
        plt.loglog()
        minp, maxp = min(ps), max(ps)
        minq, maxq = min(qs), max(qs)
        plt.plot([minp, maxp], [minq, maxq])
        plt.xlim(minp, maxp)
        plt.ylim(minq, maxq)
        plt.show()
def expected_mi(L, k):
    """expected MI between two columns, given site length L and k mismatches per site"""
    L = float(L)
    q = k / L  # prob mismatch
    p = 1 - q
    match = lambda b: b == "A"
    mismatch = lambda b: b != "A"

    def joint(b1, b2):
        if match(b1):
            if match(b2):
                return (L - k) / L * (L - k - 1) / (L - 1)
            else:
                return (L - k) / L * k / (L - 1) / 3.0
        else:
            if match(b2):
                return (k) / L * (L - k) / (L - 1) / 3.0
            else:
                return (k) / L * (k - 1) / (L - 1) / 9.0

    def marg(b):
        return p if match(b) else q / 3

    return sum(
        joint(b1, b2) * log2(joint(b1, b2) /
                             (marg(b1) * marg(b2))) if joint(b1, b2) else 0
        for b1, b2 in choose2("ACGT"))
示例#7
0
    def update_model(self):
        print "trying to update model", self.data_store_file
        # print self.data_store

        entries = self.get_labels(self.meuzz_window)
        to_dump = []
        for entry in entries:
            label, feature = entry
            label = self.normalize_label(label)
            feature = feature[1]
            data = self.data_store.add_data(label, feature)
            to_dump.append((feature, label))

            # only give positive feedback
            if feature > 0:
                print "update model", feature, label
                self.meuzz.update_model([feature], [label])

        self.data_store.dump_data(to_dump,
                                  self.data_store_file,
                                  window=self.meuzz_window)
        if len(entries) != 0:
            oracle_info("queue size: {0}".format(self.queue_size))
            oracle_info("init queue size: {0}".format(self.init_queue_size))
            self.meuzz_window += 1
            self.meuzz_window = min(self.meuzz_window, \
                                    round(utils.log2(self.queue_size/self.init_queue_size)) + 1)
            oracle_info("Adjusting meuzz window size: {0}".format(
                self.meuzz_window))
示例#8
0
 def impurity(self):
     n = self.n + self.eps
     if self.__classify:
         return sum(map(lambda x: -x/n * log2(x/n + self.eps), self.counts)) # entropy
     else:
         prd = self.pred()
         return sqrt( self.ss/n - prd*prd ) # sd of node
def hamming_evolution(L):
    ps = [1] + [0]*(L)
    dt = 0.001
    t = 0
    tf = 5
    hist = []
    hs = []
    while t < tf:
        psp = ps[:]
        hist.append(ps[:])
        for i in range(L+1):
            bolus = ps[i]*dt
            if 0 <= i - 1 <= L:
                psp[i] -= i*bolus
                psp[i-1] += i*bolus
            if 0 <= i + 1 <= L:
                psp[i] -= 3*(L-i)*bolus
                psp[i+1] += 3*(L-i)*bolus
        ps = psp[:]
        this_h = -sum(p*(log2(p/num_seqs_at(L,i)) if p > 0 else 0) for i,p in enumerate(ps))
        pred_h = 2*L*(1-exp(-10*t))
        print t,sum(ps),this_h,pred_h
        hs.append(this_h)
        t += dt
    return hist,hs
def make_ecoli_df():
    Ls = []
    Ls_adj = []
    ns = []
    sigmas = []
    labels = []
    motif_ics = []
    motif_ics_per_base = []
    for tf in Escherichia_coli.tfs:
        sites = getattr(Escherichia_coli, tf)
        L = len(sites[0])
        n = len(sites)
        ns.append(n)
        L_adj = len(sites[0]) + log2(n)
        sigma = mean((map(sd, make_pssm(sites))))
        Ls.append(L)
        Ls_adj.append(L_adj)
        motif_ics.append(motif_ic(sites))
        motif_ics_per_base.append(motif_ic(sites) / float(L))
        sigmas.append(sigma)
    df = pd.DataFrame(
        {
            "L": Ls,
            "n": ns,
            "sigma": sigmas,
            "motif_ic": motif_ics,
            "info_density": motif_ics_per_base
        },
        index=Escherichia_coli.tfs)
    return df
def hamming_evolution(L):
    ps = [1] + [0] * (L)
    dt = 0.001
    t = 0
    tf = 5
    hist = []
    hs = []
    while t < tf:
        psp = ps[:]
        hist.append(ps[:])
        for i in range(L + 1):
            bolus = ps[i] * dt
            if 0 <= i - 1 <= L:
                psp[i] -= i * bolus
                psp[i - 1] += i * bolus
            if 0 <= i + 1 <= L:
                psp[i] -= 3 * (L - i) * bolus
                psp[i + 1] += 3 * (L - i) * bolus
        ps = psp[:]
        this_h = -sum(p * (log2(p / num_seqs_at(L, i)) if p > 0 else 0)
                      for i, p in enumerate(ps))
        pred_h = 2 * L * (1 - exp(-10 * t))
        print t, sum(ps), this_h, pred_h
        hs.append(this_h)
        t += dt
    return hist, hs
示例#12
0
def make_pssm(binding_sites):
    """
    Return the PSSM as a list of dictionaries of the form:
    [{A:a_val,...,T:t_val}]
    """
    cols = transpose(binding_sites)
    n = float(len(binding_sites))
    return [{b: log2(((col.count(b) + 1) / (n + 4)) / 0.25)
             for b in "ACGT"} for col in cols]
示例#13
0
 def impurity(self):
     n = self.n + self.eps
     if self.__classify:
         return sum(
             map(lambda x: -x / n * log2(x / n + self.eps),
                 self.counts))  # entropy
     else:
         prd = self.pred()
         return sqrt(self.ss / n - prd * prd)  # sd of node
示例#14
0
def recognizer_non_linearity((sites,recognizer)):
    L = log(len(idx_of_word),4)
    motif = [w for w,i in idx_of_word.items() if recognizer[i]]
    if len(motif) == 0:
        return -1
    else:
        total_info = 2*L - log2(len(motif))
        col_info = motif_ic(motif,correct=False)
        return total_info - col_info
示例#15
0
def make_pssm(binding_sites):
    """
    Return the PSSM as a list of dictionaries of the form:
    [{A:a_val,...,T:t_val}]
    """
    cols = transpose(binding_sites)
    n = float(len(binding_sites))
    return [{b:log2(((col.count(b)+1)/(n+4))/0.25)
             for b in "ACGT"}
            for col in cols]
def sample_entropic_prior(K,eta=10**-3):
    max_ent = log2(K)
    hf = random.random() * max_ent
    p = flow_to_h(hf,simplex_sample(K),eta=eta)
    attempts = 0
    while p is None:
        #print attempts
        p = flow_to_h(hf,simplex_sample(K),eta=eta)
        #print p
        attempts += 1
    return p
示例#17
0
def pairwise_mi_ref(ps,M=None):
    w = int(log(len(ps),4))
    dimer_freqs = defaultdict(lambda: np.zeros(16))
    dimer_base_index = {"".join(comb):k for k,comb in enumerate(product("ACGT","ACGT"))}
    psfm = marginalize(ps,M)
    for k,(kmer,p) in enumerate(zip(make_kmers(w),ps)):
        for (i,j) in choose2(range(w)):
            dimer = kmer[i] + kmer[j]
            dimer_freqs[(i,j)][dimer_base_index[dimer]]+=p
    return sum(dimer_freqs[(i,j)][k]*log2(dimer_freqs[(i,j)][k]/(psfm[i][k/4]*psfm[j][k%4]))
                      for k in range(16) for (i,j) in choose2(range(w)))
示例#18
0
def sample_entropic_prior(K,eta=10**-3):
    max_ent = log2(K)
    hf = random.random() * max_ent
    p = flow_to_h(hf,simplex_sample(K),eta=eta)
    attempts = 0
    while p is None:
        #print attempts
        p = flow_to_h(hf,simplex_sample(K),eta=eta)
        #print p
        attempts += 1
    return p
    def call(self, input):
        rois = input[0]
        features = input[1:]
        #assign feature to each rois
        y1, x1, y2, x2 = tf.split(rois, 4, axis=2)
        h = y2 - y1
        w = x2 - x1

        area = tf.cast(self.image_shape[0] * self.image_shape[1], tf.float32)
        spec = utils.log2(tf.sqrt(h * w) / (224.0 / tf.sqrt(area)))
        roi_level = tf.minimum(
            5, tf.maximum(2, 4 + tf.cast(tf.round(spec), tf.int32)))
        roi_level = tf.squeeze(roi_level, 2)

        pooled = []
        roi_to_level = []
        for i, level in enumerate(range(2, 6)):
            ix = tf.where(tf.equal(roi_level, level))
            roi_with_level = tf.gather_nd(rois,
                                          ix)  #list roi with "level" feature

            roi_ids = tf.cast(ix[:, 0], tf.int32)
            roi_to_level.append(ix)  #map level with roi list

            #stop gradient propogation
            roi_with_level = tf.stop_gradient(roi_with_level)
            roi_ids = tf.stop_gradient(roi_ids)

            pooled.append(
                tf.image.crop_and_resize(features[i],
                                         roi_with_level,
                                         roi_ids,
                                         self.pool_shape,
                                         method="bilinear"))

        pooled = tf.concat(pooled, axis=0)

        roi_to_level = tf.concat(roi_to_level, axis=0)
        roi_range = tf.expand_dims(tf.range(tf.shape(roi_to_level)[0]), 1)
        roi_to_level = tf.concat([tf.cast(roi_to_level, tf.int32), roi_range],
                                 axis=1)

        ## Rearrange pooled features to match the order of the original boxes
        # Sort roi_to_level by batch then box index
        sorting_ts = roi_to_level[:, 0] * 100000 + roi_to_level[:, 1]
        ix = tf.nn.top_k(sorting_ts, k=tf.shape(roi_to_level)[0]).indices[::-1]
        ix = tf.gather(roi_to_level[:, 2], ix)
        pooled = tf.gather(pooled, ix)

        pooled = tf.expand_dims(pooled, 0)
        return pooled
def expected_mi(sigma,Ne,L,copies):
    """How much MI should you expect due to ROR effect?"""
    ps = ps_from_copies(sigma,Ne,L,copies)
    misX = sum(k*p for k,p in enumerate(ps))/L
    matX = 1-misX
    misY = misX
    matY = matX
    L = float(L)
    matYmatX = sum(ps[k]*((L-k)/L)*(L-k-1)/(L-1) for k in range(int(L+1)))
    matYmisX = sum(ps[k]*((L-k)/L)*(k)/(L-1) for k in range(int(L+1)))
    misYmatX = matYmisX
    misYmisX = sum(ps[k]*(k/L)*(k-1)/(L-1) for k in range(int(L+1)))
    #print "joints sum to:", (matYmatX + matYmisX + misYmatX + misYmisX)
    HX = HY = -(matX*log2(matX) + 3*(misX/3)*log2(misX/3))
    #print "HX:",HX
    MI_ref = (misYmisX*log2(misYmisX/(misY*misX)) +
           matYmisX*log2(matYmisX/(matY*misX)) +
           misYmatX*log2(misYmatX/(misY*matX)) +
           matYmatX*log2(matYmatX/(matY*matX)))
    MI = (9*(misYmisX/9)*log2((misYmisX/9)/((misY/3)*(misX/3))) +
           3*(matYmisX/3)*log2((matYmisX/3)/(matY*(misX/3))) +
           3*(misYmatX/3)*log2((misYmatX/3)/((misY/3)*matX)) +
           matYmatX*log2(matYmatX/(matY*matX)))
    return (MI)*choose(int(L),2)
def rfreq_rseq_experiment(obj,filename="rfreq_vs_rseq_in_sefas_collection.png"):
    Rfreqs = []
    Rseqs = []
    G = 5.0*10**6
    min_rfreq = log2(G/500)
    for tf in obj.tfs:
        motif = getattr(obj,tf)
        Rfreqs.append(log(G/len(motif),2))
        Rseqs.append(motif_ic(motif))
    plt.scatter(Rfreqs,Rseqs)
    plt.xlabel("log(G/n) (bits)")
    plt.ylabel("Motif Information Content (bits)")
    plt.plot([0,20],[0,20],linestyle='--',label='Theory')
    plt.plot([min_rfreq,min_rfreq],[0,30],linestyle='--',label='Maximum Plausible Regulon Size')
    plt.title("Motif Information Content vs. Search Difficulty")
    plt.legend(loc='upper left')
    maybesave(filename)
def expected_mi(L,k):
    """expected MI between two columns, given site length L and k mismatches per site"""
    L = float(L)
    q = k/L # prob mismatch
    p = 1 - q
    match = lambda b:b=="A"
    mismatch = lambda b:b!="A"
    def joint(b1,b2):
        if match(b1):
             if match(b2):
                 return (L-k)/L * (L-k-1)/(L-1)
             else:
                 return (L - k)/L * k/(L - 1) / 3.0
        else:
            if match(b2):
                return (k)/L * (L-k)/(L-1) / 3.0
            else:
                return (k)/L * (k-1)/(L-1) / 9.0
    def marg(b):
        return p if match(b) else q/3
    return sum(joint(b1,b2)*log2(joint(b1,b2)/(marg(b1)*marg(b2))) if joint(b1,b2) else 0
               for b1,b2 in choose2("ACGT"))
def expected_ic(sigma,Ne,L,copies):
    p = mismatch_probability(sigma,Ne,L,copies)
    q = 1-p
    return L*(2 - (-(q*log2(q) + 3*(p/3)*log2(p/3))))
示例#24
0
def find_alpha(K,entropy,tol_factor=0.01):
    ub = 1/(log2(K)-entropy)
    #print "K:%s,desired entropy:%s, ub:%s" % (K,entropy,ub)
    alpha = bisect_interval(lambda alpha:expected_entropy(K,alpha)-entropy,10**-10,ub)
    return alpha
示例#25
0
import utils
import sys

utils.log2()

print('hello')

print(sys.path)

print(dir(utils))

print utils.__name__

print __name__
def make_ecoli_sigma_L_plot():
    Ls = []
    Ls_adj = []
    ns = []
    sigmas = []
    labels = []
    motif_ics = []
    motif_ics_per_base = []
    for tf in Escherichia_coli.tfs:
        sites = getattr(Escherichia_coli, tf)
        L = len(sites[0])
        n = len(sites)
        ns.append(n)
        L_adj = len(sites[0]) + log2(n)
        sigma = mean((map(sd, make_pssm(sites))))
        Ls.append(L)
        Ls_adj.append(L_adj)
        motif_ics.append(motif_ic(sites))
        motif_ics_per_base.append(motif_ic(sites) / float(L))
        sigmas.append(sigma)
        labels.append(tf)
    sigma_space = np.linspace(0.1, 3, 10)
    crit_lambs_actual = map(
        lambda sigma: critical_lamb_actual(sigma, G=4.5 * 10**6, trials=100),
        tqdm(sigma_space))
    plt.subplot(1, 6, 1)
    plt.scatter(sigmas, Ls)
    for L, sigma, label in zip(Ls, sigmas, labels):
        plt.annotate(label, xy=(sigma, L))
    plt.plot(*pl(lambda sigma: critical_lamb(sigma, G=5 * 10**6), sigma_space))
    plt.plot(
        *pl(lambda sigma: critical_lamb(sigma, G=4.5 * 10**6), sigma_space))
    plt.plot(sigma_space, crit_lambs_actual)
    plt.subplot(1, 6, 2)
    plt.scatter(sigmas, Ls_adj)
    for L_adj, sigma, label in zip(Ls_adj, sigmas, labels):
        plt.annotate(label, xy=(sigma, L_adj))
    plt.plot(*pl(lambda sigma: critical_lamb(sigma, G=5 * 10**6), sigma_space))
    plt.plot(
        *pl(lambda sigma: critical_lamb(sigma, G=4.5 * 10**6), sigma_space))
    plt.plot(sigma_space, crit_lambs_actual)
    preds = [critical_lamb(sigma, G=4.5 * 10**6) for sigma in tqdm(sigmas)]
    preds_actual = [
        critical_lamb_actual(sigma, G=4.5 * 10**6, trials=100)
        for sigma in tqdm(sigmas)
    ]
    plt.subplot(1, 6, 3)
    plt.scatter(preds, Ls)
    plt.xlabel("Predicted Length")
    plt.ylabel("Observed Length")
    plt.title("Preds vs Ls")
    print "Preds vs Ls", pearsonr(preds, Ls)
    plt.plot([0, 30], [0, 30])
    plt.subplot(1, 6, 4)
    plt.scatter(preds, Ls_adj)
    plt.xlabel("Predicted Length")
    plt.ylabel("Observed Length")
    plt.plot([0, 30], [0, 30])
    plt.title("Preds vs Ls_adj")
    print "Preds vs Ls_adj", pearsonr(preds, Ls_adj)
    plt.subplot(1, 6, 5)
    plt.scatter(preds_actual, Ls)
    plt.xlabel("Predicted Length")
    plt.ylabel("Observed Length")
    plt.plot([0, 30], [0, 30])
    plt.title("Preds_actual vs Ls")
    print "Preds_actual vs Ls", pearsonr(preds_actual, Ls)
    plt.subplot(1, 6, 6)
    plt.scatter(preds_actual, Ls_adj)
    plt.xlabel("Predicted Length")
    plt.ylabel("Observed Length")
    plt.plot([0, 30], [0, 30])
    plt.title("Preds_actual vs Ls_adj")
    print "Preds_actual vs Ls_adj", pearsonr(preds_actual, Ls_adj)
    return Ls, sigmas
示例#27
0
 def normalize_label(self, label):
     # Label normalization should be related to the batch run input number
     L_NORM = utils.log2(
         self.batch_run_input_num * self.batch_run_input_num) + 1
     return int(label) / L_NORM
示例#28
0
def main():
    os.system("reset")

    os.system("rm -f /tmp/bgp-R?.pid /tmp/zebra-R?.pid 2> /dev/null")
    os.system("rm -f /tmp/R*.log /tmp/R*.pcap 2> /dev/null")
    os.system("rm logs/R*stdout 2> /dev/null")
    os.system(
        "rm /tmp/hub.log /tmp/c*.log /tmp/attacks.* /tmp/atk1*.pcap 2> /dev/null"
    )
    os.system("rm /tmp/tcpdump*.out /tmp/tcpdump*.err 2> /dev/null")
    os.system("rm /tmp/R*-complete.out /tmp/R*-complete.err 2> /dev/null")

    os.system("mn -c > /dev/null 2>&1")

    os.system('pgrep zebra | xargs kill -9')
    os.system('pgrep bgpd | xargs kill -9')
    os.system('pgrep pox | xargs kill -9')
    os.system('pgrep -f webserver.py | xargs kill -9')

    init_quagga_state_dir()

    startPOXHub()

    net = Mininet(topo=SimpleTopo(), switch=Router)
    net.addController(name='poxController', controller=RemoteController, \
     ip='127.0.0.1', port=6633)
    net.start()

    log("Configuring hosts ...")
    for host in net.hosts:
        if host.name != ATTACKER_NAME:
            host.cmd("ifconfig %s-eth0 %s" % (host.name, getIP(host.name)))
            host.cmd("route add default gw %s" % (getGateway(host.name)))
        else:
            host.cmd("ifconfig %s-eth0 %s" % (host.name, '9.0.0.3'))
            host.cmd("ifconfig %s-eth1 %s" % (host.name, '9.0.1.3'))

            for i in host.intfList():
                i.setMAC(get_MAC(i.name))

            host.cmd(
                "tcpdump -i atk1-eth0 -w /tmp/atk1-eth0-blind-attack.pcap not arp > /tmp/tcpdump-atk1-eth0.out 2> /tmp/tcpdump-atk1-eth0.err &",
                shell=True)
            host.cmd(
                "tcpdump -i atk1-eth1 -w /tmp/atk1-eth1-blind-attack.pcap not arp > /tmp/tcpdump-atk1-eth1.out 2> /tmp/tcpdump-atk1-eth1.err &",
                shell=True)

    for i in xrange(ASES):
        log("Starting web server on h%s-1" % (i + 1), 'yellow')
        startWebserver(net, 'h%s-1' % (i + 1), "Web server on h%s-1" % (i + 1))

    log("Configuring routers ...")
    for router in net.switches:

        if HUB_NAME not in router.name:
            # 0 = no RPF
            # 1 = RPF strict mode
            # 2 = RPF loose mode
            router.cmd("sysctl -w net.ipv4.conf.all.rp_filter=2")

            router.cmd("sysctl -w net.ipv4.ip_forward=1")
            router.waitOutput()

            if router.name == 'R2':
                router.cmd(
                    "tcpdump -i R2-eth4 -w /tmp/R2-eth4-complete.pcap not arp > /tmp/R2-eth4-complete.out 2> /tmp/R2-eth4-complete.err &",
                    shell=True)

    log2("sysctl changes to take effect", args.sleep, col='cyan')

    for router in net.switches:
        if HUB_NAME not in router.name:
            router.cmd("~/quagga-1.2.4/zebra/zebra -f conf/zebra-%s.conf -d -i /tmp/zebra-%s.pid > logs/%s-zebra-stdout 2>&1" % \
             (router.name, router.name, router.name))
            router.waitOutput()

            router.cmd("~/quagga-1.2.4/bgpd/bgpd -f conf/bgpd-%s.conf -d -i /tmp/bgp-%s.pid > logs/%s-bgpd-stdout 2>&1" % \
             (router.name, router.name, router.name), shell=True)
            router.waitOutput()

            log("Starting zebra and bgpd on %s" % router.name)

    log2("BGP convergence", BGP_CONVERGENCE_TIME, 'cyan')

    choice = -1

    while choice != 0:
        choice = raw_input(
            "Choose:\n1) blind RST attack\n2) blind SYN attack\n3) blind UPDATE attack\n4) mininet CLI\n0) exit\n> "
        )

        if choice != '':
            choice = int(choice)

            if 0 < choice < 4:
                #remote_flag = -1
                #while remote_flag < 0 or remote_flag > 1:
                #	remote_flag = int(raw_input("Local (0) or Remote (1) attack? "))
                remote_flag = 1

                launch_attack(net, choice, remote_flag)

                for router in net.switches:
                    if HUB_NAME not in router.name:
                        if router.name == 'R2':
                            router.cmd(
                                "tcpdump -i R2-eth4 -w /tmp/R2-eth4-blind-attack.pcap not arp > /tmp/tcpdump-R2-eth4.out 2> /tmp/tcpdump-R2-eth4.err &",
                                shell=True)
                            router.cmd(
                                "tcpdump -i R2-eth5 -w /tmp/R2-eth5-blind-attack.pcap not arp > /tmp/tcpdump-R2-eth5.out 2> /tmp/tcpdump-R2-eth5.err &",
                                shell=True)
            elif choice == 4:
                CLI(net)

    net.stop()

    stopPOXHub()

    os.system('pgrep zebra | xargs kill -9')
    os.system('pgrep bgpd | xargs kill -9')
    os.system('pgrep pox | xargs kill -9')
    os.system('pgrep -f webserver.py | xargs kill -9')

    # os.system('sudo wireshark /tmp/atk1-eth0-blind-attack.pcap -Y \'not ipv6\' &')
    # os.system('sudo wireshark /tmp/atk1-eth1-blind-attack.pcap -Y \'not ipv6\' &')
    os.system(
        'sudo wireshark /tmp/R2-eth4-blind-attack.pcap -Y \'not ipv6\' &')
    os.system(
        'sudo wireshark /tmp/R2-eth5-blind-attack.pcap -Y \'not ipv6\' &')
示例#29
0
def startPOXHub():
    log("Starting POX RemoteController")
    os.system("python %s --verbose forwarding.hub > /tmp/hub.log 2>&1 &" % POX)

    log2("pox RemoteController to start", args.sleep, col='cyan')
示例#30
0
def entropy(eps,mu):
    return sum((lambda p:-(p*log2(p)+(1-p)*log2(1-p)))(fd(ep,mu))
               for ep in eps)
示例#31
0
def main():
    os.system("reset")
    os.system(
        "rm -f /tmp/R*.log /tmp/ospf-R*.pid /tmp/zebra-R*.pid 2> /dev/null")
    os.system("rm -r logs/*stdout 2> /dev/null")
    os.system("rm -r /tmp/*_tcpdump.cap 2> /dev/null")
    os.system("mn -c > /dev/null 2>&1")
    os.system("killall -9 zebra ospfd > /dev/null 2>&1")
    os.system('pgrep -f webserver.py | xargs kill -9')

    init_quagga_state_dir()

    net = Mininet(topo=SimpleTopo(), switch=Router)
    net.start()

    attacker_router = None

    for host in net.hosts:
        host.cmd("ifconfig %s-eth0 %s" % (host.name, getIP(host.name)))
        host.cmd("route add default gw %s" % (getGateway(host.name)))

        host.cmd("tcpdump -i %s-eth0 -w /tmp/%s-eth0_tcpdump.cap not arp &" %
                 (host.name, host.name))

        log("Starting web server on %s" % host.name, 'yellow')
        startWebserver(net, host.name, "Web server on %s" % host.name)

    for router in net.switches:
        if SWITCH_NAME not in router.name:
            router.cmd("sysctl -w net.ipv4.ip_forward=1")
            router.waitOutput()

    log2("sysctl changes to take effect", args.sleep, 'cyan')

    for router in net.switches:
        if SWITCH_NAME not in router.name:
            if CUSTOM_IMPLEMENTATION == 0:
                router.cmd(
                    "~/quagga-1.2.4/zebra/zebra -f conf/zebra-%s.conf -d -i /tmp/zebra-%s.pid > logs/%s-zebra-stdout 2>&1"
                    % (router.name, router.name, router.name))
            else:
                router.cmd(
                    "./quagga-1.2.4/zebra/zebra -f conf/zebra-%s.conf -d -i /tmp/zebra-%s.pid > logs/%s-zebra-stdout 2>&1"
                    % (router.name, router.name, router.name))
            router.waitOutput()

            if CUSTOM_IMPLEMENTATION == 0:
                router.cmd(
                    "~/quagga-1.2.4/ospfd/ospfd -f conf/ospfd-%s.conf -d -i /tmp/ospf-%s.pid > logs/%s-ospfd-stdout 2>&1"
                    % (router.name, router.name, router.name),
                    shell=True)
            else:
                router.cmd(
                    "./quagga-1.2.4/ospfd/ospfd -f conf/ospfd-%s.conf -d -i /tmp/ospf-%s.pid > logs/%s-ospfd-stdout 2>&1"
                    % (router.name, router.name, router.name),
                    shell=True)
            router.waitOutput()

            log("Starting zebra and ospfd on %s" % router.name)

            router.cmd(
                "tcpdump -i %s-eth1 -w /tmp/%s-eth1_tcpdump.cap not arp &" %
                (router.name, router.name))
            router.cmd(
                "tcpdump -i %s-eth2 -w /tmp/%s-eth2_tcpdump.cap not arp &" %
                (router.name, router.name))
            router.cmd(
                "tcpdump -i %s-eth3 -w /tmp/%s-eth3_tcpdump.cap not arp &" %
                (router.name, router.name))
            router.cmd(
                "tcpdump -i %s-eth4 -w /tmp/%s-eth4_tcpdump.cap not arp &" %
                (router.name, router.name))

            if router.name == ATTACKER_ROUTER_NAME:
                attacker_router = router

    assert attacker_router is not None

    #log2('OSPF convergence', OSPF_CONVERGENCE_TIME, 'cyan')

    #CLI(net)
    #raw_input('Press ENTER to launch attack ...')
    launch_attack(attacker_router)
    #raw_input('Press ENTER to exit ...')

    log2('collecting data', CAPTURING_WINDOW, 'cyan')

    net.stop()

    os.system(
        'sudo wireshark /tmp/R6-eth2_tcpdump.cap -Y \'not ipv6\' 2>/dev/null &'
    )

    os.system("killall -9 zebra ospfd > /dev/null 2>&1")
    os.system('pgrep -f webserver.py | xargs kill -9')
def expected_mi2(L, k):
    possible_sites = choose(L, k) * (1**(L - k)) * (3**k)
    total_IC = 2 * L - log2(possible_sites)
    return total_IC - expected_ic(L, k)
def expected_mi_from_undersampling(sigma,Ne,L,copies,n):
    p = mismatch_probability(sigma,Ne,L,copies)
    q = 1-p
    Hx = -(q*log2(q) + p*log2(p/3))
    Hxy = -(q**2*log2(q**2) + 2*p*q*log2(p*q/3) + p**2*log2(p**2/9))
    return 
示例#34
0
def main():
	os.system("reset")

	os.system("rm -r logs/*stdout 2> /dev/null")
	os.system("rm -f /tmp/R*.log /tmp/ospf-R?.pid /tmp/zebra-R?.pid 2> /dev/null")
	os.system("rm -f /tmp/c0.log /tmp/hub.log 2> /dev/null")
	os.system("rm -r /tmp/R*-tcpdump.cap 2> /dev/null")
	os.system("rm -r /tmp/h*tcpdump.cap /tmp/atk*tcpdump.cap 2> /dev/null")

	os.system("mn -c > /dev/null 2>&1")

	os.system('pgrep zebra | xargs kill -9')
	os.system('pgrep ospfd | xargs kill -9')
	os.system('pgrep pox | xargs kill -9')
	os.system('pgrep -f webserver.py | xargs kill -9')

	init_quagga_state_dir()

	startPOXHub()

	net = Mininet(topo=SimpleTopo(), switch=Router)
	net.addController(name='poxController', controller=RemoteController, ip='127.0.0.1', port=6633)
	net.start()

	local_attacker_host = None
	remote_attacker_host = None

	# CONFIGURING HOSTS
	for host in net.hosts:
		host.cmd("ifconfig %s-eth0 %s" % (host.name, getIP(host.name)))
		host.cmd("route add default gw %s" % (getGateway(host.name)))
		host.cmd("tcpdump -i %s-eth0 -w /tmp/%s-tcpdump.cap not arp &" % (host.name, host.name))

		if host.name == LOCAL_ATTACKER_NAME:
			local_attacker_host = host
			continue
		elif host.name == REMOTE_ATTACKER_NAME:
			remote_attacker_host = host
			continue
		else:
			log("Starting web server on %s" % host.name, 'yellow')
			startWebserver(net, host.name, "Web server on %s" % host.name)

		#if host.name == 'h4-1':
		#	host.cmd('ping 10.0.1.2 -i 10 2>&1> /tmp/h4-1-ping.log &')

	local_atk1_mac_address = local_attacker_host.MAC()
	remote_atk1_mac_address = remote_attacker_host.MAC()

	# CONFIGURING ROUTERS
	for router in net.switches:
		if SWITCH_NAME not in router.name:
			# 0 = no RPF
			# 1 = RPF strict mode
			# 2 = RPF loose mode
			router.cmd("sysctl -w net.ipv4.conf.all.rp_filter=2")

			router.cmd("sysctl -w net.ipv4.ip_forward=1")
			router.waitOutput()

	log2('sysctl changes to take effect', args.sleep, 'cyan')

	r1_eth1_mac_address = None
	r3_eth2_mac_address = None

	for router in net.switches:
		if SWITCH_NAME not in router.name:
			router.cmd("~/quagga-1.2.4/zebra/zebra -f conf/zebra-%s.conf -d -i /tmp/zebra-%s.pid > logs/%s-zebra-stdout 2>&1" % \
				(router.name, router.name, router.name))
			router.waitOutput()
			router.cmd("~/quagga-1.2.4/ospfd/ospfd -f conf/ospfd-%s.conf -d -i /tmp/ospf-%s.pid > logs/%s-ospfd-stdout 2>&1" % \
				(router.name, router.name, router.name), shell=True)
			router.waitOutput()
			log("Starting zebra and ospfd on %s" % router.name)

			router.cmd("tcpdump -i %s-eth1 -w /tmp/%s-eth1-tcpdump.cap not arp 2>&1 > /tmp/%s-eth1-tcpdump.log &" % \
				(router.name, router.name, router.name))
			router.cmd("tcpdump -i %s-eth2 -w /tmp/%s-eth2-tcpdump.cap not arp 2>&1 > /tmp/%s-eth2-tcpdump.log &" % \
				(router.name, router.name, router.name))

		if router.name == 'R1':
			for i in router.intfList():
				if i.name == 'R1-eth1':
					r1_eth1_mac_address = i.MAC()

		if router.name == 'R3':
			for i in router.intfList():
				if i.name == 'R3-eth2':
					r3_eth2_mac_address = i.MAC()

	log2('OSPF convergence', OSPF_CONVERGENCE_TIME, 'cyan')

	choice = -1

	while choice != 0:
		choice = raw_input("Choose:\n1) launch Remote False Adjacency attack\n2) mininet CLI\n0) exit\n> ")

		if choice != '':
			choice = int(choice)

			if choice == 1:
				#remote_flag = -1
				#while remote_flag < 0 or remote_flag > 1:
				#	remote_flag = int(raw_input("Local (0) or Remote (1) attack? "))
				remote_flag = 1

				#"""
				if remote_flag == 0:
					launch_attack(remote_flag, local_attacker_host, local_atk1_mac_address, r1_eth1_mac_address)
				else:
					launch_attack(remote_flag, remote_attacker_host, local_atk1_mac_address, r3_eth2_mac_address)
				#"""
				log2('Collecting data', CAPTURING_WINDOW, 'cyan')

				choice = 0

			elif choice == 2:
				CLI(net)

	net.stop()

	stopPOXHub()

	os.system('pgrep zebra | xargs kill -9')
	os.system('pgrep bgpd | xargs kill -9')
	os.system('pgrep pox | xargs kill -9')
	os.system('pgrep -f webserver.py | xargs kill -9')

	os.system('sudo wireshark /tmp/R1-eth1-tcpdump.cap -Y \'not ipv6\' 2>/dev/null &')
	os.system('sudo wireshark /tmp/R1-eth2-tcpdump.cap -Y \'not ipv6\' 2>/dev/null &')

	"""
def main():
    os.system("reset")

    os.system("rm -f /tmp/bgp-R?.pid /tmp/zebra-R?.pid 2> /dev/null")
    os.system("rm -f /tmp/R*.log /tmp/R*.pcap 2> /dev/null")
    os.system("rm logs/R*stdout 2> /dev/null")
    os.system("rm /tmp/hub.log /tmp/c*.log /tmp/attacks.* /tmp/atk1*.pcap "
              "2> /dev/null")
    os.system("rm /tmp/tcpdump*.out /tmp/tcpdump*.err 2> /dev/null")
    os.system("rm /tmp/R*-complete.out /tmp/R*-complete.err 2> /dev/null")

    os.system("mn -c > /dev/null 2>&1")

    os.system('pgrep zebra | xargs kill -9')
    os.system('pgrep bgpd | xargs kill -9')
    os.system('pgrep -f webserver.py | xargs kill -9')
    os.system('pgrep -f webserver-https.py | xargs kill -9')

    init_quagga_state_dir()

    net = Mininet(topo=SimpleTopo(), switch=Router)
    net.start()

    log("Configuring hosts ...")
    for host in net.hosts:
        host.cmd("ifconfig %s-eth0 %s" % (host.name, getIP(host.name)))
        host.cmd("route add default gw %s" % (getGateway(host.name)))

    log("Configuring routers ...")
    for router in net.switches:
        router.cmd("sysctl -w net.ipv4.ip_forward=1")
        router.waitOutput()

    log2("sysctl changes to take effect", args.sleep, col='cyan')

    for router in net.switches:
        if router.name == ATTACKER_ROUTER:
            continue

        router.cmd("tcpdump -i %s-eth1 -w /tmp/%s-eth1.pcap not arp"
         " > /tmp/tcpdump-%s-eth1.out 2> /tmp/tcpdump-%s-eth1.err &" \
         % (router.name, router.name, router.name, router.name), shell=True)
        router.cmd("tcpdump -i %s-eth2 -w /tmp/%s-eth2.pcap not arp"
         " > /tmp/tcpdump-%s-eth2.out 2> /tmp/tcpdump-%s-eth2.err &" \
         % (router.name, router.name, router.name, router.name), shell=True)
        router.cmd("tcpdump -i %s-eth3 -w /tmp/%s-eth3.pcap not arp"
         " > /tmp/tcpdump-%s-eth3.out 2> /tmp/tcpdump-%s-eth3.err &" \
         % (router.name, router.name, router.name, router.name), shell=True)
        router.cmd("tcpdump -i %s-eth4 -w /tmp/%s-eth4.pcap not arp"
         " > /tmp/tcpdump-%s-eth4.out 2> /tmp/tcpdump-%s-eth4.err &" \
         % (router.name, router.name, router.name, router.name), shell=True)

        router.cmd("~/quagga-1.2.4/zebra/zebra -f conf/zebra-%s.conf -d -i "
         "/tmp/zebra-%s.pid > logs/%s-zebra-stdout 2>&1" % \
         (router.name, router.name, router.name))
        router.waitOutput()

        router.cmd("~/quagga-1.2.4/bgpd/bgpd -f conf/bgpd-%s.conf -d -i "
         "/tmp/bgp-%s.pid > logs/%s-bgpd-stdout 2>&1" % \
         (router.name, router.name, router.name), shell=True)
        router.waitOutput()

        log("Starting zebra and bgpd on %s" % router.name)

    log2("BGP convergence", BGP_CONVERGENCE_TIME, 'cyan')

    log("Starting web servers", 'yellow')
    #startWebserver(net, 'h3-1', "Default web server")
    startHTTPSWebserver(net, 'h3-1', "Default HTTPS web server")
    #startWebserver(net, 'h5-1', "Malicious Default web server")

    CLI(net)

    net.stop()

    os.system('pgrep zebra | xargs kill -9')
    os.system('pgrep bgpd | xargs kill -9')
    os.system('pgrep -f webserver.py | xargs kill -9')
    os.system('pgrep -f webserver-https.py | xargs kill -9')
示例#36
0
def total_ic(ps):
    return log2(len(ps)) - h_np(ps)
def expected_mi2(L,k):
    possible_sites = choose(L,k)*(1**(L-k))*(3**k)
    total_IC = 2*L - log2(possible_sites)
    return total_IC - expected_ic(L,k)
def expected_ic(sigma, Ne, L, copies):
    p = mismatch_probability(sigma, Ne, L, copies)
    q = 1 - p
    return L * (2 - (-(q * log2(q) + 3 * (p / 3) * log2(p / 3))))