|
| 1 | +//go:build darwin |
| 2 | + |
| 3 | +package mount |
| 4 | + |
| 5 | +/* |
| 6 | + Sliver Implant Framework |
| 7 | + Copyright (C) 2023 Bishop Fox |
| 8 | +
|
| 9 | + This program is free software: you can redistribute it and/or modify |
| 10 | + it under the terms of the GNU General Public License as published by |
| 11 | + the Free Software Foundation, either version 3 of the License, or |
| 12 | + (at your option) any later version. |
| 13 | +
|
| 14 | + This program is distributed in the hope that it will be useful, |
| 15 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + GNU General Public License for more details. |
| 18 | +
|
| 19 | + You should have received a copy of the GNU General Public License |
| 20 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 21 | +*/ |
| 22 | + |
| 23 | +import ( |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/bishopfox/sliver/protobuf/sliverpb" |
| 27 | + "golang.org/x/sys/unix" |
| 28 | +) |
| 29 | + |
| 30 | +func getString(input []byte) string { |
| 31 | + ver := string(input) |
| 32 | + if i := strings.Index(ver, "\x00"); i != -1 { |
| 33 | + ver = ver[:i] |
| 34 | + } |
| 35 | + return ver |
| 36 | +} |
| 37 | + |
| 38 | +func formatMountFlags(flags uint32) string { |
| 39 | + opts := make([]string, 0, 12) |
| 40 | + |
| 41 | + if flags&unix.MNT_RDONLY != 0 { |
| 42 | + opts = append(opts, "ro") |
| 43 | + } else { |
| 44 | + opts = append(opts, "rw") |
| 45 | + } |
| 46 | + if flags&unix.MNT_NOSUID != 0 { |
| 47 | + opts = append(opts, "nosuid") |
| 48 | + } |
| 49 | + if flags&unix.MNT_NODEV != 0 { |
| 50 | + opts = append(opts, "nodev") |
| 51 | + } |
| 52 | + if flags&unix.MNT_NOEXEC != 0 { |
| 53 | + opts = append(opts, "noexec") |
| 54 | + } |
| 55 | + if flags&unix.MNT_SYNCHRONOUS != 0 { |
| 56 | + opts = append(opts, "sync") |
| 57 | + } |
| 58 | + if flags&unix.MNT_ASYNC != 0 { |
| 59 | + opts = append(opts, "async") |
| 60 | + } |
| 61 | + if flags&unix.MNT_NOATIME != 0 { |
| 62 | + opts = append(opts, "noatime") |
| 63 | + } |
| 64 | + if flags&unix.MNT_JOURNALED != 0 { |
| 65 | + opts = append(opts, "journaled") |
| 66 | + } |
| 67 | + if flags&unix.MNT_QUOTA != 0 { |
| 68 | + opts = append(opts, "quota") |
| 69 | + } |
| 70 | + if flags&unix.MNT_UNION != 0 { |
| 71 | + opts = append(opts, "union") |
| 72 | + } |
| 73 | + if flags&unix.MNT_AUTOMOUNTED != 0 { |
| 74 | + opts = append(opts, "automounted") |
| 75 | + } |
| 76 | + if flags&unix.MNT_REMOVABLE != 0 { |
| 77 | + opts = append(opts, "removable") |
| 78 | + } |
| 79 | + if flags&unix.MNT_DONTBROWSE != 0 { |
| 80 | + opts = append(opts, "nobrowse") |
| 81 | + } |
| 82 | + if flags&unix.MNT_IGNORE_OWNERSHIP != 0 { |
| 83 | + opts = append(opts, "ignore-ownership") |
| 84 | + } |
| 85 | + if flags&unix.MNT_LOCAL != 0 { |
| 86 | + opts = append(opts, "local") |
| 87 | + } |
| 88 | + if flags&unix.MNT_ROOTFS != 0 { |
| 89 | + opts = append(opts, "rootfs") |
| 90 | + } |
| 91 | + if flags&unix.MNT_SNAPSHOT != 0 { |
| 92 | + opts = append(opts, "snapshot") |
| 93 | + } |
| 94 | + if flags&unix.MNT_CPROTECT != 0 { |
| 95 | + opts = append(opts, "cprotect") |
| 96 | + } |
| 97 | + if flags&unix.MNT_NOUSERXATTR != 0 { |
| 98 | + opts = append(opts, "nouserxattr") |
| 99 | + } |
| 100 | + if flags&unix.MNT_QUARANTINE != 0 { |
| 101 | + opts = append(opts, "quarantine") |
| 102 | + } |
| 103 | + if flags&unix.MNT_UNKNOWNPERMISSIONS != 0 { |
| 104 | + opts = append(opts, "unknown-permissions") |
| 105 | + } |
| 106 | + if flags&unix.MNT_DEFWRITE != 0 { |
| 107 | + opts = append(opts, "defwrite") |
| 108 | + } |
| 109 | + if flags&unix.MNT_DOVOLFS != 0 { |
| 110 | + opts = append(opts, "volfs") |
| 111 | + } |
| 112 | + if flags&unix.MNT_NOBLOCK != 0 { |
| 113 | + opts = append(opts, "noblock") |
| 114 | + } |
| 115 | + if flags&unix.MNT_RELOAD != 0 { |
| 116 | + opts = append(opts, "reload") |
| 117 | + } |
| 118 | + if flags&unix.MNT_UPDATE != 0 { |
| 119 | + opts = append(opts, "update") |
| 120 | + } |
| 121 | + if flags&unix.MNT_EXPORTED != 0 { |
| 122 | + opts = append(opts, "exported") |
| 123 | + } |
| 124 | + if flags&unix.MNT_MULTILABEL != 0 { |
| 125 | + opts = append(opts, "multilabel") |
| 126 | + } |
| 127 | + if flags&unix.MNT_STRICTATIME != 0 { |
| 128 | + opts = append(opts, "strictatime") |
| 129 | + } |
| 130 | + if flags&unix.MNT_FORCE != 0 { |
| 131 | + opts = append(opts, "force") |
| 132 | + } |
| 133 | + |
| 134 | + return strings.Join(opts, ",") |
| 135 | +} |
| 136 | + |
| 137 | +func GetMountInformation() ([]*sliverpb.MountInfo, error) { |
| 138 | + mountInfo := make([]*sliverpb.MountInfo, 0) |
| 139 | + |
| 140 | + count, err := unix.Getfsstat(nil, unix.MNT_NOWAIT) |
| 141 | + if err != nil || count == 0 { |
| 142 | + return mountInfo, err |
| 143 | + } |
| 144 | + |
| 145 | + stats := make([]unix.Statfs_t, count) |
| 146 | + count, err = unix.Getfsstat(stats, unix.MNT_NOWAIT) |
| 147 | + if err != nil { |
| 148 | + return mountInfo, err |
| 149 | + } |
| 150 | + |
| 151 | + stats = stats[:count] |
| 152 | + for _, stat := range stats { |
| 153 | + var mountData sliverpb.MountInfo |
| 154 | + mountData.VolumeName = getString(stat.Mntfromname[:]) |
| 155 | + mountData.MountPoint = getString(stat.Mntonname[:]) |
| 156 | + mountData.Label = "/" |
| 157 | + mountData.VolumeType = getString(stat.Fstypename[:]) |
| 158 | + mountData.MountOptions = formatMountFlags(stat.Flags) |
| 159 | + mountData.TotalSpace = stat.Blocks * uint64(stat.Bsize) |
| 160 | + mountData.FreeSpace = stat.Bavail * uint64(stat.Bsize) |
| 161 | + mountData.UsedSpace = (stat.Blocks - stat.Bfree) * uint64(stat.Bsize) |
| 162 | + mountInfo = append(mountInfo, &mountData) |
| 163 | + } |
| 164 | + |
| 165 | + return mountInfo, nil |
| 166 | +} |
0 commit comments