start	end	text
1560	7040	Welcome, welcome, welcome to DEF CON. My name is Dr. Bramall
7040	10340	Brizendine. I am the director of the Verona Lab, which deals
10340	14300	with vulnerability research. Creator of the Shell WOS, which
14300	18200	allows us to utilize Windows syscalls in shellcode. I've
18200	20920	created the JopRocket and RopRocket, both of which deal
20920	23940	with return oriented programming and jump oriented programming.
23940	27720	And I've also created an NSA funded research project called
27720	31520	Sherem, which allows us to analyze, uh, shellcode. Uh, I've
31520	34900	spoken at many different conferences in the past. Um, and
34900	39880	I'm also an assistant professor of cyber security. I have a PhD
39880	45120	in cyber operations, which is a highly technical degree. And all
45120	48700	of those, uh, previous projects have, um, previously been
48700	54120	presented at DEF CON, so check them out. My co-speaker is, uh,
54120	59140	Shiva Shashank Kusuma. He is a research assistant and, uh, he
59140	62000	has a master's in computer science and is pursuing a
62000	65520	master's in cyber security. Uh, he's looking for internship
65520	70420	opportunities for the summer as well. So, our agenda. What are
70420	74300	we going to focus on? Well, firstly, we'll talk briefly
74300	78900	about a general approach to shellcodeless ROP. How do we
78900	81720	create unique patterns to facilitate process injection?
81720	85320	Some generic ROP techniques for process injection. And then we
85320	90240	will look at a sample process injection technique that has a,
90240	95020	uh, highly novel solution to a rather significant problem. We'll
95020	100360	have a demo and then we'll have another brief, uh, uh, process
100360	104760	injection and then we'll, we will conclude. So, some, uh,
104760	108360	preliminary words on our experiment. Now this, when we,
108360	111380	we conducted this research, we did it on a contrived binary
111380	115000	uh, one that is intentionally vulnerable and is filled with
115000	117780	convenient gadgets because we want our work to be applicable
117780	122160	not just to one particular vulnerable binary but to
122160	125380	potentially, uh, any binary. So we want to discover everything
125380	128420	that is possible. And this research is really focused on,
129080	133280	uh, ROP from the standpoint of you compromise a binary. It's
133280	135880	not something where we're trying to integrate ROP and some kind
135880	141040	of custom malware or some kind of custom red team software. So
141040	144300	let's introduce the idea of shellcode with ROP. What does
144300	148020	it mean exactly? Well, it's this idea of instead of, uh, bypassing
148020	151980	data execution prevention depth, we are instead going to invoke
151980	156700	the, uh, functionality of a shellcode directly via ROP
156700	161880	gadgets. And it's something that has very seldom been done in the
161880	168240	Windows ecosystem. When you see it, it's almost always 2, 3 APIs
168240	171340	chained together. But you'll see, uh, later today that we
171340	176900	have, uh, vastly, uh, more APIs to achieve what we want to do.
177300	180280	Some also, uh, some previous work on shellcode with ROP.
180280	183300	There's a video link in the slides so I do encourage you to,
183300	187500	uh, check it out if you're interested. So in terms of
187500	191220	approaching shellcode with ROP, there are a couple approaches
191220	195200	we could take. One is utilizing push add where we're going to
195200	199680	populate values on, uh, certain registers, uh, with certain
199680	204300	predefined values. Um, and then that causes them to be loaded
204300	207800	onto the stack in a, a certain predefined order. And we'll also
207800	211120	use move dereference or the sniper technique and this allows
211120	214160	us to precisely write our values to the stack and we can then
214160	218880	increment or decrement to the next, uh, slot that we want to
218880	223280	provide values for. So those are both critically, uh, important
223280	226640	for us. And I want to re-emphasize the importance of
226640	229200	this idea of finding patterns because again, we're not really
229200	232160	just approaching one process injection technique, but we want
232160	234960	our work to be applicable broadly to many different
234960	238380	techniques. How do we do this in terms of return oriented
238380	242000	programming? So we really want to emphasize that, you know, we
242000	245360	have these particular slots, uh, where we have, uh, registers
245360	249900	going in a certain order. Uh, so we really, uh, weaponize this in
249900	254160	terms of developing new patterns for different APIs, uh, both
254160	257940	Windows APIs as well as native APIs to allow us to invoke those
257940	263320	easily using return oriented programming. So, uh, and when
263320	267080	using many different APIs, we want to be able to chain them
267080	270640	together. Lar- kind of like the sense where you have a, uh, a
270640	272800	ROP chain, you have different, uh, ROP gadgets chained
272800	275260	together, we'll be chaining different APIs together. So you
275260	279360	may get one, uh, return value, uh, in this case, uh, open
279360	283040	process runtime address is provided to us in EAX. We can
283040	286480	use that subsequently in a pattern for open process
286480	292500	because, of course, we need to call open process. So a large
292500	295500	part of our research is really focused on generating a lot of
295500	297600	these different types of patterns. And actually we've
297720	302820	de- developed more than 150 unique patterns to provide a lot
302820	306740	of opportunities. Um, we'll talk more in depth about some of them
306740	309740	and we'll give some examples. But historically, uh, some of
309740	312200	the tools that have done this have been things like, uh,
312200	316100	Mona, Mona, you know, Virtual Alloc, Virtual Protect, and you
316100	318620	have a couple patterns. So it's not a significant number of
318620	323300	patterns. So let's talk now briefly about some generic ROP
323300	327100	techniques that we can use to help us with, uh, process
327100	332920	injection. So one is the idea of using function output, using
332920	337280	that function output as a way to, um, to be a parameter. So
337280	340900	Gitproc address and little library often go hand in hand
340900	344520	and they're very familiar and well known. Uh, so with a little
344520	349960	library, we can get a handle to a module, which is the, the base
349960	353780	address of a certain DLL, and that's required for Gitproc
353780	357460	address. So we take the output from the one, which is going to
357460	362460	be returned to us in EAX, and then we use that subsequently in,
362460	366060	uh, Gitproc address. And we'll do this time and time again, so
366060	369220	return values as well as other out parameters if you're dealing
369220	374020	with native APIs. And then this gets a little bit more complex,
374020	377840	so structures is parameters. Can we do that with return oriented
377840	381580	programming? Yeah, we actually can. So with a structure, we
381580	384460	need to look clearly at the, the definition of the structure, and
384460	387200	then we need to build that byte by byte through return oriented
387200	390140	programming. And we'll typically do that with a series of move
390140	393420	dereferences, and we can just increment and decrement to the,
393420	398280	to the next slot as we slowly build our, uh, structure. And in
398280	401180	some cases we may actually have nested structures, so take a
401180	404960	look at adjust token privileges. We have several nested
404960	408940	structures, and it needs to be done exactly correctly, or else
408940	416000	it simply will just not work. Um, one thing that is also, uh,
416000	419440	immensely important is this idea of storing important values,
419940	426340	um, addresses, handles, things which may be obtained by calling
426480	429060	a Windows API. If we call a Windows API to obtain one of
429060	432200	these values, well that takes a lot of effort. And if we needed
432200	434880	it again, well of course we don't want to call it a second
434880	439760	time, so we will make an effort to save that value somewhere in
439760	442860	memory, and then we can retrieve it later. So we might have a
442860	446680	common reference point, we move ESP to a location, maybe we
446680	449760	advance forward 500 bytes, and we just start writing different
449760	453260	values there. And that common, uh, reference point can, will
453260	456260	change from gadget to gadget, but we can manually calculate
456260	460080	that and make adjustments, uh, as need be. So, again, you'll
460080	462940	have lots of important values and you can save them and then
462940	468020	retrieve them subsequently, uh, later on. So another thing we
468020	471200	did is, and this is actually, uh, completely novel, combining
471200	475260	move dereference and push add in terms of building a pattern. So,
475260	479460	we can use that to create a, a, a ROP chain pattern. Uh, push
479460	483840	add, you can have a maximum of 4 parameters, 5 if you use ESP,
483840	488120	but that can be rather, uh, challenging to do. Um, so we can
488120	490620	do push add and then do the remainder, we can supply those
490620	495680	via move dereference. Now, if you have the option to, uh, place
495680	498720	your strings and data in the payload directly, then by all
498720	502540	means do it, because it's going to save you effort. Again, if
502540	504740	you have a common reference point, you can use these, you
504740	507880	have a pointer to a string and you might be saying, well, what
507880	511200	about EDR? Well, yes, EDR will be an issue at times and so this
511200	514420	may not always be plausible. If you have no payload size
514420	518200	restrictions, one option is to obfuscate some of this and then
518200	523240	you could use a ROP to go and de-obfuscate it as need be. So,
523240	526220	let's get into an actual, uh, process injection technique. Now
526220	528980	this is based on something that I actually did at Hack in the
528980	533100	Box Amsterdam 2023. That was all Windows sys calls and shell
533100	535860	code. This obviously is not, it's return oriented
535860	538680	programming. And so these are more, uh, these are Windows
538680	542480	APIs and native APIs, it's not necessarily strikingly
542480	546980	original. Um, but in any case, we have a new URL, we're going
546980	550420	to create that, it's going to slither up inside of our victim
550420	553160	target and external process and then we're going to write our
553160	557320	shell code there and then cause it to launch. So, in terms of
557320	561260	ROP, the actual complete chain of gadgets that we have, 29
561260	565740	gadgets. It's an extraordinary amount. Uh, I'm not familiar
565740	569640	with any, uh, ROP chain in, in the Windows ecosystem that has
569640	572980	anywhere near this level of gadgets. And so it's a lot of
572980	577640	work to, to maintain these from, um, from API, API call to API
577640	580800	call. And it would be a lot more actually if we did not use that
580800	583960	technique of storing and retrieving certain values that
583960	588100	we need. So, let's talk about load library. We can use that
588100	592080	with, uh, push add in a pattern. So we load the particular values
592080	596080	into, uh, registers and then we go and do push add. Then that
596080	600820	causes it to go in a predefined, uh, location. And so let's see
600820	604540	what happens exactly. So we, we have our stack values there and
604540	607680	those correspond to our pattern. So we have red 8, that means
607680	610660	we're going to do this ROP NOP. We're going to skip 8 bytes, so
610660	614760	we skip that, we do a jump EDP dereference. We therefore invoke
614760	618120	load library, we have a return address, we have a parameter,
618120	622400	everything checks out. And then we get a result. Exact result
622400	628240	that we want. So we get the base address of NTDLL. And then we can
628240	630660	use that subsequently for git proc address, which, which is
630660	633260	our next one. And we'll do the same thing, we'll populate
633260	635920	different values into the registers that we can then call
635920	639420	push add. And we get the runtime address, in this case we're
639420	644600	doing create tool help 32 snapshot. An extremely important
644600	647780	thing that we'll talk about momentarily. So these are the
647780	651060	patterns for load library, we have a lot of them, 9. And git
651060	654380	proc address, we have 11. So that gives us a lot of
654380	656860	flexibilities because if you're trying to do something, you
656860	660160	don't have a particular gadget, well you just give up. If you do
660160	663920	then the chain is incomplete. But having multiple patterns, we
663920	667120	can drill down deeper and maybe find something that actually
667120	672300	works. In any case, our next API is create tool help 32 snapshot,
672300	674380	which will allow us to take a snapshot of all the active
674380	677820	processes that are out there. And it's immensely important, we
677820	681220	need it to discover uh the different PIDs, we're gonna
681220	686280	target one of those. So there are actually 11 patterns, so a
686280	688520	lot of flexibility there in terms of how you can invoke it
688520	692860	with the push add instruction. Um there's an example of the ROP
692860	696280	and we got the push add at the bottom. Virtual alloc, um it's
696280	698900	one way to allocate memory, yes some EDR can block it, there are
698900	702620	other ways you could do it. Uh the pattern for that is very uh
702620	705220	familiar, uh one of the most familiar goes back probably
705220	709980	about 15 years. Um but with this we're gonna allocate a region of
709980	713220	memory, we're gonna use that for a couple very uh important uh
713220	717940	things. One of which is to build the process entry 32, the process
717940	722820	entry 32 is where we're going to go in and check our PID, we're
722820	727960	gonna check our uh string. So the first thing is we process 32
727960	730020	first, it's gonna kick everything off, it's gonna
730020	736580	initiate the whole uh process. So how we're gonna do that, uh
736580	739240	first we actually need to build the structure, so we can go ahead
739240	742500	and build the structure via ROP and our move dereferences. Most
742500	744880	of it are null bytes so it's relatively simple, we have a DW
744880	748540	size, uh it's an arbitrary value so you can make it whatever you
748540	751900	want. And then once we're done we get a pointer to that, we
751900	755500	supply that pointer as the parameter. So all of that work
755500	758720	and we just get a pointer to that. So again the idea of being
758720	762140	able to uh dynamically locate something in memory and use that
762140	765860	is kind of like a pointer, can be very useful. And then the
765860	769280	patterns for process 32 first and process 32 next are actually
769280	773660	interchangeable. So one of the biggest challenges is this idea
773660	775920	of string comparison, how do we do that with ROP? Well the
775920	780400	gadgets are not really very common so that can be very uh
780400	785160	problematic. We need to identify a specific process otherwise you
785160	790040	really can't proceed forward. So you're kind of at a, an uh, you
790040	792380	can't really go forward without this. So we came up with a
792380	795080	solution, we're going to build our own custom function in
795080	798040	memory. It's an enumerate process function and we're
798040	802100	actually writing the raw assembly. So remember we made a
802100	805060	writable memory with virtual alloc, we can write our code
805060	807740	there. You can do it other ways as well. And so this will cycle
807740	810820	through process 32 next, it can do it hundreds of times in a
810820	816560	loop until it finds the exact uh string, VU player. We can supply
816560	819820	that and then once it does that it'll go ahead and exit out. So
819820	822780	how do we write that? Well we simply just compile it, we can
822780	825860	then write our bytes to memory using the move dereference
826220	829020	technique one by one until they're all written. Make sure
829020	831300	the code is executable and there are multiple ways in which you
831300	834260	can do that. And then we need to supply the appropriate values so
834260	838600	things like our HP snapshot, our VU player string, uh
838600	843040	dereference process 32 address. And then how do we kick it off?
843040	845920	Uh again multiple ways but in this case we're gonna do a
845920	848480	simple push ret which will then push the address onto the stack
848480	851840	and then return to that and then we can uh immediately start
851840	856880	executing our custom code that we just wrote there. And so
856880	860340	that'll invoke process 32 uh repeatedly until it finds our
860340	865140	particular PID. In this case we have VU player. It's gonna look
865140	869040	at offset 24 and once it finds offset 24 then again it's gonna
869040	872980	break out of that loop. And um from there the base of the
872980	876340	structure we can just go to simply offset 8. Offset 8 will
876340	881020	then hold the PID. We can capture that PID. In this case
881020	885400	it's 668. And how do we do that via return oriented programming?
885400	888260	Well we just basically take exactly what I said, translate
888260	891860	it to ROP. So some move dereferences, some POPs, uh
891860	895940	sequence of 8 uh increments. And uh in this case we have a
895940	901420	different PID B98 and uh we can supply that. Uh we can save that
901420	904940	to memory so we can use that later. Remember the idea of save
904940	909240	and then retrieve. And then that brings us to our next API and
909240	916640	with that we're gonna have Shiva uh take the podium.
916640	919700	Here we take this PID that we just obtained after all of that
919700	922780	hard work and uh we are going to convert that into the handle
922780	926440	to the process. We're gonna have this. It's impossible to move
926440	930700	forward if you don't have a PID to your target process. And we
930700	933260	don't want to just randomly inject into some other unknown
933260	936780	process. Uh so this one we are going to use both push add as
936780	940380	well as move dereference technique. So here we have
940380	944020	multiple patterns for open process and then we'll follow
944020	947900	that up after push add. And we also have a move dereference
947900	953460	with the process ID. In this ROP chain the process ID has been
953460	956900	provided by move dereference uh with everything else being done
956900	962340	via push add. Next we'll open a handle to our current process
962340	966580	with open process token. And then we'll use that so right
966580	970160	here we have a handle and we are using ROP to create a pointer
970580	975740	to hold the access token that we'll retrieve. With adjust
975740	979700	token privileges uh we are going to take that token handle and we
979700	982760	can then change the privileges as we need. We need to do this
982760	987760	to avoid some problems that could happen otherwise. And this
987760	990640	requires multiple complex structures such as token
990640	994680	privileges. Such as uh where we can specify the AC privilege
994680	1000740	enabled and AC debug privilege. With adjust token privileges uh
1000740	1003940	here we have a ROP. So we are setting disable all privileges
1003940	1008120	to false. And what that means is that whatever we specify inside
1008120	1013260	the new state structure will be then done. With adjust token
1013260	1016200	privileges we can then go inspect where the debugger and
1016200	1021120	see if it has all of our values. Like AC privilege enabled and AC
1021120	1024540	debug privilege. And then everything is going according to
1024540	1030320	the plan. Our goal with this is to take a common DLL URL mon,
1030320	1033840	create a file for it and then go ahead and map that out. And
1033840	1037220	then we can inject our payload into that. Then causing that to
1037220	1041080	be executed. And we'll do this with these APIs which we'll be
1041080	1046260	talking about. So create file A is the first. We get a handle to
1046260	1049860	the URL mon which is our file. We are going to be using this
1049860	1053280	later on. Uh but we are going to do this with sequence of moody
1053280	1058020	references. And here we are providing the absolute address,
1058020	1062060	the string pointer to that and also generic read. We are doing
1062060	1067460	generic read to avoid some possible detections. And then
1067460	1071780	NT create section, a lower level native API will allow us to then
1071780	1076140	create a section object for URL mon DLL. And here we are doing
1076140	1080840	page read only. And then with drop we are specifying the sec
1080840	1086780	image. NT map your section. Now that we have the URL mon section
1086780	1090500	handle, we can now map that view of the section into address
1090500	1093880	space of view player. We'll do this purely through moody
1093880	1098140	references because uh we have many nulls and also we have page
1098140	1102820	read only. So it's a bit more limited. One more important
1102820	1105380	thing is providing the base address or pointer to the base
1105380	1109480	address. And that will receive a base address inside of view
1109480	1113920	player. So that will be provided to us as you can see in this
1113920	1117960	example which we'll be talking about later. So it is very
1117960	1123980	important. Virtual protect EX will then allow us to change the
1123980	1128820	target DLL URL mon to RWX. And so we are going to do this with
1128820	1132440	sequence of moody references. Because remember we have limited
1132440	1136740	permissions for it. And so we'll get the handle to the view
1136740	1139860	player as well as the base address that we just retrieved.
1139940	1142780	And then once it's done we'll have the page execute read
1142780	1147240	right. Here is a route where we can provide the pointer to the
1147240	1150360	base address. We then go and dereference that and we'll pull
1150360	1153400	the base address out of that. And then we do the moody
1153400	1158020	reference. Right process memory allows us to inject the cell
1158020	1161900	code into the process memory of view player. We are doing this
1161900	1166140	via push add as well as moody references. Which you can see
1166140	1169360	right here so pointing to the cell code up there. And also
1169360	1174100	providing the size. Same thing here we are, we have the cell
1174100	1176620	code and the size and then the remaining parameters with push
1176620	1181220	add. Right process memory at this point once it's done then
1181220	1185260	our cell code will be injected into inside of urlmon which is
1185260	1189060	an external process. So we have gone from one process to a
1189060	1191740	completely different process. And the cell code is just
1191740	1196240	sitting there waiting for us. So how do we cause it to execute?
1196240	1199360	The answer is very simple. It's create remote thread. But the
1199360	1202460	actual technique is not so simple. So we'll have a moody
1202460	1205640	reference and also push add pattern. And then we'll have a
1205640	1210160	sequence of moody references. We'll go inside the other space
1210160	1217240	of view player and then we do the push add. So here is some of
1217240	1220200	our ROP. We can provide the LP parameter as well as the
1220200	1223920	address. And then we follow that up after push add. And finally
1223920	1228040	with create remote thread our injected cell code will execute.
1228100	1234700	We have achieved our goal. Let's do a demo. We start the things
1234700	1238140	with create help 32 snapshot and we have been given a handle.
1238840	1243420	Let's write the process function to memory byte by byte. We
1243420	1247480	invoke the function using push thread. Now we are inside the
1247480	1251440	function, special function that we created. And let's take the
1251440	1255600	process entry. Let's see what we have as the first entry. We
1255600	1260020	have system. Let's move forward until we find our desired PID.
1260020	1265800	And we finally found the PID for view player. That's good. We'll
1265800	1271460	keep moving forward. And we'll do the create file A. We'll get a
1271460	1275300	handle from create file A. Let's verify what we have. We have the
1275300	1279420	URL month. That's good. Let's map that out using NT map section
1279420	1281800	and let's set the base address. Right now we don't have any base
1281800	1285680	address. But right after the function execution we'll get the
1285680	1291860	base address and base address has been provided. We'll change
1291860	1296320	the permissions for the base address. And we'll see. We have
1296320	1302970	the page execute read write permissions. We have the MZ.
1302970	1305390	It's a PE file and let's inject the shellcode using write
1305390	1311370	process memory. And let's see what we have. We have successfully
1311370	1315750	injected the shellcode into external process. And finally
1315750	1319690	create remote thread. We got it.
1324650	1328190	Thank you Shiva. So we're going to wrap things up very briefly
1328190	1335290	here. So I just want to say our goal is not to enumerate all
1335290	1338110	process injection techniques. We've seen talks like that
1338110	1341450	before. But we really want to provide a methodology in terms of
1341450	1344370	how we can do that with ROP and focus on the practicalities. We
1344370	1348390	want to provide a novel concept of building a enumerate
1348390	1351970	processes function purely via ROP which can be extendable to
1351970	1355290	other process injection techniques. And then create a
1355290	1358290	rich set of patterns that we can utilize for that purpose. With
1358290	1361170	that we'd like to thank you and invite you to check out the ROP
1361170	1361850	rocket tool.
