Nmap match three ports - lua

I'm trying to make a script for nmap, which only executes its action, if three ports are open.
The end result would be if port 515, 631 and 9100 are open for tcp traffic, script should print a message (at first, later it should make a os detection).
My script so far:
function portrule(host,port)
return port.state == 'open' and
port.protocol == 'tcp' and
515 <= port.number and
631 <= port.number and
9100 <= port.number
end
action = function(host, port)
return "hello"
end
But this will execute for all ports after 9100 is hit.
Any help is appriciated!

NSE scripts match based on a rule function, and there are four different types. You are using a portrule, which tests each port on a host. Since you want to test multiple ports on a host, you should be using a hostrule instead.
The hostrule is tested after the state of all ports is known. You can check the state of all of these ports by using the nmap.get_ports function like nfs-ls does, or you can check individual ports using the nmap.get_port_state function like the nbstat script does. Actually, nbstat looks like a good example of exactly what you want to do.

Related

HAProxy Lua how to change server port?

Server.set_addr (sv, addr)
I can not understand what the method takes as input.
It would seem that Server.get_addr(sv) returns ip:port - so you also need to pass ip:port, but no. This does not work.
The documentation reads:
See the documentation for the control socket for more information on the string format.
Well, I saw that you need to transfer the ip port: port (example: 127.0.0.1 port 80). But that doesn't work either.
The only thing that works is pass only ip (example: 127.0.0.1).
The question is how to change the port?
Code sample: (if not work - no have any entry in log)
-- work
-- log entry: changed its IP from 1.1.1.1 to 2.2.2.2 by Lua script.
local newAdr = backendServer.ip
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.port
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.ip .. ":" .. backendServer.port
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.ip .. " " .. backendServer.port
server:set_addr(newAdr)
-- not work
local newAdr = backendServer.ip .. " port " .. backendServer.port
server:set_addr(newAdr)
Update: As of HAProxy 2.2dev7, this functionality has now been merged: set_addr takes an additional optional parameter to specify a new port.
It's not currently possible to do that via Lua. A look through HAProxy's source code reveals that set_addr calls hlua_server_set_addr, which calls server_parse_addr_change_request, which calls update_server_addr, which only updates the address and not the port. Contrast this with the management socket's set server, which calls cli_parse_set_server, which calls update_server_addr_port, which does update the port too.
However, there are two pieces of good news:
Since the management socket can do it, you can use it as a workaround.
It would be very simple to modify HAProxy's Lua interface to support updating the port, since all the logic is already there. I sent a patch that does so to their mailing list.

