start	end	text
880	4780	So it's 20 minutes. There's a lot of information.
4840	10460	There's links in the slides at the end for, well, the whole version. Thank you all for coming.
11280	18900	Table of contents for today. We'll be looking at Ghidra and some related info to that, mostly.
19460	24080	So first I'll introduce myself and then I'll give you some background information.
24080	28280	We'll go a bit more into the nitty gritty details and I want to end a bit with the methodology
28830	32680	and then in the end there might be time for Q&A.
32680	36780	If not, find me afterwards or run to a microphone if there is time.
37700	40840	So my name is Max Hersta. I go by the nickname of Libra.
40840	44180	I work for Trellix within the advanced research team.
44180	49300	We do research into all kinds of malware related things that can be something new,
49300	53020	can be a trend, can be anything that piques interest.
53180	58260	And one of the things we do is outreach and share that with the community such as,
58260	59240	here.
60400	65760	Now, the talk itself is about symbols or rather the lack thereof in binaries
66400	68740	and how to recover them, right?
68740	71880	But before diving into that, I want to share a bit what is a symbol
72420	76380	and this slide should show you exactly what it's not, right?
76380	81180	So a symbol is normally the function name from a reversing perspective
81780	85220	and normally when you have symbols they should be recognizable,
85220	87940	they should be self-explanatory and it should be unambiguous.
88460	90420	Unlike the symbols I used here,
90420	95440	this would be more useful for you to recognize something with.
98060	101620	So binaries consist of multiple types of code.
101620	105940	Now, this is no official definition but it's more to what I adhere to
105940	108660	and it will make more sense down the road.
108840	113560	So in terms of types of code, I'm going to be making a difference
113560	117560	between the runtime, libraries and user code.
117560	123080	Now, there's some gray areas in between, it obviously has room for discussion
123080	130540	but in this case the purpose is mainly to focus on the big aspects that they each have, right?
130540	134740	So for runtime, I'm going to use Golang as an example.
135220	140500	The runtime is statically compiled within your binary or at least the part that you use,
140500	146000	meaning that if you have 10 different Golang binaries of the same Golang version,
146000	149260	you will have a lot of overlap of code.
149280	153120	Now, that's not to say that those 10 binaries are related to each other
153120	156720	other than the fact that they share the same Golang runtime.
156800	161620	But if you have zero functions or function names within your binary,
161620	164180	it's kind of hard to find out what the function does
164720	167140	even though your tools might say there's overlap.
167540	169340	Now, the same kind of goes for the libraries
169920	173100	but libraries might give you more of an indication, right?
173100	175740	If you're searching for a specific version of a library
175740	180780	or if you're tracking a piece of malware based on the library that they use,
180780	182680	you might be very interested in the library
182680	188280	but to do so you would need to know where to find it and what library is included.
188760	190700	And then as a last part, we have the user code
191240	194620	and the user code would be anything that the user writes.
194620	198420	So the user code generally calls functions within the runtime and the library
199320	202980	or consists of purely user code itself, right?
202980	208040	You make a minimalistic C binary and you use no external software
208840	213800	then your binary will be very small and you'll just have your user code.
213800	218740	Now, the user code is obviously of interest from my perspective as a malware researcher
219300	222740	because if I can find that in other binaries,
222740	226980	it will tell me that this is likely a shared piece of code
226980	230680	and the user code is generally relatively unique
230680	233760	to the extent that we can say these binaries overlap.
235160	238780	So to recover symbols, we have multiple ways
238780	243200	and three of them are what I'm going to be focusing on.
243200	247480	So you have metadata in your binaries that you might be able to use.
247480	251180	Golang is another great example of this where you have the PCLN tab
251180	255020	and the information within so you can recover the function names.
255040	257660	Obviously, using this is of great benefit.
257660	259860	However, not all binaries have this.
259860	264880	You have a stripped C++ binary, you're going to have a hard time, right?
265080	269980	Now, Ghidra has two features that you can use to recover symbols.
270380	273700	One is specifically made for this, which is function ID.
273700	278420	For those using IDA, that would be flirt signatures, but then Ghidra is equivalent.
278940	282940	And it has a hash of a function and a function name.
282940	286820	So it hashes your function, cutting some corners here,
286820	288880	but the gist of it is that you have a hash
288880	291180	and if the hash is found in another place,
291180	295860	you can compare the hash and take the name from the mapping that you already have.
296160	299380	And then there's bsim, which stands for behavioral similarity.
299640	305980	This was recently released by the NSA, recently being the day before last Christmas.
306440	310720	And what you can do is you have the decompiler,
310720	312980	produces intermediate language called p-code,
312980	317220	and based on the high p-code vectors and some other things are calculated,
317940	321680	based on which you can find functions that overlap code.
321960	329040	Within Ghidra, you have the advantage that you have the decompiler of your match
329040	331700	and of your original function side by side.
331700	336660	So say you're patch diffing a file, you can very nicely see the differences.
337940	342260	In order to do that, you do need to retain access to the Ghidra project
342260	345640	and the files therein which you're using,
345640	349620	or where the match is found as well as your program that you're analyzing.
350780	356940	The script I have is kind of shoehorning that where we do not use the last part,
356940	362220	meaning we have less data required and we can also automate the process
362220	366100	because in the end it's code that you can use for this.
366720	374060	So for the function ID databases, I wrote a script that you can automate.
374100	378760	First the ingestion, you can run Ghidra headless to just import your files,
378760	382840	and then this script will make for each unique architecture
382840	385520	and what Ghidra calls a language ID,
385520	389740	it will make a different function ID database that you can use later on.
389740	395340	A language ID, very briefly put, is a PE file, 32 bits,
395340	400060	for Windows for a specific compiler, and then we do that for each.
400160	403660	This allows you to have the files that you can share with colleagues,
403660	410100	either internally or externally, and this is the quickest way of recovering the symbols.
412120	417380	As a case study, I have a Qubit Stealer sample which is Golang-based,
417380	421120	and it has a lot of functions, but it has no real names,
421120	423640	and we'll get more statistics on this in a bit.
423640	426940	But just to showcase function ID, the first top screenshot
426940	431940	is a screenshot that is post-default analysis from Ghidra.
431960	437940	Right, so it just has fun underscore, which is the equivalent in IDA of saying sub underscore.
438180	441140	So these are just functions that have no names.
441220	446260	Now in the bottom screenshot, we can see that the P256 sub-internal function has been recognized
446260	450340	and subsequently been renamed, which saves you time as the analyst, right?
450340	453420	This is part of your default analysis,
453420	457180	because the function ID is an analyzer which runs within.
457480	462160	For the sake here, I first ran it without the function ID library
462160	465400	which contains these functions, and then the one after.
467400	470380	So for bsim, I already mentioned it's a bit shoehorned,
470380	472340	because we're not going to be using the GUI to do this,
472340	476980	and we're also not relying on the fact that you need access to the Ghidra project.
476980	481280	Meaning that if I make a bsim database and I give it to you,
481280	484560	then you can still use those databases with this script,
484560	488020	even though you do not have access to the Ghidra project
488020	491460	and the files therein, which I used to create it.
491660	496800	So these are, along with function ID databases and some more information,
496800	499640	all available on GitHub afterwards.
500420	503700	So it's not necessarily using bsim as intended,
503700	509680	but since the screenshot on top is relatively readable, I think, on screen,
509680	512920	you can see that it has a function name,
512920	515160	which is within the fdesc, right?
515160	517860	Function description XML object.
517980	520300	And within there, you can see that there are some vectors
520300	522720	and calls being made to other functions,
522720	526200	which is what bsim uses to identify this.
526740	528540	The fact that you can automate this
529100	532340	doesn't mean it's fully magically working for you.
532340	537720	You still need to specify what your threshold is for the similarity,
537720	540760	which is a value between zero, meaning no overlap,
540760	542860	and one being an exact match.
542940	546240	You also need to specify how many results do you want
546240	553660	from the bsim database to match or to be collected for use, right?
553660	557020	So you can do one, meaning if you have the first match it finds,
557020	558540	it will say, well, it's this.
558820	561560	But especially if you're not relying on an exact match,
561560	566820	but rather, say, 75% or more of overlap,
566820	570500	you might be interested in what other functions might be matching as well.
570500	573580	Now, obviously, if all those functions have the exact same name,
573580	577820	it's pretty clear that based on the database that you use,
577820	579300	there's only one match.
579760	582920	But you can also have the case that you have multiple functions,
582920	585420	different names that match,
585420	588160	which requires you to do a bit more research.
590420	594660	To automate this in the script, you have identical matches.
594740	597060	So these are renamed as is, right?
597060	599540	Identical meaning there's only one name matched,
599540	601500	even if you have multiple functions.
602680	605760	The information, the metadata that's still available in the signatures
605760	609360	is added to that function within Kedra.
609620	612340	And it will show you the original file,
612340	616240	the similarity for the match based on the function you're looking at.
616240	621220	The significance, which tends to be based on your function size.
621220	623100	And the architecture, right?
623100	626120	It should only match the architecture that you have,
626780	628720	though bsim has specifics.
628720	630980	So since it's based on a decompiler,
630980	634140	you can make databases that are known specifically
634140	637160	for the bitness of your architecture.
637160	639840	So say you have a Intel-based binary,
640620	643820	132-bit version and 164-bit version.
643820	646680	The size of an integer, for example, is different, right?
646680	648340	It's four versus eight bytes.
648840	651660	What you can tell bsim to do when making the
651660	653680	or importing the signatures in a database
653680	656840	is you can give it a no-size database,
656840	659200	meaning that it will ignore certain values
659760	664560	and allow you to find matches of code from a 64-bit binary
664560	667360	in a 32-bit binary or vice versa.
668820	670840	So the information here will allow you
670840	675340	to more accurately decide if this matches.
675400	677620	And given the original file name, right?
677620	681800	This says empty project go 1.9, for example,
681800	684080	and you can see 1.14, 1.13.
684080	686300	So these are the go versions where this function
686300	688760	was also found in when making signatures.
688820	690020	So this gives you an indication
690020	693400	as to which Golang version it might be.
693400	697340	And it will also tell you it's a Golang function on its own.
697360	699660	Obviously, the original file name
699660	702040	is based on what you use as a file name
702040	704200	in Ghidra when making the signatures.
704220	706840	So if you give it useful names, that's great.
706840	710580	If not, then it might not be as great.
712020	715140	Having said that, there's also a generic match, right?
715140	716540	Because we want to automate this.
716540	718660	You can have functions with thousands of,
719280	722120	well, unknown functions within your binary.
722400	724780	So there's also what I call a generic match,
724780	727320	meaning there is more than one unique name
728100	730800	that has been returned from the database.
730920	736220	Now, not renaming these at all will not help you, right?
736220	737620	Because you can make these databases
737620	739480	for very specific purposes.
740100	743880	Say, right, this is my database for the Golang runtime.
743880	749760	Then any match that I have based on my similarity threshold
750300	752640	will be Golang runtime related,
752640	756280	or at least be very likely to be related to it.
756380	759180	So in this case, we have two functions
759180	762140	and we can see that the package name within Go
763540	765340	has changed over time.
765340	767880	Yet the function name itself is the same.
767900	769600	Now, obviously, we could add a lot more logic
769600	772260	as to how do we identify this between strings.
772380	775340	But that is a future work.
775400	777840	But also kind of depends on what you're using, right?
777840	780640	If it's multiple versions of the same runtime, that works.
780640	782620	But maybe you have a database where you put in
782620	784860	not only Golang, but a lot more.
784860	787180	It gets a lot harder to do the differentiation.
788200	792340	So instead, I opted to rename all those functions
792340	795000	that match generically with a prefix.
795000	796260	And you can provide this prefix
796260	798100	when running the script headless
798100	800580	so that any match is then renamed.
800840	803480	And if you run it over multiple databases, right?
803480	805960	So the script, unless you change it,
805960	808200	uses only one database at a time.
808200	812840	What you can do is simply provide the prefix.
812840	816820	And any function that has the non-default function naming scheme
816820	820360	is not included in subsequent runs, right?
820360	822700	So if a function starts with fun underscore
822700	824940	or thunk underscore fun underscore,
825340	827500	it's included in the search.
827500	830800	However, if you have this function right now,
830800	833100	it's Golang underscore whatever.
833100	839480	If I now run it on another database I have for bsim,
839480	842360	then this function is not going to be included anymore.
842360	844840	So your subsequent runs will A, be faster,
844840	847720	but also likely more accurate
847720	849500	depending on the order you run it
849500	851600	and how you structure your databases.
853040	855220	So to continue with our case study,
855220	859660	we have the top screenshot, which is after function ID, right?
859660	861800	And I would recommend you first run function ID
861800	862760	because it's faster
862760	866700	and it is designed literally for this purpose.
867440	871520	And bsim is a bit misused for this purpose, yet it works.
872120	873780	And then we see in the bottom one,
873780	875720	we see that the different functions
875720	878940	are now renamed to Golang underscore, right?
878940	881300	So these are all generic matches.
882420	884240	Now looking at this function,
884240	886500	and it's a bit of a spoiler alert,
886500	889080	it is just a function within the Golang runtime
889080	890520	that I used as an example.
890520	893480	But you would also guess that it is,
893480	898060	given that there's only calls to the Golang runtime within.
900080	902540	So aside from making these scripts,
902540	905280	I decided that making this invoke
905280	907260	would A, be a good test case,
907260	909840	but it would also save CPU cycles for others
909840	911980	as soon as we share this, right?
911980	913900	So I downloaded hundreds of gigabytes
913900	917100	of benign binaries,
917100	919820	mostly runtimes and libraries,
919820	921960	compiled them if need be.
922160	926100	And I ended up with 117 gigabytes
926620	928750	of signatures for bsim.
930900	933740	And the data doesn't necessarily say much,
933740	935900	but there's not a lot of exact details
935900	937460	I have for the bsim part.
937600	939800	I do, however, for function ID,
939800	942680	because you get a lot of detailed information in return.
942700	947420	So 60% is Golang based as to what I created.
947420	950120	And then we have 35 related to Boost, right?
950120	953480	The C++ library, which contains a lot
953480	954880	and it's used a lot.
954880	956660	And then there's, well, other.
956660	957580	Now, what is other?
957580	958760	You might ask the 5%.
959660	961740	So here I have the signature generation
961740	964920	with the unique number of signatures within each.
965080	967920	So for Golang, that was any runtime available
967920	969440	that I had at the time.
969440	973340	For Boost, it's roughly the last 10 years of Boost.
973380	977100	And then we have, say, LibXML, OpenSSL,
977100	978800	other libraries that are used.
978880	980340	Now, depending on how big they are,
980340	981980	you have more or less functions.
982220	984540	But overall, this is, well, I would say
984700	985740	a decent amount of functions
985740	987480	that now you don't have to reverse
987480	990260	or make signatures for anymore.
991160	992880	So to put that in a broader perspective
992880	994760	for the Qubits dealer,
994760	997100	when you do the default Gitter analysis,
997100	999480	it will find 2016 functions,
999480	1001900	out of which one has a name, right?
1001900	1003080	The entry function.
1003740	1005260	If we then run function ID,
1005260	1006700	one function is merged, right?
1006700	1008860	The total goes down one,
1008860	1012180	but we recover about 40% of the function names.
1012180	1017100	If we then use bsim with a 75%, right?
1017100	1020380	0.75 threshold for similarity,
1020380	1025140	we recover another 16%, leaving us at 58.
1025200	1027840	Now, that doesn't necessarily sound too much.
1027840	1029140	So why is that?
1029140	1032680	Now, we had access to the Qubit dealer source code,
1033420	1036380	and it imports a library.
1036380	1037280	And there's no signatures
1037280	1040380	for Golang libraries in this corpus.
1040380	1041560	So if you make this,
1041560	1044920	you will have even more matches.
1046000	1047760	So as a methodology,
1049320	1052040	I would suggest you use,
1052040	1054400	feedback is obviously welcome,
1054400	1055380	that you identify metadata
1055380	1057600	within the binary if you can.
1057620	1060800	You can utilize that if you find it.
1060800	1062660	You want to identify the runtimes
1062660	1064680	and libraries used within your binary.
1064680	1066960	You might need to make signatures for them
1066960	1068820	if you don't have them yet, right?
1068820	1070240	Maybe you identified something.
1070240	1071980	So you first make the signatures,
1071980	1072960	you apply them,
1072960	1074780	and you do that process both for function ID
1074780	1076580	and for the behavior signatures.
1076980	1077900	Once that's done,
1077900	1079840	you can have any other further logic
1079840	1081320	that you can use, right?
1081320	1082780	A lot of people are using LLMs
1082780	1084180	to help rename functions
1084180	1085900	without looking at them necessarily.
1085900	1087900	So you can apply more scripts.
1088080	1089520	And you can all do this
1089520	1092040	from importing a binary,
1092040	1093660	running function ID, bsim,
1093660	1094600	and your other logic
1094600	1098120	within the headless execution.
1098120	1099780	So essentially, you can do that at night.
1099780	1100440	When you wake up,
1100440	1103320	everything is ready for you to analyze.
1105080	1107220	That leaves room for questions.
1107360	1109340	There's two minutes left.
1109360	1110940	Either ask them now
1111480	1113460	or find me off stage.
1113600	1115340	The GitHub link here,
1115340	1117600	I will activate them after the talk,
1117600	1119080	has the GitHub scripts
1119660	1121840	for this talk that I created.
1121840	1123640	It has function ID databases
1123640	1126220	and it has the bsim databases.
1126220	1128280	Additionally, there is about 66 gigabytes
1128280	1130490	of Golang binaries.
1130840	1132180	It's an empty main function
1132180	1133660	and contains the entire runtime
1133660	1135020	of that version.
1135240	1136880	And if you want to make signatures,
1136880	1138120	play around with them,
1138120	1140800	then feel very much free to as well.
1140820	1143940	So the URL is at the top
1143940	1145300	and then you have the slash
1145300	1146500	for each of the sub options
1146500	1148970	or just go there and find the repositories.
1149900	1151280	Thank you very much.
1173310	1176310	You have a microphone in the front.
1184380	1185960	All right, if you just shout,
1185960	1187300	I'll repeat the question.
1192280	1193680	So the question is,
1193680	1196780	how good is bsim when matching functions
1196780	1199140	compiled with different compilers?
1200140	1202300	I haven't tested this myself.
1202300	1207400	It's a relatively new feature within Kedra.
1207400	1210000	I think it does relatively well
1210000	1212360	because it uses the decompiler.
1212360	1215320	Some of the abstractions are moved away.
1215900	1216740	And I think it also depends
1216740	1218580	if Kedra knows the used
1219160	1221260	calling conventions and such.
1225310	1227670	Hi, so in a similar vein,
1227670	1228710	how do you deal with
1228710	1231270	different compiler options?
1231270	1233590	Things like f-function sections,
1233590	1236310	all the moving symbols around.
1236310	1239230	What of those kind of break this process?
1241550	1243290	Yes and no.
1244570	1246770	It's the standard engineering answer, right?
1246770	1247870	It depends.
1248090	1249850	So if you say,
1249850	1252630	use an unoptimized compiler run
1252630	1254230	versus a very optimized,
1254230	1256250	you will have differences in code,
1256250	1257170	meaning there's differences
1257170	1259050	within the signatures as well.
1259050	1260570	For bsim, it might break it
1260570	1262750	because it does match certain things.
1262750	1266150	But if the match isn't matching well enough,
1266150	1268630	it will not find a hit.
1269030	1270450	For bsim, you can tweak
1270450	1272530	how much it should look alike.
1272530	1274330	If you are aware
1274330	1275830	that something specific is used
1275830	1277130	and you have a database for this,
1277130	1278290	it might help.
1279590	1281390	But if it changed too much,
1281390	1283490	then you need to lower your threshold too much
1283490	1285630	and you will probably get too many hits
1285630	1287770	to make sense of it.
1287810	1290330	So I think the best option
1290330	1293290	is to compile the target libraries
1293290	1294490	in all options
1294490	1296330	and then make signatures for all,
1296330	1298270	which is obviously a lot more work.
1298730	1302210	But I think that is the most accurate option
1302210	1303290	that you have.
1303710	1304710	Thank you.
1308270	1308970	All right.
1308970	1310030	Thank you very much.
