Analyzer.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. package analyzer;
  2. import analyzer.models.Client;
  3. import analyzer.models.Hashtag;
  4. import analyzer.models.Round;
  5. import analyzer.models.User;
  6. import java.io.IOException;
  7. import java.util.*;
  8. import java.util.concurrent.ExecutionException;
  9. import java.util.concurrent.ExecutorService;
  10. import java.util.concurrent.Executors;
  11. import java.util.concurrent.Future;
  12. import java.util.concurrent.atomic.AtomicInteger;
  13. /**
  14. * This class calls the parsers running parallel, then performs intersection attacks from the loaded objects.
  15. */
  16. public class Analyzer {
  17. private static final int threadPoolSize = 8;
  18. public static final int roundLength = 1800;
  19. public static final int clientNo = 1000000;
  20. public static final boolean coverTraffic = false;
  21. public static final int delay = 0;
  22. public static final String childPath = coverTraffic ? "ct" : delay > 0 ? "delay" : roundLength + "";
  23. public static final String logType = "random_"; //"top-" "random_"
  24. //public static final String logPath = "C:\\Users\\Admin\\Desktop\\Skripts\\Thesis\\Repo\\local\\vm-mount\\logs\\" + logType + clientNo + "\\" + roundLength;
  25. public static final String logPath = "C:\\Users\\Admin\\Desktop\\Skripts\\Thesis\\Repo\\local\\vm-mount\\logs\\" + logType + clientNo + "\\" + childPath;
  26. public static String clientLogPath = logPath + "\\clogs.txt";
  27. public static String serverLogPath = logPath + "\\slogs.txt";
  28. //TEST
  29. //private static final User toTraceUser = new User(306637085);
  30. private static final String toTraceHashtag = "InstantFollowBack";
  31. private static final String intersectingClientID = "client-1034";
  32. private static final long targetUserID = 398449825L;
  33. public static void main(String[] args) throws InterruptedException, ExecutionException {
  34. //a.traceHashtag();
  35. //traceClientHashtagLink();
  36. traceClientUserLink();
  37. }
  38. public Analyzer () throws InterruptedException {
  39. Thread slogs = new Thread(() -> {
  40. try {
  41. ServerLogParser.run();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. });
  46. Thread clogs = new Thread(ClientLogParser::run);
  47. slogs.start();
  48. clogs.start();
  49. slogs.join();
  50. clogs.join();
  51. //checkParsing();
  52. System.out.println("round length: " + Analyzer.roundLength);
  53. //new ServerLogParser(serverLogPath).traceUser();
  54. }
  55. private static void traceClientUserLink() throws InterruptedException, ExecutionException {
  56. Analyzer privateInst = new Analyzer();
  57. int roundNum = privateInst.intersectUsers(ClientLogParser.clients.get(intersectingClientID), 1, true);
  58. System.out.println("roundNum: " + roundNum);
  59. }
  60. private static void traceClientHashtagLink() throws ExecutionException, InterruptedException {
  61. Analyzer privateInst = new Analyzer();
  62. List<Map.Entry<String, Integer>> intersection = privateInst.intersectHashtags(ClientLogParser.clients.get(intersectingClientID), 1000, true);
  63. Hashtag[] hashtags = new Hashtag[intersection.size()];
  64. for(int i = 0; i < hashtags.length; i++)
  65. hashtags[i] = ServerLogParser.hashtags.get(intersection.get(i).getKey());
  66. System.out.println();
  67. List<Map.Entry<Hashtag, Integer>> results = privateInst.calculatePointsGivenHashtags(ClientLogParser.clients.get(intersectingClientID), hashtags, 1, true);
  68. System.out.println("Result size: " + results.size());
  69. User targetUser = ServerLogParser.users.get(targetUserID);
  70. System.out.println();
  71. targetUser.getHashtags().forEach((k, v) -> System.out.println(k.getName()));
  72. }
  73. public void traceHashtag() throws InterruptedException, ExecutionException {
  74. Hashtag staticTargetHashtag = ServerLogParser.hashtags.get(toTraceHashtag);
  75. Client staticTargetClient = ClientLogParser.clients.get(intersectingClientID);
  76. System.out.println(staticTargetHashtag.getName() + " size: " + staticTargetHashtag.getRoundMap().size());
  77. long startCalculatingMaxPoint = System.currentTimeMillis();
  78. calculatePointsAllClients(staticTargetHashtag, 0, true);
  79. long calculatingMaxPointTime = System.currentTimeMillis() - startCalculatingMaxPoint;
  80. System.out.println("CalculatingMaxPointTime: " + calculatingMaxPointTime + " ms");
  81. System.out.println();
  82. System.out.println(staticTargetClient.getId() + " " + staticTargetClient.postingTraces());
  83. //print traced hashtag
  84. System.out.println("Hashtag " + staticTargetHashtag.getName() + " " + staticTargetHashtag.hashtagTraces());
  85. long startIntersecting = System.currentTimeMillis();
  86. intersectHashtags(staticTargetClient, 0, true);
  87. long intersectingTime = System.currentTimeMillis() - startIntersecting;
  88. System.out.println("Intersecting Time: " + intersectingTime + " ms");
  89. }
  90. /**
  91. * This method calculates points between a given hashtag and all clients, and prints out these points if needed.
  92. * @param hashtag hashtag to calculate points against all clients
  93. * @param returnNum number of returned highest point 2-tuples (client, point)
  94. * @param printOut true if log print-out is needed
  95. * @return list of highest point 2-tuples (client, point)
  96. * @throws InterruptedException
  97. * @throws ExecutionException
  98. */
  99. public List<Map.Entry<Client, Integer>> calculatePointsAllClients(Hashtag hashtag, int returnNum, boolean printOut) throws InterruptedException, ExecutionException {
  100. Hashtable<Client, Integer> clientPointsWithHashtag = new Hashtable<>(ClientLogParser.clients.size(), 1);
  101. ClientLogParser.clients.values().forEach(c -> clientPointsWithHashtag.put(c, 0));
  102. ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
  103. List<Future<?>> tasks = new ArrayList<>();
  104. for(Client c : clientPointsWithHashtag.keySet()) {
  105. Future<?> task = executor.submit(new PointCalculatorTask(c, hashtag, clientPointsWithHashtag, null));
  106. tasks.add(task);
  107. }
  108. for(Future<?> task : tasks)
  109. task.get();
  110. executor.shutdown();
  111. long startSortTime = System.currentTimeMillis();
  112. List<Map.Entry<Client, Integer>> sortedList = new ArrayList<>(clientPointsWithHashtag.entrySet());
  113. sortedList.sort(((o1, o2) -> o2.getValue().compareTo(o1.getValue())));
  114. long sortTime = System.currentTimeMillis() - startSortTime;
  115. if(printOut) {
  116. System.out.println("Points for hashtag " + hashtag.getName());
  117. System.out.println("Sorting time : " + sortTime + " ms");
  118. System.out.println("Client-ID\tPoints");
  119. int maxPrintOut = Math.min(20, sortedList.size());
  120. for(int i=0; i < maxPrintOut; i++) {
  121. Map.Entry<Client, Integer> e = sortedList.get(i);
  122. System.out.println(e.getKey().getId() + "\t" + e.getValue());
  123. }
  124. }
  125. if(returnNum > 0) {
  126. if(returnNum >= sortedList.size())
  127. return sortedList;
  128. else {
  129. List<Map.Entry<Client, Integer>> returnList = new ArrayList<>();
  130. int maxVal = sortedList.get(0).getValue();
  131. for (Map.Entry<Client, Integer> entry : sortedList) {
  132. if (entry.getValue().equals(maxVal)) {
  133. returnList.add(entry);
  134. } else {
  135. if (returnList.size() < returnNum) {
  136. maxVal = entry.getValue();
  137. returnList.add(entry);
  138. } else
  139. break;
  140. }
  141. }
  142. return returnList;
  143. }
  144. }
  145. return null;
  146. }
  147. /**
  148. * This method calculates points between a client and a set of hashtags and prints out these points if needed.
  149. * @param client client to calculate point against a set of hashtags
  150. * @param hashtags set of hashtags to calculate point against client
  151. * @param returnNum number of returned highest point 2-tuples (hashtag, point)
  152. * @param printOut true if log print-out is needed
  153. * @return list of highest point 2-tuples (hashtag, point)
  154. * @throws InterruptedException
  155. * @throws ExecutionException
  156. */
  157. public List<Map.Entry<Hashtag, Integer>> calculatePointsGivenHashtags(Client client, Hashtag[] hashtags, int returnNum, boolean printOut) throws InterruptedException, ExecutionException {
  158. Hashtable<Hashtag, Integer> hashtagPointMap = new Hashtable<>(hashtags.length, 1);
  159. Arrays.stream(hashtags).forEach(h -> hashtagPointMap.put(h, 0));
  160. ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize);
  161. List<Future<?>> tasks = new ArrayList<>();
  162. for(Hashtag h : hashtags) {
  163. Future<?> task = executor.submit(new PointCalculatorTask(client, h, null, hashtagPointMap));
  164. tasks.add(task);
  165. }
  166. for(Future<?> task : tasks)
  167. task.get();
  168. executor.shutdown();
  169. long startSortTime = System.currentTimeMillis();
  170. List<Map.Entry<Hashtag, Integer>> sortedList = new ArrayList<>(hashtagPointMap.entrySet());
  171. sortedList.sort(((o1, o2) -> o2.getValue().compareTo(o1.getValue())));
  172. long sortTime = System.currentTimeMillis() - startSortTime;
  173. if(returnNum > 0) {
  174. if(returnNum >= sortedList.size())
  175. return sortedList;
  176. else {
  177. List<Map.Entry<Hashtag, Integer>> returnList = new ArrayList<>();
  178. int maxVal = sortedList.get(0).getValue();
  179. for (Map.Entry<Hashtag, Integer> entry : sortedList) {
  180. if (entry.getValue().equals(maxVal)) {
  181. returnList.add(entry);
  182. } else {
  183. if (returnList.size() < returnNum) {
  184. maxVal = entry.getValue();
  185. returnList.add(entry);
  186. } else
  187. break;
  188. }
  189. }
  190. if(printOut) {
  191. System.out.println("Points for Client " + client.getId());
  192. System.out.println("Sorting time : " + sortTime + " ms");
  193. System.out.println("Client-ID\tPoints");
  194. for(int i=0; i < returnList.size(); i++) {
  195. Map.Entry<Hashtag, Integer> e = returnList.get(i);
  196. System.out.println(e.getKey().getName() + "\t" + e.getValue());
  197. }
  198. }
  199. return returnList;
  200. }
  201. }
  202. return null;
  203. }
  204. /**
  205. * This method performs intersection attacks by calculating the correlation points from a given targetClient against hashtags based on active rounds
  206. * @param targetClient target client to perform the attack
  207. * @param returnNum number of returned 2-tuples of most relevant suspects
  208. * @param printOut true if log print-out is needed
  209. * @return list of highest point 2-tuples (hashtag, point)
  210. */
  211. public List<Map.Entry<String, Integer>> intersectHashtags(Client targetClient, int returnNum, boolean printOut) {
  212. //initialize a mapping of all possible hashtags with their no of occurrences
  213. Map<String, Integer> results = new Hashtable<>();
  214. List<Round> targetRounds = new ArrayList<>(targetClient.getRounds().keySet());
  215. int targetNoRounds = targetRounds.size();
  216. for(int i = 0; i < targetNoRounds; i++) {
  217. results.forEach((k, v) -> results.replace(k, v - 1));
  218. //results.entrySet().removeIf(e -> (e.getValue() < 0));
  219. int targetRoundNo = targetRounds.get(i).getNo();
  220. List<Hashtag> hashtagsOfARound = new ArrayList<>(ServerLogParser.rounds.get(targetRoundNo).getHashtags().keySet());
  221. for(Hashtag hashtag: hashtagsOfARound) {
  222. if(results.containsKey(hashtag.getName()))
  223. results.replace(hashtag.getName(), results.get(hashtag.getName()) + 2);
  224. else
  225. results.put(hashtag.getName(), -(i + 1) + 1);
  226. }
  227. }
  228. //results.entrySet().removeIf(e -> (e.getValue() < targetNoRound /2));
  229. List<Map.Entry<String, Integer>> sortedList = new ArrayList<>(results.entrySet());
  230. sortedList.sort(Map.Entry.comparingByValue((v1, v2) -> Integer.compare(v2, v1)));
  231. if(returnNum > 0) {
  232. if(returnNum >= sortedList.size())
  233. return sortedList;
  234. else {
  235. List<Map.Entry<String, Integer>> returnList = new ArrayList<>();
  236. int maxVal = sortedList.get(0).getValue();
  237. for (Map.Entry<String, Integer> entry : sortedList) {
  238. if (entry.getValue().equals(maxVal)) {
  239. returnList.add(entry);
  240. } else {
  241. if (returnList.size() < returnNum) {
  242. maxVal = entry.getValue();
  243. returnList.add(entry);
  244. } else
  245. break;
  246. }
  247. }
  248. if(printOut) {
  249. System.out.print(targetClient.getId() + ": ");
  250. System.out.println("no of rounds of " + targetClient.getId() + ": " + targetNoRounds);
  251. for(int i = 0; i < returnList.size(); i++) {
  252. System.out.print(returnList.get(i).getKey() + "|" + returnList.get(i).getValue() + " ");
  253. }
  254. //sortedList.forEach(e -> System.out.print(e.getKey() + "|" + e.getValue() + " "));
  255. System.out.println();
  256. }
  257. return returnList;
  258. }
  259. }
  260. return null;
  261. }
  262. /**
  263. * This method performs intersection attacks by matching the behaviour of targetClient with all other clients.
  264. * @param targetClient target client to perform the attacks
  265. * @param returnNum number of returned 2-tuples of most relevant suspects
  266. * @param printOut true if log print-out is needed
  267. * @return number of rounds needed until the attacks finish
  268. */
  269. public int intersectUsers(Client targetClient, int returnNum, boolean printOut) {
  270. int roundProcessed = -1;
  271. List<User> userList = new ArrayList<>(ServerLogParser.users.values());
  272. List<Round> roundList = new ArrayList<>(ServerLogParser.rounds.values());
  273. roundList.sort(Comparator.comparingInt(Round::getNo));
  274. for(Round r: roundList) {
  275. if(targetClient.getRounds().containsKey(r)) {
  276. userList.removeIf(u -> !u.getRounds().containsKey(r) || !u.getRounds().get(r).equals(targetClient.getRounds().get(r)));
  277. } else {
  278. userList.removeIf(u -> u.getRounds().containsKey(r));
  279. }
  280. if(userList.size() <= 1) {
  281. roundProcessed = r.getNo();
  282. break;
  283. }
  284. }
  285. if(userList.size() != 1) {
  286. roundProcessed = -1;
  287. }
  288. if(printOut) {
  289. System.out.println("Suspect(s): ");
  290. userList.forEach(e -> System.out.println(e.getId()));
  291. System.out.println();
  292. System.out.println("rounds processed: " + roundProcessed);
  293. System.out.println();
  294. System.out.println();
  295. }
  296. return roundProcessed;
  297. }
  298. /**
  299. * This method checks the parsing processes, exits the JVM if there is an error.
  300. */
  301. private static void checkParsing() {
  302. if(ClientLogParser.clientRoundNo != ServerLogParser.serverRoundNo) {
  303. System.out.println("Parsing Error: Round numbers are not equals");
  304. System.exit(1);
  305. }
  306. if(ClientLogParser.clients.size() != ServerLogParser.users.size()) {
  307. System.out.println("Parsing Error: Client/User numbers are not equals");
  308. System.exit(1);
  309. }
  310. }
  311. /**
  312. * This class models the parallel execution of point calculations
  313. */
  314. private static class PointCalculatorTask implements Runnable {
  315. private final Client client;
  316. private final Hashtag hashtag;
  317. private final Hashtable<Client, Integer> clientMap;
  318. private final Hashtable<Hashtag, Integer> hashtagMap;
  319. private PointCalculatorTask(Client c, Hashtag hashtag, Hashtable<Client, Integer> clientMap, Hashtable<Hashtag, Integer> hashtagMap) {
  320. this.client = c;
  321. this.hashtag = hashtag;
  322. this.clientMap = clientMap;
  323. this.hashtagMap = hashtagMap;
  324. }
  325. @Override
  326. public void run() {
  327. Map<Round, Integer> clone = new Hashtable<>(client.getRounds());
  328. AtomicInteger n = new AtomicInteger();
  329. clone.keySet().retainAll(this.hashtag.getRoundMap().keySet());
  330. clone.forEach((k, v) -> n.addAndGet((Math.min(v, this.hashtag.getRoundMap().get(k)))));
  331. if(clientMap != null)
  332. clientMap.put(this.client, n.get());
  333. if(hashtagMap != null)
  334. hashtagMap.put(this.hashtag, n.get());
  335. }
  336. }
  337. }