其他
AI驱动的安全扫描工具:HackBot(OpenAI+ Projectdiscovery)
package main
import (
"context"
"fmt"
openai "github.com/sashabaranov/go-openai"
)
func main() {
client := openai.NewClient("your token")
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "Hello!",
},
},
},
)
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return
}
fmt.Println(resp.Choices[0].Message.Content)
}
func (g *GptClient) InitClient(opt *Options) {
config := openai.DefaultConfig(opt.AutoToken)
if opt.Proxy != "" {
proxyURL, err := url.Parse(opt.Proxy)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
httpClient := &http.Client{
Transport: transport,
}
config.HTTPClient = httpClient
}
g.Client = openai.NewClientWithConfig(config)
}
package core
import (
"fmt"
"github.com/projectdiscovery/goflags"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/gologger/levels"
"github.com/projectdiscovery/httpx/runner"
)
type Httpx struct {
Url string `json:"url"`
}
func (h *Httpx) Run() (content string, err error) {
gologger.DefaultLogger.SetMaxLevel(levels.LevelSilent)
options := runner.Options{
Methods: "GET",
InputTargetHost: goflags.StringSlice{h.Url},
FollowRedirects: true,
Silent: true,
OnResult: func(r runner.Result) {
// handle error
if r.Err != nil {
err = r.Err
return
}
content = fmt.Sprintf("Url:[%s] Host:[%s] Title:[%s] Status:[%d] Length:[%d] Favicon:[%s] Technologies:%v",
r.URL, r.Host, r.Title, r.StatusCode, r.ContentLength, r.FavIconMMH3, r.Technologies)
},
}
if err = options.ValidateOptions(); err != nil {
return
}
r, err := runner.New(&options)
if err != nil {
return
}
defer r.Close()
r.RunEnumeration()
return
}
var HTTPXFunc = openai.FunctionDefinition{
Name: "httpx",
Description: "Used for website survival detection and basic website information acquisition, such as website title, HTTP response content, etc.(用于网站存活探测和网站基本信息获取,如网站标题、http响应内容等)",
Parameters: &openai.JSONSchemaDefinition{
Type: openai.JSONSchemaTypeObject,
Properties: map[string]openai.JSONSchemaDefinition{
"url": {
Type: openai.JSONSchemaTypeString,
Description: "destination address(url)",
},
},
Required: []string{"url"},
},
}
var MyFunctions = []openai.FunctionDefinition{
HTTPXFunc,
}
resp, err := g.Client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo0613,
Messages: g.MessageHistory,
Functions: MyFunctions,
FunctionCall: "auto",
},
)
if err != nil {
return
}
respMessage := resp.Choices[0].Message
if respMessage.FunctionCall == nil {
fmt.Println("Error: The input information is too little to call the function")
return
}
funcName := resp.Choices[0].Message.FunctionCall
content, err := FuncCall(funcName)
if err != nil {
return
}
func FuncCall(call *openai.FunctionCall) (content string, err error) {
switch call.Name {
case "httpx":
var f Httpx
if err = json.Unmarshal([]byte(call.Arguments), &f); err != nil {
return
}
content, err = f.Run()
}
return
}
https://openai.com/blog/function-calling-and-other-api-updates https://blog.csdn.net/asplh/article/details/131247412