diff --git a/env.example b/.env.example similarity index 100% rename from env.example rename to .env.example diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml deleted file mode 100644 index 3b777ef..0000000 --- a/.github/FUNDING.yml +++ /dev/null @@ -1,12 +0,0 @@ -# These are supported funding model platforms - -github: jayofelony # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] -patreon: -open_collective: # Replace with a single Open Collective username -ko_fi: # Replace with a single Ko-fi username -tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel -community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry -liberapay: # Replace with a single Liberapay username -issuehunt: # Replace with a single IssueHunt username -otechie: # Replace with a single Otechie username -custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] diff --git a/.gitignore b/.gitignore index 75b968a..a5caaa1 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,3 @@ test-unit.pub build id_rsa id_rsa.pub -key.json diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4135322 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,96 @@ +sudo: false +os: linux +dist: bionic +language: go +go: +- 1.13.x + +env: + global: + - LANG=C + - LC_ALL=C + - OUTPUT="shikigrid" + - VERSION=$(echo ${TRAVIS_BRANCH} | sed "s/\//_/g") + +cache: + apt: true +addons: + apt: + packages: + - wget + - p7zip-full + - libpcap-dev + update: true + +cross: &cross + before_install: + - wget --show-progress -qcO "qemu.deb" "https://debian.grena.ge/debian/pool/main/q/qemu/qemu-user-static_4.1-1+b4_amd64.deb" + - sudo dpkg -i "qemu.deb" + install: + - sudo builder/arm_builder.sh shikigrid make -e TARGET="${OUTPUT}" + +normal: &normal + install: + - make -e TARGET="${OUTPUT}" + +end: &end + after_success: + - sudo mv "build/${OUTPUT}" "${OUTPUT}" + - file "${OUTPUT}" + - openssl dgst -sha256 "${OUTPUT}" | tee "${OUTPUT}_${TARGET_OS}_${TARGET_ARCH}_${VERSION}.sha256" + - 7z a "${OUTPUT}_${TARGET_OS}_${TARGET_ARCH}_${VERSION}.zip" "${OUTPUT}" "${OUTPUT}_${TARGET_OS}_${TARGET_ARCH}_${VERSION}.sha256" + +matrix: + include: + - name: Linux - amd64 + if: tag IS present + arch: amd64 + env: + - TARGET_OS=linux + - TARGET_ARCH=amd64 + <<: *normal + <<: *end + - name: Linux - aarch64 + if: tag IS present + arch: arm64 + env: + - TARGET_OS=linux + - TARGET_ARCH=aarch64 + <<: *normal + <<: *end + - name: Linux - armhf + if: tag IS present + arch: amd64 + language: minimal + env: + - TARGET_OS=linux + - TARGET_ARCH=armhf + <<: *cross + <<: *end + # Tests + # - name: Linux - tests + # if: tag IS blank + # os: linux + # arch: amd64 + # install: + # - make deps + # script: + # - make test + # after_success: + # - bash <(curl -s https://codecov.io/bash) + +deploy: + provider: releases + api_key: + secure: ljBVe/wVAtOPwCWJPlJ7D1hWGfm6GtHOLgq3wmP4jw/9a2RYV41xJ7g+4R1mm9R8waqtTm9QPDHIKFuN3N9cNs83ZY/fkSJ2WwU3IDV1ZvKPAuucrMSsyOGc08poXj6mmUDs/9LRb100qG81Y5dD+WB6Ep6vWOT7aOi9QNp/WWQ3IDYp5QJIocRHcJhGFH8JO1699mpdNgaukmPHIYK6uVu15TCkYOrvNTD0OTpthN6hIwCBwQ0agFNBbqmwyYsAdUZsjdU7QVOCnPUeXWqoZwq3klFKymsf8f4xra7ou5hsBkL+GFESiGGy0TdU7ZTZjPRKgkpIWtHOURq3WSVtYvCTnKI8h+HdBbKlQeO5g611gUw3CEU5HZxKlG18JTSD5TJNuEAFBVA7X385cVnWbgCLIwLiCDzjKPeVJvyDxyKC1CCtmfYZtanyn18qz/VRtMWrLFRcr5jNrQcloiuJbQzteoxtvbt5c0nM7b1b/AZ4zzGH75MLADxbHw2CThN4R+dxx3lqD0YM3fxbWiWCgZlbrc7GNRq1ilhX0YEDBOVfcdJxYARwrzovrO8bMFGerur4C7HzkpVgE6jfQiZJdXU/5javkLnww6xjDC/jfiMJ7i4OqZ2sgISSVL8Fq4LMqaAumdSHZK/GoJ97PTWUE9sBD7tIGSzHReA8DhpweFI= + skip_cleanup: true + file_glob: true + file: + - shikigrid_*.zip + - shikigrid_*.sha256 + on: + tags: true + repo: evilsocket/shikigrid + branches: + only: + - "/^v[0-9]+\\.[0-9]+\\.[0-9]+[A-Za-z0-9]+?$/" diff --git a/Dockerfile b/Dockerfile index 098563a..8aba81f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM golang:alpine as builder # ENV GO111MODULE=on -LABEL maintainer="Anda Toshiki " +LABEL maintainer="Simone Margaritelli " RUN apk update && apk add --no-cache git diff --git a/Makefile b/Makefile index 6ff509e..0de09b9 100644 --- a/Makefile +++ b/Makefile @@ -18,18 +18,18 @@ clean: restart: @service shikigrid restart -release_files: clean cross_compile_libpcap_arm64 # cross_compile_libpcap_arm +release_files: clean cross_compile_libpcap_x64 cross_compile_libpcap_arm @mkdir build @echo building for linux/amd64 ... @CGO_ENABLED=1 CC=x86_64-linux-gnu-gcc GOARCH=amd64 GOOS=linux go build -o build/shikigrid cmd/shikigrid/*.go @openssl dgst -sha256 "build/shikigrid" > "build/shikigrid-amd64.sha256" @zip -j "build/shikigrid-$(VERSION)-amd64.zip" build/shikigrid build/shikigrid-amd64.sha256 > /dev/null @rm -rf build/shikigrid build/shikigrid-amd64.sha256 - @echo building for linux/armv6l ... + @echo building for linux/armhf ... @CGO_ENABLED=1 CC=arm-linux-gnueabi-gcc GOARM=6 GOARCH=arm GOOS=linux go build -o build/shikigrid cmd/shikigrid/*.go - @openssl dgst -sha256 "build/shikigrid" > "build/shikigrid-armv6l.sha256" - @zip -j "build/shikigrid-$(VERSION)-armv6l.zip" build/shikigrid build/shikigrid-armv6l.sha256 > /dev/null - @rm -rf build/shikigrid build/shikigrid-armv6l.sha256 + @openssl dgst -sha256 "build/shikigrid" > "build/shikigrid-armhf.sha256" + @zip -j "build/shikigrid-$(VERSION)-armhf.zip" build/shikigrid build/shikigrid-armhf.sha256 > /dev/null + @rm -rf build/shikigrid build/shikigrid-armhf.sha256 @echo building for linux/aarch64 ... @CGO_ENABLED=1 CC=aarch64-linux-gnu-gcc GOARCH=arm64 GOOS=linux go build -o build/shikigrid cmd/shikigrid/*.go @openssl dgst -sha256 "build/shikigrid" > "build/shikigrid-aarch64.sha256" @@ -37,9 +37,9 @@ release_files: clean cross_compile_libpcap_arm64 # cross_compile_libpcap_arm @rm -rf build/shikigrid build/shikigrid-aarch64.sha256 @ls -la build -# requires sudo apt-get install bison flex gcc-arm-linux-gnueabi libpcap0.8 libpcap-dev +# requires sudo apt-get install bison flex gcc-arm-linux-gnueabihf cross_compile_libpcap_arm: - @echo "Cross-compiling libpcap for armv6l..." + @echo "Cross-compiling libpcap for armhf..." @wget https://www.tcpdump.org/release/libpcap-1.9.1.tar.gz @tar -zxvf libpcap-1.9.1.tar.gz @cd libpcap-1.9.1 && \ @@ -51,18 +51,18 @@ cross_compile_libpcap_arm: @echo "Clean up..." @rm -rf libpcap-1.9.1 libpcap-1.9.1.tar.gz -# requires sudo apt-get install bison flex gcc-aarch64-linux-gnu libpcap0.8 libpcap-dev -cross_compile_libpcap_arm64: - @echo "Cross-compiling libpcap for arm64..." +# requires sudo apt-get install bison flex gcc-x86-64-linux-gnu +cross_compile_libpcap_x64: + @echo "Cross-compiling libpcap for armhf..." @wget https://www.tcpdump.org/release/libpcap-1.9.1.tar.gz @tar -zxvf libpcap-1.9.1.tar.gz @cd libpcap-1.9.1 && \ - export CC=aarch64-linux-gnu-gcc && \ - ./configure --host=aarch64-linux-gnu && \ + export CC=x86_64-linux-gnu-gcc && \ + ./configure --host=x86_64-linux-gnu && \ make @echo "Copying cross-compiled libpcap to /usr/lib/x86_64-linux-gnu/" - @sudo cp libpcap-1.9.1/libpcap.a /usr/lib/aarch64-linux-gnu/ + @sudo cp libpcap-1.9.1/libpcap.a /usr/lib/x86_64-linux-gnu/ @echo "Clean up..." @rm -rf libpcap-1.9.1 libpcap-1.9.1.tar.gz -.PHONY: cross_compile_libpcap_arm cross_compile_libpcap_arm64 \ No newline at end of file +.PHONY: cross_compile_libpcap_arm cross_compile_libpcap_x64 \ No newline at end of file diff --git a/api/client.go b/api/client.go index 11026c7..70453f7 100644 --- a/api/client.go +++ b/api/client.go @@ -5,8 +5,7 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io" - "net" + "io/ioutil" "net/http" "os" "sync" @@ -20,7 +19,6 @@ import ( var ( ClientTimeout = 60 - ClientKeepalive = 30 ClientTokenFile = "/tmp/shikigrid-api-enrollment.json" Endpoint = "" ) @@ -32,34 +30,20 @@ var ( type Client struct { sync.Mutex - cli *http.Client - keys *crypto.KeyPair - token string - tokenAt time.Time - data map[string]interface{} - hostname string + cli *http.Client + keys *crypto.KeyPair + token string + tokenAt time.Time + data map[string]interface{} } -func NewClient(keys *crypto.KeyPair, endpoint string, hostname string) *Client { - - t := &http.Transport{ - Dial: (&net.Dialer{ - Timeout: time.Duration(ClientTimeout) * time.Second, - KeepAlive: time.Duration(ClientKeepalive) * time.Second, - }).Dial, - TLSHandshakeTimeout: time.Duration(ClientTimeout) * time.Second, - ResponseHeaderTimeout: time.Duration(ClientTimeout) * time.Second, - ExpectContinueTimeout: 4 * time.Second, - } - +func NewClient(keys *crypto.KeyPair, endpoint string) *Client { cli := &Client{ cli: &http.Client{ - Transport: t, - Timeout: time.Duration(ClientTimeout) * time.Second, + Timeout: time.Duration(ClientTimeout) * time.Second, }, - keys: keys, - data: make(map[string]interface{}), - hostname: hostname, + keys: keys, + data: make(map[string]interface{}), } Endpoint = endpoint @@ -68,7 +52,7 @@ func NewClient(keys *crypto.KeyPair, endpoint string, hostname string) *Client { if time.Since(info.ModTime()) < models.TokenTTL { log.Debug("loading token from %s ...", ClientTokenFile) var data map[string]interface{} - if raw, err := os.ReadFile(ClientTokenFile); err == nil { + if raw, err := ioutil.ReadFile(ClientTokenFile); err == nil { if err := json.Unmarshal(raw, &data); err == nil { cli.token = data["token"].(string) cli.tokenAt = info.ModTime() @@ -88,12 +72,7 @@ func NewClient(keys *crypto.KeyPair, endpoint string, hostname string) *Client { } func (c *Client) enroll() error { - - hostname := c.hostname - if hostname == "" { - hostname = utils.Hostname() - } - identity := fmt.Sprintf("%s@%s", hostname, c.keys.FingerprintHex) + identity := fmt.Sprintf("%s@%s", utils.Hostname(), c.keys.FingerprintHex) log.Debug("refreshing api token as %s ...", identity) @@ -125,7 +104,7 @@ func (c *Client) enroll() error { if raw, err := json.Marshal(obj); err == nil { log.Debug("saving token to %s ...", ClientTokenFile) - if err = os.WriteFile(ClientTokenFile, raw, 0644); err != nil { + if err = ioutil.WriteFile(ClientTokenFile, raw, 0644); err != nil { log.Warning("error saving token to %s: %v", ClientTokenFile, err) } } else { @@ -172,20 +151,19 @@ func (c *Client) request(method string, path string, data interface{}, auth bool if err != nil { return nil, err } - body, err := io.ReadAll(res.Body) + body, err := ioutil.ReadAll(res.Body) if err != nil { return nil, err } var obj map[string]interface{} if err = json.Unmarshal(body, &obj); err != nil { - log.Debug(fmt.Sprintf("Error Unmarshalling json body from request: %v", body)) return nil, err } if res.StatusCode == 401 { if err := c.enroll(); err != nil { - log.Warning("error token expired during operation: %v", err) + log.Warning("error token expired failed to re-enroll: %v", err) return nil, err } log.Warning("token expired, re-enroll success") diff --git a/api/peer_data.go b/api/peer_data.go index 6fe0b0c..195fb52 100644 --- a/api/peer_data.go +++ b/api/peer_data.go @@ -3,20 +3,20 @@ package api import ( "encoding/json" "github.com/evilsocket/islazy/log" - "io" + "io/ioutil" "net/http" ) -// PeerGetData GET /api/v1/data +// GET /api/v1/data func (api *API) PeerGetData(w http.ResponseWriter, r *http.Request) { JSON(w, http.StatusOK, api.Client.Data()) } -// PeerSetData POST /api/v1/data +// POST /api/v1/data func (api *API) PeerSetData(w http.ResponseWriter, r *http.Request) { var newData map[string]interface{} - body, err := io.ReadAll(r.Body) + body, err := ioutil.ReadAll(r.Body) if err != nil { ERROR(w, http.StatusUnprocessableEntity, err) return diff --git a/api/peer_inbox.go b/api/peer_inbox.go index 963144f..c4601e1 100644 --- a/api/peer_inbox.go +++ b/api/peer_inbox.go @@ -8,7 +8,7 @@ import ( "github.com/go-chi/chi/v5" "github.com/andatoshiki/shikigrid/crypto" "github.com/andatoshiki/shikigrid/models" - "io" + "io/ioutil" "net/http" "strconv" ) @@ -18,7 +18,7 @@ var ( ErrSenderNotFound = errors.New("sender not found") ) -// PeerGetInbox /api/v1/inbox/ +// /api/v1/inbox/ func (api *API) PeerGetInbox(w http.ResponseWriter, r *http.Request) { page, err := pageNum(r) if err != nil { @@ -175,7 +175,7 @@ func (api *API) SendMessage(fingerprint string, cleartext []byte) (int, error) { // POST /api/v1/unit//inbox func (api *API) PeerSendMessageTo(w http.ResponseWriter, r *http.Request) { - cleartextMessage, err := io.ReadAll(r.Body) + cleartextMessage, err := ioutil.ReadAll(r.Body) if err != nil { log.Error("error reading request body: %v", err) ERROR(w, http.StatusUnprocessableEntity, err) diff --git a/api/peer_mesh.go b/api/peer_mesh.go index 2f3978c..f8f2282 100644 --- a/api/peer_mesh.go +++ b/api/peer_mesh.go @@ -2,16 +2,15 @@ package api import ( "encoding/json" - "io" - "net/http" - "sort" - "github.com/evilsocket/islazy/log" "github.com/go-chi/chi/v5" "github.com/andatoshiki/shikigrid/mesh" + "io/ioutil" + "net/http" + "sort" ) -// PeerGetPeers GET /api/v1/mesh/peers +// GET /api/v1/mesh/peers func (api *API) PeerGetPeers(w http.ResponseWriter, r *http.Request) { peers := make([]*mesh.Peer, 0) mesh.Peers.Range(func(key, value interface{}) bool { @@ -27,7 +26,7 @@ func (api *API) PeerGetPeers(w http.ResponseWriter, r *http.Request) { JSON(w, http.StatusOK, peers) } -// PeerGetMemory GET /api/v1/mesh/memory +// GET /api/v1/mesh/memory func (api *API) PeerGetMemory(w http.ResponseWriter, r *http.Request) { peers := api.Mesh.Memory() // higher number of encounters first @@ -37,7 +36,7 @@ func (api *API) PeerGetMemory(w http.ResponseWriter, r *http.Request) { JSON(w, http.StatusOK, peers) } -// PeerGetMemoryOf GET /api/v1/mesh/memory/ +// GET /api/v1/mesh/memory/ func (api *API) PeerGetMemoryOf(w http.ResponseWriter, r *http.Request) { fingerprint := chi.URLParam(r, "fingerprint") peer := api.Mesh.MemoryOf(fingerprint) @@ -48,7 +47,7 @@ func (api *API) PeerGetMemoryOf(w http.ResponseWriter, r *http.Request) { JSON(w, http.StatusOK, peer) } -// PeerSetSignaling GET /api/v1/mesh/ +// GET /api/v1/mesh/ func (api *API) PeerSetSignaling(w http.ResponseWriter, r *http.Request) { status := chi.URLParam(r, "status") @@ -66,24 +65,16 @@ func (api *API) PeerSetSignaling(w http.ResponseWriter, r *http.Request) { }) } -// PeerGetMeshData GET /api/v1/mesh/data +// GET /api/v1/mesh/data func (api *API) PeerGetMeshData(w http.ResponseWriter, r *http.Request) { JSON(w, http.StatusOK, api.Peer.Data()) } -// PeerSetMeshData POST /api/v1/mesh/data +// POST /api/v1/mesh/data func (api *API) PeerSetMeshData(w http.ResponseWriter, r *http.Request) { var newData map[string]interface{} - if api.Peer.ForceDisabled == true { - api.Peer.Advertise(false) - JSON(w, http.StatusOK, map[string]interface{}{ - "success": true, // this should be changed later when shikigotchi can handle shikigrid being force advertise disabled - }) - return - } - - body, err := io.ReadAll(r.Body) + body, err := ioutil.ReadAll(r.Body) if err != nil { ERROR(w, http.StatusUnprocessableEntity, err) return diff --git a/api/peer_report.go b/api/peer_report.go index 20313d8..0b1f461 100644 --- a/api/peer_report.go +++ b/api/peer_report.go @@ -3,15 +3,15 @@ package api import ( "encoding/json" "github.com/evilsocket/islazy/log" - "io" + "io/ioutil" "net/http" ) -// PeerReportAP POST /api/v1/report/ap +// POST /api/v1/report/ap func (api *API) PeerReportAP(w http.ResponseWriter, r *http.Request) { var report apReport - body, err := io.ReadAll(r.Body) + body, err := ioutil.ReadAll(r.Body) if err != nil { ERROR(w, http.StatusUnprocessableEntity, err) return diff --git a/api/setup.go b/api/setup.go index 83b9178..73aefb5 100644 --- a/api/setup.go +++ b/api/setup.go @@ -20,13 +20,13 @@ type API struct { Client *Client } -func Setup(keys *crypto.KeyPair, peer *mesh.Peer, router *mesh.Router, Endpoint string, Hostname string) (err error, api *API) { +func Setup(keys *crypto.KeyPair, peer *mesh.Peer, router *mesh.Router, Endpoint string) (err error, api *API) { api = &API{ Router: chi.NewRouter(), Keys: keys, Peer: peer, Mesh: router, - Client: NewClient(keys, Endpoint, Hostname), + Client: NewClient(keys, Endpoint), } api.Router.Use(CORS) diff --git a/api/unit_enroll.go b/api/unit_enroll.go index a6567f2..d3dc362 100644 --- a/api/unit_enroll.go +++ b/api/unit_enroll.go @@ -4,14 +4,14 @@ import ( "encoding/json" "github.com/evilsocket/islazy/log" "github.com/andatoshiki/shikigrid/models" - "io" + "io/ioutil" "net/http" ) func (api *API) readEnrollment(w http.ResponseWriter, r *http.Request) (error, models.EnrollmentRequest) { var enroll models.EnrollmentRequest - body, err := io.ReadAll(r.Body) + body, err := ioutil.ReadAll(r.Body) if err != nil { ERROR(w, http.StatusUnprocessableEntity, err) return err, enroll diff --git a/api/unit_inbox.go b/api/unit_inbox.go index 2245cb1..5f815ec 100644 --- a/api/unit_inbox.go +++ b/api/unit_inbox.go @@ -8,7 +8,7 @@ import ( "github.com/go-chi/chi/v5" "github.com/andatoshiki/shikigrid/crypto" "github.com/andatoshiki/shikigrid/models" - "io" + "io/ioutil" "net/http" "strconv" "time" @@ -148,7 +148,7 @@ func (api *API) SendMessageTo(w http.ResponseWriter, r *http.Request) { // read the message and signature from the source unit client := clientIP(r) - body, err := io.ReadAll(r.Body) + body, err := ioutil.ReadAll(r.Body) if err != nil { ERROR(w, http.StatusUnprocessableEntity, err) return diff --git a/api/unit_report.go b/api/unit_report.go index 3585ff4..0fd96d3 100644 --- a/api/unit_report.go +++ b/api/unit_report.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/evilsocket/islazy/log" "github.com/andatoshiki/shikigrid/models" - "io" + "io/ioutil" "net" "net/http" ) @@ -50,7 +50,7 @@ func (api *API) UnitReportAP(w http.ResponseWriter, r *http.Request) { } client := clientIP(r) - body, err := io.ReadAll(r.Body) + body, err := ioutil.ReadAll(r.Body) if err != nil { ERROR(w, http.StatusUnprocessableEntity, ErrEmpty) return @@ -81,7 +81,7 @@ func (api *API) UnitReportMultipleAP(w http.ResponseWriter, r *http.Request) { } client := clientIP(r) - body, err := io.ReadAll(r.Body) + body, err := ioutil.ReadAll(r.Body) if err != nil { ERROR(w, http.StatusUnprocessableEntity, ErrEmpty) return diff --git a/cmd/shikigrid/inbox.go b/cmd/pwngrid/inbox.go similarity index 95% rename from cmd/shikigrid/inbox.go rename to cmd/pwngrid/inbox.go index 6f8b5c2..83aeba4 100644 --- a/cmd/shikigrid/inbox.go +++ b/cmd/pwngrid/inbox.go @@ -5,6 +5,7 @@ import ( "github.com/evilsocket/islazy/log" "github.com/evilsocket/islazy/tui" "github.com/andatoshiki/shikigrid/api" + "io/ioutil" "os" "os/exec" "runtime" @@ -20,10 +21,7 @@ func clearScreen() { } cmd := exec.Command(what[0], what[1:]...) cmd.Stdout = os.Stdout - err := cmd.Run() - if err != nil { - return - } + cmd.Run() } func showInbox(server *api.API, box map[string]interface{}) { @@ -100,7 +98,7 @@ func showMessage(msg map[string]interface{}) { if output == "" { fmt.Printf("%s\n", msg["data"]) fmt.Println() - } else if err := os.WriteFile(output, msg["data"].([]byte), os.ModePerm); err != nil { + } else if err := ioutil.WriteFile(output, msg["data"].([]byte), os.ModePerm); err != nil { log.Fatal("error writing to %s: %v", output, err) } else { log.Info("%s written", output) @@ -116,7 +114,7 @@ func sendMessage() { log.Fatal("-message can not be empty") } else if message[0] == '@' { log.Info("reading %s ...", message[1:]) - if raw, err = os.ReadFile(message[1:]); err != nil { + if raw, err = ioutil.ReadFile(message[1:]); err != nil { log.Fatal("error reading %s: %v", message[1:], err) } } else { diff --git a/cmd/shikigrid/main.go b/cmd/pwngrid/main.go similarity index 100% rename from cmd/shikigrid/main.go rename to cmd/pwngrid/main.go diff --git a/cmd/shikigrid/setup.go b/cmd/pwngrid/setup.go similarity index 78% rename from cmd/shikigrid/setup.go rename to cmd/pwngrid/setup.go index 35813e3..b40cefe 100644 --- a/cmd/shikigrid/setup.go +++ b/cmd/pwngrid/setup.go @@ -86,11 +86,12 @@ func waitForKeys() { func setupMesh() { var err error - peer = mesh.MakeLocalPeer(utils.Hostname(), keys, advertise) - if !advertise { - return //this probably doesn't work + + if advertise == false { + return //this probably doesnt work } + peer = mesh.MakeLocalPeer(utils.Hostname(), keys) if err = peer.StartAdvertising(iface); err != nil { log.Fatal("error while starting signaling: %v", err) } @@ -130,7 +131,7 @@ func setupMode() string { // for inbox actions, set the keys to the default path if empty if (whoami || inbox) && keysPath == "" { - keysPath = "/etc/shikigotchi/" + keysPath = "/etc/pwnagotchi/" } // generate keypair @@ -158,30 +159,35 @@ func setupMode() string { log.Info("shikigrid v%s starting in %s mode ...", version.Version, mode) - // wait for keys to be generated - if wait { - waitForKeys() - } - // load the keys - if keys, err = crypto.Load(keysPath); err != nil { - log.Fatal("error while loading keys from %s: %v", keysPath, err) - } - // print identity and exit - if whoami { - if Endpoint == "https://grid-api.toshiki.dev/api/v1" { - log.Info("https://grid.toshiki.dev/search/%s", keys.FingerprintHex) - } else { - log.Info("https://pwnagotchi.ai/pwnfile/#!%s", keys.FingerprintHex) + if mode == "peer" { + // wait for keys to be generated + if wait { + waitForKeys() } - os.Exit(0) - } - // only start mesh signaling if this is not an inbox action - if !inbox { - setupMesh() + // load the keys + if keys, err = crypto.Load(keysPath); err != nil { + log.Fatal("error while loading keys from %s: %v", keysPath, err) + } + // print identity and exit + if whoami { + if Endpoint == "https://grid-api.toshiki.dev/api/v1" { + log.Info("https://toshiki.dev/search/%s", keys.FingerprintHex) + } else { + log.Info("https://pwnagotchi.ai/pwnfile/#!%s", keys.FingerprintHex) + } + os.Exit(0) + } + // only start mesh signaling if this is not an inbox action + if !inbox { + setupMesh() + } + } else if mode == "server" { + // server side we just need to setup the database connection + setupDB() } - // set up the proper routes for either server or peer mode - err, server = api.Setup(keys, peer, router, Endpoint, Hostname) + // setup the proper routes for either server or peer mode + err, server = api.Setup(keys, peer, router, Endpoint) if err != nil { log.Fatal("%v", err) } diff --git a/cmd/shikigrid/vars.go b/cmd/pwngrid/vars.go similarity index 95% rename from cmd/shikigrid/vars.go rename to cmd/pwngrid/vars.go index 7d01d85..89c8aed 100644 --- a/cmd/shikigrid/vars.go +++ b/cmd/pwngrid/vars.go @@ -29,7 +29,7 @@ var ( id = 0 address = "0.0.0.0:8666" env = ".env" - iface = "wlan0mon" + iface = "mon0" keysPath = "" peersPath = "/root/peers" keys = (*crypto.KeyPair)(nil) @@ -40,7 +40,6 @@ var ( memProfile = "" Endpoint = "https://grid-api.toshiki.dev/api/v1" advertise = true - Hostname = "" ) func init() { @@ -79,5 +78,4 @@ func init() { flag.StringVar(&Endpoint, "endpoint", Endpoint, "Pass which endpoint shikigrid should be using.") flag.BoolVar(&advertise, "advertise", advertise, "Advertise?") - flag.StringVar(&Hostname, "hostname", Hostname, "Pass hostname to shikigrid, makes it so it wont read os.hostname()") } diff --git a/crypto/keypair.go b/crypto/keypair.go index bfc2edf..b860532 100644 --- a/crypto/keypair.go +++ b/crypto/keypair.go @@ -8,6 +8,7 @@ import ( "fmt" "github.com/evilsocket/islazy/fs" "github.com/evilsocket/islazy/log" + "io/ioutil" "os" "path" "strings" @@ -137,7 +138,7 @@ func (pair *KeyPair) Save() (err error) { }, ) - if err = os.WriteFile(pair.PrivatePath, pair.PrivatePEM, os.ModePerm); err != nil { + if err = ioutil.WriteFile(pair.PrivatePath, pair.PrivatePEM, os.ModePerm); err != nil { return } @@ -147,7 +148,7 @@ func (pair *KeyPair) Save() (err error) { return err } - err = os.WriteFile(pair.PublicPath, pair.PublicPEM, os.ModePerm) + err = ioutil.WriteFile(pair.PublicPath, pair.PublicPEM, os.ModePerm) log.Debug("%s created", pair.PublicPath) return @@ -155,7 +156,7 @@ func (pair *KeyPair) Save() (err error) { func (pair *KeyPair) Load() (err error) { log.Debug("reading %s ...", pair.PrivatePath) - if pair.PrivatePEM, err = os.ReadFile(pair.PrivatePath); err != nil { + if pair.PrivatePEM, err = ioutil.ReadFile(pair.PrivatePath); err != nil { return } diff --git a/go.mod b/go.mod index 320c87b..ecd604d 100644 --- a/go.mod +++ b/go.mod @@ -5,17 +5,16 @@ go 1.21 require ( github.com/biezhi/gorm-paginator/pagination v0.0.0-20190124091837-7a5c8ed20334 github.com/evilsocket/islazy v1.11.0 - github.com/go-chi/chi/v5 v5.1.0 + github.com/go-chi/chi/v5 v5.0.11 github.com/go-chi/cors v1.2.1 - github.com/golang-jwt/jwt/v5 v5.2.1 + github.com/golang-jwt/jwt/v5 v5.2.0 github.com/gopacket/gopacket v1.2.0 github.com/jinzhu/gorm v1.9.16 github.com/joho/godotenv v1.5.1 ) require ( - filippo.io/edwards25519 v1.1.0 // indirect - github.com/go-sql-driver/mysql v1.8.1 // indirect + github.com/go-sql-driver/mysql v1.7.1 // indirect github.com/jinzhu/inflection v1.0.0 // indirect - golang.org/x/sys v0.22.0 // indirect + golang.org/x/sys v0.15.0 // indirect ) diff --git a/go.sum b/go.sum index 772d575..b198540 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= -filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/biezhi/gorm-paginator/pagination v0.0.0-20190124091837-7a5c8ed20334 h1:ptFjQ4+vPGZDiNmBuKUetQoREFiPz/WB29CfQfdfeKc= @@ -10,17 +8,15 @@ github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DP github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= github.com/evilsocket/islazy v1.11.0 h1:B5w6uuS6ki6iDG+aH/RFeoMb8ijQh/pGabewqp2UeJ0= github.com/evilsocket/islazy v1.11.0/go.mod h1:muYH4x5MB5YRdkxnrOtrXLIBX6LySj1uFIqys94LKdo= -github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= -github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= -github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= -github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA= +github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= -github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= -github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= -github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw= +github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/gopacket/gopacket v1.2.0 h1:eXbzFad7f73P1n2EJHQlsKuvIMJjVXK5tXoSca78I3A= @@ -48,11 +44,12 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/mesh/hopping.go b/mesh/hopping.go index c3dc5ab..2531267 100644 --- a/mesh/hopping.go +++ b/mesh/hopping.go @@ -9,7 +9,7 @@ import ( ) func ChannelHopping(iface string, chanList string, allChannels []int, hopPeriod int) { - var channels []int + channels := []int{} for _, s := range str.Comma(chanList) { if ch, err := strconv.Atoi(s); err != nil { log.Fatal("%v", err) diff --git a/mesh/interface.go b/mesh/interface.go index 0b0dfc9..75e55b9 100644 --- a/mesh/interface.go +++ b/mesh/interface.go @@ -9,7 +9,7 @@ import ( "strings" ) -var chanParser = regexp.MustCompile(`^\s+Channel.([0-9]+)\s+:\s+([0-9.]+)\s+GHz.*$`) +var chanParser = regexp.MustCompile(`^\s+Channel.([0-9]+)\s+:\s+([0-9\.]+)\s+GHz.*$`) func ActivateInterface(name string) error { if out, err := utils.Exec("ifconfig", []string{name, "up"}); err != nil { @@ -36,7 +36,7 @@ func SupportedChannels(iface string) ([]int, error) { return nil, err } - var channels []int + channels := []int{} scanner := bufio.NewScanner(strings.NewReader(out)) for scanner.Scan() { line := scanner.Text() diff --git a/mesh/memory.go b/mesh/memory.go index 1520c33..ff832e1 100644 --- a/mesh/memory.go +++ b/mesh/memory.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/evilsocket/islazy/fs" "github.com/evilsocket/islazy/log" + "io/ioutil" "math" "os" "path" @@ -37,7 +38,7 @@ func MemoryFromPath(path string) (err error, mem *Memory) { err = fs.Glob(path, "*.json", func(fileName string) error { log.Debug("loading %s ...", fileName) - data, err := os.ReadFile(fileName) + data, err := ioutil.ReadFile(fileName) if err != nil { log.Error("error loading %s: %v", fileName, err) return nil @@ -114,7 +115,7 @@ func (mem *Memory) Track(fingerprint string, peer *Peer) error { fileName := path.Join(mem.path, fmt.Sprintf("%s.json", fingerprint)) if data, err := json.Marshal(peer); err != nil { return err - } else if err := os.WriteFile(fileName, data, os.ModePerm); err != nil { + } else if err := ioutil.WriteFile(fileName, data, os.ModePerm); err != nil { return err } diff --git a/mesh/packet_muxer.go b/mesh/packet_muxer.go index 600cd15..09ccf1e 100644 --- a/mesh/packet_muxer.go +++ b/mesh/packet_muxer.go @@ -11,7 +11,7 @@ import ( ) const ( - // ErrIfaceNotUp Ugly, but gopacket folks are not exporting pcap errors, so ... + // Ugly, but gopacket folks are not exporting pcap errors, so ... // ref. https://github.com/gopacket/gopacket/blob/96986c90e3e5c7e01deed713ff8058e357c0c047/pcap/pcap.go#L281 ErrIfaceNotUp = "Interface Not Up" ) diff --git a/mesh/peer.go b/mesh/peer.go index 296c16f..b75e8c6 100644 --- a/mesh/peer.go +++ b/mesh/peer.go @@ -6,17 +6,16 @@ import ( "encoding/base64" "encoding/json" "fmt" - "net" - "regexp" - "strings" - "sync" - "time" - "github.com/evilsocket/islazy/log" "github.com/gopacket/gopacket/layers" "github.com/andatoshiki/shikigrid/crypto" "github.com/andatoshiki/shikigrid/version" "github.com/andatoshiki/shikigrid/wifi" + "net" + "regexp" + "strings" + "sync" + "time" ) var ( @@ -43,29 +42,23 @@ type Peer struct { AdvData sync.Map AdvPeriod int - advEnabled bool - ForceDisabled bool - - mux *PacketMuxer - stop chan struct{} + advEnabled bool + mux *PacketMuxer + stop chan struct{} } -func MakeLocalPeer(name string, keys *crypto.KeyPair, advertise bool) *Peer { +func MakeLocalPeer(name string, keys *crypto.KeyPair) *Peer { now := time.Now() peer := &Peer{ - DetectedAt: now, - SeenAt: now, - PrevSeenAt: now, - SessionID: make([]byte, 6), - Keys: keys, - AdvData: sync.Map{}, - AdvPeriod: SignalingPeriod, - stop: make(chan struct{}), - advEnabled: false, - ForceDisabled: false, - } - if !advertise { - peer.ForceDisabled = true + DetectedAt: now, + SeenAt: now, + PrevSeenAt: now, + SessionID: make([]byte, 6), + Keys: keys, + AdvData: sync.Map{}, + AdvPeriod: SignalingPeriod, + stop: make(chan struct{}), + advEnabled: false, } if _, err := rand.Read(peer.SessionID); err != nil { @@ -216,7 +209,7 @@ func (peer *Peer) Update(radio *layers.RadioTap, dot11 *layers.Dot11, adv map[st } // the signature is SIGN(advertisement), so we need to remove the signature field and convert back to json. - // NOTE: fortunately, keys will always be sorted, so we don't have to do anything in order to guarantee signature + // NOTE: fortunately, keys will be always sorted, so we don't have to do anything in order to guarantee signature // consistency (https://stackoverflow.com/questions/18668652/how-to-produce-json-with-sorted-keys-in-go) signedMap := adv delete(signedMap, "signature") @@ -272,9 +265,6 @@ func (peer *Peer) InactiveFor() float64 { } func (peer *Peer) SetData(adv map[string]interface{}) { - if peer == nil { - return - } peer.Lock() defer peer.Unlock() diff --git a/mesh/routing.go b/mesh/routing.go index fa1aba8..7489185 100644 --- a/mesh/routing.go +++ b/mesh/routing.go @@ -81,7 +81,7 @@ func (router *Router) peersPruner() { log.Debug("peers pruner started with a %s period", period) - for range tick.C { + for _ = range tick.C { stale := map[string]*Peer{} Peers.Range(func(key, value interface{}) bool { diff --git a/release.sh b/release.sh index 9490e73..bbb320f 100755 --- a/release.sh +++ b/release.sh @@ -3,28 +3,25 @@ VERSION_FILE=$(dirname "${BASH_SOURCE[0]}")/version/ver.go echo "version file is $VERSION_FILE" -CURRENT_VERSION=$(cat "$VERSION_FILE" | grep Version | cut -d '"' -f 2) +CURRENT_VERSION=$(cat $VERSION_FILE | grep Version | cut -d '"' -f 2) TO_UPDATE=( - # shellcheck disable=SC2206 $VERSION_FILE ) echo -n "current version is $CURRENT_VERSION, select new version: " read NEW_VERSION -# shellcheck disable=SC2028 echo "creating version $NEW_VERSION ...\n" for file in "${TO_UPDATE[@]}"; do echo "patching $file ..." sed -i.bak "s/$CURRENT_VERSION/$NEW_VERSION/g" "$file" rm -rf "$file.bak" - git add "$file" + git add $file done git commit -m "releasing v$NEW_VERSION" git push -git tag -a v"$NEW_VERSION" -m "release v$NEW_VERSION" -# shellcheck disable=SC2086 +git tag -a v$NEW_VERSION -m "release v$NEW_VERSION" git push origin v$NEW_VERSION echo diff --git a/shikigrid-peer.service b/shikigrid-peer.service index 5a41ac2..2f2d7e5 100644 --- a/shikigrid-peer.service +++ b/shikigrid-peer.service @@ -1,11 +1,12 @@ [Unit] Description=shikigrid peer service -Documentation=https://shikigotchi.org/ +Documentation=https://shikigotchi.toshiki.dev/ Wants=network.target After=network.target [Service] Type=simple +PermissionsStartOnly=true ExecStart=/usr/local/bin/shikigrid -log /var/log/shikigrid.log -peers /root/peers -address 127.0.0.1:8666 Restart=always RestartSec=30 diff --git a/shikigrid.service b/shikigrid.service index 8432cc1..0f8b2cb 100644 --- a/shikigrid.service +++ b/shikigrid.service @@ -1,11 +1,12 @@ [Unit] Description=shikigrid api service -Documentation=https://shikigotchi.org/ +Documentation=https://shikigotchi.toshiki.dev/ Wants=network.target After=network.target [Service] Type=simple +PermissionsStartOnly=true ExecStart=/usr/local/bin/shikigrid -log /var/log/shikigrid.log -env /etc/shikigrid/shikigrid.conf -address 127.0.0.1:8666 Restart=always RestartSec=30 diff --git a/version/ver.go b/version/ver.go index 745f41a..907c4b5 100644 --- a/version/ver.go +++ b/version/ver.go @@ -1,5 +1,5 @@ package version const ( - Version = "0.0.1" + Version = "1.10.7" ) diff --git a/wifi/compression.go b/wifi/compression.go index 2e39e30..9eda553 100644 --- a/wifi/compression.go +++ b/wifi/compression.go @@ -4,7 +4,7 @@ import ( "bytes" "compress/gzip" "fmt" - "io" + "io/ioutil" ) func Compress(data []byte) (bool, []byte, error) { @@ -33,12 +33,7 @@ func Decompress(data []byte) ([]byte, error) { if zr, err := gzip.NewReader(bytes.NewBuffer(data)); err != nil { return nil, fmt.Errorf("error initializing payload decompression: %v", err) } else { - defer func(zr *gzip.Reader) { - err := zr.Close() - if err != nil { - - } - }(zr) - return io.ReadAll(zr) + defer zr.Close() + return ioutil.ReadAll(zr) } } diff --git a/wifi/utils.go b/wifi/utils.go index 273fca6..1302f1a 100644 --- a/wifi/utils.go +++ b/wifi/utils.go @@ -23,7 +23,7 @@ func Freq2Chan(freq int) int { return ((freq - 2412) / 5) + 1 } else if freq == 2484 { return 14 - } else if freq >= 5035 && freq <= 5885 { + } else if freq >= 5035 && freq <= 5865 { return ((freq - 5035) / 5) + 7 } return 0 @@ -34,8 +34,9 @@ func Chan2Freq(channel int) int { return ((channel - 1) * 5) + 2412 } else if channel == 14 { return 2484 - } else if channel <= 177 { + } else if channel <= 173 { return ((channel - 7) * 5) + 5035 } + return 0 }