-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from tainguyenbp/feat/learning-hacking-with-go…
…lang-20240706 test feat/learning-hacking-with-golang-20240706 171000
- Loading branch information
Showing
14 changed files
with
542 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// package main | ||
|
||
// import ( | ||
// "fmt" | ||
// "net/http" | ||
// ) | ||
|
||
// func main() { | ||
// http.HandleFunc("/users", getUsers) | ||
// http.ListenAndServe(":8080", nil) | ||
// } | ||
|
||
// func getUsers(w http.ResponseWriter, r *http.Request) { | ||
// // Access sensitive data from the database | ||
// username := "admin" | ||
// password := "secret" | ||
|
||
// // Return the sensitive information in the HTTP response | ||
// fmt.Fprintf(w, "Username: %s, Password: %s", username, password) | ||
// } | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/users", getUsers) | ||
log.Println("Starting server on :8080") | ||
if err := http.ListenAndServe(":8080", nil); err != nil { | ||
log.Fatalf("Server failed to start: %v", err) | ||
} | ||
} | ||
|
||
func getUsers(w http.ResponseWriter, r *http.Request) { | ||
// Access sensitive data from the database | ||
username := "admin" | ||
password := "secret" | ||
|
||
// Log the request | ||
log.Printf("Received request for /users from %s", r.RemoteAddr) | ||
|
||
// Return the sensitive information in the HTTP response | ||
fmt.Fprintf(w, "Username: %s, Password: %s", username, password) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/users", getUsers) | ||
log.Println("Starting server on :8080") | ||
if err := http.ListenAndServe(":8080", nil); err != nil { | ||
log.Fatalf("Server failed to start: %v", err) | ||
} | ||
} | ||
|
||
func getUsers(w http.ResponseWriter, r *http.Request) { | ||
// Access sensitive data from the database | ||
// username := "admin" | ||
// password := "secret" | ||
|
||
// Instead of returning sensitive information, return a generic message | ||
fmt.Fprint(w, "Access denied") | ||
log.Printf("Received request for /users from Access denied", r.RemoteAddr) | ||
|
||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Money Transfer</title> | ||
</head> | ||
<body> | ||
<h1>Money Transfer</h1> | ||
<form action="/" method="post"> | ||
<label for="amount">Amount:</label> | ||
<input type="text" id="amount" name="amount" required> | ||
<br> | ||
<label for="account">Account:</label> | ||
<input type="text" id="account" name="account" required> | ||
<br> | ||
<input type="submit" value="Transfer"> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
var ( | ||
templates = template.Must(template.ParseFiles("index.html")) | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", indexHandler) | ||
http.HandleFunc("/transfer", transferHandler) | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
func indexHandler(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == http.MethodGet { | ||
templates.ExecuteTemplate(w, "index.html", nil) | ||
} else if r.Method == http.MethodPost { | ||
amount := r.FormValue("amount") | ||
account := r.FormValue("account") | ||
|
||
// Perform the money transfer | ||
if transferMoney(amount, account) { | ||
fmt.Fprintln(w, "Transfer successful!") | ||
} else { | ||
fmt.Fprintln(w, "Transfer failed!") | ||
} | ||
} | ||
} | ||
|
||
func transferHandler(w http.ResponseWriter, r *http.Request) { | ||
// Process transfer request | ||
// ... | ||
} | ||
|
||
func transferMoney(amount, account string) bool { | ||
// Perform money transfer logic | ||
// ... | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
var ( | ||
templates = template.Must(template.ParseFiles("index.html")) | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", indexHandler) | ||
http.HandleFunc("/transfer", transferHandler) | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
func indexHandler(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == http.MethodGet { | ||
templates.ExecuteTemplate(w, "index.html", nil) | ||
} else if r.Method == http.MethodPost { | ||
amount := r.FormValue("amount") | ||
account := r.FormValue("account") | ||
|
||
// Perform the money transfer | ||
if transferMoney(amount, account) { | ||
fmt.Fprintln(w, "Transfer successful!") | ||
} else { | ||
fmt.Fprintln(w, "Transfer failed!") | ||
} | ||
} | ||
} | ||
|
||
func transferHandler(w http.ResponseWriter, r *http.Request) { | ||
// Additional transfer processing logic can be placed here if needed | ||
} | ||
|
||
func transferMoney(amount, account string) bool { | ||
// Simulate a successful transfer | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gorilla/csrf" | ||
) | ||
|
||
var ( | ||
templates = template.Must(template.ParseFiles("index.html")) | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/", indexHandler) | ||
http.HandleFunc("/transfer", transferHandler) | ||
log.Fatal(http.ListenAndServe(":8080", csrf.Protect([]byte("32-byte-long-auth-key"))(nil))) | ||
} | ||
|
||
func indexHandler(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == http.MethodGet { | ||
token := csrf.Token(r) | ||
data := struct { | ||
Token string | ||
}{ | ||
Token: token, | ||
} | ||
templates.ExecuteTemplate(w, "index.html", data) | ||
} else if r.Method == http.MethodPost { | ||
if err := r.ParseForm(); err != nil { | ||
http.Error(w, "Bad Request", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// Validate CSRF token | ||
if err := csrf.Protect([]byte("32-byte-long-auth-key")).VerifyToken(csrf.Token(r)); err != nil { | ||
http.Error(w, "Invalid CSRF token", http.StatusForbidden) | ||
return | ||
} | ||
|
||
amount := r.FormValue("amount") | ||
account := r.FormValue("account") | ||
|
||
// Perform the money transfer | ||
if transferMoney(amount, account) { | ||
fmt.Fprintln(w, "Transfer successful!") | ||
} else { | ||
fmt.Fprintln(w, "Transfer failed!") | ||
} | ||
} | ||
} | ||
|
||
func transferHandler(w http.ResponseWriter, r *http.Request) { | ||
// Process transfer request | ||
// ... | ||
} | ||
|
||
func transferMoney(amount, account string) bool { | ||
// Perform money transfer logic | ||
// ... | ||
return false | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
### go run main.go | ||
``` | ||
go run main.go | ||
curl http://localhost:8080 | ||
curl -X POST -d "amount=100" -d "account=12345" http://localhost:8080 | ||
``` | ||
|
||
### go run main1.go | ||
``` | ||
go run main1.go | ||
curl http://localhost:8080 | ||
curl -X POST -d "amount=100" -d "account=12345" http://localhost:8080 | ||
``` | ||
### go run main2.go | ||
``` | ||
go run main2.go | ||
curl http://localhost:8080 | ||
curl -X POST -d "amount=100" -d "account=12345" http://localhost:8080 | ||
``` | ||
### go run main3.go | ||
``` | ||
go run main3.go | ||
curl http://localhost:8080 | ||
curl -X POST -d "amount=100" -d "account=12345" http://localhost:8080 | ||
``` |
37 changes: 37 additions & 0 deletions
37
hacking-go/insertion-sensitive-information-sent-data/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
http.HandleFunc("/login", login) | ||
http.ListenAndServe(":8080", nil) | ||
} | ||
|
||
func login(w http.ResponseWriter, r *http.Request) { | ||
username := r.FormValue("username") | ||
password := r.FormValue("password") | ||
|
||
// Authenticate the user | ||
if !authenticate(username, password) { | ||
errMsg := fmt.Sprintf("Login failed for user: %s", username) | ||
log.Println(errMsg) | ||
http.Error(w, "Invalid credentials", http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
// Proceed with successful login | ||
// ... | ||
// Code for handling successful login | ||
} | ||
|
||
func authenticate(username, password string) bool { | ||
// Perform authentication logic | ||
// ... | ||
// Code for authenticating the user | ||
|
||
return false | ||
} |
Oops, something went wrong.