{"id":34,"date":"2019-02-14T03:16:14","date_gmt":"2019-02-14T03:16:14","guid":{"rendered":"http:\/\/robertmccallum.nl\/?p=34"},"modified":"2019-02-15T01:23:42","modified_gmt":"2019-02-15T01:23:42","slug":"35c3ctf-box-of-blink","status":"publish","type":"post","link":"http:\/\/robertmccallum.nl\/index.php\/2019\/02\/14\/35c3ctf-box-of-blink\/","title":{"rendered":"Terminal led matrix emulator."},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">35C3CTF &#8211; box of blink<\/h1>\n\n\n\n<p>provided is a 400MB .csv file and a photo of an oscilloscope with logic inputs connected on to data bus between a raspberry pi and 2 chained led matrix boards.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/blink-1024x1024.jpg\" alt=\"\" class=\"wp-image-39\" srcset=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/blink-1024x1024.jpg 1024w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/blink-150x150.jpg 150w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/blink-300x300.jpg 300w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/blink-768x768.jpg 768w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/blink-1200x1200.jpg 1200w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Recon<\/h5>\n\n\n\n<p>after quick examination i realized the csv file was a log of the measurements from the oscilloscope. <\/p>\n\n\n\n<p>the beginning of the file looked like this.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Model,MDO3014<br> Firmware Version,1.26<br> #<br> Waveform Type,DIGITAL,,,,,,,,,,,,,<br> Point Format,Y,,,,,,,,,,,,,<br> Horizontal Units,s,,,,,,,,,,,,,<br> Horizontal Scale,0.004,,,,,,,,,,,,,<br> ,,,,,,,,,,,,,,<br> Sample Interval,4e-09,,,,,,,,,,,,,<br> Record Length,1e+07,,,,,,,,,,,,,<br> Gating,0.0% to 100.0%,,,,,,,,,,,,,<br> ,,,,,,,,,,,,,,<br> Vertical Units,V,V,V,V,V,V,V,V,V,V,V,V,V,V<br> Threshold Used,1.65,1.65,1.65,1.65,1.65,1.65,1.65,1.65,1.65,1.65,1.65,1.65,1.65,1.65<br> ,,,,,,,,,,,,,,<br> ,,,,,,,,,,,,,,<br> ,,,,,,,,,,,,,,<br> ,,,,,,,,,,,,,,<br> ,,,,,,,,,,,,,,<br> Label,OE,LAT,CLK,E,D,C,B,A,B2,B1,G2,G1,R2,R1<br> TIME,D13,D12,D11,D10,D9,D8,D7,D6,D5,D4,D3,D2,D1,D0<br> -1.0000000e-03,0,0,0,0,1,0,0,0,0,0,0,1,0,1<\/pre>\n\n\n\n<p>and continued with 400MB worth of lines like<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">-1.0000000e-03,0,0,0,0,1,0,0,0,0,0,0,1,0,1<\/pre>\n\n\n\n<p>I&#8217;ve worked a lot with digital oscilloscopes in the past so no need to look up any Information on this, since it&#8217;s still in my head.<\/p>\n\n\n\n<p>but the only led matrix I&#8217;ve ever worked with was a home build one made from some cheap xmas-lights and a IR-strobe light for a industrial sheet metal inspection line.<\/p>\n\n\n\n<p>After some googeling i found a nice tutorial on how to connect a led matrix to a arduino. with some explanation on what each pin is used for. <a href=\"https:\/\/learn.adafruit.com\/32x16-32x32-rgb-led-matrix\/connecting-with-jumper-wires\">https:\/\/learn.adafruit.com\/32&#215;16-32&#215;32-rgb-led-matrix\/connecting-with-jumper-wires<\/a> <\/p>\n\n\n\n<p>and since the labels are in the header of the blink.csv file are the same as the pin names. i can conclude that this it the same type of data bus.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">so we have the following signal:\n\noutput enable bit - - to enable output.\nlatch bit         - - to indicate end of data\nclock puls        - - to tell the board to read new data from the bus\nA,B,C,D,E         - - to select what line(s) to send the output to.\nR1,G1,B1          - - color data for the firs row.   \nR2,G2,B2          - - color data for the second row.\n\n\n                     \/------- OUTPUT ENABLE (TURNS ALL LEDS OFF OR ON)\n                    \/   \/------LATCH (INDICATED END OF DATASTREAM)\n                   \/   \/   \/-- CLOCK PULSE\n                  |   |   \/  [    select rows   ][ blues][greens][reds  ]\nLabel,         | OE |LAT|CLK|E  |D  |C  |B  |A  |B2 |B1 |G2 |G1 |R2 |R1 |\n-1.0000000e-03 | 0  | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 |<\/pre>\n\n\n\n<p>The board &#8216;writes&#8217; the color data from left to right to the selected line, latch resets the write Postion to the beginning of the line.<\/p>\n\n\n\n<p>There are 2 lines writen at the same time its a 5bit adress so that means the first line is some were between 0 and 32 and the second is the same as the first+33<\/p>\n\n\n\n<p>looking at the photo of the matrix broad that seemed to be right.<\/p>\n\n\n\n<p>Now i know what im looking at, i just need to find a way to reproduce what was displayed on the matrix board.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">cleaning up file.<\/h5>\n\n\n\n<p>There were a lot of dupplicate measurements because the sample rate of the oscilloscope was much higher than the bus speed. <\/p>\n\n\n\n<p>So i decided to write a script cleaning out duplicate measurements to reduce file size. while i was doing this i was brain stroming on how to parse the data and display it.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Solution<\/h5>\n\n\n\n<p>I decided it would be fun to output it in a terminal screen. write block Charterers and display and use v100 esacpe codes for color.<\/p>\n\n\n\n<p>This is the code i came up with to pars the oscilloscope log file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/usr\/bin\/python\n# -*- coding: utf-8 -*-\n'''''''''''''''''''''''''''''''' ' ' ' '''''''''''''''''''''''''''''''''''''''''\n\n    \u2584\u2584\u2584\u2584    \u2584\u2584\u2584       \u2588     \u2588\u2591 \u2588\u2588\u2593      \u2588\u2588\u2588\u2588\u2588\u2588 \u2593\u2588\u2588\u2588\u2588\u2588  \u2584\u2588\u2588\u2588\u2588\u2584\n    \u2593\u2588\u2588\u2588\u2588\u2588\u2584 \u2592\u2588\u2588\u2588\u2588\u2584    \u2593\u2588\u2591 \u2588 \u2591\u2588\u2591\u2593\u2588\u2588\u2592    \u2592\u2588\u2588    \u2592 \u2593\u2588   \u2580 \u2592\u2588\u2588\u2580 \u2580\u2588\n    \u2592\u2588\u2588\u2592 \u2584\u2588\u2588\u2592\u2588\u2588  \u2580\u2588\u2584  \u2592\u2588\u2591 \u2588 \u2591\u2588 \u2592\u2588\u2588\u2591    \u2591 \u2593\u2588\u2588\u2584   \u2592\u2588\u2588\u2588   \u2592\u2593\u2588    \u2584\n    \u2592\u2588\u2588\u2591\u2588\u2580  \u2591\u2588\u2588\u2584\u2584\u2584\u2584\u2588\u2588 \u2591\u2588\u2591 \u2588 \u2591\u2588 \u2592\u2588\u2588\u2591      \u2592   \u2588\u2588\u2592\u2592\u2593\u2588  \u2584 \u2592\u2593\u2593\u2584 \u2584\u2588\u2588\u2592\n    \u2591\u2593\u2588  \u2580\u2588\u2593 \u2593\u2588   \u2593\u2588\u2588\u2592\u2591\u2591\u2588\u2588\u2592\u2588\u2588\u2593 \u2591\u2588\u2588\u2588\u2588\u2588\u2588\u2592\u2592\u2588\u2588\u2588\u2588\u2588\u2588\u2592\u2592\u2591\u2592\u2588\u2588\u2588\u2588\u2592\u2592 \u2593\u2588\u2588\u2588\u2580 \u2591\n    \u2591\u2592\u2593\u2588\u2588\u2588\u2580\u2592 \u2592\u2592   \u2593\u2592\u2588\u2591\u2591 \u2593\u2591\u2592 \u2592  \u2591 \u2592\u2591\u2593  \u2591\u2592 \u2592\u2593\u2592 \u2592 \u2591\u2591\u2591 \u2592\u2591 \u2591\u2591 \u2591\u2592 \u2592  \u2591\n    \u2592\u2591\u2592   \u2591   \u2592   \u2592\u2592 \u2591  \u2592 \u2591 \u2591  \u2591 \u2591 \u2592  \u2591\u2591 \u2591\u2592  \u2591 \u2591 \u2591 \u2591  \u2591  \u2591  \u2592\n    \u2591    \u2591   \u2591   \u2592     \u2591   \u2591    \u2591 \u2591   \u2591  \u2591  \u2591     \u2591   \u2591\n    \u2591            \u2591  \u2591    \u2591        \u2591  \u2591      \u2591     \u2591  \u2591\u2591 \u2591\n      \u2591                                            \u2591\n                       -=Bawling since 2017=-           \u2592\u2591\n        \u2592                                       \u2591\n              \u2592\u2591        By: M42D                                \u2591\n            \u2591          CTF: 35c3ctf\n                  challege: box of blink                 \u2591\n            \u2591                                         \u2591\n                      date: 27-12-2018\n\n              usage : cat blink.csv | .\/blink.py\n\n              notes : for best cinematic experiance adjust\n                      your termial window to display a\n                      resolution of 265x64 charcters\n\n                      it's a damm shame the output wasn't\n                      more animated and colorfull.\n\n'''''''''''''''''''''''''''''''' ' ' ' '''''''''''''''''''''''''''''''''''''''''\n\nB,A,W,L='',' ',' ',open(__file__).readlines()\nfor S in range(3,30):\n    B+=L[S]\n    if S>7 and S<13:\n        A=L[S]+A\n        W+=L[S]\nprint '\\033[94m'+B+'\\033[0m'\nPB='\\033[94m'+A+W+'\\033[0m'\nprint \"         sit back 'n enjoy\"\nprint \" \"*40+\"-xXx- M42D\"\nprint PB\nfrom time import sleep\nsleep(5)\n#-------------------------------------------------------------------------------\n# END OF HEADER\n#-------------------------------------------------------------------------------\n\nfile_out = 'catme.txt'\nf = open(file_out, \"w\")\n\nlast_clk = '1'\nlast_oe  = '1'\nlast_lat = '1'\n\ni=0 #string in counter\no=0 #string out counter\n\nx=5 #knowing me x is probebly y\nwhile 1:\n    try:\n        input = raw_input()\n    except:\n        f.close()\n        print 'done..'\n        print 'results saved to catme.txt'\n        break\n    #input skip the whole header section of the file\n    i +=1\n    if i > 24:\n        if input == '' or input[0] == '#':\n            continue\n\n        tt = input.split(',')\n        '''\n        0  = TIMESTAMP\n        1  = OUTPUT ENABLE    --- i guess to select matrix board 1 or 2\n        2  = LATCH                               but we can ignore this\n        3  = CLOCK\n        4  = E    MSB --\\\n        5  = D           \\\n        6  = C            >-- ROW ADDRESS\n        7  = B           \/\n        8  = A    LSB --\/\n        9  = B2  ---\\\n        10 = B1      \\\n        11 = G2       \\____RGB COLORS\n        12 = G1       \/                B1,G1,R1 = top half of matrix\n        13 = R2      \/                 B2,G2,R2 = bottom half of matrix\n        14 = R1  ---\/\n        '''\n        if tt[3] != last_clk :\n            last_clk = tt[3]\n            last_lat = tt[2]\n            last_oe = tt[1]\n\n            adr_1 = int(''.join(tt[4:9]),2)\n            adr_2 = adr_1+32\n\n            '''\n    ive used ansi-vt100 escape sequences to move cursor and change color.\n    for a nice overview check: http:\/\/www.termsys.demon.co.uk\/vtansi.htm\n\n\n            '''\n            color1 = (int(tt[10])*4)+(int(tt[12])*2)+(int(tt[14])*1)\n            color2 = (int(tt[9])*4)+(int(tt[11])*2)+(int(tt[13])*1)\n\n            line1 = \"\\033[\"+str(adr_1)+\";\"+str(x)+\"H\" #place cursor top half\n            line1 +='\\x1b[6;3'+str(color1)+';40m'+'\u2588' #print a colored block\n            line2 = \"\\033[\"+str(adr_2)+\";\"+str(x)+\"H\" #place cursor bottom half\n            line2 +='\\x1b[6;3'+str(color2)+';40m'+'\u2588' #print a colored block\n            print line1+line2\n            f.write(line1+line2) # i know this slows it al down for now\n                                 # but we can just cat the output later on\n                                 # and hopefully see a nice animation in an\n                                 # resonable frame rate.\n            x +=1\n        if tt[2] == \"1\":  #end of line\n                x= 5\n<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">output<\/h5>\n\n\n\n<ul class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/2019-02-14-035047_1440x900_scrot-1024x640.png\" alt=\"\" data-id=\"41\" data-link=\"http:\/\/robertmccallum.nl\/?attachment_id=41\" class=\"wp-image-41\" srcset=\"http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/2019-02-14-035047_1440x900_scrot-1024x640.png 1024w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/2019-02-14-035047_1440x900_scrot-300x188.png 300w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/2019-02-14-035047_1440x900_scrot-768x480.png 768w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/2019-02-14-035047_1440x900_scrot-1200x750.png 1200w, http:\/\/robertmccallum.nl\/wp-content\/uploads\/2019\/02\/2019-02-14-035047_1440x900_scrot.png 1440w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>35C3CTF &#8211; box of blink provided is a 400MB .csv file and a photo of an oscilloscope with logic inputs connected on to data bus between a raspberry pi and 2 chained led matrix boards. Recon after quick examination i realized the csv file was a log of the measurements from the oscilloscope. the beginning &hellip; <a href=\"http:\/\/robertmccallum.nl\/index.php\/2019\/02\/14\/35c3ctf-box-of-blink\/\" class=\"more-link\">Lees <span class=\"screen-reader-text\">&#8220;Terminal led matrix emulator.&#8221;<\/span> verder<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,3],"tags":[13,4],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-35c3ctf","category-writeups","tag-electronics","tag-reverse-engineering"],"_links":{"self":[{"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":13,"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":84,"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/posts\/34\/revisions\/84"}],"wp:attachment":[{"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/robertmccallum.nl\/index.php\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}