1 00:00:08,040 --> 00:00:09,860 Hello, everyone. 2 00:00:09,860 --> 00:00:10,760 Good afternoon. 3 00:00:10,760 --> 00:00:14,080 I hope you're enjoying Devcon as well as I am. 4 00:00:14,560 --> 00:00:22,820 So my talk is about Androzia, which is a tool for securing data in process for your Android applications. 5 00:00:23,140 --> 00:00:34,020 So if I had to explain what Androzia is doing in a couple of lines, it's basically providing you the features of a proactive garbage collector. 6 00:00:34,480 --> 00:00:39,580 And garbage collectors inherently are lazy. 7 00:00:39,580 --> 00:00:44,480 So they only kick in when your application is memory constrained. 8 00:00:44,480 --> 00:00:48,680 And they will reclaim memory from objects which are unreferenced. 9 00:00:48,680 --> 00:00:58,980 But with Androzia, you will be able to reclaim memory from objects immediately after their last use. 10 00:00:59,280 --> 00:01:17,620 So this is helpful because if you have a lot of sensitive information lying around in your heap memory, as soon as it is last used, Androzia is going to kick in and collect all that sensitive information from your application and replace the memory contents with the default values. 11 00:01:17,620 --> 00:01:21,100 So your sensitive data will not remain on the memory anymore. 12 00:01:21,100 --> 00:01:24,160 So before we begin, a little bit about myself. 13 00:01:24,160 --> 00:01:24,800 I'm Samit. 14 00:01:24,800 --> 00:01:27,740 I work for the product security team at Citrix. 15 00:01:27,840 --> 00:01:31,080 I'm a web and mobile application security enthusiast. 16 00:01:31,080 --> 00:01:40,600 Spoken at a bunch of conferences, including Black Hat Asia, AppSec USA, Cocon, CodeBlue, IEEE Services, MobileSoft, and CloudCom. 17 00:01:41,220 --> 00:01:47,580 So, yeah, a little bit of contact information in case you want to contact me. 18 00:01:49,380 --> 00:01:57,780 So starting off, the first question for you is, which one of these is the most difficult to protect? 19 00:01:58,280 --> 00:02:03,220 Number one data at rest, number two data in process, or number three data in motion? 20 00:02:03,900 --> 00:02:07,800 So arguably, I would say that it's number two. 21 00:02:07,800 --> 00:02:08,740 Why? 22 00:02:09,220 --> 00:02:14,540 Because the least number of solutions in the market are for number two. 23 00:02:14,540 --> 00:02:17,100 Because this is a very gray area. 24 00:02:18,160 --> 00:02:23,400 You know, EMM providers do not really understand how to protect data in process. 25 00:02:23,760 --> 00:02:25,680 Data at rest is very simple. 26 00:02:25,680 --> 00:02:28,860 You use any ciphers and then protect your data. 27 00:02:28,860 --> 00:02:31,440 Data in motion can be protected using TLS. 28 00:02:31,480 --> 00:02:36,840 However, data in process is still a mystery for most of us. 29 00:02:36,840 --> 00:02:39,780 So this tool targets number two. 30 00:02:40,640 --> 00:02:44,380 And we'll see how, yeah. 31 00:02:44,380 --> 00:02:47,020 So, going ahead. 32 00:02:48,780 --> 00:02:55,200 So Java creates a lot of objects, or Android itself creates a lot of objects on the heap, right? 33 00:02:55,200 --> 00:03:08,920 And the objects may contain a lot of sensitive information which may include your authentication credentials, authorisation tokens, and encryption decryption keys, bins, ODPs, and personally identifiable information. 34 00:03:09,300 --> 00:03:15,560 So, as in when the application is executing, all of these will be on your heap memory. 35 00:03:15,560 --> 00:03:23,260 So the developers want to ensure that they get rid of all the sensitive information as soon as it is last used in the programme. 36 00:03:24,400 --> 00:03:28,360 So the myth is that the garbage collector will eventually collect it. 37 00:03:28,380 --> 00:03:32,400 But, you know, the garbage collector is very lazy, as I said. 38 00:03:32,400 --> 00:03:39,160 It will not come into the picture, or it will not collect all your data until it is essential for it. 39 00:03:39,160 --> 00:03:44,060 Because unless your application is memory-constrained, it will not come into the picture. 40 00:03:44,060 --> 00:03:48,180 And even if it does, right, its scope is very limited. 41 00:03:48,540 --> 00:03:53,320 So this is something which is called as an object tree on the heap. 42 00:03:53,540 --> 00:04:00,200 And how garbage collectors actually does a collection is using mark-and-sweep algorithm. 43 00:04:00,380 --> 00:04:05,140 So it will start off from the root nodes which are called GC root. 44 00:04:05,560 --> 00:04:14,900 And from the GC root, it traverses the object tree using depth-first search and keeps on marking all the objects that are reachable. 45 00:04:15,120 --> 00:04:26,860 In the sweep phase of the direct garbage collector's collection process, it is going to find out all the objects on the heap which are unreachable, and it is going to collect only those objects. 46 00:04:27,500 --> 00:04:41,420 So when a developer is, you know, writing code, he may forget to unreference some objects which may be these ones which are in the circle. 47 00:04:42,220 --> 00:04:49,820 And garbage collector will not collect these set of objects just because they are reachable from the GC roots. 48 00:04:50,240 --> 00:05:00,880 And these objects may contain sensitive information and they're going to lie around in your application throughout the lifetime of your application until the application is terminated. 49 00:05:01,420 --> 00:05:05,160 So this is where Androsia kicks in. 50 00:05:05,160 --> 00:05:10,800 It will clean off all the unused but reachable objects in your memory. 51 00:05:10,820 --> 00:05:19,000 So to summarize what I've been talking about until now, reachable and unused string builder objects may contain sensitive information. 52 00:05:19,000 --> 00:05:24,480 So throughout the course of this presentation, I'm going to specifically talk about string builder objects as an example. 53 00:05:24,480 --> 00:05:29,460 But this framework is, you know, extendable to any other kind of objects as well. 54 00:05:29,980 --> 00:05:41,240 So reachable and unused string builder objects may contain sensitive information and a heap dump or an application compromise will help an attacker to reveal that sensitive information easily. 55 00:05:41,260 --> 00:05:50,080 So don't just rely on the garbage collector to collect all that information, but instead destroy by overwriting all the critical data that you have in the application. 56 00:05:51,760 --> 00:06:02,920 So with that in mind, we do have some solutions provided by Java libraries which allow you to destroy objects that are created on the heap. 57 00:06:03,200 --> 00:06:07,520 One of them is the keystore.password protection class. 58 00:06:07,760 --> 00:06:13,160 And this class provides a destroy method which can be used to destroy the object's content. 59 00:06:14,480 --> 00:06:20,940 But again, you know, it's the developers, you know, it's upon the developer to use this API. 60 00:06:20,940 --> 00:06:25,640 He may use it at a very late stage in the program, or he may not even use it at all. 61 00:06:25,720 --> 00:06:28,840 So, ideally, we want to automate this process. 62 00:06:28,840 --> 00:06:31,660 We don't want to rely on the developer to destroy our data. 63 00:06:31,660 --> 00:06:34,240 Our data should be in our hands, not the developer's hands. 64 00:06:34,240 --> 00:06:44,700 So, you know, this is where Androsia will again automate that process and instrument code to remove or destroy any object that has critical information. 65 00:06:47,930 --> 00:06:49,990 So how does Androsia help? 66 00:06:52,630 --> 00:07:00,330 So Androsia uses static code analysis, and it determines the last use of objects at a whole program level. 67 00:07:01,890 --> 00:07:07,170 If I had to be specific, it uses a summary-based interprocedural data flow analysis. 68 00:07:07,450 --> 00:07:09,730 So there are three terms here, three key terms. 69 00:07:09,730 --> 00:07:14,450 One is summary-based, second is interprocedural, and the third one is data flow analysis. 70 00:07:14,450 --> 00:07:24,470 So static code analysis power is just not grabbing data and giving you information about what the Android applications permissions are or whatever stuff. 71 00:07:24,470 --> 00:07:26,690 It comes from data flow analysis. 72 00:07:26,690 --> 00:07:31,410 So every statement in your program has some data to contribute. 73 00:07:31,730 --> 00:07:43,950 And that data needs to be propagated across all the statements in your application, and every statement could probably add or remove some data from the data flow sets that are going on. 74 00:07:44,010 --> 00:07:47,990 So that's how powerful data flow analysis is. 75 00:07:47,990 --> 00:07:55,970 It will help you perform any sort of data related analysis, which may include even taint analysis. 76 00:07:56,650 --> 00:08:05,610 So that's where the power of static code analysis is better used. 77 00:08:05,870 --> 00:08:11,250 What I mean by summary-based interprocedural analysis is that this is a whole program analysis. 78 00:08:11,250 --> 00:08:13,950 This is not just analyzing a specific method. 79 00:08:13,970 --> 00:08:20,790 And every method, when it is analyzed, the analysis results can be maintained or cached in a summary. 80 00:08:21,750 --> 00:08:27,790 The reason for that is that a particular method, let's say foo, can be called from multiple locations. 81 00:08:27,790 --> 00:08:37,590 And if you have already computed the summary for foo, you may want to reuse that computed summary instead of computing it again and again whenever the method is called. 82 00:08:38,010 --> 00:08:44,150 So that's why we use summary as a cache for storing our analysis results for a specific method. 83 00:08:44,270 --> 00:08:51,990 And after we have determined the last usage point of a particular object, we are going to instrument byte code to clear its memory content. 84 00:08:53,870 --> 00:09:04,050 So to give you a very simple example, I have statements numbered 1 to 10 here, and there is a definition of variable x at statement number 3. 85 00:09:04,470 --> 00:09:08,310 And then there is a use of variable x at statement number 5. 86 00:09:08,590 --> 00:09:16,490 So between statement number 3 and 5, since the variable is defined and used, I can say that the variable is live. 87 00:09:17,510 --> 00:09:23,010 But before statement number 3, the variable x wasn't even defined and neither was it used. 88 00:09:23,010 --> 00:09:26,430 So it is said to be dead between 1 and 3. 89 00:09:26,430 --> 00:09:34,190 And now at 7, the variable x gets redefined and then reused at 8. 90 00:09:34,190 --> 00:09:37,570 So between 7 and 8, the variable is again live. 91 00:09:37,610 --> 00:09:40,810 However, between 5 and 7, the variable won't be live. 92 00:09:40,810 --> 00:09:43,630 It has been defined, but it is not being used. 93 00:09:44,510 --> 00:09:50,250 So this is just to give you an idea about how liveness of a variable looks like. 94 00:09:50,250 --> 00:10:01,070 And we are going to use it further ahead when we are seeing how Androsia really works and how it computes the liveness sets and how it then infers the last usage point. 95 00:10:04,200 --> 00:10:11,040 So basically, if you have a span of definition and usage, between that span, a variable is going to be live. 96 00:10:13,540 --> 00:10:18,400 So this is a snapshot of the heap dump taken using Eclipse Memory Analyzer Toolkit. 97 00:10:18,400 --> 00:10:26,880 So I had this sample code which contained a static field which is called static secret and it contained a password. 98 00:10:27,000 --> 00:10:29,200 And it shows up in the heap dump. 99 00:10:29,200 --> 00:10:40,220 And after the optimization or after the instrumentation, the static secret does not contain any password at all because Androsia has successfully removed the password from the heap memory. 100 00:10:40,220 --> 00:10:53,180 So, in order to give you an overview where Androsia would fit in is that a user could provide an application or source code to Androsia server. 101 00:10:53,180 --> 00:10:58,340 And Androsia server can then unpack the application to Dalvik byte code. 102 00:11:00,100 --> 00:11:04,640 And the Dalvik byte code gets converted into Gimple code. 103 00:11:04,740 --> 00:11:07,800 Now, Gimple is an intermediate representation. 104 00:11:08,320 --> 00:11:14,300 Just like Smiley, it is an intermediate representation which takes good from both the worlds. 105 00:11:14,300 --> 00:11:20,700 It takes good from the Dalvik byte code as well as good from the Java high-level language as well. 106 00:11:20,720 --> 00:11:25,680 So Gimple is basically a mixture of Java and it is simple so they call it Gimple. 107 00:11:26,180 --> 00:11:29,300 So Gimple has only 15 different kind of statements. 108 00:11:29,300 --> 00:11:37,380 Unlike Java which has a lot of complicated structures and different kind of statements, even Dalvik byte code has like 200 different opcodes. 109 00:11:37,620 --> 00:11:47,860 So Gimple has only a precise number of statements which is 15 and, you know, it is easier to analyze when you have just 15 odd statements to play around with. 110 00:11:47,880 --> 00:12:08,540 And this Gimple code is then analyzed by the analysis that we have embedded into Androsia and then the code instrumentation happens and then we convert the Gimple code back to Dalvik byte code and then the Dalvik byte code is packaged again and signed or we could just provide the analysis results to the user. 111 00:12:09,700 --> 00:12:15,440 So this is the entire flow of how will you eventually use Androsia. 112 00:12:16,920 --> 00:12:28,840 So an important fact that I should also mention here is that the framework which is called suit which is the backbone of Androsia can be used to perform any sort of data flow analysis. 113 00:12:28,840 --> 00:12:31,740 It's not just the analysis that I have done. 114 00:12:31,780 --> 00:12:39,200 So I'm going to talk about suit next and it can be used, you know, you can build your own data flow analysis over suit. 115 00:12:39,980 --> 00:12:49,640 So suit is basically static, so it's a framework for Java byte code analysis and it can be used to implement your own analysis as well. 116 00:12:50,040 --> 00:12:55,760 It provides this three address code representation called Gimple and it looks something like this. 117 00:12:55,760 --> 00:13:05,520 So for example, I have an if statement here and in any line of Gimple code, you will not see more than three operands in one line. 118 00:13:05,520 --> 00:13:09,060 So you have R1, null and label zero here. 119 00:13:09,280 --> 00:13:12,500 So it is very simple to view, you know. 120 00:13:12,500 --> 00:13:18,220 It's not like Java that you can have a lot of complicated structures in one statement itself. 121 00:13:19,520 --> 00:13:31,660 And this diagram shows you that the suit framework itself takes in the Java source code or the class files and converts it into Gimple three address intermediate representation. 122 00:13:32,000 --> 00:13:37,860 And then you can perform your analysis optimization and then again convert it back to the class files. 123 00:13:38,040 --> 00:13:48,960 So until recently, actually, you know, as I talked about that we are going to do an inter-procedural analysis, so suit also provides you the ability to perform an intra-procedural analysis. 124 00:13:51,300 --> 00:13:57,180 So until now, until recently, suit was missing a Dalvik to Gimple transformation module. 125 00:13:57,620 --> 00:14:02,280 And now that void has been fulfilled. 126 00:14:02,940 --> 00:14:10,800 And that void has been fulfilled by a plugin called Dexplore which allows you to transform Dalvik to Gimple code. 127 00:14:11,820 --> 00:14:15,560 So it makes it easier for you to analyse any Android application. 128 00:14:16,040 --> 00:14:16,480 Okay. 129 00:14:16,480 --> 00:14:21,460 So there's one more tool that I want to, you know, talk about here which is called FlowDroid. 130 00:14:21,680 --> 00:14:31,300 Now, FlowDroid allows you to generate a dummy main method because unlike Java, Android does not have, Android applications do not have a main method. 131 00:14:31,520 --> 00:14:35,520 And to start your data flow analysis, you need a starting point. 132 00:14:35,600 --> 00:14:39,960 And this dummy main method acts like a starting point for your data flow analysis. 133 00:14:43,290 --> 00:14:48,330 The dummy main method actually connects all the Android life cycle callbacks. 134 00:14:48,330 --> 00:14:51,250 So it gives you, you 135 00:15:04,630 --> 00:15:13,670 know, a dummy main method. 136 00:15:13,850 --> 00:15:17,470 So that way, you can start your data flow analysis from a single point. 137 00:15:17,710 --> 00:15:18,770 Okay. 138 00:15:18,770 --> 00:15:24,030 So objects can, you know, exist in various scopes. 139 00:15:24,050 --> 00:15:27,890 They can either exist as a local variable inside a method. 140 00:15:27,890 --> 00:15:35,010 For example, I have XYZ variables here which contain eventually the string builder objects. 141 00:15:35,030 --> 00:15:38,710 And their scope is limited to this method foo only. 142 00:15:38,710 --> 00:15:41,450 They're not used outside this method anywhere. 143 00:15:41,630 --> 00:15:50,710 So this is one simple example which I'm going to take or, you know, it will be a walk-through example throughout the course of this presentation. 144 00:15:50,830 --> 00:15:53,110 So this example will be recurring. 145 00:15:53,950 --> 00:16:00,530 And I have a variable X here which I'm instantiating with a string builder object. 146 00:16:00,630 --> 00:16:05,090 So basically X will be referring to the string builder which contains the value secret. 147 00:16:05,270 --> 00:16:08,510 Y will be referring to the string builder which contains the value password. 148 00:16:08,510 --> 00:16:14,690 And there's some logic here which I'm going to, you know, I'll just add it for the sake of explaining things. 149 00:16:15,430 --> 00:16:24,710 So the scope could be either as a local variable or your objects could even exist as static fields of a class. 150 00:16:24,750 --> 00:16:30,330 And this is completely different because, you know, the scope of a static field is the entire program. 151 00:16:30,330 --> 00:16:32,590 It's not just a specific method. 152 00:16:33,790 --> 00:16:41,830 So X, the value X, can be invoked from anywhere else using the class name which is my class. 153 00:16:43,610 --> 00:16:50,970 So this is how you will invoke variable, sorry, this is how you will invoke variable X. 154 00:16:54,020 --> 00:16:58,980 And this invocation can happen in any other method which is bar in my case. 155 00:16:59,280 --> 00:17:02,820 So the scope, you know, becomes the entire program. 156 00:17:04,020 --> 00:17:07,360 Or you can have objects as instance field. 157 00:17:07,520 --> 00:17:17,220 Here you can see that I have a private access modifier specified for instance field X inside the my instance field string builder class. 158 00:17:17,300 --> 00:17:27,560 So the scope of this variable X will now be limited to the scope of the object of this class, my instance field SB. 159 00:17:30,710 --> 00:17:31,390 Okay. 160 00:17:31,610 --> 00:17:36,830 So coming to the demo, I have three classes here. 161 00:17:36,830 --> 00:17:37,410 So I have three classes here . 162 00:17:37,610 --> 00:17:41,770 And one of them is the main activity where my control will begin. 163 00:17:41,950 --> 00:17:45,190 And this is where the secret has been defined. 164 00:17:45,470 --> 00:17:48,290 And then I'm calling the useStaticField method. 165 00:17:49,150 --> 00:17:55,170 And the useStaticField method uses the static secret and then calls bar method. 166 00:17:55,290 --> 00:17:58,850 And inside the bar method, I'm again using the static secret. 167 00:17:59,470 --> 00:18:08,230 So if I had to, you know, just ask you guys to point out the last usage of the static secret, it would be like inside the bar method, right? 168 00:18:08,230 --> 00:18:08,850 Here. 169 00:18:08,850 --> 00:18:14,390 So the instrumentation should happen immediately after the system.out.println statement of static secret. 170 00:18:16,290 --> 00:18:17,290 That's correct. 171 00:18:17,290 --> 00:18:19,710 But, you know, there can be loops as well. 172 00:18:20,370 --> 00:18:23,030 Like this statement could be inside a for loop. 173 00:18:23,030 --> 00:18:26,590 And then the instrumentation point will not be immediately after static secret. 174 00:18:26,590 --> 00:18:29,770 It will be right before the return statement of bar method. 175 00:18:29,770 --> 00:18:36,230 If there's a loop outside the call to the bar method, then your instrumentation will happen here. 176 00:18:36,430 --> 00:18:37,270 Why is that? 177 00:18:37,270 --> 00:18:43,390 Because if you instrument it within the loop, then your logic of the application will break. 178 00:18:43,390 --> 00:18:46,370 And you don't want that to happen. 179 00:18:47,230 --> 00:18:53,090 So we'll see an example, actually a demo on it, which is on the next slide. 180 00:18:53,590 --> 00:18:56,790 So this is the same code which I showed you on the last slide. 181 00:18:57,190 --> 00:19:03,570 I have a password string builder object being created which contains the value password. 182 00:19:03,750 --> 00:19:06,390 And then I'm calling the useStaticField method here. 183 00:19:07,270 --> 00:19:10,870 Now we'll see the definition of the useStaticField. 184 00:19:10,870 --> 00:19:14,930 The useStaticField is using the static secret and calling bar method. 185 00:19:15,450 --> 00:19:19,070 And inside the bar method, we have again useStaticSecret. 186 00:19:19,070 --> 00:19:23,390 And then we have two print statements which are printing hello and bye. 187 00:19:24,530 --> 00:19:33,670 So if you see, the instrumentation point should be right now just after the last use of static secret, which is on line 20. 188 00:19:33,670 --> 00:19:36,830 So let's run the tool on this. 189 00:19:41,650 --> 00:19:44,710 So this is how the dummy main method looks like. 190 00:19:44,710 --> 00:19:47,970 You'll see it in the stack trace that gets printed out. 191 00:19:51,090 --> 00:19:56,750 And right now, the output format is set to jimple so that we can see where the instrumentation is happening. 192 00:19:56,750 --> 00:20:00,850 You can even set it to Dalvik bytecode so you'll get a dex format as an output. 193 00:20:02,870 --> 00:20:03,510 Alright. 194 00:20:03,510 --> 00:20:06,670 We're in the onCreate method, but we need to go to the bar method, right? 195 00:20:06,750 --> 00:20:14,830 So inside the bar method... so can we pause the video, please? 196 00:20:16,530 --> 00:20:25,410 So you'll see that right here, you have the print line statement for static secret. 197 00:20:25,410 --> 00:20:29,810 This is the static secret reference variable, which is an R2. 198 00:20:29,970 --> 00:20:32,310 And you're printing the value of R2. 199 00:20:33,830 --> 00:20:42,210 And from line number 30 to 34 is where the code from Androsia that has been instrumented. 200 00:20:42,210 --> 00:20:46,410 So what I'm doing here is that I'm getting a reference to the static secret field. 201 00:20:46,410 --> 00:20:50,130 I'm calculating the length of the static secret field. 202 00:20:50,570 --> 00:20:58,670 And I'm using the delete API from zero to the length of the static secret to remove or clear its memory content. 203 00:20:59,170 --> 00:21:06,770 So the instrumentation point right now is right after the print line statement for static secret, which is the correct instrumentation point. 204 00:21:06,870 --> 00:21:08,790 So can we resume the video, please? 205 00:21:11,150 --> 00:21:12,150 Alright. 206 00:21:14,130 --> 00:21:18,170 Now what we're going to do is that we're going to remove the for loop here. 207 00:21:18,170 --> 00:21:20,890 We're going to uncomment the for loop here. 208 00:21:21,190 --> 00:21:32,930 And now the instrumentation point will not be between, will not be actually right after the static secret statement, but it will be right before the by statement here. 209 00:21:32,930 --> 00:21:37,510 The system.out.print line for the by statement, by keyword. 210 00:21:37,510 --> 00:21:39,090 So yeah. 211 00:21:39,090 --> 00:21:47,730 That's because you don't want to instrument within the loop, because in the next iteration of the loop, your resetted value will be used, which will break the logic of the code. 212 00:21:48,350 --> 00:21:49,050 Yeah. 213 00:21:49,150 --> 00:22:01,650 So this time the instrumentation, which is from 31 to 34, 33, is happening right before the print statement for by, which is the right instrumentation point. 214 00:22:01,750 --> 00:22:09,650 And Androzia is smart enough to figure out if there is a loop, it can hoist the instrumentation point outside the loop. 215 00:22:09,670 --> 00:22:12,650 So that's the main idea that I want to convey here. 216 00:22:13,050 --> 00:22:16,870 And that goes on for any level of nested for loops. 217 00:22:16,870 --> 00:22:28,410 You know, you can have the bar method being called from a for loop like this, and then the instrumentation point will change to somewhere in between, somewhere just before returning the use static field method. 218 00:22:28,490 --> 00:22:36,010 So the return statement would be somewhere here in the Gimple code, and the instrumentation point will be right before the return statement. 219 00:22:37,050 --> 00:22:47,750 This is the use static field, and right before the return statement of the use static field, you'll find the instrumented code from line number 73 to 74, 75, actually. 220 00:22:48,010 --> 00:22:49,110 Even 77. 221 00:22:49,110 --> 00:22:53,630 So this is the part which gets instrumented, just before the return statement. 222 00:22:53,750 --> 00:23:08,890 So you see that the instrumentation points are changing according to the loops that you have inside the code, which is, you know, which may not be because of nested loops, it may be even because of recursion of a specific method. 223 00:23:08,890 --> 00:23:16,170 So Androsia will be smartly identifying if there is recursion or loops, and changing the instrumentation points accordingly. 224 00:23:16,670 --> 00:23:29,290 So we've seen how Androsia is doing or performing the instrumentation, but we really don't know what is the logic behind that, what is the algorithm that is running behind Androsia. 225 00:23:29,490 --> 00:23:36,690 So to come to that, first let's identify what information can a single statement provide us. 226 00:23:36,750 --> 00:23:41,630 So there is, every line of statement has some or the other data to add, right? 227 00:23:41,630 --> 00:23:46,090 And the data that we are concerned about here is called liveness data. 228 00:23:50,660 --> 00:24:04,600 So the next few slides are going to talk about the live variable analysis definition, and how we compute the summary for every method, which is going to look something like this. 229 00:24:04,600 --> 00:24:07,100 It will be a two-tuple combination. 230 00:24:07,480 --> 00:24:10,060 Basically, the first tuple will be the variable. 231 00:24:10,140 --> 00:24:19,740 So the summary of foo method will tell you that the variable x was last used in the statement if y.length is less than x.length. 232 00:24:19,740 --> 00:24:21,280 It will be something like this. 233 00:24:21,400 --> 00:24:33,880 So for every variable, you will have, you know, a two-tuple element, and the summary will tell you how many variables are there, and what is the last usage point of every variable inside it. 234 00:24:34,600 --> 00:24:38,160 So to compute the summary, we have a two-step process. 235 00:24:38,160 --> 00:24:44,380 One is to use of, one is to compute the definition and use sets for every statement. 236 00:24:44,820 --> 00:24:51,160 Then using the definition and use sets, we are going to compute live variable sets for every statement. 237 00:24:52,080 --> 00:24:53,960 LVENTRY and LVEXIT. 238 00:24:55,060 --> 00:25:05,220 And using the live variable entry and exit sets, we're going to infer the last usage point for a local or static field reference within that method. 239 00:25:05,280 --> 00:25:07,320 And we're going to store that as a summary. 240 00:25:10,190 --> 00:25:23,090 And now, once we have the summaries for every method that is analyzed by Androsia, we're going to compute the last usage point of a static field reference, considering the whole program, not just within a method. 241 00:25:23,090 --> 00:25:29,590 Because we know that the summary will tell us that within a method, this is the last usage point of a particular local variable. 242 00:25:29,590 --> 00:25:34,490 But it will not tell us across the entire program where is the last usage actually happening. 243 00:25:34,490 --> 00:25:41,030 So the summaries need to be combined and propagated across your program to infer that knowledge. 244 00:25:44,400 --> 00:25:59,860 So to start off with the definition of live variable analysis, live variable analysis determines for each statement, which variables must have a subsequent use prior to the next definition, which is what we learned from the diagram we saw earlier, right? 245 00:25:59,860 --> 00:26:02,560 We had a definition and use of variable X. 246 00:26:02,560 --> 00:26:04,760 And between that span, the variable was live. 247 00:26:04,760 --> 00:26:06,600 And after that, it was dead. 248 00:26:06,600 --> 00:26:09,440 So this is exactly what the statement is telling us. 249 00:26:09,500 --> 00:26:17,980 So in this particular code, the last usage point of variable X is this blue statement, which is number 4. 250 00:26:18,120 --> 00:26:23,260 And the last usage point of Y is statement number 5 and 6. 251 00:26:23,260 --> 00:26:29,600 Because they are two different statements within the if clause. 252 00:26:29,600 --> 00:26:35,360 So either this one could execute or this one could execute based on the result from the if clause. 253 00:26:35,660 --> 00:26:39,060 And the last use of Z is statement number 7. 254 00:26:39,060 --> 00:26:40,400 So this is very intuitive, right? 255 00:26:40,400 --> 00:26:41,680 We can see it. 256 00:26:41,680 --> 00:26:47,180 But how do you determine this using an automated way? 257 00:26:47,180 --> 00:26:48,460 That's the question. 258 00:26:48,720 --> 00:26:51,200 And it's not as easy as it seems like. 259 00:26:51,200 --> 00:26:59,980 So we're going to run through this same example in the next few slides and figure out how we determine the last usage points of X, Y, and Z. 260 00:27:00,120 --> 00:27:06,160 So the last usage point of a variable is also the last statement where that variable was live. 261 00:27:06,620 --> 00:27:09,360 So X is last live here. 262 00:27:09,360 --> 00:27:12,540 And it is also the last usage point of that variable X. 263 00:27:12,600 --> 00:27:17,580 So we're going to use this fact to determine the last usage point of every variable. 264 00:27:19,920 --> 00:27:30,340 So talking about the def use sets, the definition of a definition set is that it will contain all the variables defined in a particular statement. 265 00:27:30,460 --> 00:27:34,220 So in statement number 1, X is being defined. 266 00:27:34,360 --> 00:27:37,420 So for statement 1, you'll have an entry X. 267 00:27:37,980 --> 00:27:43,340 So the use set are the set of variables that are used in a statement. 268 00:27:43,580 --> 00:27:49,620 So if we run through this example and compute the def use set, they will look something like this. 269 00:27:49,620 --> 00:27:50,960 So we start from the bottom. 270 00:27:50,960 --> 00:27:52,660 Z is being used here. 271 00:27:52,660 --> 00:27:57,600 So it will go in the use column for statement number 7. 272 00:27:58,540 --> 00:28:00,120 And X is being defined here. 273 00:28:00,120 --> 00:28:03,380 So it will go in the definition column for statement number 7. 274 00:28:03,380 --> 00:28:09,400 And similarly, if we keep going, it will eventually populate the entire table. 275 00:28:10,420 --> 00:28:19,640 So the direction of propagating that data or propagating the data flow facts is from bottom to top. 276 00:28:19,700 --> 00:28:23,160 It is the reverse order of execution of the program. 277 00:28:23,540 --> 00:28:34,500 So if I compute the live variable of exit of 6th statement, which is the last statement in my code, then it is going to be 5. 278 00:28:34,600 --> 00:28:40,100 Because there is no other variable which is being used after statement number 6. 279 00:28:40,440 --> 00:28:41,720 So this is a must. 280 00:28:41,720 --> 00:28:47,960 Like this has to be 5 because there cannot be any other variable which is used after the last statement in the code. 281 00:28:48,080 --> 00:28:52,400 So this will be an initial point for the algorithm to run. 282 00:28:52,520 --> 00:29:00,440 And once you compute LVExit of 6, you are going to use the def use set of 6 to compute LVEntry of 6. 283 00:29:00,940 --> 00:29:10,040 And then you will compute LVExit of 5, then you will compute LVEntry of 5, then LVExit of 4, LVEntry of 4. 284 00:29:10,040 --> 00:29:22,540 Similarly, once you reach at a point where there is two branches, you are going to merge the results from LVEntry 3 and LVEntry 4 using a union operator. 285 00:29:25,260 --> 00:29:29,080 And that result will go into LVExit of 2. 286 00:29:29,380 --> 00:29:33,780 And using LVExit of 2, we are going to compute LVEntry of 2 and so on. 287 00:29:34,960 --> 00:29:36,500 So that's up next. 288 00:29:36,500 --> 00:29:42,960 We are going to use the same example, the one we discussed for the def use set and we are going to compute these things. 289 00:29:43,080 --> 00:29:53,820 So if I had to give you a mathematical representation, I mean, there's nothing possible without math here because data flow analysis needs to have math backing it up for proof of correctness. 290 00:29:53,820 --> 00:29:59,780 And without any algorithm or without some math, there's no way you can perform a data flow analysis. 291 00:30:00,180 --> 00:30:10,620 So the LVExit of a statement L will be 5 if L is the last statement of the body, which is what we discussed on the last slide. 292 00:30:10,880 --> 00:30:17,460 And it will be the union over the LVEntry set of the successors of L. 293 00:30:18,880 --> 00:30:30,480 Otherwise, so if you have a merge point, we'll have to take this LVEntry sets and do a union over all the entry sets to compute LVExit of L. 294 00:30:30,640 --> 00:30:40,920 Now once we have the LVExit of L, we are going to plug that value here and we already computed LVEfinition and use it for a statement L. 295 00:30:41,660 --> 00:30:44,780 And that's going to give us LVEntry of L. 296 00:30:46,060 --> 00:30:47,800 Let's see this in action. 297 00:30:47,860 --> 00:30:50,920 So we have the def use columns populated. 298 00:30:52,480 --> 00:30:58,000 We know that the LVExit of 7 statement will be 5 because this is the last statement. 299 00:30:58,420 --> 00:31:03,300 And now we are going to use this formula to compute LVEntry of 7. 300 00:31:03,300 --> 00:31:12,920 So LVExit of 7 will be 5 minus x union z, which will give you z. 301 00:31:14,060 --> 00:31:19,620 And then LVEntry of 7 will form the LVExit of 6. 302 00:31:19,700 --> 00:31:22,660 So LVExit of 6 will be z. 303 00:31:22,860 --> 00:31:27,620 And now z minus z union y will give you y. 304 00:31:27,620 --> 00:31:31,620 So you get the LVEntry of statement number 6 as well. 305 00:31:31,620 --> 00:31:39,780 And so on, you can populate this table iteratively and you get the entire table. 306 00:31:39,900 --> 00:31:58,040 Now, the interesting part to note here is that if a variable disappears from the entry set to the exit set in the live variable table, like in the fourth statement, we have x disappearing from the entry set to the exit set. 307 00:31:58,040 --> 00:32:03,560 In the fifth and sixth statement, y disappears from entry set to the exit set. 308 00:32:03,560 --> 00:32:10,120 And in the seventh statement, we see that z disappears from the entry set to the exit set. 309 00:32:10,120 --> 00:32:15,540 So those will be, the corresponding numbers will be the last usage point of those variables. 310 00:32:16,060 --> 00:32:17,820 And that's what we were after. 311 00:32:17,820 --> 00:32:19,100 We've got that now. 312 00:32:19,640 --> 00:32:28,120 So now we're going to store all these results, the last usage point results, as the summary of foo method. 313 00:32:29,400 --> 00:32:35,860 And we're going to use this summary in other parts of the program when the foo method gets invoked from there. 314 00:32:35,860 --> 00:32:38,620 We don't want to compute this again and again and again. 315 00:32:40,020 --> 00:32:45,440 So let's just say that foo was, there was a method foo which calls bar and bar called baz. 316 00:32:45,440 --> 00:32:46,200 Okay? 317 00:32:46,200 --> 00:32:54,640 So, and just, we've already computed, let's just assume that we've already computed summaries for each of these individual methods. 318 00:32:54,640 --> 00:32:56,440 Foo, bar, as well as baz. 319 00:32:56,520 --> 00:33:03,880 And baz will tell me that, the summary of baz is going to tell me that static field reference was last used at C4. 320 00:33:04,140 --> 00:33:09,920 The summary for bar is telling me that the static field reference was used at B5. 321 00:33:09,920 --> 00:33:12,740 And there was no use of SFR in foo. 322 00:33:13,020 --> 00:33:21,020 Now what I'm going to do is, I'm going to propagate the summaries across these methods in a reverse topological order. 323 00:33:21,080 --> 00:33:25,600 So, this is how the analysis will run. 324 00:33:25,600 --> 00:33:30,320 And when you read statement C4, you see that SFR is being used. 325 00:33:30,360 --> 00:33:45,020 What I'm going to do is create a data structure with the value baz, SFR, C4, which is going to tell me that the last use of SFR is happening at C4 statement in baz method. 326 00:33:45,500 --> 00:33:46,320 Okay? 327 00:33:46,820 --> 00:33:54,800 And once I reach, I've already analyzed, I've completed the analysis for baz method, I'm going to analyze the bar method. 328 00:33:55,100 --> 00:33:58,980 So, the analysis happens in a reverse topological order. 329 00:33:59,040 --> 00:34:02,860 So, if baz is the last method that is called, it will be analyzed first. 330 00:34:03,200 --> 00:34:11,260 If bar is the second last method that is analyzed, it will be, you know, if bar is the second last method that is called, it will be analyzed second last. 331 00:34:11,420 --> 00:34:20,720 And if the first, sorry, so basically foo is calling bar, bar is calling baz, baz will be analyzed first, bar will be analyzed second, and then you'll analyze foo. 332 00:34:22,800 --> 00:34:26,300 So, here, we see that SFR is being used at B5. 333 00:34:26,300 --> 00:34:29,360 So, the LV entry will contain B5 SFR. 334 00:34:29,360 --> 00:34:37,580 And once we reach the call statement for baz, we're going to pull in the summary of baz method from here. 335 00:34:37,760 --> 00:34:46,120 And now we're going to see whether the LV entries, LV exit set of B3 already contains an SFR's use, and it does in our case. 336 00:34:46,180 --> 00:34:59,760 So, we're going to replace the value of this little red data structure with the new entry, which will be bar SFR B5, which would mean that SFR is last used at B5 statement inside bar method. 337 00:35:00,060 --> 00:35:05,320 So, this is how you'll keep on going and updating the red data structure. 338 00:35:05,340 --> 00:35:17,860 And once the analysis is complete over all these methods, you'll realize that the red data structure will complete the last usage point of the SFR variable across the entire program. 339 00:35:18,160 --> 00:35:34,120 So, similarly, if you go ahead and see that there's a bar method invocation here, you're going to pull in the summary for bar method, but the exit set of A3 contains 5, because SFR is not being used ahead in the program inside the foo method. 340 00:35:34,540 --> 00:35:44,580 So, this summary will not be over, this particular value will not be overwritten by anything, because it's just 5 that is being coming from the exit set of A3. 341 00:35:44,580 --> 00:35:53,140 We just completed our analysis, and the results tell us that SFR was last being used at B5 statement inside the bar method. 342 00:35:53,320 --> 00:36:03,700 So, this is how, you know, algorithmically, Androsia will run on your code and figure out what is the last usage point of any object in that application. 343 00:36:04,780 --> 00:36:10,280 Just to summarize, we went through this definition of live variable analysis. 344 00:36:10,540 --> 00:36:15,700 We computed summaries for individual methods using a two-step process. 345 00:36:15,700 --> 00:36:32,780 One of the steps is to compute the defuse set for every statement, and using the defuse sets, we computed the LV entry and exit sets, and using the LV entry and exit sets, we figured out the last usage point of every static field reference or local method, 346 00:36:32,780 --> 00:36:40,100 and put it in a summary, and eventually, we used the summary to compute the last usage point for a static field reference at a whole program level. 347 00:36:40,260 --> 00:36:40,960 Phew! 348 00:36:40,960 --> 00:36:43,460 So, that was too much. 349 00:36:43,460 --> 00:36:50,520 I mean, just intuitively, you can see what is the last use point, but to automate that, you require a lot of algorithm and math. 350 00:36:51,640 --> 00:36:56,280 So, yeah, I mean, things are not easy always. 351 00:36:56,640 --> 00:37:03,020 So, for instance field approach, the approach, you know, for instance fields is a little different. 352 00:37:03,020 --> 00:37:07,480 What we do is we mark all the classes which are string builder instance fields. 353 00:37:07,500 --> 00:37:09,480 We find their object instances. 354 00:37:09,760 --> 00:37:16,100 We track the last usage of object instances and their aliases instead of the string builder fields themselves. 355 00:37:16,120 --> 00:37:23,100 We will actually see a demo of this again, and I think that would explain this even better. 356 00:37:23,100 --> 00:37:27,140 So, just let's see, we have three classes here, which one of them is main activity. 357 00:37:27,140 --> 00:37:29,440 This is where my control is going to start. 358 00:37:29,480 --> 00:37:45,740 I have a secret defined here, which is a variable, which is going into a variable my secret, and then I'm instantiating the my class, which is here, and the variable that is referring to the object of my class is mc. 359 00:37:45,740 --> 00:37:52,120 Then we have a wrapper class whom I'm instantiating, and the reference variable for that is w. 360 00:37:52,300 --> 00:38:01,240 And then I have a call to call w method, which is defined inside the wrapper method. 361 00:38:01,400 --> 00:38:02,680 Wrapper class, sorry. 362 00:38:02,760 --> 00:38:14,880 And I'm passing the mc object instance to this call w method, which is invoking set a to pass on the secret value here, and this is just a setter method, which is setting the value of a. 363 00:38:14,880 --> 00:38:27,000 So, basically, the secret is here, then it goes here, and then it gets defined in this, and then it gets set in the setter method to the instance field a, which is a private instance field. 364 00:38:27,920 --> 00:38:37,400 So, in order to reset, if we had to reset this private instance field, we cannot just do it outside from within the on create method. 365 00:38:37,400 --> 00:38:50,280 We need to invoke a method within the class to reset the value of a, because it has been privately, the access modifier is private, so we can't, you know, access it outside its class. 366 00:38:52,100 --> 00:38:55,960 So, let's just quickly jump on to the demo. 367 00:38:58,940 --> 00:39:07,880 So, the steps that I had explained on the previous slides were we mark all classes which are string builder instance fields, which will be my class. 368 00:39:08,780 --> 00:39:12,740 We find the object instances, which is MC. 369 00:39:13,500 --> 00:39:19,280 Then we track the last uses of object instances, which is here in the call W statement. 370 00:39:19,880 --> 00:39:24,820 And then we instrument the code right after the call W statement. 371 00:39:25,180 --> 00:39:30,020 And here, you can see that I'm invoking a method, which is reset SBA. 372 00:39:30,040 --> 00:39:34,320 And this reset SBA is being defined in the class right here. 373 00:39:34,320 --> 00:39:41,200 That's because I cannot access the private instance field small a within the on create method. 374 00:39:42,240 --> 00:39:44,980 So, I have to invoke this reset method. 375 00:39:45,900 --> 00:39:46,620 All right. 376 00:39:46,620 --> 00:39:48,440 So, let's have a look at the demo. 377 00:39:49,880 --> 00:39:51,180 Pretty same code. 378 00:39:51,180 --> 00:39:52,780 Pretty much the same code. 379 00:39:52,780 --> 00:39:56,460 I have the call W statement here where I'm passing MC and my secret. 380 00:39:57,000 --> 00:40:02,440 And I also have the wrapper class here instantiated. 381 00:40:02,440 --> 00:40:04,400 And the reference variable is W. 382 00:40:04,560 --> 00:40:07,280 And I'm printing the W.SB field. 383 00:40:07,280 --> 00:40:12,000 So, SBField is another string builder instance field of wrapper class. 384 00:40:12,240 --> 00:40:15,160 And A and B, we can see that. 385 00:40:15,520 --> 00:40:20,400 So, A and B will be instance fields of my class. 386 00:40:20,400 --> 00:40:23,460 And SB will be an instance field in the wrapper class. 387 00:40:23,520 --> 00:40:25,800 We'll see that just in a while. 388 00:40:32,000 --> 00:40:35,720 So, you can see that we have an instance field SB here in the wrapper class. 389 00:40:36,080 --> 00:40:41,960 And I'm calling set A and passing on the secrets value to the set A method. 390 00:40:41,960 --> 00:40:45,660 And my class has A and B as its instance fields. 391 00:40:45,660 --> 00:40:49,880 And inside the set A method, I'm setting the value of the secret to A. 392 00:40:51,180 --> 00:40:53,380 B is nowhere used right now. 393 00:41:00,820 --> 00:41:06,780 So, inside the wrapper class, you see that the method call W also has a secret defined called password. 394 00:41:07,160 --> 00:41:10,980 And its last use is inside the call W method. 395 00:41:14,610 --> 00:41:16,710 So, B is used here. 396 00:41:19,130 --> 00:41:20,830 MC is last used here. 397 00:41:20,830 --> 00:41:22,630 And W is last used here. 398 00:41:22,950 --> 00:41:29,290 So, the instrumentation point for A and B should be right after the call W statement here. 399 00:41:29,450 --> 00:41:35,010 And the instrumentation point for the string build SB should be right after the WSB statement here. 400 00:41:35,590 --> 00:41:43,450 So, you see here that there's a call W method being invoked. 401 00:41:43,690 --> 00:41:50,230 And I have the reset methods being injected or instrumented right after the call W method. 402 00:41:50,230 --> 00:41:52,010 Because we were tracking MC. 403 00:41:52,010 --> 00:41:55,890 And MC's last use was in the call W method call. 404 00:41:55,890 --> 00:41:59,030 And after that, we have reset both the instance fields. 405 00:41:59,030 --> 00:42:09,630 And similarly, for the wrapper class, we had an instance field SB which is being reset right here in statement number 58. 406 00:42:18,490 --> 00:42:25,330 So, these reset methods are defined in the respective classes which have those instance fields. 407 00:42:25,950 --> 00:42:34,230 So, if we go to the respective classes, this is the wrapper class and the reset method has been defined here. 408 00:42:34,510 --> 00:42:36,190 What it's doing is the same thing. 409 00:42:36,190 --> 00:42:39,490 It's calling the length function on the string builder. 410 00:42:39,490 --> 00:42:43,310 And then it's calling the delete API to remove the content of the string builder. 411 00:42:46,860 --> 00:42:54,940 And similarly, we have reset methods for variable A and B, which were the instance field in my class. 412 00:42:57,500 --> 00:43:02,360 Here we have reset SBB, which is the reset method for variable B. 413 00:43:04,440 --> 00:43:08,060 So, you know, we've tackled all the three scopes now. 414 00:43:08,060 --> 00:43:10,480 We know how to deal with instance fields. 415 00:43:10,480 --> 00:43:13,540 We know how to deal with static fields. 416 00:43:13,540 --> 00:43:15,560 We also know how to deal with local variables. 417 00:43:16,060 --> 00:43:20,880 So, I mean, we did not need to see all of this because this is too much in depth. 418 00:43:20,880 --> 00:43:25,760 But if you want to go ahead and implement your own static code analysis, this is important stuff for you. 419 00:43:25,860 --> 00:43:29,780 So, I mean, I thought it appropriate to be, you know, displayed here. 420 00:43:30,820 --> 00:43:41,080 So, the work in progress is basically we're working on a test development so that we can, you know, get rid of the remaining bugs that we have. 421 00:43:41,080 --> 00:43:47,020 And we're also planning to include this into the CICD pipeline, which should be straightforward. 422 00:43:47,280 --> 00:43:54,040 And that way, a lot of big companies could, you know, just plug in their APKs and just push it across to us. 423 00:43:54,040 --> 00:44:03,760 And then we can just analyze those APKs and instrument the code that Androsia, you know, instruments to clear the memory contents of the APK. 424 00:44:03,820 --> 00:44:13,140 So, that way, it will be helpful for big corporates and especially the enterprise mobility management companies. 425 00:44:14,040 --> 00:44:20,840 So, if you want to use Androsia or you want to contribute or get in touch, this is the URL you should take a note of. 426 00:44:20,860 --> 00:44:23,460 And the tool and documentation will be available here . 427 00:44:23,700 --> 00:44:29,100 And if you want to contribute to the tool, here's my Twitter account or email. 428 00:44:29,100 --> 00:44:31,140 You can just shoot out an email to me. 429 00:44:32,840 --> 00:44:39,880 All right. 430 00:44:39,880 --> 00:44:42,120 So, these are some good references. 431 00:44:42,160 --> 00:44:48,100 The first three ones will get you started when it comes to creating your own data flow analysis. 432 00:44:48,320 --> 00:44:52,160 And the last two ones are a little bit more in-depth. 433 00:44:52,160 --> 00:44:57,460 If you want to, you know, see how things are actually working in the background. 434 00:44:57,460 --> 00:45:01,100 I'll anyways post these slides on the website of DEF CON. 435 00:45:01,100 --> 00:45:04,320 So, you can have a look at these references. 436 00:45:06,880 --> 00:45:07,920 Cool. 437 00:45:08,840 --> 00:45:11,640 So, that brings me to the end of my talk. 438 00:45:11,680 --> 00:45:13,520 Thank you for your time. 439 00:45:13,900 --> 00:45:16,540 I'm really happy to speak over here on this podium. 440 00:45:16,540 --> 00:45:17,820 I hope you enjoyed it. 441 00:45:17,820 --> 00:45:19,160 Thank you very much.