Skip to content

Add Wrapper Package with a Couple of Existing Examples #380

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import (
"sync/atomic"
"time"

"github.com/Masterminds/semver"
"github.com/vulncheck-oss/go-exploit/c2"
"github.com/vulncheck-oss/go-exploit/c2/channel"
"github.com/vulncheck-oss/go-exploit/cli"
Expand Down Expand Up @@ -428,6 +429,27 @@ func StoreVersion(conf *config.Config, version string) {
db.UpdateVerified(conf.Product, true, version, conf.Rhost, conf.Rport)
}

// Compare a version to a semantic version constraint using the [Masterminds semver constraints](https://github.com/Masterminds/semver?tab=readme-ov-file#checking-version-constraints).
// Provide a version string and a constraint and if the semver is within the constraint a boolean
// response of whether the version is constrained or not will occur. Any errors from the constraint
// or version will propagate through the framework errors and the value will be false.
func CheckSemVer(version string, constraint string) bool {
c, err := semver.NewConstraint(constraint)
if err != nil {
output.PrintfFrameworkError("Invalid constraint: %s", err.Error())

return false
}
v, err := semver.NewVersion(version)
if err != nil {
output.PrintfFrameworkError("Invalid version: %s", err.Error())

return false
}

return c.Check(v)
}

// modify godebug to re-enable old cipher suites that were removed in 1.22. This does have implications for our
// client fingerprint, and we should consider how to improve/fix that in the future. We also should be respectful
// of other disabling this feature, so we will check for it before re-enabling it.
Expand Down
34 changes: 34 additions & 0 deletions framework_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package exploit_test

import (
"testing"

"github.com/vulncheck-oss/go-exploit"
)

func TestCheckSemVer_Full(t *testing.T) {
if !exploit.CheckSemVer("1.0.0", "<= 1.0.0") {
t.Error("Constraint should have passed")
}
if exploit.CheckSemVer("1.0.0", "> 1.0.0") {
t.Error("Constraint should not have passed")
}
}

func TestCheckSemVer_BadVersion(t *testing.T) {
if exploit.CheckSemVer("uwu", "<= 1.0.0") {
t.Error("Version was invalid, should not have passed")
}
if exploit.CheckSemVer("1.0.0 ", "<= 1.0.0") {
t.Error("Version was invalid, should not have passed")
}
}

func TestCheckSemVer_BadConstraint(t *testing.T) {
if exploit.CheckSemVer("1.0.0", "<== 1.0.0") {
t.Error("Constraint was invalid, should not have passed")
}
if exploit.CheckSemVer("1.0.0", "xp") {
t.Error("Constraint was invalid, should not have passed")
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
)

require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
Expand Down
68 changes: 68 additions & 0 deletions wrappers/wrappers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package wrappers

import(
"regexp"
"net/http"

"github.com/vulncheck-oss/go-exploit"
"github.com/vulncheck-oss/go-exploit/config"
"github.com/vulncheck-oss/go-exploit/output"
"github.com/vulncheck-oss/go-exploit/protocol"
"github.com/vulncheck-oss/go-exploit/c2/channel"
"github.com/vulncheck-oss/go-exploit/c2/httpservefile"
)

// A collection of wrappers/helper functions that have otherwise no place to go and may need to be in their own packages
// to avoid certain import cycle errors.

// Helper function for use within exploits to reduce the overall amount of boilerplate when setting up a file server to host a dynamically generated file.
func HTTPServeFileInitAndRunWithFile(conf *config.Config, fileName string, routeName string, data[]byte) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk where to put this but this probably shouldn't be wrappers

httpServer := httpservefile.GetInstance()

if httpServer.HTTPAddr == "" {
httpServer.HTTPAddr = conf.Lhost
}

if !httpServer.Init(&channel.Channel{HTTPAddr: httpServer.HTTPAddr, HTTPPort: httpServer.HTTPPort}) {
output.PrintFrameworkError("Could not start http server")

return false
}

httpServer.AddFile(fileName, routeName, data)

go httpServer.Run(conf.C2Timeout)

return true
}

// This removes generic version checking boiler plate that works in a great deal of cases.
// Of course it will not work in every case but that is not the point.
func SimpleVersionCheck(conf *config.Config, rePattern string, versionConstraint string, endpoint string) exploit.VersionCheckType {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move this to the same as CheckSemVer in go-exploit

url := protocol.GenerateURL(conf.Rhost, conf.Rport, conf.SSL, endpoint)
resp, body, ok := protocol.HTTPGetCache(url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting gone wrong here and elsewhere in the file

if !ok || resp == nil {
return exploit.Unknown
}

if resp.StatusCode != http.StatusOK {
output.PrintfFrameworkError("Version check failed: Unexpected response code during initial GET request: %d", resp.StatusCode)

return exploit.Unknown
}

matches := regexp.MustCompile(rePattern).FindStringSubmatch(body)
if len(matches) < 2 {
output.PrintFrameworkError("Version check failed: No matches found for the provided pattern")

return exploit.Unknown
}

exploit.StoreVersion(conf, matches[1])

if exploit.CheckSemVer(matches[1], versionConstraint) {
return exploit.Vulnerable
}

return exploit.NotVulnerable
}