Skip to content

Commit 9bf9dfa

Browse files
juzeonyuhan6665
authored andcommitted
add geoip
1 parent 91ff827 commit 9bf9dfa

File tree

7 files changed

+90
-16
lines changed

7 files changed

+90
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ results.txt
55
in.txt
66
out.txt
77
out.csv
8+
Country.mmdb

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ It is recommended to run this tool locally, as running the scanner in the cloud
4141
./RealiTLScanner -addr 107.172.1.1/16 -timeout 5
4242
```
4343

44+
### Enable Geo IP
45+
46+
To enable Geo IP information, place a MaxMind GeoLite2/GeoIP2 Country Database in the executing folder with the exact name `Country.mmdb`. You can download one from [here](https://github.com/Loyalsoldier/geoip/releases/latest/download/Country.mmdb).
47+
48+
## Demo
49+
4450
Example stdout:
51+
4552
```bash
4653
2024/02/08 20:51:10 INFO Started all scanning threads time=2024-02-08T20:51:10.017+08:00
4754
2024/02/08 20:51:10 INFO Connected to target feasible=true host=107.172.103.9 tls=1.3 alpn=h2 domain=rocky-linux.tk issuer="Let's Encrypt"
@@ -57,16 +64,16 @@ Example stdout:
5764
Example output file:
5865

5966
```csv
60-
IP,ORIGIN,CERT_DOMAIN,CERT_ISSUER
61-
52.140.219.235,www.cherryservers.com,*.cherryservers.com,"GlobalSign nv-sa"
62-
172.66.40.234,veesp.com,veesp.com,"Cloudflare, Inc."
63-
172.66.43.22,veesp.com,veesp.com,"Cloudflare, Inc."
64-
193.1.193.205,www.heanet.ie,www.heanet.ie,"GEANT Vereniging"
65-
185.242.104.18,mirror.veesp.com,mirror.veesp.com,"Let's Encrypt"
66-
79.98.24.240,www.serveriai.lt,*.serveriai.lt,"Sectigo Limited"
67-
91.211.244.3,www.vpsnet.com,*.vpsnet.com,"Sectigo Limited"
68-
31.131.0.101,www.ihost.md,ihost.md,"Sectigo Limited"
69-
194.127.172.131,nl.mirrors.clouvider.net,nl.mirrors.clouvider.net,"Let's Encrypt"
70-
31.131.0.222,mirror.ihost.md,mirror.ihost.md,"Let's Encrypt"
67+
IP,ORIGIN,CERT_DOMAIN,CERT_ISSUER,GEO_CODE
68+
202.70.64.2,ntc.net.np,*.ntc.net.np,"GlobalSign nv-sa",NP
69+
196.200.160.70,mirror.marwan.ma,mirror.marwan.ma,"Let's Encrypt",MA
70+
103.194.167.213,mirror.i3d.net,*.i3d.net,"Sectigo Limited",JP
71+
194.127.172.131,nl.mirrors.clouvider.net,nl.mirrors.clouvider.net,"Let's Encrypt",NL
72+
202.36.220.86,mirror.2degrees.nz,mirror.2degrees.nz,"Let's Encrypt",NZ
73+
202.36.220.86,ubuntu.mirrors.theom.nz,mirror.2degrees.nz,"Let's Encrypt",NZ
74+
158.37.28.65,ubuntu.hi.no,alma.hi.no,"Let's Encrypt",NO
75+
193.136.164.6,ftp.rnl.tecnico.ulisboa.pt,ftp.rnl.ist.utl.pt,"Let's Encrypt",PT
76+
75.2.60.5,cesium.di.uminho.pt,cesium.di.uminho.pt,"Let's Encrypt",US
77+
195.14.50.21,mirror.corbina.net,ftp.corbina.net,"Let's Encrypt",RU
7178
```
7279

