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

test feat/learning-hacking-with-golang-20240706 171000 #16

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions golang-basic/exposure-sensitive-information/main.go
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)
}
26 changes: 26 additions & 0 deletions golang-basic/exposure-sensitive-information/main02.go
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.
20 changes: 20 additions & 0 deletions hacking-go/cross-site-request-forgery/index.html
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>
45 changes: 45 additions & 0 deletions hacking-go/cross-site-request-forgery/main.go
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
}
43 changes: 43 additions & 0 deletions hacking-go/cross-site-request-forgery/main1.go
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
}
64 changes: 64 additions & 0 deletions hacking-go/cross-site-request-forgery/main2.go
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.
35 changes: 35 additions & 0 deletions hacking-go/cross-site-request-forgery/readme.md
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 hacking-go/insertion-sensitive-information-sent-data/main.go
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
}
Loading