| 1 | + | package cmd |
| 2 | + | |
| 3 | + | import ( |
| 4 | + | "bytes" |
| 5 | + | "fmt" |
| 6 | + | "regexp" |
| 7 | + | "strings" |
| 8 | + | |
| 9 | + | "github.com/liamg/dismember/pkg/proc" |
| 10 | + | "github.com/spf13/cobra" |
| 11 | + | ) |
| 12 | + | |
| 13 | + | var flagPID int |
| 14 | + | var flagProcessName string |
| 15 | + | var flagIncludeSelf bool |
| 16 | + | var flagDumpRadius int |
| 17 | + | |
| 18 | + | func init() { |
| 19 | + | |
| 20 | + | grepCmd := &cobra.Command{ |
| 21 | + | Use: "grep [keyword]", |
| 22 | + | Short: "Search process memory for a given string or regex", |
| 23 | + | Long: ``, |
| 24 | + | RunE: grepHandler, |
| 25 | + | Args: cobra.ExactArgs(1), |
| 26 | + | } |
| 27 | + | |
| 28 | + | grepCmd.Flags().IntVarP(&flagPID, "pid", "p", 0, "PID of the process whose memory should be grepped. Omitting this option will grep the memory of all available processes on the system.") |
| 29 | + | grepCmd.Flags().StringVarP(&flagProcessName, "pname", "n", "", "Grep memory of all processes whose name contains this string.") |
| 30 | + | grepCmd.Flags().IntVarP(&flagDumpRadius, "dump-radius", "r", 2, "The number of lines of memory to dump both above and below each match.") |
| 31 | + | grepCmd.Flags().BoolVarP(&flagIncludeSelf, "self", "s", false, "Include results that are matched against the current process, or an ancestor of that process.") |
| 32 | + | rootCmd.AddCommand(grepCmd) |
| 33 | + | } |
| 34 | + | |
| 35 | + | func grepHandler(cmd *cobra.Command, args []string) error { |
| 36 | + | |
| 37 | + | var processes []proc.Process |
| 38 | + | |
| 39 | + | if flagPID == 0 { |
| 40 | + | var err error |
| 41 | + | processes, err = proc.List(false) |
| 42 | + | if err != nil { |
| 43 | + | return err |
| 44 | + | } |
| 45 | + | } else { |
| 46 | + | processes = []proc.Process{proc.Process(flagPID)} |
| 47 | + | } |
| 48 | + | |
| 49 | + | regex, err := regexp.Compile(args[0]) |
| 50 | + | if err != nil { |
| 51 | + | return fmt.Errorf("invalid regex pattern: %w", err) |
| 52 | + | } |
| 53 | + | |
| 54 | + | stdErr := cmd.ErrOrStderr() |
| 55 | + | _ = stdErr |
| 56 | + | stdOut := cmd.OutOrStdout() |
| 57 | + | |
| 58 | + | var total int |
| 59 | + | for _, process := range processes { |
| 60 | + | if flagProcessName != "" { |
| 61 | + | status, err := process.Status() |
| 62 | + | if err != nil { |
| 63 | + | continue |
| 64 | + | } |
| 65 | + | if !strings.Contains(status.Name, flagProcessName) { |
| 66 | + | continue |
| 67 | + | } |
| 68 | + | } |
| 69 | + | if !flagIncludeSelf && process.IsInHierarchyOf(proc.Self()) { |
| 70 | + | continue |
| 71 | + | } |
| 72 | + | results, err := grepProcessMemory(process, regex) |
| 73 | + | if err != nil { |
| 74 | + | // TODO: add to debug log |
| 75 | + | //_, _ = fmt.Fprintf(stdErr, "failed to access memory for process %d: %s\n", process.PID(), err) |
| 76 | + | continue |
| 77 | + | } |
| 78 | + | for i, result := range results { |
| 79 | + | _, _ = fmt.Fprint(stdOut, summariseResult(total+i+1, result)) |
| 80 | + | } |
| 81 | + | total += len(results) |
| 82 | + | } |
| 83 | + | if total == 0 { |
| 84 | + | _, _ = fmt.Fprintf(stdOut, "%sOperation Complete. No results found.%s\n\n", ansiRed, ansiReset) |
| 85 | + | } else { |
| 86 | + | _, _ = fmt.Fprintf(stdOut, "%sOperation Complete. %s%d%s%s results found.%s\n\n", ansiGreen, ansiBold, total, ansiReset, ansiGreen, ansiReset) |
| 87 | + | } |
| 88 | + | |
| 89 | + | return nil |
| 90 | + | } |
| 91 | + | |
| 92 | + | type GrepResult struct { |
| 93 | + | Pattern *regexp.Regexp |
| 94 | + | Process proc.Process |
| 95 | + | Map proc.Map |
| 96 | + | Address uint64 |
| 97 | + | Match []byte |
| 98 | + | } |
| 99 | + | |
| 100 | + | const ( |
| 101 | + | ansiReset = "\x1b[0m" |
| 102 | + | ansiBold = "\x1b[1m" |
| 103 | + | ansiDim = "\x1b[2m" |
| 104 | + | ansiUnderline = "\x1b[4m" |
| 105 | + | ansiItalic = "\x1b[3m" |
| 106 | + | ansiRed = "\x1b[31m" |
| 107 | + | ansiGreen = "\x1b[32m" |
| 108 | + | ) |
| 109 | + | |
| 110 | + | func summariseResult(number int, g GrepResult) string { |
| 111 | + | |
| 112 | + | buffer := bytes.NewBuffer(nil) |
| 113 | + | |
| 114 | + | _, _ = fmt.Fprintf(buffer, " %sMatch #%d%s\n\n", ansiUnderline, number, ansiReset) |
| 115 | + | _, _ = fmt.Fprintf(buffer, " %sMatched%s %s\n", ansiBold, ansiReset, string(g.Match)) |
| 116 | + | _, _ = fmt.Fprintf(buffer, " %sPattern%s %s\n", ansiBold, ansiReset, g.Pattern.String()) |
| 117 | + | _, _ = fmt.Fprintf(buffer, " %sProcess%s %s\n", ansiBold, ansiReset, g.Process.String()) |
| 118 | + | _, _ = fmt.Fprintf(buffer, " %sAddress%s 0x%x %s\n\n", ansiBold, ansiReset, g.Address, g.Map.Path) |
| 119 | + | _, _ = fmt.Fprintf(buffer, " %sMemory Dump%s\n\n%s\n\n", ansiBold, ansiReset, hexDump(g)) |
| 120 | + | |
| 121 | + | return buffer.String() |
| 122 | + | } |
| 123 | + | |
| 124 | + | func hexDump(g GrepResult) string { |
| 125 | + | |
| 126 | + | buffer := bytes.NewBuffer(nil) |
| 127 | + | |
| 128 | + | offset := g.Address - g.Map.Address |
| 129 | + | |
| 130 | + | linesEitherSide := uint64(flagDumpRadius) |
| 131 | + | if linesEitherSide < 0 { |
| 132 | + | linesEitherSide = 0 |
| 133 | + | } |
| 134 | + | |
| 135 | + | start := ((offset / 16) * 16) - (16 * linesEitherSide) |
| 136 | + | size := (((uint64(len(g.Match)) + (16 * (2 * linesEitherSide))) / 16) + 1) * 16 |
| 137 | + | |
| 138 | + | data, err := g.Process.ReadMemory(g.Map, start, size) |
| 139 | + | if err != nil { |
| 140 | + | return fmt.Sprintf("dump not available: %s", err) |
| 141 | + | } |
| 142 | + | |
| 143 | + | literalStartAddr := g.Map.Address + start |
| 144 | + | |
| 145 | + | _, _ = fmt.Fprintf(buffer, " %s", ansiDim) |
| 146 | + | for i := 0; i < 0x10; i++ { |
| 147 | + | _, _ = fmt.Fprintf(buffer, "%02X ", i) |
| 148 | + | } |
| 149 | + | _, _ = fmt.Fprintln(buffer, ansiReset) |
| 150 | + | |
| 151 | + | var ascii string |
| 152 | + | for index, b := range data { |
| 153 | + | |
| 154 | + | localIndex := uint64(index) + start |
| 155 | + | inSecret := localIndex >= offset && localIndex < offset+uint64(len(g.Match)) |
| 156 | + | |
| 157 | + | if index%16 == 0 && index > 0 { |
| 158 | + | _, _ = fmt.Fprintf(buffer, " %s\n", ascii) |
| 159 | + | ascii = "" |
| 160 | + | } |
| 161 | + | if index%16 == 0 { |
| 162 | + | _, _ = fmt.Fprintf(buffer, " %s%016x%s ", ansiDim, literalStartAddr+uint64(index/16), ansiReset) |
| 163 | + | } |
| 164 | + | |
| 165 | + | if inSecret { |
| 166 | + | _, _ = fmt.Fprintf(buffer, "%s%s", ansiBold, ansiRed) |
| 167 | + | } |
| 168 | + | _, _ = fmt.Fprintf(buffer, "%02x%s ", b, ansiReset) |
| 169 | + | ascii += asciify(b, inSecret) |
| 170 | + | } |
| 171 | + | if ascii != "" { |
| 172 | + | _, _ = fmt.Fprintf(buffer, " %s\n", ascii) |
| 173 | + | } |
| 174 | + | |
| 175 | + | return buffer.String() |
| 176 | + | } |
| 177 | + | |
| 178 | + | func asciify(b byte, hl bool) string { |
| 179 | + | if b < ' ' || b > '~' { |
| 180 | + | b = '.' |
| 181 | + | } |
| 182 | + | if !hl { |
| 183 | + | return fmt.Sprintf("%s%c%s", ansiDim, b, ansiReset) |
| 184 | + | } |
| 185 | + | return fmt.Sprintf("%s%s%c%s", ansiBold, ansiRed, b, ansiReset) |
| 186 | + | } |
| 187 | + | |
| 188 | + | func grepProcessMemory(p proc.Process, regex *regexp.Regexp) ([]GrepResult, error) { |
| 189 | + | var results []GrepResult |
| 190 | + | maps, err := p.Maps() |
| 191 | + | if err != nil { |
| 192 | + | return nil, err |
| 193 | + | } |
| 194 | + | for _, map_ := range maps { |
| 195 | + | if !map_.Permissions.Readable { |
| 196 | + | continue |
| 197 | + | } |
| 198 | + | memory, err := p.ReadMemory(map_, 0, 0) |
| 199 | + | if err != nil { |
| 200 | + | continue |
| 201 | + | } |
| 202 | + | for _, matches := range regex.FindAllIndex(memory, -1) { |
| 203 | + | results = append(results, GrepResult{ |
| 204 | + | Process: p, |
| 205 | + | Map: map_, |
| 206 | + | Address: map_.Address + uint64(matches[0]), |
| 207 | + | Match: shrinkMatch(memory[matches[0]:matches[1]]), |
| 208 | + | Pattern: regex, |
| 209 | + | }) |
| 210 | + | } |
| 211 | + | } |
| 212 | + | return results, nil |
| 213 | + | } |
| 214 | + | |
| 215 | + | func shrinkMatch(match []byte) []byte { |
| 216 | + | return bytes.Split(match, []byte{0x00})[0] |
| 217 | + | } |
| 218 | + | |