Help me in check udp port

my code show all ports opened in udp protocol
please help me to fix this

for i := 1; i <= 65535; i++ {
	
	go func(j int) {

		address := net.JoinHostPort(ip, strconv.Itoa(j))
		// 3 second timeout
		//fmt.Println(j)
		conn, err := net.DialTimeout("udp", address, 60*time.Second)

		//fmt.Println(err)
		if err != nil {
			faildcount++
			//m2 := Message{j, "Closed"}
			//list2 = append(list2, m2)
		} else {
			count++
			conn.Close()
			m := Message{j, "open"}
			list = append(list, m)

		}
		
	}(i)
}

please answer this

What is the question?

this code show all udp ports open
but not open

Sorry, I really do not understand.

  1. What do you expect to happen?
  2. What does happen instead?
  3. Can you show output of your program or tools you use to observe the behaviour and explain how the output is different from what you would expect?

package main

import (
“encoding/json”
“flag”
“fmt”
“io/ioutil”
“net”
“os”
“runtime”
“strconv”
“sync”
“time”
)

type Message struct {
Port int
Status string
}

var list []Message
var list2 []Message

func main() {

runtime.GOMAXPROCS(1)
//fmt.Println(time.Now().Weekday())
fmt.Println("Developed By Omid For test")
ipadd := flag.String("ip", "127.0.0.1", "IP Address To scan")
protocols := flag.String("proto", "tcp", "Protocol To scan")
flag.Parse()

if *protocols == "tcp" {
	fmt.Println("Scanning " + *ipadd + " TCP Ports")
	tcpscanner(*ipadd)

} else if *protocols == "udp" {
	fmt.Println("Scanning " + *ipadd + " UDP Ports")
	udpportscanner(*ipadd)

} else {
	fmt.Println("Enter Protocol by proto")
	fmt.Println("example to scan file.go -ip=localhost -proto=tcp")
}

}

func tcpscanner(ip string) {

count := 0
faildcount := 0
var wg sync.WaitGroup
for i := 1; i <= 65535; i++ {

	wg.Add(1)
	go func(j int) {

		address := net.JoinHostPort(ip, strconv.Itoa(j))
		// 3 second timeout
		//fmt.Println(j)
		conn, err := net.DialTimeout("tcp", address, 2*time.Second)
		//fmt.Println(err)
		if err != nil {
			faildcount++
			//m2 := Message{j, "Closed"}
			//list2 = append(list2, m2)
		} else {
			count++
			conn.Close()

			m := Message{j, "open"}
			list = append(list, m)

		}
		defer wg.Done()
		time.Sleep(5 * time.Second)
	}(i)

}
wg.Wait()
jso, _ := json.Marshal(list)
//jso2, _ := json.Marshal(list2)
//fmt.Println(string(jso))
week := time.Now().Weekday()
timm := time.Now().Minute()
week2 := fmt.Sprint(week)
timm2 := fmt.Sprint(timm)
if _, errf := os.Stat(ip + week2 + "tcpopen.json"); errf == nil {
	_ = ioutil.WriteFile(ip+week2+"tcpopen"+timm2+".json", jso, 0644)
} else {
	_ = ioutil.WriteFile(ip+week2+"tcpopen.json", jso, 0644)
}

fmt.Println("success count :" + strconv.Itoa(count))
fmt.Println("faild count :" + strconv.Itoa(faildcount))

}

func udpportscanner(ip string) {

count := 0
faildcount := 0
var wg sync.WaitGroup
for i := 1; i <= 65535; i++ {
	wg.Add(1)
	go func(j int) {

		address := net.JoinHostPort(ip, strconv.Itoa(j))
		// 3 second timeout
		//fmt.Println(j)
		conn, err := net.DialTimeout("udp", address, 60*time.Second)

		//fmt.Println(err)
		if err != nil {
			faildcount++
			//m2 := Message{j, "Closed"}
			//list2 = append(list2, m2)
		} else {
			count++
			conn.Close()
			m := Message{j, "open"}
			list = append(list, m)

		}
		defer wg.Done()
	}(i)
}
wg.Wait()
jso, _ := json.Marshal(list)
//jso2, _ := json.Marshal(list2)
//fmt.Println(string(jso))
/* _ = ioutil.WriteFile("ok2.json", jso, 0644)
_ = ioutil.WriteFile("closed2.json", jso2, 0644)*/
fmt.Println("success count :" + strconv.Itoa(count))
fmt.Println("faild count :" + strconv.Itoa(faildcount))
week := time.Now().Weekday()
timm := time.Now().Minute()
week2 := fmt.Sprint(week)
timm2 := fmt.Sprint(timm)
if _, errf := os.Stat(ip + week2 + "udpopen.json"); errf == nil {
	_ = ioutil.WriteFile(ip+week2+"udpopen"+timm2+".json", jso, 0644)
} else {
	_ = ioutil.WriteFile(ip+week2+"udpopen.json", jso, 0644)
}
/* if _, errf := os.Stat(ip + week2 + "udpclose.json"); errf == nil {
	_ = ioutil.WriteFile(ip+week2+"udpclose"+timm2+".json", jso2, 0644)
} else {
	_ = ioutil.WriteFile(ip+week2+"udpclose.json", jso2, 0644)
} */

}
use this code and show for example use go run ./filename.go -ip “127.0.0.1” -proto “udp”

should be just show opened udp port but show all ports opened

for example u can use nmap -sU 127.0.0.1 -p 1-65500

The major difference is, nmap sends a payload and only checks the response then. Just “dialing” does not establish a connection. UDP is stateless.

I hope this answers your question.

Also please make sure to use markdown to properly format your post.

On the desktop you’ll also see some controls over the text box that help with formatting.

On mobiles you have to toggle those controls via the hamburger menu in the top right of the text field.

tnx
but how i can send payload in golang and check udp port response ??

how i can send payload in golang and check udp port response ??

for this you find documentation in many tutorials and official net package documentation.
But in general for UDP you need to go to lower level because UDP does not have a CONNECTED status.

Honestly I never had this need so I don’t know how to do, I can suggest either:

  • use netstat
  • have a look to nmap’s source code

netstat show local
i need show remote ip ports
nmap source very difficult
if you have any source to help me plz send this

as I said I never did it; most likely is a low level networking topic and is independent from the language.

maybe you can have a look to ICMP; no idea if it can help

ICMP can’t tell you anything about UDP being listened on or not.

I have to be honest, it will be massively easier to just shell out to nmap and parse it’s output rather than implementing something half-backed from a non existent understanding of how UDP works and is related to also occuring ICMP “events”.

I do not claim to fully understand this myself, therefore I’d use a scanner that already exists and is battle proven.

please help and send example code

exec.Command("nmap", ...)