Recent

Author Topic: [SOLVED] How to open UPnP port with synapse  (Read 11266 times)

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #30 on: June 13, 2023, 03:57:08 pm »
That look correct. (you do see the port changing each time but that shouldn't be a problem, it's the port the router provides.)

I don't know what we did different from 15:02:01 where the AddPort was done correctly.
(you could try to go back to that version)

But still... if even upnpc can't pick a whole in the router, I wonder if there is something else that's wrong.

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #31 on: June 13, 2023, 04:04:35 pm »
Your Post at:
« Reply #9 on: Today at 02:18:33 pm »


gives:

Quote
Location is http://192.168.0.1:49153/IGDdevicedesc_brlan0.xml
Base is http://192.168.0.1:49153
SCPDURL is http://192.168.0.1:49153/WANIPConnectionServiceSCPD.xml
Control URL is http://192.168.0.1:49153/upnp/control/WANIPConnection0
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring>UPnPError</faultstring>
<detail>
<UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
<errorCode>402</errorCode>
<errorDescription>PAL_UPNP_SOAP_E_INVALID_ARGS</errorDescription>
</UPnPError>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>

we are done, press enter





PS. I restored the code

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #32 on: June 13, 2023, 04:06:37 pm »
Your Post at:
« Reply #9 on: Today at 02:18:33 pm »
Yes, but your post from 15:02 has a valid xml response from the router for AddPort.

Quote
Location is http://192.168.0.1:49155/IGDdevicedesc_brlan0.xml
Base is http://192.168.0.1:49155
SCPDURL is http://192.168.0.1:49155/WANIPConnectionServiceSCPD.xml
Control URL is http://192.168.0.1:49155/upnp/control/WANIPConnection0
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>
<u:AddPortMappingResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:AddPortMappingResponse>
</s:Body> </s:Envelope>

we are done, press enter


How did you manage that?


Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #33 on: June 13, 2023, 04:11:53 pm »
You mean this:
Quote
Location is http://192.168.0.1:49153/IGDdevicedesc_brlan0.xml
Base is http://192.168.0.1:49153
SCPDURL is http://192.168.0.1:49153/WANIPConnectionServiceSCPD.xml
Control URL is http://192.168.0.1:49153/upnp/control/WANIPConnection0
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>
<u:AddPortMappingResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:AddPortMappingResponse>
</s:Body> </s:Envelope>

we are done, press enter


it's:

Code: Pascal  [Select][+][-]
  1.     program upnp;
  2.      
  3.     {$MODE OBJFPC}{$H+}
  4.     uses
  5.       Classes,
  6.       blcksock,
  7.       synautil,
  8.       SysUtils,
  9.       RegExpr,
  10.       httpsend;
  11.      
  12.       function GetStringBetweenAndStrip(var str: string; startStr, endStr: string): string;
  13.       var
  14.         startPos, endPos: integer;
  15.       begin
  16.         startStr := uppercase(startStr);
  17.         endStr := uppercase(endStr);
  18.         startPos := Pos(startStr, uppercase(str));
  19.         if startPos = 0 then exit('');
  20.         startPos := startPos + Length(startStr);
  21.         endPos := Pos(endStr, uppercase(str), startPos);
  22.         if endPos = 0 then exit('');
  23.         Result := Copy(str, startPos, endPos - startPos);
  24.         // strip the string
  25.         startPos := startPos - Length(startStr);
  26.         endPos := endPos + Length(endStr);
  27.         System.Delete(str, startPos, endPos - startPos);
  28.       end;
  29.      
  30.     var
  31.       GatewayIP, Location, Base, Found, Found1, Service, S: string;
  32.       ServiceType: string;
  33.       Socket: TUDPBlockSocket;
  34.       Response: TStringList;
  35.       HTTP: THTTPSend;
  36.       ResultCode: integer;
  37.       Idx: integer;
  38.       eH, eP, Pr, iP, iH, iE, iD, iL: string;
  39.     begin
  40.      
  41.       GatewayIP := '192.168.0.1';  // I used hardcoded Gateway here. You can also listen for any other upnp device and pick that one.
  42.      
  43.       S := 'M-SEARCH * HTTP/1.1'#13#10 + 'HOST: 239.255.255.250:1900'#13#10 + 'MAN: "ssdp:discover"'#13#10 + 'MX: 3'#13#10 + 'ST: upnp:rootdevice'#13#10#13#10;
  44.       Socket := TUDPBlockSocket.Create;
  45.       Socket.EnableBroadcast(True);
  46.       Socket.Connect(GatewayIP, '1900');
  47.       Socket.SendString(S);
  48.       repeat
  49.         if Socket.CanRead(3000) then
  50.         begin
  51.           Found := Socket.RecvPacket(3000);
  52.           if Pos('LOCATION: ', uppercase(Found)) > 0 then
  53.           begin
  54.             Location := Copy(Found, Pos('LOCATION:', uppercase(Found)) + 9);
  55.             Location := Trim(Copy(Location, 1, Pos(#13#10, Location) - 1));
  56.             writeln('Location is ' + Location);
  57.             Response := TStringList.Create;
  58.             try
  59.      
  60.               HttpGetText(Location, Response);
  61.               Found := Response.Text;
  62.      
  63.               Base := Location; // take base of Location for control
  64.               while (Base <> '') and (Location[Length(Base)] <> '/') do Delete(Base, Length(Base), 1);
  65.               if Base <> '' then Delete(Base, Length(Base), 1);
  66.               writeln('Base is ' + Base);
  67.      
  68.               // loop all services
  69.               repeat
  70.                 Service := GetStringBetweenAndStrip(Found, '<service>', '</service>');
  71.                 if Pos(uppercase(':WANIPConnection:'), uppercase(service)) > 0 then
  72.                 begin
  73.                   S := GetStringBetweenAndStrip(Service, '<SCPDURL>', '</SCPDURL>');
  74.                   if S <> '' then
  75.                   begin
  76.                     Location := Base + S;
  77.                     writeln('SCPDURL is ' + Location);
  78.                     HttpGetText(Location, Response);
  79.                     S := Response.Text;
  80.                     if Pos(uppercase('<name>AddPortMapping</name>'), uppercase(S)) > 0 then
  81.                     begin
  82.                       ServiceType := GetStringBetweenAndStrip(Service, '<serviceType>', '</serviceType>');
  83.                       S := GetStringBetweenAndStrip(Service, '<controlURL>', '</controlURL>');
  84.                       Location := Base + S;
  85.                       writeln('Control URL is ' + Location);
  86.      
  87.                       // HERE WE INSERT
  88.      
  89.                       s := '<?xml version="1.0"?>';
  90.                   s := s + '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">';
  91.                   s := s + '<SOAP-ENV:Body>';
  92.                   s := s + '    <m:AddPortMapping xmlns:m="urn:schemas-upnp-org:service:WANIPConnection:1">';
  93.                   s := s + '        <NewRemoteHost></NewRemoteHost>';
  94.                   s := s + '        <NewExternalPort>3331</NewExternalPort>';
  95.                   s := s + '        <NewProtocol>UDP</NewProtocol>';
  96.                   s := s + '        <NewInternalPort>4444</NewInternalPort>';
  97.                   s := s + '        <NewInternalClient>192.168.0.4</NewInternalClient>';
  98.                   s := s + '        <NewEnabled>1</NewEnabled>';
  99.                   s := s + '        <NewPortMappingDescription>test</NewPortMappingDescription>';
  100.                   s := s + '        <NewLeaseDuration>0</NewLeaseDuration>';
  101.                   s := s + '    </m:AddPortMapping>';
  102.                   s := s + '</SOAP-ENV:Body>';
  103.                   s := s + '</SOAP-ENV:Envelope>';
  104.      
  105.                       HTTP := THTTPSend.Create;
  106.                       try
  107.      
  108.                         WriteStrToStream(HTTP.Document, S);
  109.                         HTTP.MimeType := 'text/xml; charset="utf-8"';
  110.                         http.Headers.Add('SOAPAction: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"');
  111.      
  112.                         if HTTP.HTTPMethod('POST', Location) then
  113.                         begin
  114.                           Resultcode := HTTP.ResultCode;
  115.                           Response.LoadFromStream(HTTP.Document);
  116.                           Found := Response.Text;
  117.                           writeln(found);
  118.                         end;
  119.      
  120.                       finally
  121.                         HTTP.Free;
  122.                       end;
  123.      
  124.                       // HERE WE CHECK
  125.      
  126.      
  127.                       Idx := 0;
  128.                       repeat
  129.      
  130.                         S := '';
  131.                         S := S + '<?xml version="1.0" encoding="utf-8"?>';
  132.                         S := S + '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">';
  133.                         S := S + ' <s:Body>';
  134.                         S := S + '  <u:GetGenericPortMappingEntry xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">';
  135.                         S := S + '   <NewPortMappingIndex>' + IntToStr(Idx) + '</NewPortMappingIndex>';
  136.                         S := S + '  </u:GetGenericPortMappingEntry>';
  137.                         S := S + ' </s:Body>';
  138.                         S := S + '</s:Envelope>';
  139.      
  140.                         HTTP := THTTPSend.Create;
  141.                         try
  142.      
  143.                           WriteStrToStream(HTTP.Document, S);
  144.                           HTTP.MimeType := 'text/xml; charset="utf-8"';
  145.                           HTTP.Headers.Add('SOAPAction: ' + ServiceType + '#GetGenericPortMappingEntry');
  146.      
  147.                           if HTTP.HTTPMethod('POST', Location) then
  148.                           begin
  149.                             Resultcode := HTTP.ResultCode;
  150.                             Response.LoadFromStream(HTTP.Document);
  151.                             Found := Response.Text;
  152.                             // writeln(found);
  153.                             repeat
  154.                               Found1 := GetStringBetweenAndStrip(Found, '<u:GetGenericPortMappingEntryResponse', '</u:GetGenericPortMappingEntryResponse>');
  155.                               if Found1 <> '' then
  156.                               begin
  157.                                 eH := GetStringBetweenAndStrip(Found1, '<NewRemoteHost>', '</NewRemoteHost>');
  158.                                 eP := GetStringBetweenAndStrip(Found1, '<NewExternalPort>', '</NewExternalPort>');
  159.                                 Pr := GetStringBetweenAndStrip(Found1, '<NewProtocol>', '</NewProtocol>');
  160.                                 iP := GetStringBetweenAndStrip(Found1, '<NewInternalPort>', '</NewInternalPort>');
  161.                                 iH := GetStringBetweenAndStrip(Found1, '<NewInternalClient>', '</NewInternalClient>');
  162.                                 iE := GetStringBetweenAndStrip(Found1, '<NewEnabled>', '<NewEnabled>');
  163.                                 iD := GetStringBetweenAndStrip(Found1, '<NewPortMappingDescription>', '</NewPortMappingDescription>');
  164.                                 iL := GetStringBetweenAndStrip(Found1, '<NewLeaseDuration>', '</NewLeaseDuration>');
  165.                                 S := Format('%s %s:%s -> %s:%s En: %s  De: %s  Le: %s', [Pr, eH, eP, iH, iP, iE, iD, iL]);
  166.                                 writeln(S);
  167.                               end;
  168.                             until Found1 = '';
  169.                           end;
  170.      
  171.                         finally
  172.                           HTTP.Free;
  173.                         end;
  174.                         Inc(Idx);
  175.      
  176.                       until Resultcode <> 200;
  177.      
  178.                     end;
  179.      
  180.                   end;
  181.      
  182.                 end;
  183.      
  184.               until service = '';
  185.      
  186.             finally
  187.               Response.Free;
  188.             end;
  189.           end;
  190.           break;
  191.         end;
  192.         sleep(100);
  193.       until False;
  194.       Socket.CloseSocket;
  195.       Socket.Free;
  196.       writeln('we are done, press enter');
  197.       readln;
  198.      
  199.     end.
  200.  

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #34 on: June 13, 2023, 04:15:09 pm »
You mean this:
[..]
it's:
And that still gives the correct xml ??

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #35 on: June 13, 2023, 04:16:51 pm »
I restored that code and executed it.

Is the response what You expected?

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #36 on: June 13, 2023, 04:17:47 pm »
I restored that code and executed it.

Is the response what You expected?
If the response is this then yes. that's correct.

Code: XML  [Select][+][-]
  1. <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>
  2. <u:AddPortMappingResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:AddPortMappingResponse>
  3. </s:Body> </s:Envelope>

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #37 on: June 13, 2023, 04:20:15 pm »
Then you can add the following line at both the AddPort and GetPortMapping code:

Code: Pascal  [Select][+][-]
  1. Found := Response.Text;
  2. writeln(HTTP.ResultCode.ToString + ' ' + found);

But the problem is the same as with upnpc.
It adds the port correctly but it doesn't list anything.
So there is a 'silent' error.

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #38 on: June 13, 2023, 04:23:29 pm »
Quote
Location is http://192.168.0.1:49153/IGDdevicedesc_brlan0.xml
Base is http://192.168.0.1:49153
SCPDURL is http://192.168.0.1:49153/WANIPConnectionServiceSCPD.xml
Control URL is http://192.168.0.1:49153/upnp/control/WANIPConnection0
200 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>
<u:AddPortMappingResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"></u:AddPortMappingResponse>
</s:Body> </s:Envelope>

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

we are done, press enter




I started the the prog, and started server but the port still looks closed, as nmap says

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #39 on: June 13, 2023, 04:25:16 pm »
OW wait it is opened!!!!!!!!

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #40 on: June 13, 2023, 04:27:58 pm »
Then maybe just the GetGenericPortMappingEntry is defective in your router.
(because upnpc also doesn't seem to get the listing correctly)

So AddPort seems to work.

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #41 on: June 13, 2023, 04:28:43 pm »
I'm trying to get the pattern of starting your prog, the server and nmap....

It looks randomly sequence...

mom......

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #42 on: June 13, 2023, 04:30:17 pm »
I posted
« Reply #24 on: Today at 03:28:28 pm »
what about it?
is there a entry for quering?

rvk

  • Hero Member
  • *****
  • Posts: 6799
Re: How to open UPnP port?
« Reply #43 on: June 13, 2023, 04:32:27 pm »
I posted
« Reply #24 on: Today at 03:28:28 pm »
what about it?
is there a entry for quering?
Yes. #GetGenericPortMappingEntry.

If you do a tcpdump with -A when doing the upnpc -l you get the correct command and xml to post.

But that's also in my code.
And when you do upnpc -l you didn't get anything.

So I wonder is the #GetGenericPortMappingEntry isn't faulty in the router.

Key-Real

  • Sr. Member
  • ****
  • Posts: 389
Re: How to open UPnP port?
« Reply #44 on: June 13, 2023, 04:34:28 pm »
The sequence seems to be:

1. start the server on port 4444
2. start the prog
3. start nmap

why i have to start the server first?

 

TinyPortal © 2005-2018