geo.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"github.com/oschwald/geoip2-golang"
5+
"log/slog"
6+
"net"
7+
"sync"
8+
)
9+
10+
type Geo struct {
11+
geoReader *geoip2.Reader
12+
mu sync.Mutex
13+
}
14+
15+
func NewGeo() *Geo {
16+
geo := &Geo{
17+
mu: sync.Mutex{},
18+
}
19+
reader, err := geoip2.Open("Country.mmdb")
20+
if err != nil {
21+
slog.Warn("Cannot open Country.mmdb")
22+
return geo
23+
}
24+
slog.Info("Enabled GeoIP")
25+
geo.geoReader = reader
26+
return geo
27+
}
28+
29+
func (o *Geo) GetGeo(ip net.IP) string {
30+
if o.geoReader == nil {
31+
return "N/A"
32+
}
33+
o.mu.Lock()
34+
defer o.mu.Unlock()
35+
country, err := o.geoReader.Country(ip)
36+
if err != nil {
37+
slog.Debug("Error reading geo", "err", err)
38+
return "N/A"
39+
}
40+
return country.Country.IsoCode
41+
}

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
module github.com/xtls/RealiTLScanner
22

33
go 1.21
4+
5+
require github.com/oschwald/geoip2-golang v1.9.0
6+
7+
require (
8+
github.com/oschwald/maxminddb-golang v1.12.0 // indirect
9+
golang.org/x/sys v0.17.0 // indirect
10+
)

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/oschwald/geoip2-golang v1.9.0 h1:uvD3O6fXAXs+usU+UGExshpdP13GAqp4GBrzN7IgKZc=
4+
github.com/oschwald/geoip2-golang v1.9.0/go.mod h1:BHK6TvDyATVQhKNbQBdrj9eAvuwOMi2zSFXizL3K81Y=
5+
github.com/oschwald/maxminddb-golang v1.12.0 h1:9FnTOD0YOhP7DGxGsq4glzpGy5+w7pq50AS6wALUMYs=
6+
github.com/oschwald/maxminddb-golang v1.12.0/go.mod h1:q0Nob5lTCqyQ8WT6FYgS1L7PXKVVbgiymefNwIjPzgY=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
10+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
11+
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
12+
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
13+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
14+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func main() {
6161
return
6262
}
6363
defer f.Close()
64-
_, _ = f.WriteString("IP,ORIGIN,CERT_DOMAIN,CERT_ISSUER\n")
64+
_, _ = f.WriteString("IP,ORIGIN,CERT_DOMAIN,CERT_ISSUER,GEO_CODE\n")
6565
outWriter = f
6666
}
6767
var hostChan <-chan Host
@@ -99,12 +99,13 @@ func main() {
9999
}
100100
outCh := OutWriter(outWriter)
101101
defer close(outCh)
102+
geo := NewGeo()
102103
var wg sync.WaitGroup
103104
wg.Add(thread)
104105
for i := 0; i < thread; i++ {
105106
go func() {
106107
for ip := range hostChan {
107-
ScanTLS(ip, outCh)
108+
ScanTLS(ip, outCh, geo)
108109
}
109110
wg.Done()
110111
}()

scanner.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"time"
1010
)
1111

12-
func ScanTLS(host Host, out chan<- string) {
12+
func ScanTLS(host Host, out chan<- string, geo *Geo) {
1313
if host.IP == nil {
1414
ip, err := LookupIP(host.Origin)
1515
if err != nil {
@@ -50,14 +50,17 @@ func ScanTLS(host Host, out chan<- string) {
5050
issuers := strings.Join(state.PeerCertificates[0].Issuer.Organization, " | ")
5151
log := slog.Info
5252
feasible := true
53+
geoCode := geo.GetGeo(host.IP)
5354
if state.Version != tls.VersionTLS13 || alpn != "h2" || len(domain) == 0 || len(issuers) == 0 {
5455
// not feasible
5556
log = slog.Debug
5657
feasible = false
5758
} else {
58-
out <- strings.Join([]string{host.IP.String(), host.Origin, domain, "\"" + issuers + "\""}, ",") + "\n"
59+
out <- strings.Join([]string{host.IP.String(), host.Origin, domain, "\"" + issuers + "\"", geoCode}, ",") +
60+
"\n"
5961
}
6062
log("Connected to target", "feasible", feasible, "ip", host.IP.String(),
6163
"origin", host.Origin,
62-
"tls", tls.VersionName(state.Version), "alpn", alpn, "cert-domain", domain, "cert-issuer", issuers)
64+
"tls", tls.VersionName(state.Version), "alpn", alpn, "cert-domain", domain, "cert-issuer", issuers,
65+
"geo", geoCode)
6366
}

0 commit comments

Comments
 (0)