1 00:00:00,880 --> 00:00:02,580 So it's 20 minutes. 2 00:00:02,900 --> 00:00:04,780 There's a lot of information. 3 00:00:04,840 --> 00:00:09,160 There's links in the slides at the end for, well, the whole version. 4 00:00:09,200 --> 00:00:10,460 Thank you all for coming. 5 00:00:11,280 --> 00:00:13,220 Table of contents for today. 6 00:00:13,220 --> 00:00:18,900 We'll be looking at Ghidra and some related info to that, mostly. 7 00:00:19,460 --> 00:00:24,080 So first I'll introduce myself and then I'll give you some background information. 8 00:00:24,080 --> 00:00:32,680 We'll go a bit more into the nitty gritty details and I want to end a bit with the methodology and then in the end there might be time for Q&A. 9 00:00:32,680 --> 00:00:36,780 If not, find me afterwards or run to a microphone if there is time. 10 00:00:37,700 --> 00:00:39,220 So my name is Max Hersta. 11 00:00:39,220 --> 00:00:40,840 I go by the nickname of Libra. 12 00:00:40,840 --> 00:00:44,180 I work for Trellix within the advanced research team. 13 00:00:44,180 --> 00:00:53,020 We do research into all kinds of malware related things that can be something new, can be a trend, can be anything that piques interest. 14 00:00:53,180 --> 00:00:59,240 And one of the things we do is outreach and share that with the community such as, here. 15 00:01:00,400 --> 00:01:08,740 Now, the talk itself is about symbols or rather the lack thereof in binaries and how to recover them, right? 16 00:01:08,740 --> 00:01:16,380 But before diving into that, I want to share a bit what is a symbol and this slide should show you exactly what it's not, right? 17 00:01:16,380 --> 00:01:27,940 So a symbol is normally the function name from a reversing perspective and normally when you have symbols they should be recognizable, they should be self-explanatory and it should be unambiguous. 18 00:01:28,460 --> 00:01:35,440 Unlike the symbols I used here, this would be more useful for you to recognize something with. 19 00:01:38,060 --> 00:01:41,620 So binaries consist of multiple types of code. 20 00:01:41,620 --> 00:01:48,660 Now, this is no official definition but it's more to what I adhere to and it will make more sense down the road. 21 00:01:48,840 --> 00:01:57,560 So in terms of types of code, I'm going to be making a difference between the runtime, libraries and user code. 22 00:01:57,560 --> 00:02:10,540 Now, there's some gray areas in between, it obviously has room for discussion but in this case the purpose is mainly to focus on the big aspects that they each have, right? 23 00:02:10,540 --> 00:02:14,740 So for runtime, I'm going to use Golang as an example. 24 00:02:15,220 --> 00:02:29,260 The runtime is statically compiled within your binary or at least the part that you use, meaning that if you have 10 different Golang binaries of the same Golang version, you will have a lot of overlap of code. 25 00:02:29,280 --> 00:02:36,720 Now, that's not to say that those 10 binaries are related to each other other than the fact that they share the same Golang runtime. 26 00:02:36,800 --> 00:02:47,140 But if you have zero functions or function names within your binary, it's kind of hard to find out what the function does even though your tools might say there's overlap. 27 00:02:47,540 --> 00:02:53,100 Now, the same kind of goes for the libraries but libraries might give you more of an indication, right? 28 00:02:53,100 --> 00:03:08,280 If you're searching for a specific version of a library or if you're tracking a piece of malware based on the library that they use, you might be very interested in the library but to do so you would need to know where to find it and what library is included. 29 00:03:08,760 --> 00:03:14,620 And then as a last part, we have the user code and the user code would be anything that the user writes. 30 00:03:14,620 --> 00:03:22,980 So the user code generally calls functions within the runtime and the library or consists of purely user code itself, right? 31 00:03:22,980 --> 00:03:33,800 You make a minimalistic C binary and you use no external software then your binary will be very small and you'll just have your user code. 32 00:03:33,800 --> 00:03:53,760 Now, the user code is obviously of interest from my perspective as a malware researcher because if I can find that in other binaries, it will tell me that this is likely a shared piece of code and the user code is generally relatively unique to the extent that we can say these binaries overlap. 33 00:03:55,160 --> 00:04:03,200 So to recover symbols, we have multiple ways and three of them are what I'm going to be focusing on. 34 00:04:03,200 --> 00:04:07,480 So you have metadata in your binaries that you might be able to use. 35 00:04:07,480 --> 00:04:15,020 Golang is another great example of this where you have the PCLN tab and the information within so you can recover the function names. 36 00:04:15,040 --> 00:04:17,660 Obviously, using this is of great benefit. 37 00:04:17,660 --> 00:04:19,860 However, not all binaries have this. 38 00:04:19,860 --> 00:04:24,880 You have a stripped C++ binary, you're going to have a hard time, right? 39 00:04:25,080 --> 00:04:29,980 Now, Ghidra has two features that you can use to recover symbols. 40 00:04:30,380 --> 00:04:33,700 One is specifically made for this, which is function ID. 41 00:04:33,700 --> 00:04:38,420 For those using IDA, that would be flirt signatures, but then Ghidra is equivalent. 42 00:04:38,940 --> 00:04:42,940 And it has a hash of a function and a function name. 43 00:04:42,940 --> 00:04:55,860 So it hashes your function, cutting some corners here, but the gist of it is that you have a hash and if the hash is found in another place, you can compare the hash and take the name from the mapping that you already have. 44 00:04:56,160 --> 00:04:59,380 And then there's bsim, which stands for behavioral similarity. 45 00:04:59,640 --> 00:05:05,980 This was recently released by the NSA, recently being the day before last Christmas. 46 00:05:06,440 --> 00:05:21,680 And what you can do is you have the decompiler, produces intermediate language called p-code, and based on the high p-code vectors and some other things are calculated, based on which you can find functions that overlap code. 47 00:05:21,960 --> 00:05:31,700 Within Ghidra, you have the advantage that you have the decompiler of your match and of your original function side by side. 48 00:05:31,700 --> 00:05:36,660 So say you're patch diffing a file, you can very nicely see the differences. 49 00:05:37,940 --> 00:05:49,620 In order to do that, you do need to retain access to the Ghidra project and the files therein which you're using, or where the match is found as well as your program that you're analyzing. 50 00:05:50,780 --> 00:06:06,100 The script I have is kind of shoehorning that where we do not use the last part, meaning we have less data required and we can also automate the process because in the end it's code that you can use for this. 51 00:06:06,720 --> 00:06:14,060 So for the function ID databases, I wrote a script that you can automate. 52 00:06:14,100 --> 00:06:29,740 First the ingestion, you can run Ghidra headless to just import your files, and then this script will make for each unique architecture and what Ghidra calls a language ID, it will make a different function ID database that you can use later on. 53 00:06:29,740 --> 00:06:40,060 A language ID, very briefly put, is a PE file, 32 bits, for Windows for a specific compiler, and then we do that for each. 54 00:06:40,160 --> 00:06:50,100 This allows you to have the files that you can share with colleagues, either internally or externally, and this is the quickest way of recovering the symbols. 55 00:06:52,120 --> 00:07:03,640 As a case study, I have a Qubit Stealer sample which is Golang-based, and it has a lot of functions, but it has no real names, and we'll get more statistics on this in a bit. 56 00:07:03,640 --> 00:07:11,940 But just to showcase function ID, the first top screenshot is a screenshot that is post-default analysis from Ghidra. 57 00:07:11,960 --> 00:07:17,940 Right, so it just has fun underscore, which is the equivalent in IDA of saying sub underscore. 58 00:07:18,180 --> 00:07:21,140 So these are just functions that have no names. 59 00:07:21,220 --> 00:07:30,340 Now in the bottom screenshot, we can see that the P256 sub-internal function has been recognized and subsequently been renamed, which saves you time as the analyst, right? 60 00:07:30,340 --> 00:07:37,180 This is part of your default analysis, because the function ID is an analyzer which runs within. 61 00:07:37,480 --> 00:07:45,400 For the sake here, I first ran it without the function ID library which contains these functions, and then the one after. 62 00:07:47,400 --> 00:07:56,980 So for bsim, I already mentioned it's a bit shoehorned, because we're not going to be using the GUI to do this, and we're also not relying on the fact that you need access to the Ghidra project. 63 00:07:56,980 --> 00:08:11,460 Meaning that if I make a bsim database and I give it to you, then you can still use those databases with this script, even though you do not have access to the Ghidra project and the files therein, which I used to create it. 64 00:08:11,660 --> 00:08:19,640 So these are, along with function ID databases and some more information, all available on GitHub afterwards. 65 00:08:20,420 --> 00:08:35,160 So it's not necessarily using bsim as intended, but since the screenshot on top is relatively readable, I think, on screen, you can see that it has a function name, which is within the fdesc, right? 66 00:08:35,160 --> 00:08:37,860 Function description XML object. 67 00:08:37,980 --> 00:08:46,200 And within there, you can see that there are some vectors and calls being made to other functions, which is what bsim uses to identify this. 68 00:08:46,740 --> 00:08:52,340 The fact that you can automate this doesn't mean it's fully magically working for you. 69 00:08:52,340 --> 00:09:02,860 You still need to specify what your threshold is for the similarity, which is a value between zero, meaning no overlap, and one being an exact match. 70 00:09:02,940 --> 00:09:13,660 You also need to specify how many results do you want from the bsim database to match or to be collected for use, right? 71 00:09:13,660 --> 00:09:18,540 So you can do one, meaning if you have the first match it finds, it will say, well, it's this. 72 00:09:18,820 --> 00:09:30,500 But especially if you're not relying on an exact match, but rather, say, 75% or more of overlap, you might be interested in what other functions might be matching as well. 73 00:09:30,500 --> 00:09:39,300 Now, obviously, if all those functions have the exact same name, it's pretty clear that based on the database that you use, there's only one match. 74 00:09:39,760 --> 00:09:48,160 But you can also have the case that you have multiple functions, different names that match, which requires you to do a bit more research. 75 00:09:50,420 --> 00:09:54,660 To automate this in the script, you have identical matches. 76 00:09:54,740 --> 00:09:57,060 So these are renamed as is, right? 77 00:09:57,060 --> 00:10:01,500 Identical meaning there's only one name matched, even if you have multiple functions. 78 00:10:02,680 --> 00:10:09,360 The information, the metadata that's still available in the signatures is added to that function within Kedra. 79 00:10:09,620 --> 00:10:16,240 And it will show you the original file, the similarity for the match based on the function you're looking at. 80 00:10:16,240 --> 00:10:21,220 The significance, which tends to be based on your function size. 81 00:10:21,220 --> 00:10:23,100 And the architecture, right? 82 00:10:23,100 --> 00:10:28,720 It should only match the architecture that you have, though bsim has specifics. 83 00:10:28,720 --> 00:10:37,160 So since it's based on a decompiler, you can make databases that are known specifically for the bitness of your architecture. 84 00:10:37,160 --> 00:10:43,820 So say you have a Intel-based binary, 132-bit version and 164-bit version. 85 00:10:43,820 --> 00:10:46,680 The size of an integer, for example, is different, right? 86 00:10:46,680 --> 00:10:48,340 It's four versus eight bytes. 87 00:10:48,840 --> 00:11:07,360 What you can tell bsim to do when making the or importing the signatures in a database is you can give it a no-size database, meaning that it will ignore certain values and allow you to find matches of code from a 64-bit binary in a 32-bit binary or vice versa. 88 00:11:08,820 --> 00:11:15,340 So the information here will allow you to more accurately decide if this matches. 89 00:11:15,400 --> 00:11:17,620 And given the original file name, right? 90 00:11:17,620 --> 00:11:24,080 This says empty project go 1.9, for example, and you can see 1.14, 1.13. 91 00:11:24,080 --> 00:11:28,760 So these are the go versions where this function was also found in when making signatures. 92 00:11:28,820 --> 00:11:33,400 So this gives you an indication as to which Golang version it might be. 93 00:11:33,400 --> 00:11:37,340 And it will also tell you it's a Golang function on its own. 94 00:11:37,360 --> 00:11:44,200 Obviously, the original file name is based on what you use as a file name in Ghidra when making the signatures. 95 00:11:44,220 --> 00:11:46,840 So if you give it useful names, that's great. 96 00:11:46,840 --> 00:11:50,580 If not, then it might not be as great. 97 00:11:52,020 --> 00:11:55,140 Having said that, there's also a generic match, right? 98 00:11:55,140 --> 00:11:56,540 Because we want to automate this. 99 00:11:56,540 --> 00:12:02,120 You can have functions with thousands of, well, unknown functions within your binary. 100 00:12:02,400 --> 00:12:10,800 So there's also what I call a generic match, meaning there is more than one unique name that has been returned from the database. 101 00:12:10,920 --> 00:12:16,220 Now, not renaming these at all will not help you, right? 102 00:12:16,220 --> 00:12:19,480 Because you can make these databases for very specific purposes. 103 00:12:20,100 --> 00:12:23,880 Say, right, this is my database for the Golang runtime. 104 00:12:23,880 --> 00:12:36,280 Then any match that I have based on my similarity threshold will be Golang runtime related, or at least be very likely to be related to it. 105 00:12:36,380 --> 00:12:45,340 So in this case, we have two functions and we can see that the package name within Go has changed over time. 106 00:12:45,340 --> 00:12:47,880 Yet the function name itself is the same. 107 00:12:47,900 --> 00:12:52,260 Now, obviously, we could add a lot more logic as to how do we identify this between strings. 108 00:12:52,380 --> 00:12:55,340 But that is a future work. 109 00:12:55,400 --> 00:12:57,840 But also kind of depends on what you're using, right? 110 00:12:57,840 --> 00:13:00,640 If it's multiple versions of the same runtime, that works. 111 00:13:00,640 --> 00:13:04,860 But maybe you have a database where you put in not only Golang, but a lot more. 112 00:13:04,860 --> 00:13:07,180 It gets a lot harder to do the differentiation. 113 00:13:08,200 --> 00:13:15,000 So instead, I opted to rename all those functions that match generically with a prefix. 114 00:13:15,000 --> 00:13:20,580 And you can provide this prefix when running the script headless so that any match is then renamed. 115 00:13:20,840 --> 00:13:23,480 And if you run it over multiple databases, right? 116 00:13:23,480 --> 00:13:28,200 So the script, unless you change it, uses only one database at a time. 117 00:13:28,200 --> 00:13:32,840 What you can do is simply provide the prefix. 118 00:13:32,840 --> 00:13:40,360 And any function that has the non-default function naming scheme is not included in subsequent runs, right? 119 00:13:40,360 --> 00:13:47,500 So if a function starts with fun underscore or thunk underscore fun underscore, it's included in the search. 120 00:13:47,500 --> 00:13:53,100 However, if you have this function right now, it's Golang underscore whatever. 121 00:13:53,100 --> 00:14:02,360 If I now run it on another database I have for bsim, then this function is not going to be included anymore. 122 00:14:02,360 --> 00:14:11,600 So your subsequent runs will A, be faster, but also likely more accurate depending on the order you run it and how you structure your databases. 123 00:14:13,040 --> 00:14:19,660 So to continue with our case study, we have the top screenshot, which is after function ID, right? 124 00:14:19,660 --> 00:14:26,700 And I would recommend you first run function ID because it's faster and it is designed literally for this purpose. 125 00:14:27,440 --> 00:14:31,520 And bsim is a bit misused for this purpose, yet it works. 126 00:14:32,120 --> 00:14:38,940 And then we see in the bottom one, we see that the different functions are now renamed to Golang underscore, right? 127 00:14:38,940 --> 00:14:41,300 So these are all generic matches. 128 00:14:42,420 --> 00:14:50,520 Now looking at this function, and it's a bit of a spoiler alert, it is just a function within the Golang runtime that I used as an example. 129 00:14:50,520 --> 00:14:58,060 But you would also guess that it is, given that there's only calls to the Golang runtime within. 130 00:15:00,080 --> 00:15:11,980 So aside from making these scripts, I decided that making this invoke would A, be a good test case, but it would also save CPU cycles for others as soon as we share this, right? 131 00:15:11,980 --> 00:15:21,960 So I downloaded hundreds of gigabytes of benign binaries, mostly runtimes and libraries, compiled them if need be. 132 00:15:22,160 --> 00:15:28,750 And I ended up with 117 gigabytes of signatures for bsim. 133 00:15:30,900 --> 00:15:37,460 And the data doesn't necessarily say much, but there's not a lot of exact details I have for the bsim part. 134 00:15:37,600 --> 00:15:42,680 I do, however, for function ID, because you get a lot of detailed information in return. 135 00:15:42,700 --> 00:15:47,420 So 60% is Golang based as to what I created. 136 00:15:47,420 --> 00:15:50,120 And then we have 35 related to Boost, right? 137 00:15:50,120 --> 00:15:54,880 The C++ library, which contains a lot and it's used a lot. 138 00:15:54,880 --> 00:15:56,660 And then there's, well, other. 139 00:15:56,660 --> 00:15:57,580 Now, what is other? 140 00:15:57,580 --> 00:15:58,760 You might ask the 5%. 141 00:15:59,660 --> 00:16:04,920 So here I have the signature generation with the unique number of signatures within each. 142 00:16:05,080 --> 00:16:09,440 So for Golang, that was any runtime available that I had at the time. 143 00:16:09,440 --> 00:16:13,340 For Boost, it's roughly the last 10 years of Boost. 144 00:16:13,380 --> 00:16:18,800 And then we have, say, LibXML, OpenSSL, other libraries that are used. 145 00:16:18,880 --> 00:16:21,980 Now, depending on how big they are, you have more or less functions. 146 00:16:22,220 --> 00:16:30,260 But overall, this is, well, I would say a decent amount of functions that now you don't have to reverse or make signatures for anymore. 147 00:16:31,160 --> 00:16:41,900 So to put that in a broader perspective for the Qubits dealer, when you do the default Gitter analysis, it will find 2016 functions, out of which one has a name, right? 148 00:16:41,900 --> 00:16:43,080 The entry function. 149 00:16:43,740 --> 00:16:46,700 If we then run function ID, one function is merged, right? 150 00:16:46,700 --> 00:16:52,180 The total goes down one, but we recover about 40% of the function names. 151 00:16:52,180 --> 00:16:57,100 If we then use bsim with a 75%, right? 152 00:16:57,100 --> 00:17:05,140 0.75 threshold for similarity, we recover another 16%, leaving us at 58. 153 00:17:05,200 --> 00:17:07,840 Now, that doesn't necessarily sound too much. 154 00:17:07,840 --> 00:17:09,140 So why is that? 155 00:17:09,140 --> 00:17:16,380 Now, we had access to the Qubit dealer source code, and it imports a library. 156 00:17:16,380 --> 00:17:20,380 And there's no signatures for Golang libraries in this corpus. 157 00:17:20,380 --> 00:17:24,920 So if you make this, you will have even more matches. 158 00:17:26,000 --> 00:17:37,600 So as a methodology, I would suggest you use, feedback is obviously welcome, that you identify metadata within the binary if you can. 159 00:17:37,620 --> 00:17:40,800 You can utilize that if you find it. 160 00:17:40,800 --> 00:17:44,680 You want to identify the runtimes and libraries used within your binary. 161 00:17:44,680 --> 00:17:48,820 You might need to make signatures for them if you don't have them yet, right? 162 00:17:48,820 --> 00:17:50,240 Maybe you identified something. 163 00:17:50,240 --> 00:17:56,580 So you first make the signatures, you apply them, and you do that process both for function ID and for the behavior signatures. 164 00:17:56,980 --> 00:18:01,320 Once that's done, you can have any other further logic that you can use, right? 165 00:18:01,320 --> 00:18:05,900 A lot of people are using LLMs to help rename functions without looking at them necessarily. 166 00:18:05,900 --> 00:18:07,900 So you can apply more scripts. 167 00:18:08,080 --> 00:18:18,120 And you can all do this from importing a binary, running function ID, bsim, and your other logic within the headless execution. 168 00:18:18,120 --> 00:18:19,780 So essentially, you can do that at night. 169 00:18:19,780 --> 00:18:23,320 When you wake up, everything is ready for you to analyze. 170 00:18:25,080 --> 00:18:27,220 That leaves room for questions. 171 00:18:27,360 --> 00:18:29,340 There's two minutes left. 172 00:18:29,360 --> 00:18:33,460 Either ask them now or find me off stage. 173 00:18:33,600 --> 00:18:41,840 The GitHub link here, I will activate them after the talk, has the GitHub scripts for this talk that I created. 174 00:18:41,840 --> 00:18:46,220 It has function ID databases and it has the bsim databases. 175 00:18:46,220 --> 00:18:50,490 Additionally, there is about 66 gigabytes of Golang binaries. 176 00:18:50,840 --> 00:18:55,020 It's an empty main function and contains the entire runtime of that version. 177 00:18:55,240 --> 00:19:00,800 And if you want to make signatures, play around with them, then feel very much free to as well. 178 00:19:00,820 --> 00:19:08,970 So the URL is at the top and then you have the slash for each of the sub options or just go there and find the repositories. 179 00:19:09,900 --> 00:19:11,280 Thank you very much. 180 00:19:33,310 --> 00:19:36,310 You have a microphone in the front. 181 00:19:44,380 --> 00:19:47,300 All right, if you just shout, I'll repeat the question. 182 00:19:52,280 --> 00:19:59,140 So the question is, how good is bsim when matching functions compiled with different compilers? 183 00:20:00,140 --> 00:20:02,300 I haven't tested this myself. 184 00:20:02,300 --> 00:20:07,400 It's a relatively new feature within Kedra. 185 00:20:07,400 --> 00:20:12,360 I think it does relatively well because it uses the decompiler. 186 00:20:12,360 --> 00:20:15,320 Some of the abstractions are moved away. 187 00:20:15,900 --> 00:20:21,260 And I think it also depends if Kedra knows the used calling conventions and such. 188 00:20:25,310 --> 00:20:31,270 Hi, so in a similar vein, how do you deal with different compiler options? 189 00:20:31,270 --> 00:20:36,310 Things like f-function sections, all the moving symbols around. 190 00:20:36,310 --> 00:20:39,230 What of those kind of break this process? 191 00:20:41,550 --> 00:20:43,290 Yes and no. 192 00:20:44,570 --> 00:20:46,770 It's the standard engineering answer, right? 193 00:20:46,770 --> 00:20:47,870 It depends. 194 00:20:48,090 --> 00:20:59,050 So if you say, use an unoptimized compiler run versus a very optimized, you will have differences in code, meaning there's differences within the signatures as well. 195 00:20:59,050 --> 00:21:02,750 For bsim, it might break it because it does match certain things. 196 00:21:02,750 --> 00:21:08,630 But if the match isn't matching well enough, it will not find a hit. 197 00:21:09,030 --> 00:21:12,530 For bsim, you can tweak how much it should look alike. 198 00:21:12,530 --> 00:21:18,290 If you are aware that something specific is used and you have a database for this, it might help. 199 00:21:19,590 --> 00:21:27,770 But if it changed too much, then you need to lower your threshold too much and you will probably get too many hits to make sense of it. 200 00:21:27,810 --> 00:21:38,270 So I think the best option is to compile the target libraries in all options and then make signatures for all, which is obviously a lot more work. 201 00:21:38,730 --> 00:21:43,290 But I think that is the most accurate option that you have. 202 00:21:43,710 --> 00:21:44,710 Thank you. 203 00:21:48,270 --> 00:21:48,970 All right. 204 00:21:48,970 --> 00:21:50,030 Thank you very much.