Hi DevCom, how's it going? So thank you guys for joining to this presentation. Contactless Overflow, where we're gonna discuss about code execution issues in NFC readers that are used in point-of-sales and ATMs. So short introduction about myself, I'm a principal security consultant at IOactive, so I'm a guy that most of his time is doing reverse engineering, basically from bare metal framework operating system, whatever that can be reverse engineered. Also I do some hardware hacking as well, code review also a lot for framework operating systems, applications, whatever. So basically I'm a guy that likes to break stuff and learn new things, but also I enjoy to find mitigations and solutions for those issues. So I'm a guy that likes to find mitigations and solutions for those findings because sometimes it's quite challenging as well. So this is the agenda for the presentation where we're going to have some interaction and then we will describe with technical details some of the code execution issues and then we'll also talk about the ATM scenario more in particular. So one important thing to take into account here is that all the issues that we will see here are in different scenarios or devices. For instance, this typical portable or mobile payment device that you use to pay in restaurants or shops are affected, as we will see later, but also in devices like to pay for the bus or the train station. In ATMs. In ATMs as well, parking machines or even vending machines. So a lot of different scenarios we have these devices. Also for previous research, I wanted to see if there were any research paper or whatever that was possible to compromise the firmware of the device over NFC and I couldn't see anything like that. So at least back then I couldn't find anything like this in this case. So... How do these devices work? I'm going to use as an example this device that you have there on the screen. In this case it's the IDtech Kiosk 3, as you can see here. I started with this device during my research because I have many years of experience working with ATMs and I knew that this device, this reader, is using most of the ATMs in the world, in the most important ATM vendors. So basically we have here this case. This is where we have the NFC antenna and also the NFC chip. And then with this serial cable connects to this board where we have the microcontroller that handles all the parsing of the credit card or debit card information. But also this microcontroller is responsible for the USB communications because this board is connected with this USB cable to a host computer in this, with this device which could be a Windows computer or Linux computer. In this case it would be the ATM computer. So what we really want to compromise is this microcontroller that is where all the UC stuff is happening. And in this case through the contactless interface. So we don't need to be experts in EMV transactions and all the details about the protocol, but we just need to know. At least that between a transaction when we are using the contactless interface, the reader is the one that starts the communication. And then the card in this case will respond back. And then we have this communication back and forth between them. And where we need to focus on is the data that, in this case the credit card, the data that sends to the reader because we want to send something in particular that allows us to get code execution in the framework of the NFC reader. So at the application layer we have what is called APDUs. So basically at the application layer is what the credit card in this case is sending to the reader. And this is based on TLV, so tag length value. So basically we have one byte that indicates the tag, one byte that indicates the length of the packet, and then the value itself. So it's pretty easy to understand the grammar of the code. Of these packets, of these APDUs. There are tools out there where, for instance, this is an APDU for a transaction where you can see like the first byte SF indicates the file control information, then the second byte 6A is the size of the entire packet in this case, and then the 84 byte in this case is the next tag. So that's how more or less it works. But again, like I said, we don't need to dig. We don't need too much in the EMV protocol in this case for EMV transactions. So yeah, if you sniff the communications like with the Proxmox for instance, this is what you get during a transaction where you have the NFC low level protocol on commands, and then you have the APDUs that the communications between the reader and the card at the application layer. Okay. So one of the first things that we need to do is to emulate the card because we want to, you know, attack the reader emulating a card and send something specific. So at first what I used was this device, the ACR122 device, which contains the NXP PM532 chip that allows emulation. So yeah, you connect that reader to your laptop over USB, and then with some Python framework, you can send some commands like start the emulation mode for that reader. And then with Python as well, you can interact with the APDU layer, with the application layer with the reader, and you can send APDUs to the reader and receive the APDUs from the reader as well. In this case, I was using this framework, the RFID IoT, which is very useful for this. But also there's another approach, which is the RFID IoT. In this case, you create your own Android application using host card emulation that allows you to also interact with, emulate a card and interact at the application layer with NFC reader in this case. So before jumping to the issues, let's talk about ID Tech. Like I said before, I started with this reader, the ID Tech Kiosk 3 device, because it was present in every ATM that has contactless capabilities. So ID Tech is a global leader in payment devices, where they make a lot of different devices, not only this Kiosk 3 device. They also make pin pads, touch screens, displays, and other devices for payment devices. And something important to understand here, as we will see later, that all devices from ID Tech are vulnerable to these code execution issues, because they basically use the same code base for the finger. So this particular one device, the Kiosk 3, I bought it in happyATMs.com website, where you can buy parts from ATMs that have been in production. You can also buy this in eBay. So this actually was using an ATM. But also I bought brand new devices from ID Tech as well, to make sure that they were also, with the newest version of finger, they were also vulnerable. Okay. So yeah, as you can see here in this picture, in the ATMs, for instance, the only part that is exposed to the user, obviously, is this case where the NFC antenna is. But also these ID Tech devices are not only in ATMs, they are also in parking machines, vending machines. They have also this device, which is also vulnerable in retail and also hospitality. So basically it's using a lot of different systems. Okay. So let's jump to the issues. So first, when I got this device from that website, happyATMs.com, what I wanted to check if JTAG was enabled. So I was able to, surprisingly, it was enabled, and I was able to extract the finger and gain debugging capabilities with GDB, which is also great, because for the reverse engineering process is really helpful, but also for the exploit development process. Okay. So in the devices that I bought from ID Tech, JTAG was enabled as well, which is not great, but yeah, basically JTAG is enabled in all of these devices. So then once I'm able to extract the finger from the microcontroller of this device, I started to reverse engineer the finger. In this case, it's a bare metal finger, where we don't have an operating system, there is no symbols and no strings, and it has more or less around 1,600 functions, so it's more or less big. So yeah, it's a bit hard to reverse engineer this kind of finger. So my first goal here is locate in the finger, in the code of the finger, where it starts reading the content of the credit card in this case. So there are different approaches here, but what I did basically is that when I was learning more about EMV transactions, I knew that there was something called proximity payment system environment. So basically these bytes, as you can see here, that the reader is sending to the card in the very first communication. So I just needed to find those bytes in the code, and I found it, as you can see here, and then start from here to reverse engineer and understand what the code was doing. Then after some hours, I identified, as you can see here, I changed the name of this function because there were no symbols as receive card, because I found that in this function is the function where it starts reading from the card over the NFC interface. So something important to understand also here is that standard APDUs are up to 255 bytes size. That's the maximum size. So as you can see here, the code was, you know, checking the byte that indicates the length of the packet. So it's only one byte because it's only 255 bytes as maximum. So yeah. I was getting more familiar with the code. But also I was a bit concerned because, you know, with only 255 bytes, you can end up in a situation where you have a stack buffer overflow, for instance. But with that size, 255 as maximum, maybe you can't overflow the other variables in the stack and then overwrite the saved return address in the stack. Of course, you can maybe overwrite some of the other local variables in the stack and achieve code execution in that way. But normally it's harder. You know, I was talks about why I was a bit concerned because of that. But then when I was getting more familiar with the code, I realized that as you can see here in these lines, the framework of the code in the framework was checking if the second byte of the APDU is the byte 82 in x, then it was getting two bytes from the APDU as the size. Meaning. That. Then. then we can have ffff a size which is 65,000 more or less bytes that was a bit I didn't understand at the beginning because as far as I knew the maximum size of an APDU was 255 bytes but then doing some searching about this and I found that there is something called extended APDUs which indeed are up to 65,000 bytes and indeed they use two bytes to indicate the size of that APDU so that was great news because now I can send I can try to use these extended APDUs then I realized in this case that the function that reads the contents of the card it's using to store that APDU in this stack buffer and if we can set a buffer bigger than 138 hex which is 312 bytes if I remember correctly then you would overflow this buffer and then get a stack buffer overflow so I saw this statically reversing it in the firmware but I needed to you know to be able to send an extended APDU and prove that it was possible but the problem was that the reader I was using to emulate a card didn't support extended APDUs by hardware so I couldn't use that and I was you know looking on the internet like how can I emulate a card but also be able to send an extended APDU and the the only solution I saw so back then was that I found in some Google forums that Google some Google pixel phones had support hardware support for extended APDUs and also Google released an Android version with support also for extended APDUs so at least a I found something that allowed me to to send those extended APDUs so this is part of the Android application that they created and here you have the APDU that I'm sending which is this initial bytes are following the protocol but then the other the rest of the packet as you can see are 414141 the typical A's in ASCII you know for the proof of concept typical proof of concept so so yeah as soon as I try the the POC with the reader as I was expecting as you can see here in the output of GDB the stack overflow was working and I was able to have control of the PC register so so in this case of code execution is possible in in this framework with through the NFC interface but also it's important to clarify that in this particular devices from ID tech in this case the kiosk 3 there is no exploit mitigation at all which makes hard easier the the exploitation of this but also there is no secure boot so you can make it persistent which is also better from the attackers perspective and also since we are using the extended APDUs which are can be 65,000 bytes big and then you have plenty of bytes that you can use for your own arm instructions to you know to create your own to inject your own malware in different part of the device so also this stack buffer flow is personally not in another 21 different functions in the same firmware that are reachable on other stages of the transaction but I just needed one but yeah I kept looking and then I found 21 more so let me show you a video of this so let me make this bigger all right so here we have the kiosk 3 device I'm attached with the JTAG interface so we can see the crash in GDB in real time and also you can see the USB cable is connected to this laptop and in the laptop is where the in this case the ID tech SDK the application is running and it's sending the commands through the USB cable to the to the reader to start the transaction and in the right side we have the GDB so first I'm gonna start a transaction here and then I'm gonna use a credit card there is no sound apparently but you should hear a beep there and then we can see the credit card information in the application so it works the transaction and now if I use my proof of concept in this case well in this case there is no sound but you you wouldn't hear a beep because the the feedback will start every single transaction once we get this recorded, right? So there is no sound, but you you would hear a beep because the where it's crashing before trying to make the beep, and then we can see the crashing in GDB. So here we can see that the LR register is with our 4141 bytes, but also if we execute the backtrace command we can see that the return address is 414141 as well. So there's the bug working in this case in the GDB. And like I said before, the other ID tech devices were having the same exact issue because they were using the same code base, at least at that particular layer of the parsing. Okay. So at this point, I was thinking, like, okay, this is pretty clear that the problem here is that this device by hardware and software supports those extended PDUs, but the developers didn't take into account that, yeah, the device can receive more than, in this case, 312 bytes. That was the size of that buffer that was overflowed with the stack buffer overflow. So I was thinking, like, okay, so maybe this issue is present in other vendors. They are having the same problem because it's pretty easy to have that mistake. And indeed, many vendors were having the same issue. So basically what I was doing is using the same proof of concept that I created for this ID tech reader. I was testing it with other vendors. Most of them, they were crashing as well. Some of them were stack buffer overflows. Some other were heap buffer overflows because we were dealing sometimes with bare metal firmware like this one, but also sometimes there were real-time operating systems or Android systems or even Linux systems. So one of the vendors affected is Ingenico. I don't know how to pronounce that in English, but basically it's a very famous vendor in the world for payment devices and payment applications. And I have another video here to show you. So basically, since sometimes it's hard to get access to these devices actually in a production environment where it works and you can perform a transaction and everything, what I did in this case, I have a friend in Spain where he owns a shop and he has these devices. So he allowed me to test it, the proof of concept. So basically, as you can see here, the device was charging me for 39 euros. And then as soon as I tap my phone, you can see reboot in progress. So basically, in this case, it was rebooting because in this case, it's a Linux system. So the issue is in a particular library. And so the operating system was detecting the crash and rebooting. But my proof of concept was just crashing it. It wasn't weaponized in this case. So yeah. And I was doing this with another device and another and another reader and all of them were crashing. Like... Every device was crashing like this. Okay. So... Verifone as well. Several Verifone devices affected. I was testing them and crashing. Sorry, I forgot to mention that my approach was also like once I found that the device was crashing, then I was trying to buy one and get debug capabilities or log access so I can see if it's a stack buffer overflow or heap buffer overflow. It was similar to вже enterprise. I learned it from my classmate who was camping there. And this is for everyone who really likeslungen, And then contact the vendor, of course. So, Verifone as well as I was saying. Crimp payment innovations. Well, several devices from them, they were affected. This is a very big company as well where they have everywhere readers. They were several of these devices were affected. We MUQUEDICY with, in this case, a Chinese company but they have devices everywhere in the world. Several of their devices were updated time and time again. And they are in this case what I said before. affected and next go in this case as well another Chinese vendor but again you can find these devices everywhere okay so let's talk about the impact like what the attack the attacker can actually do with these these attacks right with once the the firmware of the device is compromised so but it's always up to the imagination of the attacker but at least in my imagination I have these approaches so the very first one is to remember we have code execution in the in the microcontroller of the reader through the NFC the contactless interface we have full control so we can inject and install our own malware here so what we can do is typically in a transaction as you saw in the other video is that the host the the Windows computer in this case sends the start transaction command over USB to this guy and the price for the transaction, but my malware in this case that is running here because this device has been already compromised, I can change that price instead of $500 for 50 cents, you know, but even the receipt and if the device has a display, in this case this device doesn't have a display but the other devices have display, you can put the $500 but in the end in the background is charging you for 50 cents and since you can make this persistent then every future transaction will be 50 cents if you want or one cent or whatever you want to make it so that would be one possible attack that the attackers could do with this. Another thing that the attacker could do is in the US especially there's something called contactless magnetic stripe data transactions, where the number of of the full number of the credit card and the expiration date is sent in clear text to the reader, to the NFC reader, so that would be in clear text in the memory of the NFC reader in this case. So what my malware could do in this device is to, once the device is compromised, then every future credit card that will be using that type of contactless transaction, the magnetic strike data transaction my malware will store those credit card numbers in the memory or in the flash. And then if the device has internet connection, then send those credit card numbers to my server on the internet. But also if the device doesn't have internet connection, what the attacker could do is then after a few days or hours come back to the reader, send a special command to the malware that is actually running in the NFC reader and exfiltrate those credit card numbers automatically, and'd fire it in the reader. the NFC interface. So now I have here a demo of a weaponized exploit. So in this case it's an Android device but as you can see that I can't show you some parts of the of the graphical interface because I can show the name of the bank and some other details, but you will see the POC without the blurry image. But basically this is an Android device in this case and the weaponized exploit what we will do to prove that call execution is possible in this case and manipulate the firmware is going to show a message on the screen as you will see. So first we're using a regular credit card. I don't know why there's no sound but you should hear there like how the receipt is you know giving me that receipt but I don't know sorry but the sound is not working but yeah and after that then the device is ready for another transaction and I'm gonna use the proof of concept the weaponized exploit in this case and then you will see basically I will change the the the exploit will change the screen with an iOactive logo and like saying hey this device has been compromised blah blah you know just to there you go so that would be Okay and the last thing which is probably the most interesting one is that like okay now the malware that is installed in this microcontroller what if we can attack the host that is connected over USB and in this case could be the the Windows computer that is in the ATM. Okay. remember we have a USB cable that is connected to the host machine okay so to do that what I did was in the case of the ID tech device I I detect they provide the their life leap ID tech SDK to their customers so they can interact with the device and perform transactions so what I wanted to do is you know reversing the near this SDK and see if there they have issues so we can compromise the the host over USB so basically the idea here is that the SDK in the Windows computer will send the command over usually to the reader and my malware that is already there in them in the in the finger of the reader will send as a response a particular payload that allows me to get code execution in the in the Windows computer so for that the SDK provides several api's in this case when I was getting familiar with those api's I focused in the device in it API because this this particular one is always used because it's the one that is used for in it to initialize the device I mean before doing in a transaction so I was sure there that this one is it was an important one so so yeah I started to reverse engineer this API in the DLL of the SDK and I found in in the code of the SDK that I was able to reverse engineer this API in the DLL of the SDK and I found in the code of the the API were the the function where it's adversity usb payload from from the reader and in this case as you can see here there was this memcpy where the size of the même copy are two bytes that are coming from the USB payload for from from the reader so we can control the set of the main copy with two bytes and there were no sanity checks alone and the source of the memcpy was the actual usb payload ... it was receiving the from the from the nfc reader uh so we i just needed to to make sure that the destination buffer uh it wasn't too big so i was able to overflow it so in this case the buffer in this case was pretty small it was 56 hex bytes and i was able to overflow the buffer but also the other local variables in the stack and then overwrite the saved return others in in the stack and you know a straightforward stack buffer or flow to to get code execution in in the host so again so in this case we have to change two attacks right so we have first to compromise the finger of the reader through the nfc as we saw and then we inject that malware in the finger and then that malware that is running the nfc reader will receive a usb command from the windows computer and then my malware will send a specific payload to achieve code execution through that main code that we saw in in the in the code of the of the library so to to create a proof of concept of this what i did was instead of creating my own malware and and doing it with the nfc reader which takes uh some reasonable time i just uh to make it quick i use a raspberry pi with gadget fs which allows me to emulate any usb device so this raspberry pi was acting as the nfc reader so so yeah with the gadget fs i was able you know to provide the like the endpoint information and the id vendor the id product of the usb device so in this case i was emulating the nfc reader as a usb device and with python you can also you know send over usb whatever you want and receive whatever the in this case the sdk running in the host is sending you and this is actual this is actually the the proof of concept that was triggered in the stack buffer overflow and as you can see here in uh in the windows computer in this case with uh win win debug you can see how is it was actually crashing with that mem copy issue uh the return others uh as you can see here is 41 41 41 so it's basically my my payload uh so yeah so this is the way i found um a code execution in the sdk that uh idtech provides to their customers to to communicate with the device and remember that um uh so this this uh sdk from idtech uh it's it's present in in some atms in this case because it is the sdk that they provide to to communicate with the nfc reader so also this sdk is pretty pretty vulnerable they have more than 20 exploitable stack buffer flows like the the one i described that we don't have time to talk about all of them but uh but yeah it was pretty pretty bad okay so let's talk about the atm scenario so like i said i have many years of experience working with atm with atms i've been reversing during atms drivers also the atm devices firmware like for instance the gas dispenser and other devices also doing some usb hacking with atms and also i have experience working with xfs xfs is the middleware that most of the atms in the world they use to communicate with the hardware of the atm like for instance the cas dispenser and using some particular apas you can interact with the hardware and perform for instance a cash out um and again remember that uh this particular reader the id tech kiosk 3 is present in in most of the atm brands in the world most important brands, like some of them, NCR, Wincor, Diable, Fujitsu, Hayasun, and maybe more. At least I was able to confirm those some years ago. So what the attacker can do in the ATM in this particular case? So one of the first things, like before, is like, especially in the U.S. in this case, if the card supports MSD, magnetic strike data, contactless transactions, then the clear text, the full number of the credit card will be in the clear text and expiration date in the memory of the device. So the attacker could do is compromise the firmware of the reader first over NFC, like we saw, and install the malware. And then, since in the ATMs, these devices doesn't have internet connection, what the attacker would need is to leave the area, then come back after a few hours or days, or whatever the case may be, and then go back to the server, and then exfiltrate those credit card numbers from the memory of the device, using the malware that he already installed, like sending a special command to the malware, for instance, and then the malware will send you back to your phone the credit card numbers that have been used in the reader in those days. But then we also have the ATM jacobo scenario. So the idea here is like, once we have our malware installed, we have the driver running in the NFC reader, microcontroller, we want to, as we saw before, compromise the ATM computer. If it's running the IDTech SDK, we know it's possible. If it's running another driver for whatever reason, then you need to, of course, to find if the driver that is using it is vulnerable, if there's any memory corruption, and then exploit that. And then once you have code execution in the ATM computer, then you need, as you will see in the next slides, you need to be in a special payload or a program, in this case, to interact with the hardware of the ATM, like the cash dispenser, in this case, to perform the cash out, the jackpot. So yeah, one important thing also to take into account here is that we don't only, if we're working with an ATM in this case, we are not only limited to the driver or SDK that is used for the NFC reader. Because since we have full control of the microcontroller of the reader, we can modify the firmware to act as another USB device. Let's say I want to act as the USB camera, and I know that there is a USB camera driver in the Windows ATM computer, and I want to, and it's vulnerable, so I want to target that and attack that, so that would be another possible approach. So then once you have code execution in the ATM computer, through the ID Tech SDK or other driver, there are, like, if you guys, someone has experience working with ATMs, you probably know that once you have full control of the ATM computer, with admin rights, in this case, because normally it's Windows, there's just a matter of time to be able to perform the jackpot, because the way they are designed, it's impossible to make this impossible, basically. Of course, sometimes they put some mitigations, like, as we'll see, if you're using XFS, maybe there's some sort of authentication, making sure that your application can't use those XFS APIs and so on, but in the end, it's always, you know, a matter of time to find a way to do it, because the way it's designed, it's impossible to make it 100% secure. So this XFS, as I was saying before, it's a middleware that is basically in every ATM, that it provides several APIs that you can use to interact with the hardware of the ATM. So in this case, here, as you can see an example, this is one of my XFS programs that I use many times in ATMs to, you know, perform the cash out. In this case, for instance, as an example, you can see here, we are using the WFS open API to open a session with the device. In this case, the device is the cash dispenser, and then we provide several details, like the number of bills and the number of the cassette and some other details, and then we use, after this, other APIs, like, to execute the actual command and then perform the jackpot. So, as this point, you might be wondering, okay, so did you try this with an ATM? So, yes, I tried, and it was working, but it was in a lab environment, and there are some NDAs involved, so I can't show you videos about it, and I also can't tell you which ATM brand was this particular one. But if you have doubts or concerns about this, you can ask me. I think here you have enough technical details, so you can do it by yourself if you have the motivation. But also, if you have specifically concerns about this because you are working in an ATM security team or whatever, my recommendation here is that, first, make sure that the finger of this reader in the ATM is already patched, because there are available patches for these issues. And also check and make sure that the driver of the ATM is already patched. If the driver or SDK that is using the ATM computer doesn't have any memory corruptions. But the most important thing, obviously, is to make sure that the finger of this reader is already patched, because if the attacker can't compromise this over NFC, then more or less the problem is solved, right? Like, it's going to be really hard to compromise the ATM in this way. So, regarding disclosure. So, yeah, we contacted all these affected vendors. As you saw, there were many. All of them told us that they fixed the issues. Some of them, they provided us the fixes, and we tested that, and we were able to confirm that the fix was okay. But not all of them provided us these fixes, so we don't know for some others what happened. And, yeah, in this case, we waited almost two years, because this research, I mean, started almost two years ago. Because the problem was that, as you saw, many parties were involved here. And the problem is, like, since I was testing my proof of concept with another device, oh, this vendor is also vulnerable. Shit, again. And another. And another. So, you know, we had to wait time. But also, the problem is that many of these devices, they don't have remote update capabilities. So, sometimes they have to go physically there to, you know, to update the finger of this device. Which is a problem, right? So, yeah, that's a problem. So, yeah, I think that's everything. If you have any questions, I'll be around. Thank you.