-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
201 lines (176 loc) · 5.58 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"errors"
"fmt"
"io"
"log"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/urfave/cli"
)
const (
version = "dev"
commit = "HEAD"
builtBy = "dev"
)
func main() {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
app := cli.NewApp()
app.Name = "bitflip"
app.Usage = "Flip bits in files."
app.Author = "Antoine Grondin"
app.Email = "[email protected]"
app.Version = version + "@" + commit
app.Description = "If you want to flip bits in files, this utility has got you covered. Flip specific bits, randomly flip a single bit, or spray hell all around..! Whatever rocks your boat, as long as you need some bits flipped."
app.Metadata = map[string]interface{}{
"commit": commit,
"builtBy": builtBy,
}
app.Commands = []cli.Command{
cli.Command{
Name: "offset",
Description: "flip a bit in a file at an offset",
Usage: "byte-offset filepath",
ArgsUsage: `byte-offset filepath
byte-offset: of the form <offset>@<bit>, where:
- <offset> is a integer speciying in which byte the flip should occur
- <bit> is an integer from 0 to 7 speciying which bit to flip (from LSB)
filepath: is the file in which to do the bitflipping`,
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 2 {
return cli.ShowCommandHelp(cctx, "offset")
}
byteOffset, bitOffset, err := parseByteOffset(cctx.Args().Get(0))
if err != nil {
return err
}
filename := cctx.Args().Get(1)
file, err := os.OpenFile(filename, os.O_RDWR, 0)
if err != nil {
return err
}
defer file.Close()
log.Printf("flipping %dth bit of byte %d in file %q", bitOffset, byteOffset, filename)
if err := flipBitAtOffset(file, int64(byteOffset), bitOffset); err != nil {
return fmt.Errorf("flipping bit: %v", err)
}
return nil
},
},
cli.Command{
Name: "random",
Description: "flip a random bit in a file",
Usage: "filepath",
ArgsUsage: `filepath
filepath: is the file in which to do the random bitflipping`,
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 1 {
return cli.ShowCommandHelp(cctx, "random")
}
filename := cctx.Args().Get(0)
file, err := os.OpenFile(filename, os.O_RDWR, 0)
if err != nil {
return err
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
return err
}
byteOffset := r.Int63n(fi.Size())
bitOffset := uint8(r.Intn(8))
log.Printf("flipping %dth bit of byte %d in file %q", bitOffset, byteOffset, filename)
if err := flipBitAtOffset(file, int64(byteOffset), bitOffset); err != nil {
return fmt.Errorf("flipping bit: %v", err)
}
return nil
},
},
cli.Command{
Name: "spray",
Description: "flip a bunch of bits at random in a file",
Usage: "spray-pattern filepath",
ArgsUsage: `spray-pattern filepath
spray-pattern: is a pattern that describe how to spray bitflips. It takes the form
<type>:<args> where <type> is a type of spray, and <args> depends on
the type. Valid values for <type> are:
- percent: pattern "percent:double" sprays a random and uniform percentage of bits. e.g:
bitflip spray percent:11.5 /var/lib/mysqld/db
filepath: is the file in which to do the random bitflipping`,
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 2 {
return cli.ShowCommandHelp(cctx, "spray")
}
sprayerFactory, err := parseSprayPattern(cctx.Args().Get(0))
if err != nil {
return err
}
filename := cctx.Args().Get(1)
file, err := os.OpenFile(filename, os.O_RDWR, 0)
if err != nil {
return err
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
return err
}
sprayer := sprayerFactory(fi)
return sprayer.Spray(file, flipBitAtOffset)
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func parseByteOffset(offset string) (byteOffset uint64, bitOffset uint8, err error) {
args := strings.Split(offset, "@")
if len(args) > 2 {
return byteOffset, bitOffset, errors.New("invalid offset string, too many '@' symbols found")
}
if len(args) < 2 {
return byteOffset, bitOffset, errors.New("invalid offset string, no '@' symbols found")
}
iByteOffset, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return byteOffset, bitOffset, fmt.Errorf("invalid byte offset: %v", err)
}
iBitOffset, err := strconv.ParseUint(args[1], 10, 8)
if err != nil {
return byteOffset, bitOffset, fmt.Errorf("invalid bit offset: %v", err)
}
if iBitOffset > 7 {
return byteOffset, bitOffset, fmt.Errorf("bit offset must be between 0 and 7, was %d", iBitOffset)
}
byteOffset = uint64(iByteOffset)
bitOffset = uint8(iBitOffset)
return byteOffset, bitOffset, nil
}
type bitflipFunc func(file io.ReadWriteSeeker, byteOffset int64, bitOffset uint8) error
func flipBitAtOffset(file io.ReadWriteSeeker, byteOffset int64, bitOffset uint8) error {
_, err := file.Seek(byteOffset, io.SeekStart)
if err != nil {
return fmt.Errorf("seeking to flip offset: %v", err)
}
oneByte := make([]byte, 1)
if _, err := file.Read(oneByte); err != nil {
return fmt.Errorf("reading data from flip offset: %v", err)
}
oneByte[0] = toggleNthBit(oneByte[0], bitOffset)
_, err = file.Seek(-1, io.SeekCurrent)
if err != nil {
return fmt.Errorf("seeking back to flip offset: %v", err)
}
if _, err := file.Write(oneByte); err != nil {
return fmt.Errorf("writing back the flipped bit: %v", err)
}
return nil
}
func toggleNthBit(b byte, n uint8) byte {
singleBitMask := uint8(1) << (n)
return b ^ singleBitMask
}