Two Lslidar(Leishen) connected with One Embedded Board(Jetson Xavier)`s possible?

Running one of the Lslidar(16 Channels) on the Embeded board(ROS development environments) is not a problem.
This is because you can use the default IP and ports.
But I plan to run two at the same time, and I want to use values other than the default values of ports and IP..
For example, you can use ports like 2368 and 2369.
I need a reference to refer to how to change the port value and IP value...
Help Plz...
Here is code from lsLidar driver from ROS Wiki:
bool LslidarC16Driver::loadParameters() {
//pnh.param("frame_id", frame_id, std::string("lslidar"));
pnh.param("lidar_ip", lidar_ip_string, std::string("192.168.1.222"));
pnh.param<int>("device_port", UDP_PORT_NUMBER,2368);
pnh.param<bool>("add_multicast", add_multicast, false);
pnh.param("group_ip", group_ip_string, std::string("234.2.3.2"));
inet_aton(lidar_ip_string.c_str(), &lidar_ip);
ROS_INFO_STREAM("Opening UDP socket: address " << lidar_ip_string);
if(add_multicast) ROS_INFO_STREAM("Opening UDP socket: group_address " << group_ip_string);
ROS_INFO_STREAM("Opening UDP socket: port " << UDP_PORT_NUMBER);
return true;
}
As you can see there is a place where you can change port and IP. For two devices I'd advise you to modify the original driver. All source code is available on https://github.com/tongsky723/lslidar_C16
Clone it to you workspace and create additional functionality for two LiDARs.

How to obtain bluetooth port direction with pyserial?

I'm trying to connect to an RN42, module through python. When the RN42 pairs with W10 it creates two virtual COM ports(outgoing and incoming). I need to connect to the outgoing port.
I'm trying to do this automatically. I've tried:
import serial
import serial.tools.list_ports as port_lst
ports = list(port_lst.comports())
bluetooth_ports = []
for p in ports:
if 'Bluetooth' in p.description:
bluetooth_ports += [p.device]
bluetooth_com = serial.Serial(bluetooth_ports[0],115200)
I thought that the first port was usually the outgoing one, but I've paired the module to another computer, and this didn't apply (the second port was the outgoing one). Is there a way to find out the direction of the COM ports?
Thanks!!!
Although this is an antique question, I have been searching for the answer to this for some time myself and since I finally figured it out I wanted others to be able to find the answer. With help from a blog entry at in the hand and its accompanying gist:
The trick is to acquire the hwid using pySerial, then parse the address. The incoming port in a pair has an address of zero and the outgoing port has a nonzero address. Here is some ugly Python code that decodes it:
import serial.tools.list_ports
cp=serial.tools.list_ports.comports()
for p in cp:
if "BTHENUM" in p.hwid:
start_of_address=p.hwid.rfind("&")
end_of_address=p.hwid.rfind("_")
address=p.hwid[start_of_address+1:end_of_address]
if int(address,16)==0:
port_type="incoming"
else:
port_type="outgoing"
print(p.hwid)
print(p.name, address, port_type)
And the output:
BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\7&CC47540&0&000000000000_000000A8
COM4 000000000000 incoming
BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0002\7&CC47540&0&209BA5420081_C00000000
COM5 209BA5420081 outgoing

How to save only those packets using 80 port for communication in wireshark?

I am new with wire shark software and want to know that how to save those packets which use 80 port for communication?
To strictly answer your question, "packets which use 80 port", in the filter box:
tcp.port == 80 || udp.port == 80
(though I don't think you'll ever see much udp on this port)
Then, "how to save those packets":
menu > file > export specified packets
...by default this will save the packets currently displayed by the applied filter.
In case you are looking for a capture filter:
port 80
Add a capture filter:
go to
Capture
Options
enter "port 80" (without quotes) in the capture filter input field (the input field will become green when you enter a valid string)
hit start

On external port how to only close the output and wait for exit_status

Im using a port to run a pipeline with uncompresses and dd's some data:
Port = open_port({spawn, "bzcat | sudo dd of=/dev/foo},
[stream, use_stdio, exit_status]),
What I would like to do is produce a end-of-file situation on the output which causes the pipeline to complete and eventually exit.
I would like to wait for this completion and also capture the exit_status.
When I just call port_close it looks to me as if the pipeline is just terminated and there is no wait for completion. Also I don't get any exit_status ....
How can I accomplish waiting for exit before my next step (which requires the dd to have completed).
Did some experiments and it looks like at least port_close doesn't kill the process, you just don't find out when its done. Is this correct?
If you just need to wait for spawned by open_port command to complete you need to wait for exit_status message:
1> Port = open_port({spawn, "sleep 7"}, [exit_status]).
#Port<0.497>
2> receive {Port, {exit_status, Code}} -> Code after 10000 -> timeout end.
0
Update (about to say a port just close the output pipe): I think you can't just close the output pipe with the default spawn driver. Default driver doesn't have any control commands and port_close although don't kill spawned command but completely erase all port's state.
Possible solutions:
Write input stream to a file first and then run bzip/dd sequence on that file;
Write your own driver or NIF (Maybe some open source implementations already exist?)
Use some external script and control protocol, for example full (or chunk) length can be transferred before the actual content so the script will know when to close the connection
Several rather ugly workarounds to this problem can be found here: limitations of erlang:open_port() and os:cmd()
Some even use netcat to map the problem to a tcp connection.

Resources