Dear viewer, these subtitles were generated by a machine via the service Trint and therefore are (very) buggy. If you are capable, please help us to create good quality subtitles: https://c3subtitles.de/talk/1075 Thanks!
[Music]
good so welcome to clar we will be
hearing
umah uh who has been working for as an
IT consultant for more than 15 years
mostly in Source cord auditing and
penetration testing already this year he
has spoken at nullcon uh waron Defcon
and the climax of the Year must
definitely be here at the CCC we'll be
hearing about in Soviet Russia smart
cart hacks you looking forward to the
talk answer your questions later give
him a big round of
applause thank
[Music]
you uh yeah she already introduce me my
name is Eric um I'm um longtime CCC
vpart or formerly Minds member and uh
come to a congress uh since a while I
have to confess this this is the first
time I'm scared and nervous so um yeah
and um it's wonderful how all the CCC
Congress thing grew all the years and
evolved and it's so nice being here each
year it's it's fabulous thank you for
all the helpers
[Music]
um smart cards have been a topic at
Congress since um even before 19th Cy
but before then there are no recordings
and also the early ones you might not
really want to watch them but um it's a
reoccurring topic and if you have um if
you are thirsty for more information go
to the the media recordings um some of
the talks are very interesting and very
good um when you look at the history of
um smart card hacking most of the attex
focus on the smart card and uh this talk
is about a different
approach so the the issues I'm
presenting have been fixed I um I
reported them to the proper projects and
um helped them uh to to close the issues
and I mostly looked at open source
projects because I think if you are
proprietary vendor and make money money
by selling software you should also pay
somebody to uh have the software secured
there's one
[Music]
exception there there's one exception
through this um because friend of mine
has to use a certain smart c car driver
so I had a quick peek um at it but uh in
the few evenings I didn't found any
serious security issues but some crashes
but but those help to illustrate some
Concepts that's why I present them
here um what what are smart cards or
where are they used most of the you
probably know them from EC cards from
your credit card the the younger
generation maybe more from the SIM cards
you use in your mobile phone um
sometimes for for the old older
generation here you use them in the um
in the pay phone in Germany we also use
them for
healthare and um another use case for
smart cards is authentication so you use
them as a second Factor if you log into
to your Linux system you use a password
and you use a smart card and um the the
password somebody can steal from you if
they look over your shoulder or if they
have a key Locker installed but if you
have the second Factor the smart card
they need to capture both and um that's
harder so the the whole setup is to be
is considered to be more
secure
and I I looked mostly at this Lo loin
scenario and um an interesting part of
this lockin process is that it
completely runs as root so if you find
Buck there you can instantly log in as
roote instead of just um one single user
for for which you maybe uh have captured
a password and there's a certain um
interesting psychological element to it
as well the the programmers for the
smart cards they also see this um secure
device this a trustworthy device and
they trust it as well when um passing
the responses so they tend to make
mistakes they maybe might not make when
programming um for example network
drivers and um yeah why not abuse that
in order to do so we have to understand
the whole smart cut stack what layers
are there what um parts are there to the
whole
communication and um we will now go
through each of the layers and um I give
give you a few words to this in in
general you have a temper proof device
um the the vendors who build those smart
cards um try to ensure that all the
secrets that are stored in the microchip
on The Smart Card remain there right and
um in the past most attacks were like um
um trying to to glitch the smart card or
trick it somehow into revealing the
secrets that are stored on the smart
card and um you have some pins that uh
power on the chip in the in the smart
card and have an IO pin to
communicate and uh for this
communication you need a certain
protocol that the smart card and the
computer understand and this is um this
protocol consists of apdus it's um
basically small packets or small
commands you send to The Smart Card the
the first bite is a class bite that um
specifies whether the command is um from
the stand or whether it's Vendo specific
um the inspite that's the instruction
basically encodes the yeah says okay
this is the following command for
example if you have hex 20 that's a
verify command you send a you send the
the PIN to the the smart card and tell
it okay this is my pin please um unlock
yourself you have two param metals where
you can specify additional information
and um can also send for the
data um the the smart cut then answers
those uh commands with um a blob of data
usually and at the end of the data there
are two bytes um telling the computer
whether this command executed
successfully that's the 9,000 or if
there um occurred a certain error and um
one of the responses that's interesting
for example is the
61 because because it says Okay um I
have sent you something but I still have
more bites to you um other say for or
encode that um there are yeah some
errors that en code something went wrong
I can't give you more information and
others tell you okay you tried to log in
and you failed and you have one try left
to do
so on top of this you need an
abstraction layer for the for the smart
card reader and this is um pcsc that's
an API you can use to send those apdus
to The Smart Card
the there there are some um some more um
functions that allow you to um query the
reader whether C is inserted or get
notified if a c is removed but the most
interesting for me was the transmit uh
function where you basically pass two
buffers two lengths and one buffer is
the one with the data that is sent to
the smart card and the other one is the
buffer with the data that you retrieve
from the smart card and uh the nice
thing is this um abstraction layer is
the same on Windows and on
Linux on top of that you usually have
another abstraction layer that um
doesn't talk about smart cards anymore
but about cryptographic tokens and um
you can use a Smart cut there you can
use a soft token and um this one is also
like yeah standard I ized um your
browser uses it open SSL uses it wire
another abstraction layer and it's also
quite generic and um if you write a
driver for smart card you usually um
implement this interface and use the
pcsc interface to talk to the smart card
and each of each smart sorry each smart
card when inserted sends a certain
command string and an answer to reset
and this encoders um some basic
information about The Smart Card how to
talk to it but also some histor historic
bites where you can um which you can use
to see which vendor The Smart Card
belongs to or what kind of smart cards
it is and the driver uses this
information to know whether the driver
is responsible to handle the smart cut
or is able to handle a certain smart
cut
how does the lockin process usually
work usually you have something like pen
um that you GD that your um your
graphical console users or your um share
login users to um handle certain ways to
do
authorization and um the pem library
first tries to enate all the
certificates on the smart card and if
one of the certificates matches a
certain user that is allowed to log in
on the system
the revocation check is performed to see
if the certificate is really still valid
of it's maybe revoked by the by the ca
if the certificate is valid and um the
user that matches uh matches it um is
allowed to log in on the
system the computer generates announce
that's a number only used once and um
it's usually a random bit string and and
a smart card is asked to generate a
signature for this nons if the smart
card was able to do this um we know that
the smart card contains the private key
matching the certificate and if that's
the case the user is allowed to log
in um let's start with the proprietary
driver I looked at a bit um in Spain
there is a national ID card and they
have a smart card triip on this as well
and um as far as I understood my friend
um they are forced to use this for
certain actions I'm not sure what legal
um issues there are where you have to
use it but um yeah he complained quite a
lot that he's forced to use it so I took
a quick
look um the the stuff was released at
this at least for Linux from like 10
years ago so the binary St ship it's um
um it's a library it's not really
hardened or um as you can see there are
no canaries
um so um yeah it's a technology from
like 10 years
ago um the library uses two open source
libraries as well one is the crypto
library that um has non
vulnerabilities I'm not sure if any of
them really matter for the for the use
case but um yeah at least they did not
bother to um create an update the other
one is an as in one parle and from the
size of those two additional libraries
you see there's a lot of attack surface
in the driver right um there's a lot the
driver does to do its work and um yeah
at least for the open source Crow I
guess it's interesting that they don't
have a copyright notice um explaining
that they use open source so
yeah um I I toyed around a bit with it I
didn't do extensive fuzzing or something
but um just tried some some easy things
for example um sometimes the the driver
asks for a response and um tries to
retrieve data from the card and what
happens if the card answers with um yeah
I have more data for you um please
request another zero bytes from me the
driver will happily do that and um so
you can create an endless loop where um
you play this ping pong all the time
right um it's not serious but I guess
there might be cases way could do a
denial of service by just inserting a
smart card that always replies with I
have Zer more data for you and just keep
the device busy right um but usually
when doing security um denial of service
is not interesting so you try to give
the system some bites to do work with
right so the next thing I tried was some
say okay I have um 255 bites for you and
um did this in an endless loop and
whenever the the card the the drive
requests more data I gave them another
252 bytes of bogus [ __ ] um at a certain
point it just crashed because the the it
tried to allocate a buffer that was too
too big um but that was um yeah nothing
that that seemed to be exploitable but
um in other cases it might have been
right um another issue there is
um if you reply hey this command worked
and um you don't send any data but
signal with the 9,000 um Response Code
that everything worked and it's fine um
you can um at least for for this driver
generate um nighto reference and a crash
um not not really interesting but it
shows where where things can go wrong
right um there there's a lot of um other
issues um I found where things don't go
the way they should uh none of them seem
to be exploitable that's why I'm easy
about talking here um without having the
vendor notified but um yeah it shows
that in if you look a bit at propri tra
software they seem to have similar
issues but my main focus was on um open
source software um these are the
different drivers I looked at um the the
op MC1 stands out a bit but that's
because they support a whole range of
Smart cards they have drivers for a lot
lot of different smart cards so the tech
service is quite huge and I think the
projects um project has exists for 20
years or something like that so um some
of the code is quite old as
well um for for op um I have to thank
Frank mner for doing all the
coordination and fixing and everything
because with um this wide range of Smart
cards you can imagine that there's not
one single developer with the huge stack
with all of the smart cards but it's
like I don't know 20 30 people where
everybody has two different smart cards
and want to do the want to do a new
release and want to do the QA for the
for the new release it's um it's a lot
of work of um to to coordinate all
this um yeah apple apple was quite
interesting when I notified them about
to BU they um silently patched it um as
far as I understand the code is not used
in um in um the the current uh um
current um machines they are shipping um
apparently it was used um in the past
and um yeah I had to ask him again to to
um yeah at least to a change lock entry
that it's secur
related and um I tried to carry them if
it's actually new somewhere or not but
um I didn't get a response out of them
um what happens here um it's a y Loop
that um continues to Loop until the the
card signals um that um it uh no longer
has additional data left and as long as
the card sends data the data is copied
into a buffer by the M Copy so um as and
yeah the the whole trust um the the
developer who wrote that um he put a lot
of trust into the smart card to stop
sending when it's done and basically
trusted with what it does but uh the
card can just continue to say hey I
still have more um more data for the
certificate and do Heap overflow in this
case and um corrupt the
Heap um another one of the the issues um
I I think are interesting in the openc
stack is uh in the kryptoflex tool they
um have a fixed size buffer of 248 bytes
and they um use an hyper function the
select file function to um get some meta
information about the file and one
information is the file size and um when
they read the binary they use the size
that the card said um the the file has
instead of the size of the buffer so if
the car card says the file is um 5
kiloby then the buffer will obviously
overflow and
um for this I have a small
demonstration the the tool is um a
helper tool that's used to um for
example list the keys that are on the
card or um yeah for for other stuff so
as you can see if you try to read a key
from the card the the um the memory gets
corrupt and it's possible to execute
arbitrary code it's not a not a scenario
that you would think somebody would
exploit in the
white but um yeah it's a nice use
case
um another um vendor is yubico they also
generate or um they they ship or sell
small devices where you have a smart
card and the smart card reader basically
combined so they have a nice form factor
of an USB stick and one of their drivers
they they have this code um that's a
hyper function for all the times where
data is retrieved from the smart card
and um they also do a m copy of the
retrieve data into the the buffer the
the caller
supplies and um as you can clearly see
they they have a check whether
the data already received this um big
than the size of the buffer and if
that's the case they print a nice error
message and continue to copy the data
into the buffer right so um yeah the the
fix is obviously easy um but yeah those
are mistakes that commonly happen during
programming right and um I hope
the second proof of concept is
uh you can read most of it um this is
now for for the classical Linux login um
we don't care about username um as soon
as PM starts to query the The Smart Card
the the driver gets active and copies
data and um I have the the de debug um
output on and um also slow it down a bit
to help me while doing all this and
um after while you see that uh we
trigger the the error
message that's now the error message the
output buffer is
small and um I keep continue to write
stuff and Tuck at the end um I also get
a code execution and the nice thing is
if you can read it um I'm rude since the
whole login runs
rout
um how do you exploit this in white um
the the easy way would be to
um um to
um yeah use custom Hardware that allows
you to interact with the smart card
reader and everything but um this would
also require you to have maybe some
wires that leave the the reader which
would be fine for the Linux login but if
you think about 8 M or something that
might not work or exess Control Systems
so um what I used um is basic card um
yes you have to write in basic but um
you can do all the smart cut stuff that
you want to do and um the most important
thing is that you can um fully customize
the the RTR so um basically you can um
can say or can have every driver except
your card which is not the case with all
smart cards um use um yeah this um on
the lower part you declare the
historical bias of the ATR um in this
case for the for the open driver and you
also declare the the functions um that
are the the apdus um in this case if you
have a in a upper case if you have a
class bite of
c0 and an instruction bite of A4 the my
S file function is called and there can
pass the parameters and also generate
response as I want to um this is the
select file where I can um where I pass
which file the driver wants to select
and um select my in this case static
response
automatically um the code is um of
course kind of silly for for this um for
this command but um if you want to see
how it works and how to interact with a
smart card reader or smart card driver
um it's available for
you um how do you find bux and smart
card drivers one one way is to um do
manual source code auditing that's part
of what I did um the other part was
fuzzing um in a
very yeah easy way fuzzing is like
sending lots of lots of um bogus data to
a driver until it scratches and um if
most help tools for that are file
oriented right they are not um designed
to work with protocols where you send
several packet um to a card and get get
a response and send another packet so
you have the challenge of um converting
the the file input the F generates to
something that looks like protocol data
and here the nice thing is that the scar
transmit uh
repper actually tells you how much data
the the receiving end um the the driver
wants to read so what I did was um
simply get the always fill this buffer
to the end with um with data from a file
and um since it's uh you don't since
it's not Network oriented you don't need
to take care of polling or something
similar you can just copy the
buffer and um that's what it looks like
for for opsc I generated um virtual
reader and this virtual reader interacts
with the fuzer so um I used AFL for
fuzzing um which also does code coverage
to see whether an input file is
interesting or not and
um yeah that that's what I um ran most
of the time to find bux in the Ops
project um I also tried to or not tried
I um did I um programmed the library
that you you can preload on Linux or
Windows and have the smart cut driver
interact with the your code instead of
the real smart cut uh of the instead of
the real reader and um you can also use
this to um fuss yeah more or less
arbitrary smart card
drivers um but not enough um last year
somebody Tavis ported parts of Windows
Defender the antivirus to Linux and
um
basically he tried to get um the code he
wanted to fuss run in a very small
environment so you don't have much
overhead when fuzzing it's always nice
to be able to do a lot of iterations in
a short time because the f um the faster
you can fuss certain code um the more
likely you are to find bucks because you
can just try more more random data and
um I have beaten this Library um enough
to be able to load um windows drivers or
Windows smartcard drivers on Linux for
fuzzing um there are some exceptions if
you have a net driver um it will not
work um because it depends on lots of
additional other libraries but um
everything that's PL C should work more
or
less um yeah all the fs are released um
you can download them play around with
them and um if some something does not
work just uh send me an email I try to
help you
um yeah this is what the AFL looks like
when you do fuzzing um I had run it for
um yeah I I think in total 28 days or
something and uh um was able to shake a
lot of bucks out of
OPC but um when you look at the coverage
report um the coverage is
30% which um I think is not too shabby
um when you think about that
the the answers have have to be correct
for a lot of different um requests right
at the beginning the the smart cut
driver usually tries to verify that the
card is really something um he is
allowed to interact with tries to open
some files and
uh um reads them and um it um all um it
takes like 10 commands or so before the
interesting stuff happens like um the
the the eneration of certificates right
so I was kind of quite surprised that it
got that far and um yeah on the lower
case there are several things that um
have zero coverage but in this case
these are um pkcs5 functions which I
didn't touch at all some of them are
drivers that are not um enabled by
default in OPC but on the lower end um
for for some of the the drivers the the
code coverage was um quite High I was
actually quite surprised that um I was
was able to reach it with the simple
setup I didn't hand tune the input
Corpus I just threw in two sample files
and from they basically found its way to
be able to fuss a lot of different and
reach a lot of different
functions so um it's not only the
drivers that are interesting um in this
whole setup but also the the haper or
the the upper layers for example the the
PM um functions the pem
modules um one of them that you can use
to uh um log in with a smart card is PM
pkcs11 and um the the whole process
looks quite similar but it differs in
one point um and that's where the the
nons is generated when you generate a
nons you want good Randomness because if
um an attacker is able to replay a nons
or grab a nons and
um um the the nons and the signatur is
used again later it's possible to replay
this and um abuse that and what the
programmer did here was um yeah he
needed a lot of good entropy so he had
this small security module that is able
to generate entropy right so basically
he asked or they ask the the smart card
to generate the random data and then ask
the smart card again to um do the
signature so if an attacker controls a
computer that the victim locks into um
the attacker gets the nons and a
signature for this nons and then can um
yeah modify his own smart card to um
replay this at another machine right so
um yeah this was also an interesting
Buck where you can see the psychology of
the the developer trusting the small
device but not thinking about what he
does um there are some roadblocks when
you want to do exploitation um against
the drivers one is that um the data data
sent to the card is usually quite small
and um if you need an information leak
for exploitation for example this is um
not easy to do you don't have uh
interaction because you have no keyboard
on the smart card or no display but um
if you can get wires out of the reader
Sim trace or something that you can use
um there's a firware that allows you to
not only um do a man in the middle on
the communication but um send whatever
you like
but uh the advantage you have is that
you don't need um necessarily to do um
full code execution in the end you just
need to flip the the few bites of memory
that say okay this user is logged in so
that's um an advantage for the
attacker what what I learned from all
this is um I um always advocated smart
cards as a good thing and um always
thought of it of a good two Factor model
but um never really thought about um the
increase of tech surf surface um that
happens when you deploy smart card
authentication um it's a bit similar
with antivirus right antivirus helps you
for your security but um yeah if your
antivirus has a bu um that's also quite
serious um if you use open there's um is
a config option that you can use to
Blacklist um or or wh list drivers so
you only run the driver that you
actually use in your setup and this um
would greatly reduce your attack
surface and um yeah the the ubo guys um
did the right thing they they um write
the new drivers in Python instead of c
and there you don't have all those um
memory corruption bugs that um lead to
code execution so if you have the
possibility to do that go for it um yeah
thank you um
if you have any
[Applause]
[Music]
[Applause]
questions and uh yeah I I don't use
Twitter um the only social network
somebody tricked me into using this
LinkedIn so sorry for that and we have
five microphones for questions and we
have about 5 minutes so get up and stand
behind the microphones if you have any
questions there is one microphone number
1 2 4 3 and five and the exits are the
two ones to the left there is a question
from microphone number one yes I have a
question that's maybe it's a little bit
weird but maybe try such a thing with a
rid card because there are many places
that have smart card readers they have
like your doors for example you need to
present a card and then maybe you have a
card that FES the reader of the door um
as far as I know for the contacted smart
cards the the whole protocol is similar
but I didn't play around with that um
I'm I'm too oldfashioned to use W
technology if I can avoid it so that's
okay yeah good and for those of you
leaving please leave quietly so that we
can do the Q&A there is another question
for microphone number
one uh maybe haven't you tried uh
putting a simple microcontroller to do
your bidding uh easier than a basic card
you know maybe a kind of framework that
uh could be used instead of this one
um yeah I think the the um the the Sim
Trace stuff is what you want to do I um
have not tried the fil they just
released the second version of it um I
guess that's what I would go for um a
friend is um also thinking about doing a
custom
PCB
um yeah I guess there are a lot of
options
um or maybe even H uh we have a couple
of more questions waiting so uh come us
come to me later and we can talk and
there's a question for microphone number
two uh yes uh your talk is mostly about
malicious cards attacking a compromised
reader uh is there any uh possibility at
this point to get uh an RSA key or
anything like this out of a card or so
far the seem still secure um that's what
what I looked at um I'm I'm not a
hardware guy um there were people in the
past um the the talk of cast n at um uh
at one of the camps was quite good about
where it talks about um decapping the
chip and adding propes so yeah but I'm a
software guy
sorry question for microphone number
one is the pkcs protocols and the pcsc
software stack too complicated and could
it be replaced with something simp
I I don't think it's a software St the
problem is that um you have a lot of
weird data that the cards send to the
driver in some cases it's asn1 encoded
sometimes it's um zipped sometimes the
vendor does proprietary whatever and um
all dis passing is um I guess where a
lot of problems happen we have a second
question uh for microphone number one
two but if you have a second question
and didn't get time to ask it before
there's time enough to stand up by on
the microphone and ask it again
microphone number two um thank you very
much for the talk one question you
showed that the vulnerability in the
driver would that mean that every PC
which allows smart card authentication
even if you don't use it is vulnerable
to a root login by this uh vulnerability
no um usually uh you have to install the
drivers um on Linux they are not
installed by default on um Windows as
well they aren't um on Windows sometimes
it's quite hard to actually get access
to the drivers because the the Wenders
are very often keeping them only for
selling customers and stuff like that
it's I don't know why they do that but
um it seems like a business model for
them yeah but usually the the stuff is
not instored by default you have to
actually enable it at least on on
Windows and Linux I don't know about
me we still have time for a few more
questions so please get out behind the
microphones if you have more question
questions question from microphone
number one yes uh thing is uh um
regarding this R cards I'm just thinking
there are some applications where you
can use an NFC controller in your
Android phone to simulate an NFC card so
you use the basic card to simulate the
smart card so it should be possible to
use this NFC Smart Emulator to do this
fuzzing with RFID cards would you think
this would be a good approach um I I
didn't do the the fuzzing from the basic
card the basic card was only used to
exploit it because um when when doing
fuzzing you want to do a lot of
iterations so you want to do trigger as
many code paths as quickly as possible
and how can you interface
it so um you I guess you can do that um
I I think the the basic card guys also
sell um um um contactless version um I
don't have it so but um yeah I guess the
the tech surface um with this is quite
youu can could think about um attacking
the firmware on your mobile phone via a
b SIM card maybe to unlock it if it's I
don't know right
it's good do we have any more questions
please get up behind the
microphones seems like we've all run out
and there are no questions from the
internet either so please give Eric a
big round of applause thank
[Music]
you
[Music]
a