-
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.
Signed-off-by: yylt <[email protected]>
- Loading branch information
Showing
6 changed files
with
366 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,5 @@ | ||
package main | ||
|
||
import ( | ||
_ "github.com/yylt/gomsf" | ||
) |
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,159 @@ | ||
package gomsf | ||
|
||
import ( | ||
"github.com/vmihailenco/msgpack" | ||
|
||
"fmt" | ||
"strings" | ||
) | ||
|
||
const ( | ||
cslcreate = "console.create" | ||
csllist = "console.list" | ||
cslrm = "console.destroy" | ||
cslread = "console.read" | ||
cslwrite= "console.write" | ||
csltabs = "console.tabs" | ||
cslsskill = "console.session_kill" | ||
cslssdetach = "console.session_detach" | ||
) | ||
|
||
type ConsoleInfo struct { | ||
Id string `msgpack:"id"` | ||
Prompt string `msgpack:"prompt"` | ||
Busy bool `msgpack:"busy"` | ||
} | ||
|
||
type ConsoleDataInfo struct { | ||
Data string `msgpack:"data"` | ||
Prompt string `msgpack:"prompt"` | ||
Busy bool `msgpack:"busy"` | ||
} | ||
|
||
func (m *MsfGo) ConsoleCreate() (*ConsoleInfo,error) { | ||
var ( | ||
bs []byte | ||
err error | ||
token = m.GetToken() | ||
) | ||
if token==""{ | ||
return nil,ErrNotAuth | ||
} | ||
bodys := []string{cslcreate,token} | ||
bs,err=msgpack.Marshal(bodys) | ||
if err!= nil{ | ||
return nil,err | ||
} | ||
bs,err=m.send(false,bs) | ||
if err!= nil{ | ||
return nil,err | ||
} | ||
var ct ConsoleInfo | ||
err=msgpack.Unmarshal(bs,&ct) | ||
if err!= nil{ | ||
return nil,err | ||
} | ||
return &ct,nil | ||
} | ||
|
||
func (m *MsfGo) ConsoleList() ([]*ConsoleInfo,error) { | ||
var ( | ||
bs []byte | ||
err error | ||
token = m.GetToken() | ||
infos []*ConsoleInfo | ||
) | ||
if token==""{ | ||
return nil,ErrNotAuth | ||
} | ||
bodys := []string{csllist,token} | ||
bs,err=msgpack.Marshal(bodys) | ||
if err!= nil{ | ||
return nil,err | ||
} | ||
bs,err=m.send(false,bs) | ||
if err!= nil{ | ||
return nil,err | ||
} | ||
var ct = map[string]*ConsoleInfo{} | ||
err=msgpack.Unmarshal(bs,&ct) | ||
if err!= nil{ | ||
return nil,err | ||
} | ||
for _,v:=range ct{ | ||
infos=append(infos,v) | ||
} | ||
return infos,nil | ||
} | ||
|
||
func (m *MsfGo) ConsoleDestroy(id string) (error) { | ||
var ( | ||
bs []byte | ||
err error | ||
token = m.GetToken() | ||
) | ||
if token==""{ | ||
return ErrNotAuth | ||
} | ||
bodys := []string{cslrm,token,id} | ||
bs,err=msgpack.Marshal(bodys) | ||
if err!= nil{ | ||
return err | ||
} | ||
_,err=m.send(true,bs) | ||
return err | ||
} | ||
|
||
func (m *MsfGo) ConsoleWrite(cmd string) (error) { | ||
var ( | ||
bs []byte | ||
err error | ||
token = m.GetToken() | ||
) | ||
if token==""{ | ||
return ErrNotAuth | ||
} | ||
cmd = fmt.Sprintf("%s\n",cmd) | ||
length := len(cmd) | ||
bodys := []string{cslwrite,token,cmd} | ||
bs,err=msgpack.Marshal(bodys) | ||
if err!= nil{ | ||
return err | ||
} | ||
var ct = map[string]int{} | ||
bs,err=m.send(false,bs) | ||
err=msgpack.Unmarshal(bs,&ct) | ||
if err!= nil{ | ||
return err | ||
} | ||
if v,ok:=ct["wrote"];ok{ | ||
if v==length{ | ||
return nil | ||
} | ||
return fmt.Errorf("wrong length %d",v) | ||
} | ||
return fmt.Errorf("wrong return-type: %v",ct) | ||
} | ||
|
||
func (m *MsfGo) ConsoleRead(id string) (string,error) { | ||
var ( | ||
bs []byte | ||
err error | ||
token = m.GetToken() | ||
) | ||
if token==""{ | ||
return "",ErrNotAuth | ||
} | ||
bodys := []string{cslread,token,id} | ||
bs,err=msgpack.Marshal(bodys) | ||
if err!= nil{ | ||
return "",err | ||
} | ||
var ct ConsoleDataInfo | ||
bs,err=m.send(false,bs) | ||
err=msgpack.Unmarshal(bs,&ct) | ||
if err!= nil{ | ||
return "",err | ||
} | ||
return strings.TrimSpace(ct.Data),nil | ||
} |
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,3 @@ | ||
package gomsf | ||
|
||
|
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,71 @@ | ||
package gomsf | ||
|
||
import ( | ||
"fmt" | ||
"errors" | ||
|
||
"github.com/vmihailenco/msgpack" | ||
"github.com/imroc/req" | ||
) | ||
|
||
type MsfGo struct { | ||
addr string | ||
token string | ||
ssl bool | ||
|
||
httpcli *req.Req | ||
} | ||
|
||
type result struct { | ||
Result string `msgpack:"result"` | ||
} | ||
|
||
var ( | ||
defaultParm = req.Param{ | ||
"Content-Type":"binary/message-pack", | ||
"Content-Length":1024, | ||
} | ||
msfdUrl string | ||
ErrNotAuth = errors.New("not auth") | ||
ErrAuthFailed = errors.New("auth failed") | ||
) | ||
|
||
func NewMsf(addr string,port int,ssl bool) *MsfGo{ | ||
rq:=req.New() | ||
if ssl{ | ||
rq.EnableInsecureTLS(true) | ||
msfdUrl = fmt.Sprintf("https://%s:%d/api/1.0/",addr,port) | ||
}else{ | ||
msfdUrl = fmt.Sprintf("http://%s:%d/api/1.0/",addr,port) | ||
} | ||
|
||
return &MsfGo{ | ||
addr: addr, | ||
ssl:ssl, | ||
httpcli:rq, | ||
} | ||
} | ||
|
||
func (m *MsfGo) send(checkresult bool,cnt []byte) ([]byte,error){ | ||
resq,err:=m.httpcli.Post(msfdUrl,defaultParm,cnt) | ||
if err!= nil { | ||
return nil,err | ||
} | ||
bs:=resq.Bytes() | ||
if checkresult{ | ||
var res result | ||
msgpack.Unmarshal(bs,&res) | ||
if res.Result!= "success"{ | ||
return nil,errors.New(res.Result) | ||
} | ||
} | ||
return bs,nil | ||
} | ||
|
||
func (m *MsfGo) GetToken() string { | ||
return m.token | ||
} | ||
|
||
func (m *MsfGo) SetToken(token string) { | ||
m.token=token | ||
} |
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,121 @@ | ||
package gomsf | ||
|
||
import ( | ||
"github.com/vmihailenco/msgpack" | ||
) | ||
|
||
const ( | ||
authlogin = "auth.login" | ||
authlogou = "auth.logout" | ||
tokenlist = "auth.token_list" | ||
tokenadd = "auth.token_add" | ||
tokengen= "auth.token_generate" | ||
tokenrm = "auth.token_remove" | ||
) | ||
|
||
type tokendt struct { | ||
Result string `msgpack:"result"` | ||
Token string `msgpack:"token"` | ||
} | ||
|
||
type tlistdt struct { | ||
Tokens []string `msgpack:"tokens"` | ||
} | ||
|
||
func (m *MsfGo) Login(user,pass string) (string,error) { | ||
bodys := []string{authlogin,user,pass} | ||
bs,err:=msgpack.Marshal(bodys) | ||
if err!= nil{ | ||
return "",err | ||
} | ||
resp,err:=m.send(true,bs) | ||
if err!= nil{ | ||
return "",err | ||
} | ||
|
||
var auth tokendt | ||
err = msgpack.Unmarshal(resp,&auth) | ||
if err!= nil{ | ||
return "",err | ||
} | ||
return auth.Token,nil | ||
|
||
} | ||
|
||
func (m *MsfGo) Logout() error { | ||
var ( | ||
token = m.GetToken() | ||
) | ||
if token==""{ | ||
return ErrNotAuth | ||
} | ||
bodys := []string{authlogou,token} | ||
bs,err:=msgpack.Marshal(bodys) | ||
if err!= nil{ | ||
return err | ||
} | ||
_,err=m.send(true,bs) | ||
return err | ||
} | ||
|
||
|
||
func (m *MsfGo) TokenAdd(newt string) error { | ||
var ( | ||
token = m.GetToken() | ||
) | ||
if token==""{ | ||
return ErrNotAuth | ||
} | ||
bodys := []string{tokenadd,newt} | ||
bs,err:=msgpack.Marshal(bodys) | ||
if err!= nil{ | ||
return err | ||
} | ||
_,err=m.send(true,bs) | ||
return err | ||
} | ||
|
||
func (m *MsfGo) TokenGen( ) (string,error) { | ||
var ( | ||
token = m.GetToken() | ||
) | ||
if token==""{ | ||
return "",ErrNotAuth | ||
} | ||
bodys := []string{tokengen,token} | ||
bs,err:=msgpack.Marshal(bodys) | ||
if err!= nil{ | ||
return "",err | ||
} | ||
var auth tokendt | ||
err = msgpack.Unmarshal(bs,&auth) | ||
if err!= nil{ | ||
return "",err | ||
} | ||
return auth.Token,nil | ||
} | ||
|
||
|
||
func (m *MsfGo) TokenList() ([]string,error) { | ||
var ( | ||
token = m.GetToken() | ||
) | ||
if token==""{ | ||
return nil,ErrNotAuth | ||
} | ||
bodys := []string{tokenlist,token} | ||
bs,err:=msgpack.Marshal(bodys) | ||
if err!= nil{ | ||
return nil,err | ||
} | ||
resp,err:=m.send(false,bs) | ||
if err!= nil{ | ||
return nil,err | ||
} | ||
var t tlistdt | ||
err = msgpack.Unmarshal(resp,&t) | ||
if err!= nil{ | ||
return nil,err | ||
} | ||
return t.Tokens,nil | ||
} |
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,7 @@ | ||
## gomsf | ||
|
||
- metasploit rpcd golang library | ||
|
||
## reference | ||
|
||
- https://metasploit.help.rapid7.com/docs/standard-api-methods-reference |