1

I want to change the port number a program listens on. The program is a proprietary linux binary, Plex, that always listens to tcp port 32400 (hard-coded). Since I want to run this on a shared server, another person may already be using this port. I do not have root access on the server. The VPS provider is OK with Plex running on their servers.

Would it be possible to use something like LD_PRELOAD to change the port number? If so, how would I go about doing this? If not, how else might I accomplish this?

Issues changing default port from 32400 on VPS

3

You have a couple of options:

  1. Use an hex editor to change the value manually. This might be the easiest option, provided you don't update that binary too often. A method to find the offset to modify would involve disassembling the binary and identify the value to change.

  2. Use a container, like docker, where the binary could still be listening to 32400 inside the container, but you could map this to another port outside of it.

  3. Use LD_PRELOAD, you could use that to intercept calls to bind() and change what is passed to it

| improve this answer | |
3

Probably the easiest way to do this, if you don't have any special tools, is run plex under the control of gdb, with the input of gdb comping from a script. To do that, you need to find the address where 32400 is hardcoded.

objdump -D plex | grep -i 7e90

should give you something like

40090b: bf 90 7e 00 00          mov    $0x7e90,%edi

Start gdb, set a breakpoint there, run the program until it hits the breakpoint.

gdb plex
break *0x40090b
run

Step one instruction, change the register value, and continue running the program:

stepi
>> 0x0000000000400910 in main ()             <-- address of next instruction
info registers                               <-- just to make sure
>> rdi            0x7e90    32400
set $rdi=0x7e91                              <-- patch the value
info registers                               <-- again, just to make sure
>> rdi            0x7e91    32401
cont

In a different window, check if plex really listens on the other port:

netstat -ntap | grep plex
tcp   0      0 0.0.0.0:32401     0.0.0.0:*     LISTEN      22018/plex 

yes it did!

Now automate this in a script:

gdb plex <<EOF
break *0x400910      <-- note this is the instruction AFTER the load, not the one we breaked on last time
run
set $rdi=0x7e91
cont
EOF

and run that script instead of plex everytime you want to start it on a different port.

Of course, you could also use a hex editor to change the value in the binary directly. You'll even have to do this if the routine that sets up the listening port gets called repeatedly (so you can't predict which exact input gdb needs). However, in many cases, this might be the easiest method, since it doesn't require changing the binary or programming a LD_PRELOAD handler, and changing the port number later is super-easy.

| improve this answer | |
  • I found rdx 0x7e90 32400 in gdb and changed it. It is now running on a different port number. In a hex editor, what would I look for to make the change permanent? – jftuga May 23 '15 at 12:00
  • I found rdx 0x7e90 32400 in gdb and changed it. It is now listening on both 32400 and 32401. I also checked the registers a 6 other breakpoints as that it what objump|grep returned. Any ideas as to why it would be doing this? – jftuga May 23 '15 at 12:06
  • Ah, it looks like Plex uses both 32400 and 32401 by default. I will try changing the register to another value. – jftuga May 23 '15 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.