00:00:00.000 --> 00:00:06.440 Hi everyone. So I have a lot of stuff to get through and not a 00:00:06.440 --> 00:00:12.913 lot of time. I am Ryan Mitchell and I know the name is weird for 00:00:12.913 --> 00:00:17.217 me too surprise. This is separating the box from the 00:00:17.217 --> 00:00:23.257 humans so who I am other than my name. I am software engineer. I 00:00:23.257 --> 00:00:28.362 work in Boston APA back end Java stuff and I have author of two 00:00:28.362 --> 00:00:31.932 jobs that have nothing to do with my day job. I am engineer 00:00:31.932 --> 00:00:39.506 graduate and I take night classes I have been doing that 00:00:39.506 --> 00:00:47.047 for a few years and graduating with my masters in engineering. 00:00:47.047 --> 00:00:53.053 So I submitted a brief proposal to oriel I will for a hacking 00:00:55.989 --> 00:01:01.929 book and pythons is a popular language these days so I called 00:01:04.164 --> 00:01:10.170 it web scraping with python and I put together a proposal and 00:01:12.339 --> 00:01:18.979 they accepted my hacking back. Thank you. And I don't have a 00:01:18.979 --> 00:01:26.053 lot of time so instead I'm going to focus on the first step. 00:01:26.053 --> 00:01:31.491 Separating the bots from the humans. Because if you web 00:01:31.491 --> 00:01:38.565 administrator what do you want to do you want to feel out who 00:01:38.565 --> 00:01:46.139 is the human and who is the bots. And if you web scraper you 00:01:46.139 --> 00:01:48.842 want to try to avoid the administrators from stopping you 00:01:48.842 --> 00:01:52.913 so constant back and forth so this could be called how to look 00:01:52.913 --> 00:01:58.919 like a human when you are bots and h is very important. So we 00:02:01.455 --> 00:02:04.091 are at Def Con, hopefully you heard of web scrapers before. 00:02:04.091 --> 00:02:08.261 Lost audio -- they can take their sweet time and they can be 00:02:08.261 --> 00:02:15.502 smart or dumb. But we're going tostart off with the defense 00:02:15.502 --> 00:02:21.508 stuff so we're going to go through the stupid human things 00:02:25.946 --> 00:02:31.952 to stop bots. So completely legally and unenforceable and 00:02:34.488 --> 00:02:40.494 not one standard of anything it is called robot inclusion 00:02:43.997 --> 00:02:51.571 standard but IUTF doesn't recognize and it government 00:02:51.571 --> 00:02:57.577 doesn't recognize. It is like your and your friends got 00:03:01.014 --> 00:03:07.020 together and one of you friends happen to be google so nice 00:03:12.459 --> 00:03:16.596 forgetting out of indexing but other than that is use less for 00:03:16.596 --> 00:03:19.499 blocking bots other than the good ones that want to follow 00:03:19.499 --> 00:03:24.938 it. Term of service this can be more useful but only in specific 00:03:24.938 --> 00:03:28.842 circumstances and those a usually when you and the 00:03:28.842 --> 00:03:34.948 American who wrote the bots end up in court. You have to if you 00:03:34.948 --> 00:03:40.620 have to click a agree you problem bring -- if you have to 00:03:40.620 --> 00:03:45.826 agree to terms of service that is contract so you should be 00:03:45.826 --> 00:03:50.597 really careful about scraping the site but at the bottom of 00:03:50.597 --> 00:03:56.269 the page go for it that is not enforceable and do other thing 00:03:56.269 --> 00:04:00.507 to access the site in order they can't just enforce the terms of 00:04:00.507 --> 00:04:06.513 service because it happen to be on the site. So headers, so a -- 00:04:10.817 --> 00:04:14.121 you can change the headers in chrome. So most there are use 00:04:14.121 --> 00:04:19.993 less most websites do check the heards so if you go the Amazon 00:04:19.993 --> 00:04:25.999 -- they will send you 403 forbidden. Java, I know we have 00:04:33.807 --> 00:04:39.813 other opinions about this. I'm not going the sway anyone here. 00:04:47.654 --> 00:04:52.292 You are now taking the code excuse for where you have a 00:04:52.292 --> 00:04:55.562 controlled environment and give the client -- don't quite know 00:04:55.562 --> 00:04:59.799 what is going on and makes your site less use able but you are 00:04:59.799 --> 00:05:05.739 protected against bots most can execute Java script. Embedding 00:05:08.508 --> 00:05:14.581 text and images don't do that. Your site is not use able by 00:05:14.581 --> 00:05:20.587 anyone anymore like the -- not resize able so don't try that 00:05:22.689 --> 00:05:27.828 and if it is read able if you do have text that can be read then 00:05:27.828 --> 00:05:32.699 it is very easy to OCR so not stopping anyone at all. I kind 00:05:32.699 --> 00:05:38.705 of catches most catches are breakable if you use a caption 00:05:41.107 --> 00:05:44.978 make sure sit really good like old google or make sure that is 00:05:44.978 --> 00:05:51.585 so obscure that no one is going look at it so behavioral 00:05:51.585 --> 00:05:59.426 patterns is where the future is. The google recap here is demo 00:05:59.426 --> 00:06:05.365 here and I have been doing it so often what it will mark me as a 00:06:12.072 --> 00:06:17.811 bots. It did it. But if I refresh it and click on again 00:06:17.811 --> 00:06:22.315 there see now I have not moved my mouse around enough for it to 00:06:22.315 --> 00:06:25.385 convince them I am not a robot. That is kind of thing that is 00:06:25.385 --> 00:06:31.391 robot would do so now it wants me to select all the images. 00:06:37.998 --> 00:06:43.770 Okay. So behavioral patterns, if someone is not moving over the 00:06:43.770 --> 00:06:46.706 page appropriately or loading things too fast or typing too 00:06:46.706 --> 00:06:51.344 fast or not scrolling down the page so that could be a clue 00:06:51.344 --> 00:06:56.650 that is bots. Honey bots -- what is something that is used often. 00:06:56.650 --> 00:07:01.922 Humans can't see it bots can see it and bots thinks that humans 00:07:01.922 --> 00:07:08.261 can see it over wise they would be obvious honey bots. And it is 00:07:08.261 --> 00:07:14.267 important to use the honey bots to the robot text one you have 00:07:20.640 --> 00:07:24.911 the bad robot -- tells me not to go there I'm going to go there 00:07:24.911 --> 00:07:29.182 honey bots and the google bots will not go to it and you will 00:07:29.182 --> 00:07:33.753 not block them. Now is the fun part and we are going to go over 00:07:33.753 --> 00:07:36.856 optical and Java execution. So optical character recognition. 00:07:36.856 --> 00:07:42.862 There is caption here. That we're going to go to. All right. 00:07:59.279 --> 00:08:04.217 So you can see a couple things about this caption there is 00:08:04.217 --> 00:08:10.223 prefix thing here. Notice it has this gray background and has 00:08:12.759 --> 00:08:18.765 this blue lines going through it and you can build this image 00:08:22.669 --> 00:08:28.675 getter that uses the library to go and clean this image you 00:08:32.579 --> 00:08:38.585 functions like it is gray if so take it out. You have things you 00:08:42.021 --> 00:08:45.825 can go around the perimeter of the image and see what the first 00:08:45.825 --> 00:08:50.130 non-gray pixels are and these are the lines here and cleaning 00:08:50.130 --> 00:08:53.500 the image you can get really nice looking things like this. 00:08:53.500 --> 00:09:00.874 And then you can start to do things with it. So the first 00:09:00.874 --> 00:09:06.880 step in using this tool that I made that you can get to I have 00:09:13.253 --> 00:09:19.259 the link here. Here is the test track trainer so you download 00:09:30.036 --> 00:09:36.042 the folder and label them so this is 40 AGU labeled -- I 00:09:46.286 --> 00:09:52.358 suppose you can ship it off to amazon -- but it is not that 00:09:52.358 --> 00:09:57.564 hard. Watch a few movies and kind of relaxing. The next step 00:09:57.564 --> 00:10:02.735 is to bring it into the software. This is just being run 00:10:02.735 --> 00:10:08.374 from my documents. It is just Java and the with be found the 00:10:08.374 --> 00:10:14.414 hub page. So add a collection of files let's go to ones that are 00:10:14.414 --> 00:10:20.053 labeled but let's do them again. So open as many files as you 00:10:20.053 --> 00:10:25.258 want you get these boxes and when you move them around it 00:10:25.258 --> 00:10:29.596 creates bots files. Creates these bots files and this is 00:10:29.596 --> 00:10:34.167 very necessary stop for training optical character recognizers 00:10:34.167 --> 00:10:36.736 and I have tried to make it painless as possible. So 00:10:36.736 --> 00:10:40.974 download -- knew one and this goes through all the images 00:10:40.974 --> 00:10:46.713 until you run out and you get a nice folder full of bots file 00:10:46.713 --> 00:10:52.719 that's look like this. So this is really important you need to 00:10:55.855 --> 00:11:01.794 keep a back up because it will over write them and destroy 00:11:06.666 --> 00:11:13.806 them. So you have bots PNG and put it into the images folder 00:11:13.806 --> 00:11:19.812 here. And now we go to fun part. Training so this is what uses 00:11:22.015 --> 00:11:28.021 tester acts. Run shape clustering. I don't remember 00:11:31.925 --> 00:11:36.496 what half these things are and I spent hour was documentation. 00:11:36.496 --> 00:11:43.336 This is library that was built by academics very difficult to 00:11:43.336 --> 00:11:48.942 use. All you have to do is get your bots files with the 00:11:48.942 --> 00:11:53.413 appropriate names named with their solution and you say 00:11:53.413 --> 00:11:58.518 python 3 I have both of them installed. Trainer . PY. Go and 00:11:58.518 --> 00:12:01.688 just does everything for you which is really nice because 00:12:01.688 --> 00:12:05.158 back in the days we had to write commands. Now the next step is 00:12:05.158 --> 00:12:10.029 you get this -- so our language that we're using for this is 00:12:10.029 --> 00:12:16.035 called CAP. If we wanted to solve any regular text we use 00:12:31.951 --> 00:12:37.957 ENG training file but we make our own here and we need to move 00:12:50.336 --> 00:12:54.674 that. I'm just trying to copy the file. So we created this 00:12:54.674 --> 00:12:57.577 file in the images director with all the bots files and image 00:12:57.577 --> 00:13:03.683 files then we just move it over and now test track knows what 00:13:03.683 --> 00:13:07.787 the language CAP is and how the run it so we can do something 00:13:07.787 --> 00:13:13.793 like this where we want to get the text from let's just use 00:13:20.199 --> 00:13:26.205 image that images to by the labeled folder. Let's check that 00:13:34.013 --> 00:13:41.988 one out. It Y4EE. So I did plan that one ahead of time but does 00:13:41.988 --> 00:13:48.061 work well. But remember our goal is to go to this and post a 00:13:48.061 --> 00:13:51.264 comment. So let's check out the code what does now that we have 00:13:51.264 --> 00:13:56.903 everything all built. So this uses the same code that is in 00:13:56.903 --> 00:14:02.942 the image training file so it is you colored and we clean our 00:14:02.942 --> 00:14:09.515 images before we trained it so when we get the image we have to 00:14:09.515 --> 00:14:15.421 clean it again before we can do that comparison. So the main 00:14:15.421 --> 00:14:18.658 function is post comment and goes in here and first thing it 00:14:18.658 --> 00:14:24.997 does is get me a caption. So this is where posting our 00:14:24.997 --> 00:14:28.968 comment and goes and gets this SI code that is code at the end 00:14:28.968 --> 00:14:34.841 of image that acts as a seed for the random cap you don't have to 00:14:34.841 --> 00:14:40.847 you work with it in memory but I prefer to download it and then 00:14:45.485 --> 00:14:51.491 it uses text track and then gets solution and checks to see if it 00:14:56.162 --> 00:15:02.101 has four characters and then it puts in the parameters and puts 00:15:05.204 --> 00:15:11.210 in a post request and you might get a error page in which your 00:15:16.616 --> 00:15:23.990 -- python will explode but hopefully that will not happen. 00:15:23.990 --> 00:15:29.996 So let's try this out. Python three comment poster. Let watch 00:15:33.966 --> 00:15:38.771 it. It is going to work. It is loading the page and -- success. 00:15:38.771 --> 00:15:44.777 (CLAPPING) so I did that in like 10 minutes you can too. But for 00:16:05.164 --> 00:16:11.170 the bots file creation but you can too. So I mentioned a jacks 00:16:18.477 --> 00:16:24.483 that is important part of internet today so I have a bots 00:16:30.723 --> 00:16:38.598 that can handle this. So there is this page. H is contend that 00:16:38.598 --> 00:16:42.235 will appear while the page is loading now the important text 00:16:42.235 --> 00:16:48.241 pops up. This is the contend that we would scrape so what we 00:16:50.509 --> 00:16:56.515 need the -- in my slides I have the download link. So download 00:16:59.652 --> 00:17:05.591 phantom JS browser that runs in the background you can run from 00:17:08.027 --> 00:17:14.033 the channel line and let it do its thing. You can execute Java 00:17:16.102 --> 00:17:22.108 script from the command line. So remember I have that website. So 00:17:25.478 --> 00:17:29.315 this is text that it displayed first. So this script is doing 00:17:29.315 --> 00:17:35.321 its job. It should wait for a little bit while the page is 00:17:37.523 --> 00:17:44.497 loading and the text you want to retrieve should pop up. So it is 00:17:44.497 --> 00:17:52.271 going on. I have given you web driver, the driver gets that 00:17:52.271 --> 00:17:58.277 page and sleep seconds. So honey bots. Here is bots proof form. 00:18:00.513 --> 00:18:06.519 Why it is bots proof. Have a lot of honey bots. Most people have 00:18:09.121 --> 00:18:15.127 hid input fields and you see in the CSS, I moved it 50 --> 000 00:18:39.518 --> 00:18:45.858 pixels to the write so these are things that bots try to fill out 00:18:45.858 --> 00:18:51.864 and I think there is one at the top of the page so there are 00:18:54.100 --> 00:19:01.040 other ones too that I invented and basically input type with a 00:19:01.040 --> 00:19:03.409 couple of DIVs overlaying. That make it so that you can't -- I 00:19:03.409 --> 00:19:09.415 don't notice anything going on. You can't select that so it 00:19:14.620 --> 00:19:20.626 meets the three criteria. Visible to humans and in visible 00:19:24.330 --> 00:19:30.336 to bots so we use the same celeb yum and you can select tag name 00:19:34.006 --> 00:19:40.012 if you have DIVs overlaying it and the text is the same as the 00:19:52.458 --> 00:19:58.464 backgrounding text color then it can be tricked sometimes. So 00:20:01.167 --> 00:20:08.541 let's run this. Going to give that page and every time it a 00:20:08.541 --> 00:20:15.915 field is not displayed it will tell you it is trapped. If you 00:20:15.915 --> 00:20:21.420 bots filling out the form or navigating from link to link it 00:20:21.420 --> 00:20:27.426 is easy to see if it is displayed first before you go to 00:20:32.631 --> 00:20:38.637 it. It found the type is hidden. The display non-is track. But 00:20:43.209 --> 00:20:47.947 our honey bots covered with DIVs that we can't see it all so that 00:20:47.947 --> 00:20:53.919 worked out well and in the last five minutes I wanted to take a 00:20:53.919 --> 00:20:58.357 couple questions but first run a really cool script that I like. 00:20:58.357 --> 00:21:03.829 This is so the amazon previews that are protected. These are 00:21:03.829 --> 00:21:09.835 embedded with imagines. Not difficult for a bots. These are 00:21:13.639 --> 00:21:19.645 a jacks limited so we can use a bots to go grab this text and 00:21:23.149 --> 00:21:29.155 print it out. This worked better on a larger screen but I will 00:21:36.996 --> 00:21:43.035 pop up Firefox you can see it work. You can do this with 00:21:43.035 --> 00:21:49.041 phantom JS as well. Any questions? He said do you have 00:22:11.497 --> 00:22:17.503 idea what the defense might be or attacks might be against this 00:22:26.579 --> 00:22:32.151 new generation of behavioral defenses and I think the big one 00:22:32.151 --> 00:22:37.790 is going to be things like that where you use Java script and 00:22:37.790 --> 00:22:42.995 you can make the mouse move and interact with dragon drop and 00:22:42.995 --> 00:22:46.866 send characters one key at a time so you can add in all this 00:22:46.866 --> 00:22:51.937 timing and making the mouse move with functions so I think the 00:22:51.937 --> 00:22:59.278 next version is going to emulating the behavioral 00:22:59.278 --> 00:23:05.217 patterns. It broke. It took so long to load. So I will try it 00:23:20.132 --> 00:23:25.671 against. So how do you defend against Java script. It use a 00:23:25.671 --> 00:23:32.778 brassiere. The only thing between the bots and human in 00:23:32.778 --> 00:23:38.784 this case is what the behavior is like and can hey do things 00:23:41.987 --> 00:23:47.993 like a human would be able the do. Caps and behavioral and caps 00:23:51.230 --> 00:23:57.236 might not be that great. And I wanted to let you know that 00:24:00.372 --> 00:24:06.979 everything is available at python . com and I will put it 00:24:06.979 --> 00:24:12.985 up after the talk and I will be in the Def con23 lounge after 00:24:20.225 --> 00:24:25.064 this, drinking happily. (CLAPPING)