-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jozef Reisinger
committed
Sep 9, 2021
1 parent
bc344e8
commit d11fcfa
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package checkip | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net" | ||
"os" | ||
) | ||
|
||
// ET (Emerging Threats) says whether the IP address was found among compromised | ||
// IP addresses according to rules.emergingthreats.net | ||
type ET struct { | ||
CompromisedIP bool | ||
CheckedIPs int | ||
} | ||
|
||
// Check checks whether the ippaddr is not among compromised IP addresses from | ||
// The Emerging Threats Intelligence feed (ET). | ||
// https://logz.io/blog/open-source-threat-intelligence-feeds/ | ||
func (e *ET) Check(ipaddr net.IP) (bool, error) { | ||
file := "/var/tmp/et.txt" | ||
url := "https://rules.emergingthreats.net/blockrules/compromised-ips.txt" | ||
|
||
if err := update(file, url, ""); err != nil { | ||
return true, fmt.Errorf("can't update %s from %s: %v", file, url, err) | ||
} | ||
|
||
if err := e.search(ipaddr, file); err != nil { | ||
return true, fmt.Errorf("searching %s in %s: %v", ipaddr, file, err) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// search searches the ippadrr in filename fills in ET data. | ||
func (e *ET) search(ipaddr net.IP, filename string) error { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s := bufio.NewScanner(file) | ||
for s.Scan() { | ||
line := s.Text() | ||
e.CheckedIPs++ | ||
if line == ipaddr.String() { | ||
e.CompromisedIP = true | ||
return nil | ||
} | ||
} | ||
if s.Err() != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// String returns the result of the check. | ||
func (e *ET) String() string { | ||
s := fmt.Sprintf("found among %d compromised IP addresses", e.CheckedIPs) | ||
if !e.CompromisedIP { | ||
s = "not " + s | ||
} | ||
return s | ||
} |