Commit 30915b7c authored by Botond Botyanszki's avatar Botond Botyanszki

Merge branch 'xm-admin-samples' into 'master'

xm_admin sample scripts

See merge request !1
parents 26766757 0c80e2ca
You can find sample scrips for the xm_admin module using bash, perl or python
for both the SOAP and JSON part of the module in the appropriate directories.
Those scripts are by no means completa, but just a starting point for someone
to experiment with the xm_admin module. In all cases there are more than one
ways to do the same thing and more than one library or program than can
perform a similar task. You may use a different way to achieve the same goal
and those scripts are just one possible way.
All scripts use SSL to connect to nxlog and the configuration used is the
following:
```
<Extension agent_admin>
Module xm_admin
Listen localhost
SocketType SSL
CAFile %CERTDIR%/ca.pem
CertFile %CERTDIR%/server-cert.pem
CertKeyFile %CERTDIR%/server-key.pem
KeyPass secret
Port 7070
<ACL conf>
Directory %CONFDIR%
AllowRead TRUE
AllowWrite FALSE
</ACL>
<ACL tmp>
Directory /opt/nxsec/var/lib/nxlog/tmp
AllowRead TRUE
AllowWrite TRUE
</ACL>
</Extension>
```
It is relative easy to use TCP instead of SSL, by mostly stripping away the
SSL related parts from the scripts. Each set of scripts expects a `keys`
directory where the certificates and keys reside. To create your own set of
self signed keys follow this procedure:
* Create the root certificates
```
openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -sha256 -days 1024 -out ca.pem
```
* Create the client certificates
```
genrsa -out client-key.pem 2048
openssl req -new -key client-key.pem -out client-key.csr
openssl x509 -req -in client-key.csr -CA ca.pem -CAkey ca.key -CAcreateserial -out client-key.pem -days 500 -sha256
```
Similarly create the server certificates. In both cases the important
question to answer appropriately is the Common Name with the FQDN or
the IP of your system.
{
"msg": {
"command": "getFile",
"params": {
"filetype": "conf",
"filename": "nxlog.conf"
}
}
}
{
"msg": {
"command": "moduleInfo",
"params": {
"name": "xm_soapadmin"
}
}
}
{
"msg": {
"command": "moduleStart",
"params": {
"name": "xm_soapadmin"
}
}
}
{
"msg": {
"command": "moduleStop",
"params": {
"name": "xm_soapadmin"
}
}
}
{
"msg": {
"command": "putFile",
"params": {
"filetype": "tmp",
"filename": "test.tmp",
"file": "This is my file\nNewline\n"
}
}
}
#!/bin/sh
IP=127.0.0.1
PORT=7070
USESSL="--secure-protocol=TLSv1_2 --ca-certificate=keys/ca.pem --certificate=keys/client-cert.pem --private-key=keys/client-key.pem"
wget -q https://$IP:$PORT $USESSL --output-document - --header='Content-Type: application/json' --post-file $1
{
"msg": {
"command": "serverInfo"
}
}
{
"msg": {
"command": "serverRestart"
}
}
{
"msg": {
"command": "serverStart"
}
}
{
"msg": {
"command": "serverStop"
}
}
{
"msg": {
"command": "getLog",
"params": {
"size": -1
}
}
}
}
#!/bin/sh
HOST=127.0.0.1
PORT=7070
#!/bin/sh
. ./client_param.sh
ncat -w 1 --ssl --ssl-cert keys/client-cert.pem --ssl-key keys/client-key.pem $HOST $PORT > nxlog.log << __EOF__ && sed -i 's/&#xA;/\n/g' nxlog.log
POST / HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<adm:moduleInfo xmlns:adm="http://log4ensics.com/2010/AdminInterface">
<name>xm_soapadmin</name>
</adm:moduleInfo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
__EOF__
#!/bin/sh
. ./client_param.sh
ncat -w 1 --ssl --ssl-cert keys/client-cert.pem --ssl-key keys/client-key.pem $HOST $PORT << __EOF__
POST / HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<adm:moduleStart xmlns:adm="http://log4ensics.com/2010/AdminInterface">
<name>xm_soapadmin</name>
</adm:moduleStart>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
__EOF__
#!/bin/sh
. ./client_param.sh
ncat -w 1 --ssl --ssl-cert keys/client-cert.pem --ssl-key keys/client-key.pem $HOST $PORT << __EOF__
POST / HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<adm:moduleStop xmlns:adm="http://log4ensics.com/2010/AdminInterface">
<name>xm_soapadmin</name>
</adm:moduleStop>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
__EOF__
#!/bin/sh
. ./client_param.sh
FILE=client_param.sh
FILEBODY=`cat $FILE |sed -e 's~&~\&amp;~g' -e 's~<~\&lt;~g' -e 's~>~\&gt;~g'`
ncat -w 1 --ssl --ssl-cert keys/client-cert.pem --ssl-key keys/client-key.pem $HOST $PORT << __EOF__
POST / HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<adm:putFile xmlns:adm="http://log4ensics.com/2010/AdminInterface">
<filetype>tmp</filetype>
<filename>$FILE</filename>
<file>$FILEBODY</file>
</adm:putFile>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
__EOF__
#!/bin/sh
. ./client_param.sh
ncat -w 1 --ssl --ssl-cert keys/client-cert.pem --ssl-key keys/client-key.pem $HOST $PORT << __EOF__
POST / HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<adm:serverInfo xmlns:adm="http://log4ensics.com/2010/AdminInterface"/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
__EOF__
#!/bin/sh
. ./client_param.sh
ncat -w 1 --ssl --ssl-cert keys/client-cert.pem --ssl-key keys/client-key.pem $HOST $PORT << __EOF__
POST / HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<adm:serverRestart xmlns:adm="http://log4ensics.com/2010/AdminInterface"/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
__EOF__
#!/bin/sh
. ./client_param.sh
ncat -w 1 --ssl --ssl-cert keys/client-cert.pem --ssl-key keys/client-key.pem $HOST $PORT << __EOF__
POST / HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<adm:serverStart xmlns:adm="http://log4ensics.com/2010/AdminInterface"/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
__EOF__
#!/bin/sh
. ./client_param.sh
ncat -w 1 --ssl --ssl-cert keys/client-cert.pem --ssl-key keys/client-key.pem $HOST $PORT << __EOF__
POST / HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<adm:serverStop xmlns:adm="http://log4ensics.com/2010/AdminInterface"/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
__EOF__
#!/bin/sh
. ./client_param.sh
ncat -w 1 --ssl --ssl-cert keys/client-cert.pem --ssl-key keys/client-key.pem $HOST $PORT > nxlog.log << __EOF__ && sed -i 's/&#xA;/\n/g' nxlog.log
POST / HTTP/1.1
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<adm:getLog xmlns:adm="http://log4ensics.com/2010/AdminInterface">
<size>-1</size>
</adm:getLog>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
__EOF__
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use JSON;
use Switch;
my $ua = LWP::UserAgent->new(
ssl_opts => {
SSL_ca_file => './keys/ca.pem',
SSL_cert_file => './keys/client-cert.pem',
SSL_key_file => './keys/client-key.pem'
}
);
$ua->timeout(1);
my $req = POST 'https://127.0.0.1:7070';
my ( $command, $param ) = @ARGV;
if ( not defined $command ) {
print "$0 needs a command such as serverInfo\n";
exit 1;
}
else {
if ( ( not $command =~ m/server/ )
&& ( $command ne 'getLog' )
&& ( not defined $param ) )
{
print "This command requires a parameter.\n";
exit 1;
}
}
my $json;
switch ($command) {
case ('serverInfo') {
$json = encode_json( { "msg" => { "command" => "serverInfo" } } );
$req->header(
'Content-Type' => 'application/json',
'Content-Length' => length($json)
);
}
case ('serverStart') {
$json = encode_json( { "msg" => { "command" => "serverStart" } } );
$req->header(
'Content-Type' => 'application/json',
'Content-Length' => length($json)
);
}
case ('serverStop') {
$json = encode_json( { "msg" => { "command" => "serverStop" } } );
$req->header(
'Content-Type' => 'application/json',
'Content-Length' => length($json)
);
}
case ('serverRestart') {
$json = encode_json( { "msg" => { "command" => "serverRestart" } } );
$req->header(
'Content-Type' => 'application/json',
'Content-Length' => length($json)
);
}
case ('getLog') {
$json = encode_json(
{
"msg" => { "command" => "getLog", "params" => { "size" => -1 } }
}
);
$req->header(
'Content-Type' => 'application/json',
'Content-Length' => length($json)
);
}
case ('moduleInfo') {
$json = encode_json(
{
"msg" => {
"command" => "moduleInfo",
"params" => { "name" => $param }
}
}
);
$req->header(
'Content-Type' => 'application/json',
'Content-Length' => length($json)
);
}
case ('getFile') {
$json = encode_json(
{
"msg" => {
"command" => "getFile",
"params" => { "filetype" => "conf", "filename" => $param }
}
}
);
$req->header(
'Content-Type' => 'application/json',
'Content-Length' => length($json)
);
}
else { print "Unknown command $command\n"; }
}
$req->content($json);
my $res = $ua->request($req);
if ( $res->is_success ) {
print $res->decoded_content;
}
else {
print $res->status_line . "\n";
}
exit 0;
#!/usr/bin/perl
use warnings;
use SOAP::Lite;
use Switch;
# Do not use "verify_hostname => 0" in a production environment
my $soap = SOAP::Lite->proxy(
"https://localhost:7070",
timeout => 1,
ssl_opts => [
verify_hostname => 0,
SSL_ca_file => 'keys/ca.pem',
SSL_cert_file => 'keys/client-cert.pem',
SSL_key_file => 'keys/client-key.pem'
]
)->outputxml("true");
my ( $command, $param ) = @ARGV;
if ( not defined $command ) {
print "$0 needs a command such as serverInfo\n";
exit 1;
}
else {
if ( ( not $command =~ m/server/ )
&& ( $command ne 'getLog' )
&& ( not defined $param ) )
{
print "This command requires a parameter.\n";
exit 1;
}
}
my $result;
switch ($command) {
case ('serverInfo') { $result = $soap->serverInfo(); }
case ('serverStart') { $result = $soap->serverStart(); }
case ('serverStop') { $result = $soap->serverStop(); }
case ('serverRestart') { $result = $soap->serverRestart(); }
case ('getLog') {
$result = $soap->getLog( SOAP::Data->name('size')->value(-1) );
}
case ('moduleInfo') {
$result = $soap->moduleInfo( SOAP::Data->name('name')->value($param) );
}
case ('getFile') {
$result = $soap->getFile(
SOAP::Data->name('filetype')->value("conf"),
SOAP::Data->name('filename')->value($param)
);
}
else { print "Unknown command $command\n"; }
}
print $result . "\n";
#!/usr/bin/python
import socket
import ssl
import sys
HOST, PORT = '127.0.0.1', 7070
CA = 'keys/ca.pem'
CERT = 'keys/client-cert.pem'
KEY = 'keys/client-key.pem'
if len(sys.argv) < 2:
print sys.argv[0], "requires at least one argument"
exit(1)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(1)
wrappedSocket = ssl.wrap_socket(sock, KEY, CERT, ca_certs=CA)
if sys.argv[1] == "serverInfo":
json = '{ "msg": { "command": "serverInfo" } }'
elif sys.argv[1] == "serverStop":
json = '{ "msg": { "command": "serverStop" } }'
elif sys.argv[1] == "serverStart":
json = '{ "msg": { "command": "serverStart" } }'
elif sys.argv[1] == "serverRestart":
json = '{ "msg": { "command": "serverRestart" } }'
elif sys.argv[1] == "getFile":
json = '{ "msg": { "command": "getFile", "params": \
{ "filetype": "conf", "filename": "log4ensics.conf" } } }'
elif sys.argv[1] == "moduleInfo":
json = '{ "msg": { "command": "serverInfo" } }'
else:
print "Unknown command ", sys.argv[1]
headers = ["POST / HTTP/1.1",
"Content-Type: application/json",
"Content-Length: " + str(len(json)),
"", json]
wrappedSocket.connect((HOST, PORT))
wrappedSocket.send("\r\n".join(headers))
print wrappedSocket.recv(2048)
wrappedSocket.close()
#!/usr/bin/python
import sys
from pysimplesoap.client import SoapClient
if len(sys.argv) < 2:
print sys.argv[0], "requires at least one argument"
exit(1)
CA = 'keys/ca.pem'
CERT = 'keys/client-cert.pem'
KEY = 'keys/client-key.pem'
LOC = "https://127.0.0.1:7070"
NMSP = "http://log4ensics.com/2010/AdminInterface"
client = SoapClient(location=LOC, ns="adm",
namespace=NMSP, cacert=CA, cert=CERT, key_file=KEY)
info = None
if sys.argv[1] == "serverInfo":
result = client.serverInfo()
info = result.serverInfoReply
elif sys.argv[1] == "serverStop":
result = client.serverStop()
info = result.serverStartReply
elif sys.argv[1] == "serverStart":
result = client.serverStart()
info = result.serverStartReply
elif sys.argv[1] == "serverRestart":
result = client.serverRestart()
info = result.serverStartReply
elif sys.argv[1] == "getFile":
result = client.getFile(filetype="conf", filename="log4ensics.conf")
info = result.getFileReply
elif sys.argv[1] == "moduleInfo":
result = client.moduleInfo(name=sys.argv[2])
info = result.moduleInfoReply
else:
print "Unknown command ", sys.argv[1]
if info:
for key in dir(info):
print('{}: {}'.format(key, getattr(info, key)))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment