Kick someone out of the server

Hello,

it happens someone is logged in on a server and you don’t want that or just a “little prank” to a friend or coworker (keep in mind the first example, i’m not responsible).
So at first check out who’s logged in and with which user:

# who
root     pts/0        2015-05-10 16:14 (xxx.xxx.xxx.xxx)
root     pts/3        2015-05-10 19:08 (my-super-reverse)

You noticed two persons are logged in, both as root, I’ll pretend I’m the first one and the second (i.e my-super-reverse) is someone else, and just right now I have to kick him out.
Because I’m well educated, I will tell him first, like that:

# echo 'Have a nice day sir, you are out from now! cya :)' > /dev/pts/3

With this line, you directly write in his TTY. Look on the previous who command, I only took the reference to his TTY (i.e pts/3), ja it’s pretty cool.
And now, find the PID of the TTY of pts/3

# ps -ft pts/3
UID        PID  PPID  C STIME TTY          TIME CMD
root     18999 18997  0 19:08 pts/3    00:00:00 -bash
root     20240 18999  1 19:20 pts/3    00:00:00 watch -n 1 echo toto

In this example, there are two things, first the -bash is the TTY session and secondly, the “watch -n 1 echo toto”. This guy is doing a watch to echo “toto”, OUT!
What matters here is the first PID, the TTY session. We can now kick him out.

kill -9 18999

He’s out! You can do a who once again to check.
Don’t tread on me! 🙂

How to inject CSS of jquery-ui, fontawesome, … in the current page

Hello, this time some trick about Chrome Extension.

Let’s put some background, you got a content.js script, and for some reason you need to inject jquery-ui, font-awesome and some other stuff in the page of the user because you want to display some div that are draggable and with some cute “pictures”.

If you try to inject the css of jquery-ui inside the page, for example by doing an append in the head of the page, it will work for the css itself. But what if, like jquery-ui, it requires images (for ex: background: url(‘../images/image.jpg’);)? If you try to so, you’ll notice that the browser will try to load the images from the domain name of the current page, and not from your extension, and obviously it won’t find it.

A solution could be to just modify the sources with grep or whatever you want to match your needs. But, what if you want to upgrade your jquery-ui in a few days? Dirty …

So as a workaround I did this little function (requires JQuery):

// first declare this variable, replace the xxxxxxxxxxxxxxxxxxx by the id of your extension
var CHROME_EXTENSION_URL = 'chrome-extension://xxxxxxxxxxxxxxxxxxx';

/**
	inject CSS file in the current page, it replaces the relative URL by absolute ones using the chrome-extension URL
	**/
function injectCSS(file){
	var folder = file.split('/');
	folder = (folder.length >= 2)? folder[0] + '/' : '';
	
	$.get(chrome.extension.getURL(file), function(data){
		var content = data.replace(/url(s*('|")(?!data:image)(.{2})?/g, function(str, $1){
			return 'url(' + $1 + CHROME_EXTENSION_URL + '/' + folder;
		});
		$('' + content + '').appendTo('head');
	});
}

It loads the CSS file (need to be in the “web_accessible_resources” section of the manifest.json, see below), replaces the relative path for “url(” by the the chrome-extension:// URL and injects it as a style markup in the current page.
The “(?!data:image)” is there to not replace for the base 64 encoded images.

Next step is to use this function to use jquery-ui and font-awesome, just do like that:

// for the parameter write the path from the beginning of your application's directory
injectCSS('jquery-ui/jquery-ui.min.css');

injectCSS('font-awesome/css/font-awesome.min.css');

And finally, don’t forget to modify your manifest.json to add the “web_accessible_resources” section and update the “content_scripts”.
It must look like that in this case:


{
	"name": "your app's name",
	"description": "your description",
	"version": "1.0",
	"permissions": [
		"activeTab"
	],
	"browser_action": {
		"default_title": "your title",
		"default_popup": "popup.html"
	},
	"web_accessible_resources": [
		"jquery-ui/*",
		"font-awesome/*"
	],
 	"content_scripts": [
		{
			"matches": ["http://test.com/"],
			"js": [
				"jquery/jquery.min.js",
				"jquery-ui/jquery-ui.min.js",
				"content.js"
			],
			"css": [
				"content.css"
			]
		}
	],
	"manifest_version": 2
}

You can make the whole jquery-ui folder available (to allow images, fonts, …), same goes for font-awesome.
Also in the “content-scripts”, you notice I inject “jquery/jquery.min.js” and the “jquery-ui/jquery-ui.min.js”, it’s to use them from the content.js and inside the current page.
Voilà, if you tried it and find some bug or anything, just drop some feedback. 🙂

Save RewriteCond’s backreferences to use in RewriteRule

Using Apache’s mod rewrite, it’s possible to redirect URL and stuff. One cool thing is to use backreferences and use them as environment variables. For example when you have several RewriteRule sharing some common part(s) but are different … the bad example under will, I hope, be clearer.

	# in .htaccess or vhost
	RewriteEngine On
	# define a RewriteCond with two capturing part
	RewriteCond %{HTTP_HOST} ^(.+?).(test|prod).com$ [NC]
	# there we define the value of the subdomain (i.e %1) and the host (i.e %2 which can be test or prod)
	RewriteRule .? - [E=SUBDOMAIN:%1,E=HOST:%2]
	# we apply the RewriteRule using our variables
	RewriteRule ^js/(.+?).js$ %{ENV:SUBDOMAIN}.php?js=$1&host=%{ENV:HOST} [L]
	RewriteRule ^css/(.+?)/(.+?).css$ %{ENV:SUBDOMAIN}.php?folder=$1&css=$2 [L]
	
	# other useless example of how to do so
	RewriteCond %{HTTP_USER_AGENT} ^(Mozilla.+) [NC]
	RewriteRule .? - [E=HASMOZILLA:%1]	
	
	RewriteCond %{REMOTE_ADDR} =127.0.0.1
	RewriteRule ^here$ %{ENV:SUBDOMAIN}.php?who=me [L]

Basically the part to remember is this trick wich allows to save environment variables:

        RewriteRule .? - [E=VAR1_NAME:VALUE,E=VAR2_NAME:VALUE]

Do not put any space between values, this example will give an internal error (space after the comma):

        RewriteRule .? - [E=VAR1_NAME:VALUE, E=VAR2_NAME:VALUE]

And if you have a RewriteCond above with capturing part(s), you can use backreference (%1 and so on), just like in the first example.

Finally, you can use the environment variable in your RewriteRule like so:

        %{ENV:VAR_NAME}

Voilà !