Skip to content

Commit

Permalink
Fixed Issues with CLI
Browse files Browse the repository at this point in the history
Fixed Issues with CLI
  • Loading branch information
amrudesh1 authored Jul 27, 2024
2 parents cbe4240 + bfa6757 commit 698a303
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 11,456 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Get Go Alpine Base Image from Docker Hub
FROM golang:buster AS builder

#Install JRE for AAPT2

ARG JDK_VERSION=11
Expand Down Expand Up @@ -33,6 +32,7 @@ RUN apt-get update && \
rm -rf /usr/lib/jvm/java-${JDK_VERSION}-openjdk-amd64/jre/lib/amd64/libjavafx*.so && \
rm -rf /usr/lib/jvm/java-${JDK_VERSION}-openjdk-amd64/jre/lib/amd64/libjfx*.so


## Install Ripgrep
RUN apt-get update && \
apt-get install -y --no-install-recommends ripgrep && \
Expand Down
35 changes: 13 additions & 22 deletions apk/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.

import (
"encoding/json"
"fmt"
"io"
"morf/models"
"morf/utils"
Expand All @@ -35,12 +34,12 @@ func StartMetaDataCollection(apkPath string) models.MetaDataModel {
fs := alf.NewOsFs()

if utils.CheckifmorftmpDirExists(fs) {
fmt.Println("Deleting the temp directory")
log.Debug("Deleting the temp directory")
utils.DeleteTmpDir(fs)
fmt.Println("Creating a new temp directory")
log.Debug("Creating a new temp directory")
utils.CreateMorfDirintmp(fs)
} else {
fmt.Println("Creating a new temp directory")
log.Debug("Creating a new temp directory")
utils.CreateMorfDirintmp(fs)
}

Expand All @@ -51,45 +50,37 @@ func StartMetaDataCollection(apkPath string) models.MetaDataModel {

// Move APK to input directory
apkPath = utils.CopyApktoInputDir(fs, apkPath)
fmt.Println("Starting metadata collection for " + apkPath)

log.Info("Starting metadata collection for " + apkPath)

metadata_success, metadata_error := exec.Command("java", "-cp", "tools/apkanalyzer.jar", "sk.styk.martin.bakalarka.execute.Main", "-analyze", "--in", utils.GetInputDir(), "--out", utils.GetOutputDir()).Output()

if metadata_error != nil {
fmt.Println("Error while decompiling the APK file")
log.Error("Error while decompiling the APK file")
log.Fatal(metadata_error)
return models.MetaDataModel{}
}

if metadata_success != nil {
log.Info("Metadata collection successful")

log.Debug("Metadata collection successful")
file_path, file_name := filepath.Split(apkPath)
log.Info("File path: " + file_path)
log.Info("File name: " + file_name)
log.Debug(file_path)

// Make file readable
json_file_path := utils.GetOutputDir() + strings.Replace(file_name, ".apk", ".json", -1)
// Create a file with 777 permissions
_, err := os.Create(json_file_path)
if err != nil {
log.Error("Error creating file")
log.Error(err)
}
os.Chmod(json_file_path, 0777)

os.Chmod(utils.GetOutputDir()+strings.Replace(file_name, ".apk", ".json", -1), 0777)
return startFileParser(utils.GetOutputDir() + strings.Replace(file_name, ".apk", ".json", -1))
}

return models.MetaDataModel{}
}

func startFileParser(s string) models.MetaDataModel {
fmt.Println("Starting file parser:" + s)
log.Debug("Starting file parser:" + s)
jsonFile, err := os.Open(s)
if err != nil {
fmt.Println(err)
log.Error(err)
}
fmt.Println("Successfully Opened " + s)
log.Debug("Successfully Opened " + s)
defer jsonFile.Close()

byteValue, _ := io.ReadAll(jsonFile)
Expand Down
23 changes: 3 additions & 20 deletions apk/packageparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,20 @@ limitations under the License.
*/package apk

import (
"fmt"
"morf/models"
util "morf/utils"
"os/exec"
"regexp"
"strings"

log "github.com/sirupsen/logrus"

"github.com/lib/pq"
)

func ExtractPackageData(apkPath string) models.PackageDataModel {
// Use AAPT to get APK Version etc
// Check if AAPT is installed in the system or get aapt from the tools folder

aapt_success := []byte{}
aapt_error := error(nil)
_, aapt_error = exec.LookPath("aapt")
if aapt_error != nil {
log.Error("AAPT not found in the system")
log.Error("Please install AAPT or add it to the system path")
aapt_success, aapt_error = exec.Command("tools/aapt", "dump", "badging", apkPath).Output()
} else {
aapt_success, aapt_error = exec.Command("aapt", "dump", "badging", apkPath).Output()
}

aapt_byte_to_string := aapt_success[:]

if aapt_error != nil {
log.Error("Error while getting APK version etc")
log.Error(aapt_error)
}
aapt_byte_to_string := util.RunAAPT(apkPath)[:]

aapt_split_stirng := strings.Split(string(aapt_byte_to_string), "\n")
re := regexp.MustCompile(`'[^"]+'`)
Expand Down Expand Up @@ -133,6 +115,7 @@ func ExtractPackageData(apkPath string) models.PackageDataModel {
}

packageModel := models.PackageDataModel{PackageDataID: 0, APKHash: util.ExtractHash(apkPath), PackageName: package_name, VersionCode: version_code, VersionName: version_name, CompileSdkVersion: complie_sdk_version, SdkVersion: sdk_version, TargetSdk: target_sdk, SupportScreens: pq.StringArray(support_screens), Densities: pq.StringArray(densities), NativeCode: pq.StringArray(native_code)}
fmt.Println("Package Model:", packageModel.PackageName)
return packageModel

}
19 changes: 19 additions & 0 deletions apk/resultparser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package apk

import (
"morf/models"
"morf/utils"

log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
vip "github.com/spf13/viper"
)

func ParseResults(fs afero.Fs, fileName string, json_data []byte, secret models.Secrets, secret_data []byte) {
utils.WriteToFile(fs, vip.GetString("backup_path")+fileName+"_"+secret.APKVersion+".json", string(json_data))
utils.WriteToFile(fs, vip.GetString("backup_path")+fileName+"_"+"Secrets_"+secret.APKVersion+".json", string(secret_data))
utils.WriteToFile(fs, "results"+"/"+fileName+"_"+secret.APKVersion+".json", string(json_data))
utils.WriteToFile(fs, "results"+"/"+fileName+"_"+"Secrets_"+secret.APKVersion+".json", string(secret_data))
log.Info("APK Data saved to: " + vip.GetString("backup_path") + "/" + fileName + "_" + secret.APKVersion + ".json")

}
2 changes: 2 additions & 0 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func add(cmd *cob.Command, args []string) {
apkPath, _ = cmd.Flags().GetString("apk")
is_db_req, _ = cmd.Flags().GetBool("db")

fmt.Println(is_db_req)

if is_db_req {
db.InitDB()
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func preFlight(cmd *cob.Command, args []string) error {
}

func runMORF(cmd *cob.Command, args []string) error {
logLevel := log.InfoLevel
logLevel := log.DebugLevel

log.SetFormatter(&log.TextFormatter{})
log.SetOutput(os.Stdout)
Expand All @@ -73,6 +73,7 @@ func init() {

vip.SetDefault("port", 8080)
vip.SetDefault("backup_path", "backup/")
vip.SetDefault("db_name", "Secrets")

MorfCmd.AddCommand(cliCmd)
MorfCmd.AddCommand(serverCmd)
Expand Down
13 changes: 8 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ module morf

go 1.19

require github.com/gin-gonic/gin v1.9.0
require (
github.com/gin-gonic/gin v1.9.0
google.golang.org/appengine v1.6.7
)

require (
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
Expand Down Expand Up @@ -34,13 +38,12 @@ require (
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/sirupsen/logrus v1.9.0
github.com/slack-go/slack v0.12.1
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/afero v1.9.3
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.6.1 // indirect
github.com/spf13/cobra-cli v1.3.0 // indirect
github.com/spf13/cobra v1.6.1
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.15.0 // indirect
github.com/spf13/viper v1.15.0
github.com/subosito/gotenv v1.4.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
Expand Down
Loading

0 comments on commit 698a303

Please sign in to comment.