]> git.somenet.org - pub/jan/ctf-seminar.git/blob - writeups/hah/seccon19.md
Fix Seccon19 formatting
[pub/jan/ctf-seminar.git] / writeups / hah / seccon19.md
1 # SECCON 2019
2 ## Retrospective
3 SECCON offered a nice array of challenges and while I had a quick look at most, I mainly concentrated on a crypto/web and a pwn one. Unfortunately not being able to solve either I spent most of my time on "lazy" which consisted of two stages, the first of which I solved rather quickly, but then getting totally stuck on the second part. While some of the other challenges, especially the stego ones, looked interesting they didn't seem very accessible without having experience in their domain, offering very little hints at possible avenues to test.
4
5 ## lazy
6 * Category: pwn
7 * 332 points
8 * Time spent: ~6 hours
9
10         lazy.chal.seccon.jp 33333
11
12 lazy was a binary challenge whose description only contained a server address. After connecting a simple menu was presented that offered options to login, access Public contents or disconnect. The public area contained part of the challenge source code related to the login:
13
14         #define BUFFER_LENGTH 32
15         #define PASSWORD "XXXXXXXXXX"
16         #define USERNAME "XXXXXXXX"
17         
18         int login(void){
19                 char username[BUFFER_LENGTH];
20                 char password[BUFFER_LENGTH];
21                 char input_username[BUFFER_LENGTH];
22                 char input_password[BUFFER_LENGTH];
23                 
24                 memset(username,0x0,BUFFER_LENGTH);
25                 memset(password,0x0,BUFFER_LENGTH);
26                 memset(input_username,0x0,BUFFER_LENGTH);
27                 memset(input_password,0x0,BUFFER_LENGTH);
28                 
29                 strcpy(username,USERNAME);
30                 strcpy(password,PASSWORD);
31                 
32                 printf("username : ");
33                 input(input_username);
34                 printf("Welcome, %s\n",input_username);
35                 
36                 printf("password : ");
37                 input(input_password);
38                 
39                 
40                 if(strncmp(username,input_username,strlen(USERNAME)) != 0){
41                         puts("Invalid username");
42                         return 0;
43                 }
44                 
45                 if(strncmp(password,input_password,strlen(PASSWORD)) != 0){
46                         puts("Invalid password");
47                         return 0;
48                 }
49                 
50                 return 1;
51         }
52         
53         
54         void input(char *buf){
55             int recv;
56                 int i = 0;
57                 while(1){
58                     recv = (int)read(STDIN_FILENO,&buf[i],1);
59                         if(recv == -1){
60                             puts("ERROR!");
61                                 exit(-1);
62                         }
63                         if(buf[i] == '\n'){
64                             return;
65                         }
66                         i++;
67                 }
68         }
69
70 This code contained to problems: Input parsing was only terminated upon encountering a newline which allows for a buffer overflow and overwriting the login information stored on the stack, and the printf-statement after entering a username could be used to print data from the stack. After some experimentation I had found the necessary padding to print the username and password variables:
71
72         leak_password_payload = "A" * 29 + "%s"
73         leak_username_payload = "A" * 29 + "B" * 32 + "%s"
74
75 Using those the login menu option could be used to access a private content area in which the full binary and a libc file was stored; because files containing dots in their name could not be downloaded and code to prevent directory traversal was present as well.
76 The download prompt contained another printf-vulnerability, and exploring it further I found that some environment variables were stored on the stack, including pwd and the home-directory which was used to define which folder was presented for downloading. Thinking the flag might be stored in another folder I thought about overwriting those paths, but because "/q/private" was appended to it arbitrary path access would still not have been possible. I also considered overwriting the GOT-entry of a linked function to divert program flow and skip some checks in the code and download other files, but because the binary had RELRO enabled (which it took me way to long to remember to check after having worked on the challenge for a while already) this option was out as well. In the end I was unable to get the final step of downloading the flag in time. lazy_solve.py contains a solution up to the final part which automatically downloads the login source code, leaks the username and password and downloads the challenge binary.
77
78 ## ZKPay
79 * Category: Crypto
80 * 345 points
81 * Time spent: ~3 hours
82
83 ZPKay was a crypto challenge consisting of a website that offered registered users to send custom currency to other users by use of generated QR codes. After registration each user was given 500 units of the currency, and 1 million was required to reveal the flag. Registering multiple accounts and pooling the currency was possible but would have taken too long; the intended path seemed to be to fake a QR code that allowed for withdrawal of high enough amounts from the admin account that sent the initial batch to each new user.
84 Generating a couple of QR codes revealed that they encoded the username, the amount to be transferred, a proof and an hash:
85
86         username=w0y&amount=2&proof=MP49LF+ZPCw7Erx0RtFvs9aGKy2eglOEsOmKykBf9acYMSAw2VAOJC++LtkU1VQJLjFgomQpjsTKOnFZEi7SbRSm9y4xCjABuFt915o/jZvc45hTajCjjNDHQZ6/v3vc6jQSEOUSJ3n1huxiOEuhNEYpE0OAsfatSu6ijNz8aidLUXjd6hkXMCAwR02qBLuxG/ZqqJ1YOpPdkclDPgOo7f69GgCN1JBuXBUwCjD99BQC0lBQkDDjsWNBW03uis8/40UkaVtorRTntM8OFDAgMDQMPxacCXL7q+9nxQmSKQBxiAaoa/QPEcLUKsei9KAkMAowEXGyGgNDmfmQi942WhTZkSonB0dK54yt5bQf78FUTyUxCjBxEwWHGKwCK/rGCMvjXGYD02hJewvHqZPG6bgUQGfWGjEK&hash=76bb1b5eb3cb773acdd2e1ce12b02d242ea280f211722e314b06530d55680e88
87
88 Interestingly neither the proof nor the hash seemed to depend on the amount as both stayed the same with every new QR code generated, and encoding payloads with different amounts without the use of the website yielded usable codes as well. Looking around the site the hash turned out to be a user identification, and because the transaction overview on the starting page showed the hashes of trading partners the correct one for the admin account was known.
89
90         function formatTime(timestamp){
91         let dt = new Date(timestamp * 1000);
92         let time = dt.getFullYear() + "/" + (dt.getMonth()+1) + "/" + dt.getDate() + " " + dt.getHours() + ":" + dt.getMinutes();
93         return time;
94         }
95         
96         function getAccountData()
97         {
98         $.get( "/api/accountData", function( data ) {
99                 $("#balance").text(data["balance"]);
100                 $("#flag").text(data["flag"]);
101                 let tBody = $("#trx").find('tbody');
102                 tBody.empty();
103                 for(i=0; i<data["trxList"].length; ++i){
104                 let tr = $("<tr>");
105                 let td = $("<td>");
106                 td.text(formatTime(data["trxList"][i][1]));
107                 tr.append(td);
108                 td = $("<td>");
109                 let a = $("<a>")
110                         .attr("href", "#")
111                         .attr("data-toggle", "popover")
112                         .attr("data-trigger", "hover")
113                         .attr("data-content", "Address is " + data["trxList"][i][3])
114                         .attr("data-placement", "top")
115                         .text(data["trxList"][i][2]);
116                 td.append(a);
117                 tr.append(td);
118                 td = $("<td>");
119                 td.text(data["trxList"][i][5]);
120                 tr.append(td);
121                 tBody.append(tr);
122                 }
123                 $('[data-toggle="popover"]').each(function(i, obj) {
124                 var popover_target = $(this).data("popover-target");
125                 $(this).popover({
126                         html: false,
127                         trigger: "click",
128                         placement: "top",
129                         container: "body",
130                         content: function(obj) {
131                         return $(popover_target).html();
132                         }
133                 });
134                 });
135         });
136         }
137         
138         $(document).ready(function(){
139         getAccountData();
140         });
141         
142         function createQR(){
143         sendData = {};
144         sendData["amount"]   = $("#amount").val();
145         sendData["password"] = $("#password").val();
146         $.ajax({
147                 type:"POST",
148                 url:"/api/createQR",
149                 data:JSON.stringify(sendData),
150                 contentType: "application/json; charset=utf-8",
151                 dataType: "json",
152                 success : function(data) {
153                 if(data["status"] == "ok"){
154                         $("#qr").html( data["dom"] );
155                 }else{
156                         $("#qr").html( data["error"]);
157                 }
158                 }
159         });
160         }
161         
162         function readQR(){
163         fileData = {};
164         fileData = $("#qrImg").prop("files")[0];
165         formData = new FormData();
166         formData.append('file', fileData);
167         $.ajax({
168                 type:"POST",
169                 url:"/api/readQR",
170                 data:formData,
171                 contentType: false,
172                 processData: false,
173                 success : function(data) {
174                 if(data["status"] == "ok"){
175                         Snackbar.show({text: data["dom"]});
176                 }else{
177                         Snackbar.show({text: data["error"]});
178                 }
179                 }
180         });
181         }
182
183 The javascript of the page showed that other than the amount and the user password (as well as the session cookie) no information was sent to the server in order to generate the QR codes, which meant that the proof was generated once and stored serverside or was reconstructed with each request; however I did not find out how to get to the admin proof. Other teams seem to have solved it by simply generating QR codes with negative amounts encoded which would lead to the sending account getting credits added instead of subtracted, but I'm not sure whether that was the intended solution because the challenge name seemed to hint at some kind of zero knowledge-protocol being involved.
184
185 ## Sandstorm
186 * Category: Misc
187 * 279 points
188 * Time spent: ~1 hour
189
190 Sandstorm was a forensics challenge consisting of a grainy black-and-white-picture that contained some short text. Thinking the monochromatic pixels might encode a binary message I tried finding some relevant data using [zsteg](https://github.com/zed-0xff/zsteg) and stegsolve, but nothing interesting turned up. As it [turns out](https://github.com/10secTW/ctf-writeup/tree/master/2019/SECCON%20CTF%20quals/Sandstorm) I was on the wrong track and the image hid a QR code of the flag.