Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 3: Remote code execution vulnerability #79

Open
mtlynch opened this issue Aug 14, 2022 · 0 comments
Open

Chapter 3: Remote code execution vulnerability #79

mtlynch opened this issue Aug 14, 2022 · 0 comments

Comments

@mtlynch
Copy link

mtlynch commented Aug 14, 2022

Chapter 3 features a directory traversal vulnerability, which, combined with #78 leads to remote code execution.

The vulnerability is on this line:

filename := path.Join("avatars", userID+path.Ext(header.Filename))

Both userID and header.Filename are attacker-controlled values, so if the attacker uploads a malicious binary and specifies a userid value of ../chat and a filename of foo, the resulting filename is chat, which causes the next line to overwrite the chat server binary with arbitrary attacker-controlled code.

err = ioutil.WriteFile(filename, data, 0777)

The next time the server restarts, it will execute attacker controlled code.

This vulnerability also allows the attacker to overwrite any file on the system as long as the user executing the chat server has write access to those files.

Recommended fix

You can prevent directory traversal by verifying that the userid parameter doesn't contain any path traversal characters. In other words, if the basename is the same as the string:

userID := req.FormValue("userid")
if userID != filepath.Base(userID) {
	http.Error(w, "userid contains illegal file path characters", http.StatusBadRequest)
	return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant