UPnP/SSDP를 사용하여 라우터에서 WAN IP를 얻는 방법

UPnP/SSDP를 사용하여 라우터에서 WAN IP를 얻는 방법

UPnP/SSDP를 사용하여 내 라우터가 내 WAN IP를 다시 보고하도록 하는 방법을 찾으려고 노력하고 있지만 지금까지는 내 네트워크에서 인터넷 액세스 장치를 지원하는 UPnP 목록조차 얻을 수 없습니다. 제가 보내는 요청은 다음과 같습니다.

$ cat request.txt
M-SEARCH * HTTP/1.1
HOST: 239.255.255.250:1900
MAN: "ssdp:discover"
MX: 3
ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1

다음 명령을 사용하세요.

$ nc -uvv 239.255.255.250 1900 < request.txt
Connection to 239.255.255.250 1900 port [udp/ssdp] succeeded!

연결 성공 메시지만 나오고 아무 것도 안 나오네요...

누구든지 나에게 어떤 조언을 줄 수 있습니까?메모리 주소제발!) 내가 뭘 잘못하고 있는지?

업데이트:좋아, 응답을 얻기 위해 다음을 사용하려고 했습니다 tcpdump. 내가 얻은 것은 다음과 같습니다.

$ sudo tcpdump -vv -A -s 0 -i en1 udp port 1900 and host 239.255.255.250 
NOTIFY * HTTP/1.1
Host: 239.255.255.250:1900
Cache-Control: max-age=60
Location: http://192.168.1.1:1780/InternetGatewayDevice.xml
NTS: ssdp:alive
Server: POSIX, UPnP/1.0 linux/5.100.104.2 
NT: urn:schemas-upnp-org:device:InternetGatewayDevice:1

다음 단계는 tcpdump의 출력을 구문 분석하고 헤더를 포함하지 않는 모든 응답을 필터링한 NT: urn:schemas-upnp-org:device:InternetGatewayDevice:1다음 라우터에 실제 SOAP 요청을 보내는 것입니다.

답변1

tcpdump답글을 보려면 또는 이와 유사한 것을 사용해야 합니다 .

nc당신이 요청을 보낸 끝점에서 응답을 찾고 있습니다. 그러나 요청을 보낸 엔드포인트는 일반 브로드캐스트 대상입니다. 응답은 일반 방송 대상이 아닌 응답하는 특정 장치에서 옵니다.

의 출력에 따르면 nc방송 대상에 연결되었습니다. 따라서 응답 장치에서 오는 응답을 볼 수 없습니다.

답변2

이 작업은 다소 오래되었지만 제가 찾고 있던 답변을 직접 게시합니다.

귀하의 UPnP 응답에 "http://192.168.1.1:1780/InternetGatewayDevice.xml"

여기에서 데이터 형식에 대한 자세한 정보를 얻을 수 있습니다. 내 라우터의 경우: XML에 대한 더 많은 경로가 있습니다. 전화해보시면 더 많은 UPnP 정보가 있습니다. 그 결과는 다음과 같습니다. 나는 다음과 같이 전화했습니다.

POST /upnp/control?WANIPConnection HTTP/1.1
Host: 192.168.1.1
SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress"
Accept-Language: de-de;q=1, de;q=0.5
Accept-Encoding: gzip
Content-Type: text/xml; charset="utf-8"
User-Agent: gupnp-universal-cp GUPnP/0.20.10 DLNADOC/1.50
Connection: Keep-Alive
Content-Length: 281

<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:GetExternalIPAddress></s:Body></s:Envelope>

답변을 얻으십시오.

HTTP/1.1 200 OK
EXT:
Content-Type: text/xml; charset="utf-8"
Date: Tue, 04 Aug 2015 23:55:01 GMT
Server: servername/2.0 UPnP/1.0 UPnP-Device-Host/1.0
Content-Length: 380

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <s:Body>
        <u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
            <NewExternalIPAddress>123.123.123.123</NewExternalIPAddress>
        </u:GetExternalIPAddressResponse>
    </s:Body>
</s:Envelope>

"UPnP Inspector"로부터 많은 도움을 받았습니다.

답변3

당신이 사용할 수있는socat

$ socat -T1 STDIO UDP4-DATAGRAM:239.255.255.250:1900 < request.txt

답변4

완전히 작동하는 BASH 스크립트:

#!/usr/bin/env bash
function wan_ip_connection() {
    local host=$1
    result=$( curl -s "http://$host/upnp/control?WANIPConnection" \
       -H 'SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress"' \
       --data-binary '<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:GetExternalIPAddress></s:Body></s:Envelope>'
    )

    REGEX='ExternalIPAddress>([0-9.]+)<'
    if [[ $result =~ $REGEX ]]
    then
       echo "${BASH_REMATCH[@]:1}"
    fi
}

function initial_query() {
   echo -e 'M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: "ssdp:discover"\r\nMX: 3\r\nST: urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n\r\n' |
       socat STDIO UDP4-DATAGRAM:239.255.255.250:1900
}

function main() {
   location=$( initial_query | grep LOCATION )
   location=${location%/*}
   location=${location##*/}
   ip=$( wan_ip_connection "$location" )
   echo $ip
}

main

관련 정보