Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Alexandrov_Sergushichev.doc
Скачиваний:
2
Добавлен:
27.09.2019
Размер:
214.02 Кб
Скачать

4.2 Передача данных из незащищенного сегмента сети в защищенный

Для тестирования передачи из незащищенной сети в защищенную были использованы классы DelaysGenerator и DelayReceiver. Класс DelaysGenerator эмулирует результаты замера RTT и выдает их в виде последовательности вещественных чисел. Класс DelayReceiver обрабатывает результаты замеров RTT и выдает полученное сообщение.

Приложение

Файл CovertMessageGenerator.java import org.apache.commons.cli.*; import java.util.Random; public class CovertMessageGenerator { long startTS; long period; int interval; double intervalDelta; int length; double lengthDelta; String msg; Random r; public CovertMessageGenerator(String[] args) { Options options = new Options(); options.addOption("h", "help", false, "prints this message"); options.addOption("s", "start", true, "sets the start time of the message"); options.addOption("T", "period", true, "sets the duration of one bit"); options.addOption("i", "interval", true, "sets the mean interval between packets in one bit"); options.addOption("I", "interval-delta", true, "sets the standard deviation of interval length (percents)"); options.addOption("l", "length", true, "sets the mean length of packets in one bit"); options.addOption("L", "length-delta", true, "sets the standard deviation of packets' length (percents)"); options.addOption("m", "message", true, "sets the message to deliver"); CommandLineParser parser = new PosixParser(); CommandLine cmd; try { cmd = parser.parse(options, args); } catch (ParseException e) { e.printStackTrace(System.err); throw new IllegalArgumentException(e); } if (cmd.hasOption("help")) { new HelpFormatter().printHelp("layout", options); System.exit(0); } startTS = Long.parseLong(cmd.getOptionValue("start")); period = Long.parseLong(cmd.getOptionValue("period")); interval = Integer.parseInt(cmd.getOptionValue("interval")); intervalDelta = Integer.parseInt(cmd.getOptionValue("interval-delta"))/100.; length = Integer.parseInt(cmd.getOptionValue("length")); lengthDelta = Integer.parseInt(cmd.getOptionValue("length-delta"))/100.; msg = cmd.getOptionValue("message"); r = new Random(42); } public int nextGaussian(int mean, double delta) { double x = Math.max(0, 1 + r.nextGaussian() * delta); return (int)(mean * x); } public void putPacket(long ts, int length) { System.out.println(ts + " " + length); } public void run() { for (char c: msg.toCharArray()) { long periodEnd = startTS + period; long halfPeriodEnd = startTS + period / 2; switch (c) { case '1': { long curTs = startTS + nextGaussian(interval / 2, intervalDelta); while (curTs < halfPeriodEnd) { putPacket(curTs, nextGaussian(length, lengthDelta)); curTs += nextGaussian(interval, intervalDelta); } break; } case '0' : { // silence break; } default: throw new IllegalArgumentException("expected only 0s and 1s in message"); } startTS = periodEnd; } } public static void main(String[] args) { new CovertMessageGenerator(args).run(); } }

Файл DelayReceiver.java import org.apache.commons.cli.*; import java.util.Random; import java.util.Scanner; public class DelayReceiver { public static void main(String[] args) { Options options = new Options(); options.addOption("h", "help", false, "prints this message"); options.addOption("c", "change", true, "sets the size of change"); options.addOption("p", "ping", true, "sets the value of average ping"); options.addOption("T", "period", true, "sets the duration of a third of one bit"); CommandLineParser parser = new PosixParser(); CommandLine cmd; try { cmd = parser.parse(options, args); } catch (ParseException e) { e.printStackTrace(System.err); throw new IllegalArgumentException(e); } if (cmd.hasOption("help")) { new HelpFormatter().printHelp("layout", options); System.exit(0); } double change = Double.parseDouble(cmd.getOptionValue("change")); long period = Long.parseLong(cmd.getOptionValue("period")); double P = Double.parseDouble(cmd.getOptionValue("ping")); Scanner in = new Scanner(System.in); int t = 0; int K = 3; double[] vs = new double[K]; double[] phaseWeights = new double[K]; boolean transmissionStarted = false; while (in.hasNext()) { double v = in.nextDouble(); for (int i = 0; i < K - 1; ++i) { vs[i] = vs[i + 1]; } vs[2] = v; System.err.println(vs[0] + " " + vs[1] + " " + vs[2]); boolean isSignal = vs[1] > vs[0] && vs[1] > vs[2] && vs[1] - P > 0.8 * change && vs[0] - P + vs[2] - P > 0.5 * change ; if (isSignal) { transmissionStarted = true; phaseWeights[t % K]++; } if (transmissionStarted && getPhase(phaseWeights) == t % K) { System.out.println(isSignal ? "1" : "0"); } t++; } } private static int getPhase(double[] phaseWeights) { int res = 0; for (int i = 0; i < phaseWeights.length; ++i) { if (phaseWeights[i] > phaseWeights[res]) { res = i; } } return res; } }

Файл DelaysGenerator.java import org.apache.commons.cli.*; import java.util.*; import java.io.*; public class DelaysGenerator { Random r = new Random(42); long start; double interval, intervalDelta; String msg; double ampl; int step; long length; public DelaysGenerator(String[] args) { Options options = new Options(); options.addOption("h", "help", false, "prints this message"); options.addOption("s", "start", true, "sets the start time of the message (microsecs)"); options.addOption("i", "interval", true, "sets the mean ping interval length (millisecs)"); options.addOption("I", "interval-delta", true, "sets the standard deviation of ping interval length (percents)"); options.addOption("c", "code-amplitude", true, "sets the doubled amplitude of ping interval correction (percents)"); options.addOption("m", "message", true, "sets the message to deliver"); options.addOption("l", "unit-length", true, "sets the length of one time unit (microsecs)"); CommandLineParser parser = new PosixParser(); CommandLine cmd; try { cmd = parser.parse(options, args); } catch (ParseException e) { e.printStackTrace(System.err); throw new IllegalArgumentException(e); } if (cmd.hasOption("help")) { new HelpFormatter().printHelp("layout", options); System.exit(0); } start = Long.parseLong(cmd.getOptionValue("start")); interval = Integer.parseInt(cmd.getOptionValue("interval")); intervalDelta = Integer.parseInt(cmd.getOptionValue("interval-delta"))/100.; msg = cmd.getOptionValue("message") + "00"; ampl = Integer.parseInt(cmd.getOptionValue("code-amplitude")) / 100.; step = 1000000; length = Integer.parseInt(cmd.getOptionValue("unit-length")); } public void run() { long end = start + 3 * msg.length() * length; long cstart = start + (step - start % step) % step; for (long t = 0; t < end; t += step) { double d = nextGaussian(interval, intervalDelta); if (t < start) { System.out.println(d); continue; } int i = (int)((t - cstart) / (3 * length)); int j = (int)((t - cstart) / length % 3); if (msg.charAt(i) == '0') { System.out.println(d); continue; } double u = d * (1 + ampl); double fr = (double)((t - cstart) % length) / length; switch (j) { case 0: System.out.println((d + (u - d) * fr)); break; case 1: System.out.println(u); break; case 2: System.out.println((u - (u - d) * fr)); break; } } } public double nextGaussian(double mean, double delta) { double x = Math.max(0, 1 + r.nextGaussian() * delta); return (mean * x); } public static void main(String[] args) { new DelaysGenerator(args).run(); } } IntervalsGenerator.java import java.util.Scanner; public class IntervalsGenerator { public static void main(String[] args) { Scanner in = new Scanner(System.in); long a = -1; while (in.hasNextLong()) { long b = in.nextLong(); if (a != -1) { System.out.println(b - a); } a = b; } } } Файл NormalTrafficGenerator.java import java.util.Scanner; import java.io.FileReader; import java.io.IOException; import java.util.Map; import java.util.TreeMap; import java.util.List; import java.util.ArrayList; import java.util.Random; public class NormalTrafficGenerator { public static void main(String[] args) throws IOException { if (args.length != 3) { System.err.println("Usage: java NormalTrafficGenerator "); System.exit(1); } TreeMap intervals = load(args[0]); TreeMap lengths = load(args[1]); int n = Integer.parseInt(args[2]); Random r = new Random(); long time = 0; for (int i = 0; i < n; ++i) { int interval = getRandom(r, intervals); int length = getRandom(r, lengths); System.out.println((time += interval) + " " + length); } } private static TreeMap load(String file) throws IOException { List ks = new ArrayList(); List vs = new ArrayList(); double sum = 0; Scanner in = new Scanner(new FileReader(file)); while (in.hasNextDouble()) { double k = in.nextDouble(); int v = in.nextInt(); ks.add(k); vs.add(v); sum += k; } TreeMap m = new TreeMap(); double acc = 0; for (int i = 0; i < ks.size(); ++i) { m.put(acc, vs.get(i)); acc += ks.get(i) / sum; } return m; } private static int getRandom(Random r, TreeMap d) { return d.floorEntry(r.nextDouble()).getValue(); } }

Файл SimpleReceiver.java import java.util.*; import java.io.IOException; import java.io.FileReader; public class SimpleReceiver { static int MAX_LENGTH = 10000; static int Z = 32; static int Zd = 4; public static double[] getLogBezier(Map gstat) { double[] res = new double[MAX_LENGTH]; for (int i = 0; i < MAX_LENGTH; ++i) { for (Map.Entry e: gstat.entrySet()) { res[i] += e.getValue() * Math.exp(-Math.abs(e.getKey() - i) / 2.); } } double sum = 0; for (int i = 0; i < MAX_LENGTH; ++i) { sum += res[i]; } for (int i = 0; i < MAX_LENGTH; ++i) { res[i] = Math.log(res[i]) - Math.log(sum); } return res; } public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println("Usage: java SimpleReceiver "); System.exit(1); } String file = args[0]; int n = Integer.parseInt(args[1]); int n2 = n / 2; Scanner in = new Scanner(System.in); //BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Map lstat = new HashMap(); Map gstat = load(file); double[] gLogProb = getLogBezier(gstat); Deque deque = new ArrayDeque(); Deque teque = new ArrayDeque(); /* for (int i = 0; i < 2000; ++i) { System.err.print(i + " " + (lstat.containsKey(i) ? lstat.get(i) : 0)); System.err.println(" " + (gstat.containsKey(i) ? gstat.get(i) : 0)); } */ // System.out.println(dif); int i = 1; // read long timestamp = in.nextLong(); int length = in.nextInt(); long next = n2; ArrayDeque lastVals = new ArrayDeque(); int cyclicBufferLength = Z + 1; double[] cyclicBuffer = new double[cyclicBufferLength]; int cyclicBufferNext = 0; long shift = 0; double[] phaseWeight = new double[Z]; boolean transmissionStarted = false; long lastPeek = Long.MIN_VALUE; while (true) { if (timestamp > 10e6) { break; } int curPhase = (int)(shift % Z); // del while (!deque.isEmpty() && (next - teque.getFirst() >= n2)) { int del = deque.poll(); teque.poll(); updateStat(lstat, del, -1. ); double d1 = lstat.get(del); double d2 = gstat.containsKey(del) ? gstat.get(del) : 0; } //add while (timestamp < next) { updateStat(lstat, length, 1.); deque.add(length); teque.add(timestamp); double d1 = lstat.get(length); double d2 = gstat.containsKey(length) ? gstat.get(length) : 0; timestamp = in.nextLong(); length = in.nextInt(); } double dif = calcDif(lstat, gLogProb); cyclicBuffer[cyclicBufferNext] = dif; cyclicBufferNext++; cyclicBufferNext %= cyclicBufferLength; if (shift >= Z) { double vm = Double.POSITIVE_INFINITY; double vM = Double.NEGATIVE_INFINITY; double vl = Double.POSITIVE_INFINITY; double vr = Double.POSITIVE_INFINITY; for (int j = 0; j <= Z; ++j) { int k = (cyclicBufferNext + cyclicBufferLength - 1 - j) % cyclicBufferLength; if (cyclicBuffer[k] < vm) { vm = cyclicBuffer[k]; } if (cyclicBuffer[k] > vM) { vM = cyclicBuffer[k]; } if (j == 0) { vr = cyclicBuffer[k]; } if (j == Z) { vl = cyclicBuffer[k]; } } double h = ((vl + vr) / 2 - vm); double z = ((Math.abs(vl - vr) / h < 0.2) && ((vM - Math.max(vl, vr)) / h < 0.1)) ? h : 0; phaseWeight[curPhase] += z; if (z > 0) { transmissionStarted = true; lastPeek = shift; } System.out.println(next / 1e6 + " " + z + " " + dif); if (transmissionStarted) { int mostLikelyPhase = getMostLikelyPhase(phaseWeight); // System.err.println(next / 1e6 + " " + mostLikelyPhase); if (curPhase == (mostLikelyPhase + Zd) % Z) { if (lastPeek >= shift - 2 * Zd) { System.err.println("1"); } else { System.err.println("0"); } } } } next += n / (double)Z; shift++; } } private static int modDist(int i, int j, int n) { if (i > j) { int t = i; i = j; j = t; } return Math.min(j - i, i + n - j); } private static int getMostLikelyPhase(double[] phaseWeight) { int n = phaseWeight.length; int res = -1; double resWeight = -1; for (int i = 0; i < n; ++i) { double curWeight = 0; for (int j = 0; j < n; ++j) { curWeight += phaseWeight[j] * Math.exp(-modDist(i, j, n)); } if (curWeight > resWeight) { resWeight = curWeight; res = i; } } return res; } private static void updateStat(Map stat, int key, double delta) { if (!stat.containsKey(key)) { stat.put(key, 0.); } stat.put(key, stat.get(key) + delta); } private static double calcDif(Map lstat, double[] gLogProb) { double dif = 0; double n = 0; for (int key : lstat.keySet()) { double ld = lstat.get(key); dif += ld * gLogProb[key]; n += ld; } return dif / n; } static Map load(String file) throws IOException { Scanner in = new Scanner(new FileReader(file)); Map ans = new HashMap(); double sum = 0; while (in.hasNextInt()) { int a = in.nextInt(); int b = in.nextInt(); ans.put(b, (double)a); sum += a; } for (int key : ans.keySet()) { ans.put(key, ans.get(key) / sum); } return ans; } }

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]