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

Devel update #3

Merged
merged 8 commits into from
Nov 5, 2023
Merged
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
14 changes: 14 additions & 0 deletions .github/auto_assign.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Set to true to add reviewers to pull requests
addReviewers: true

# A list of reviewers to be added to pull requests (GitHub user name)
reviewers:
- edoardottt

# A list of keywords to be skipped the process that add reviewers if pull requests include it
skipKeywords:
- wip

# A number of reviewers added to the pull request
# Set 0 to add all the reviewers (default: 0)
numberOfReviewers: 0
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2
updates:

# Maintain dependencies for go modules
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "weekly"
target-branch: "devel"
commit-message:
prefix: "chore"
include: "scope"
labels:
- "Maintenance"
28 changes: 28 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "main","devel" ]
pull_request:
branches: [ "main","devel" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.21'

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
46 changes: 46 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- devel
- main
pull_request:
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.21
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.55

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
# args: --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the all caching functionality will be complete disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
52 changes: 52 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
linters:
# Enable all available linters.
# Default: false
disable-all: true
enable:
- asciicheck
- bodyclose
- dogsled
- dupl
- errcheck
- exhaustive
- exportloopref
- gochecknoglobals
- gochecknoinits
- goconst
- gocritic
- godot
- godox
- goerr113
- goheader
- gomnd
- gomodguard
- goprintffuncname
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- nolintlint
- prealloc
- rowserrcheck
- sqlclosecheck
- staticcheck
- stylecheck
- testpackage
- typecheck
- unconvert
- unparam
- unused
- whitespace
- wsl

linters-settings:
wsl:
strict-append: false
enforce-err-cuddling: true

issues:
exclude-rules:
- path: defan.go
text: "maybe you wanted to do Index"
72 changes: 70 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,70 @@
# defango
URL / IP / Email defanging with Golang. Make IoC harmless.
<h1 align="center">
defango
<br>
</h1>

<h4 align="center">URL / IP / Email defanging with Golang. Make IoC harmless.</h4>

<h6 align="center"> Coded with 💙 by edoardottt </h6>

<p align="center">

<a href="https://github.com/edoardottt/defango/actions">
<img src="https://github.com/edoardottt/defango/actions/workflows/go.yml/badge.svg" alt="go action">
</a>

<a href="https://goreportcard.com/report/github.com/edoardottt/defango">
<img src="https://goreportcard.com/badge/github.com/edoardottt/defango" alt="go report card">
</a>

<br>
<!--Tweet button-->
<a href="https%3A%2F%2Ftwitter.com%2Fintent%2Ftweet%3Ftext%3Ddefango%20-%20URL%20%2F%20IP%20%2F%20Email%20defanging%20with%20Golang.%20Make%20IoC%20harmless.%20https%3A%2F%2Fgithub.com%2Fedoardottt%2Fdefango%20%23golang%20%23github%20%23linux%20%23infosec%20%23malware" target="_blank">Share on Twitter!
</a>
</p>

<p align="center">
<a href="#install-">Install</a> •
<a href="#get-started-">Get Started</a> •
<a href="#changelog-">Changelog</a> •
<a href="#contributing-">Contributing</a> •
<a href="#license-">License</a>
</p>

Install 📡
----------

```console
go install github.com/edoardottt/defango@latest
```

Get Started 🎉
----------

```go
...
```

Changelog 📌
-------

Detailed changes for each release are documented in the [release notes](https://github.com/edoardottt/defango/releases).

Contributing 🛠
-------

Just open an [issue](https://github.com/edoardottt/defango/issues) / [pull request](https://github.com/edoardottt/defango/pulls).

Before opening a pull request, download [golangci-lint](https://golangci-lint.run/usage/install/) and run

```bash
golangci-lint run
```

If there aren't errors, go ahead :)

License 📝
-------

This repository is under [MIT License](https://github.com/edoardottt/defango/blob/main/LICENSE).
[edoardoottavianelli.it](https://www.edoardoottavianelli.it) to contact me.
87 changes: 87 additions & 0 deletions defan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
defango - URL / IP / Email defanging with Golang. Make IoC harmless.

This repository is under MIT License https://github.com/edoardottt/defango/blob/main/LICENSE
*/

package defango

import (
"errors"
"net/url"
"strings"
)

var (
ErrUnsupportedInputType = errors.New("unsupported input type")
)

// IP returns a defanged IP.
// Support both IPv4 and IPv6.
func IP(input string) string {
result := ""

if strings.Contains(input, "::") { // IPv6 with double column
index := strings.Index(input, "::")
result = strings.ReplaceAll(input[:index], ":", "[:]") +
"[::]" +
strings.ReplaceAll(input[index+2:], ":", "[:]")
} else { // Full IPv6 or IPv4
result = strings.ReplaceAll(strings.ReplaceAll(input, ".", "[.]"), ":", "[:]")
}

return result
}

// URL returns a defanged URL.
// Accept string and url.URL.
// For security reasons, if the url is not formatted in a proper way
// the result is an empty string with a non-nil error.
func URL(input interface{}) (string, error) {
switch v := input.(type) {
case string:
return defangURL(v)
case url.URL:
return defangURL(v.String())
default:
return "", ErrUnsupportedInputType
}
}

func defangURL(input string) (string, error) {
result := ""

if strings.Contains(input, "://") { // handle protocol
result += defangProtocols(input) + "://"
result += strings.ReplaceAll(
strings.ReplaceAll(
input[strings.Index(input, "://")+3:], ".", "[.]"),
":", "[:]")
} else {
result += strings.ReplaceAll(strings.ReplaceAll(input, ".", "[.]"), ":", "[:]")
}

return result, nil
}

func defangProtocols(input string) string {
protoMap := map[string]string{
"http": "hxxp",
"https": "hxxps",
"ftp": "fxp",
"file": "fxle",
}
proto := input[:strings.Index(input, "://")]

protoDefanged, ok := protoMap[proto]
if ok {
return protoDefanged
} else {
return proto
}
}

// Email returns a defanged email address/link.
func Email(input string) string {
return strings.ReplaceAll(strings.ReplaceAll(input, ".", "[.]"), ":", "[:]")
}
Loading