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

Fetching Robots.txt File on 301 Redirect #25

Merged
merged 4 commits into from
Jun 19, 2024
Merged
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
35 changes: 28 additions & 7 deletions pkg/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,27 @@ import (
"github.com/dropalldatabases/sif/pkg/logger"
)

func Scan(url string, timeout time.Duration, threads int, logdir string) {
func fetchRobotsTXT(url string, client *http.Client) *http.Response {
resp, err := client.Get(url)
if err != nil {
log.Debugf("Error fetching robots.txt: %s", err)
return nil
}

if resp.StatusCode == http.StatusMovedPermanently {
redirectURL := resp.Header.Get("Location")
if redirectURL == "" {
log.Debugf("Redirect location is empty for %s", url)
return nil
}
resp.Body.Close()
return fetchRobotsTXT(redirectURL, client)
}

return resp
}

func Scan(url string, timeout time.Duration, threads int, logdir string) {
fmt.Println(styles.Separator.Render("🐾 Starting " + styles.Status.Render("base url scanning") + "..."))

sanitizedURL := strings.Split(url, "://")[1]
Expand All @@ -39,11 +58,12 @@ func Scan(url string, timeout time.Duration, threads int, logdir string) {
},
}

resp, err := client.Get(url + "/robots.txt")
if err != nil {
log.Debugf("Error: %s", err)
resp := fetchRobotsTXT(url+"/robots.txt", client)
if resp == nil {
return
}
defer resp.Body.Close()

if resp.StatusCode != 404 && resp.StatusCode != 301 && resp.StatusCode != 302 && resp.StatusCode != 307 {
scanlog.Infof("file [%s] found", styles.Status.Render("robots.txt"))

Expand All @@ -70,12 +90,13 @@ func Scan(url string, timeout time.Duration, threads int, logdir string) {
}

_, sanitizedRobot, _ := strings.Cut(robot, ": ")
log.Debugf("%s", robot)
scanlog.Debugf("%s", robot)
resp, err := client.Get(url + "/" + sanitizedRobot)
if err != nil {
log.Debugf("Error %s: %s", sanitizedRobot, err)
return
scanlog.Debugf("Error %s: %s", sanitizedRobot, err)
continue
}
defer resp.Body.Close()

if resp.StatusCode != 404 {
scanlog.Infof("%s from robots: [%s]", styles.Status.Render(strconv.Itoa(resp.StatusCode)), styles.Highlight.Render(sanitizedRobot))
Expand Down
Loading