September 27, 2015

Linux VNC alternative: x2go

The ideal Linux terminal server has finally come true.
It is called X2Go, and it's open source and free. It's based on the NX protocol and works great, with Windows/Mac/Linux clients. 

September 26, 2015

use supervisor to monitor your process

https://serversforhackers.com/monitoring-processes-with-supervisord

sudo apt-get install -y supervisor

Let's create a configuration for it called webhooks.conf. This file will be created at /etc/supervisor/conf.d/webhooks.conf:
[program:nodehook]
command=/usr/bin/node /srv/http.js
directory=/srv
autostart=true
autorestart=true
startretries=3
stderr_logfile=/var/log/webhook/nodehook.err.log
stdout_logfile=/var/log/webhook/nodehook.out.log
user=www-data
environment=SECRET_PASSPHRASE='this is secret',SECRET_TWO='another secret'
Then:
supervisorctl reread
supervisorctl update

September 25, 2015

hg-git push: no changes found

If you use hg-git, and you know you have new changesets, but hg push is telling you "no changes found", using the following command may solve the problem:

hg bookmark -f master


This is because hg-git uses bookmark to simulate git branches. Somehow this bookmark wasn't moved with your latest hg update. So, this should fix it.

docker make file

build:
    docker build --rm -t logtest:v1 .
clean:
    @docker rm  `docker ps -a  | awk '/Exited/ || /Created/ {print  $$1}'`
    @docker rmi `docker images | awk '/^<none>/{print $$3}'`
list:
    docker ps -a
    echo
    docker images

Docker to find host IP address

netstat -nr | awk '/^0\.0\.0\.0/{print $2}'

September 17, 2015

join Linux to Windows domain

Use likewise-open

http://askubuntu.com/questions/452904/likewise-open-14-04-other-easy-way-to-connect-ad

http://www.powerbrokeropen.org/licensing/

 PowerBroker Identity Services – Open Edition git repository: git://source.pbis.beyondtrust.com/pbis.git

Windows Machine Authentication:
A few very helpful links to me:

http://ubuntuforums.org/archive/index.php/t-2141567.html
Many corporation deploy 802.1x machine authentication, because it's more secure than username authentication. Here's a guide of how to do such authentication.

The basic idea is that when a machine joins an AD domain, DC generates a password corresponding to that machine name. The password is transparent to administrators, but an open source software "likewise open" can get this password. So we can use machine name and password to do a 802.1x machine authentication, with PEAP-MSCHAPV2, other than EAP-TLS certificate.
https://learningnetwork.cisco.com/thread/33200?start=15&tstart=0

The client is configured to provide some form of credentials when it connects to an 802.1x network.  These credentials could be in the form of a username/password, machine account/password, certificate, or a number of other lesser used credentials.

The RADIUS server (ISE in your case), just needs to be able to verify if the supplied credentials are accurate.  To verify username/password credentials, it can look in the local user database, or reach out to external repositories, such as AD.  To verify machine account/password credentials, it will need to reach out to AD.  To verify certificates, it need to have the CA cert of the server that signed the client certificates installed.

In short, the client is the one that decides the credentials to supply, and ISE just needs to be able to validate them.

When discussing machone or user account authentication, you can do one or the other or even do both on a per client basis.

If you only want to do machine authentication, that's fine.  A benefit to machine authentication is that it can be done before a user actually logs into the PC.  So things like login scripts can be run.  But you can only do this on a PC that is in AD (unless you want to consider enabling EAP-TLS with certificates).

If you only want to do user authentication, that's fine as well.  This will happen after the user has logged into the OS.  So your laptop will not have a network connect if it's just sitting at a login prompt.

You can also do both.  Historically how this has worked is that the PC will do machine authentication when it first boots up.  Then once a user logs in, it will do a new authentication with the user credentials.  Not very many people did this.  With the latest anyconnect client and ISE, you can actually do both at the same time now (I think they call it EAP chaining).  But again, not many people do this.

September 16, 2015

Using netcat to send udp broadcast packet

1. You need to be root  on Linux
2. make sure you use the traditional netcat. In Ubuntu, that's nc.traditional
3. to send broadcast to broadcast address 192.168.1.255 port 8080, do this:

       echo -n "hello" | nc -b -u 192.168.1.255 8080 
4. OR, use ncat:

       echo -n "hello" | ncat -u 192.168.1.255 8080 

5. OR, use socat:
echo -n "hello" | socat - udp-datagram:192.168.1.255:8080,broadcast

August 31, 2015

Why do some WiFi routers block multicast packets going from wired to wireless

Source: http://superuser.com/questions/730288/why-do-some-wifi-routers-block-multicast-packets-going-from-wired-to-wireless
It's usually due to bugs in the Wi-Fi home gateway routers (APs), or sometimes in the wireless client chipsets/drivers/software.
On Wi-Fi, sending multicasts from the AP to the wireless clients (this is known in the standard as "From the Distribution System" or "FromDS") is tricky, so there are lots of ways it can fail, and it's easy to introduce bugs.
  1. Even though the radio medium is unreliable enough that 802.11 unicasts are required to have link-level acknowledgements (ACKs) and get retransmitted several times if there's no ACK, FromDS multicasts are never ACKed because they'd need to be ACKed by all the wireless clients of the AP, which could be quite an "ACK storm". So instead, FromDS multicasts have to be sent at a low data rate; using a simpler, slower, easy-to-decode-even-at-low-signal-to-noise-ratios modulation scheme, that can hopefully be received reliably by all the clients of the AP. Some APs let the administrator set the multicast rate, and some administrators unwittingly set it too high for some of their clients to receive reliably, breaking multicast delivery to those clients.
  2. When WPA (TKIP) or WPA2 (AES-CCMP) encryption is in use, FromDS multicasts have to be encrypted with a separate encryption key that is known to all of the clients (this is called the Group Key).
  3. When a client leaves the network, or every hour or so, just for good measure, the Group Key needs to be changed so that the client that left no longer has access to decrypt the multicasts. This "Group Key Rotation" process sometimes has problems. If a client doesn't acknowledge receipt of the new group key, the AP is supposed to de-authenticate that client, but if it fails to do that due to a bug, a client could have the wrong group key and thus be "deaf" to multicasts without realizing it.
  4. When WPA2 "mixed mode" is enabled (that is, when both WPA and WPA2 are enabled at the same time), the FromDS multicasts typically have to be encoded with the TKIP cipher, so that all clients are guaranteed to know how to decode it.
  5. FromDS multicasts have to be queued up by the AP and only transmitted at times when all clients who care about multicasts can be expected to have their receivers powered on. The time between the "safe to transmit FromDS multicasts" periods is called the "DTIM interval". If the AP or clients screw up their DTIM interval handling, it could result in clients unable to receive multicasts reliably.
  6. Some APs have features to keep wireless clients from being able to talk directly to each other, to maybe keep your wireless guests from hacking your other wireless guests. These features usually block multicasts from WLAN devices to other WLAN devices, and could well be implemented in a naive way that even blocks multicasts from LAN to WLAN.
The crazy thing is, "ToDS" multicasts are done just like ToDS unicasts, and so they rarely break. And since ToDS multicasts (not FromDS multicasts) are all that are needed when a wireless client gets a DHCP lease and ARPs to find its default gateway, most clients are able to get connected and surf the web, check email, etc. even when FromDS multicasts are broken. So a lot of people don't realize they have multicast problems on their network until they try to do things like mDNS (a.k.a. IETF ZeroConf, Apple Bonjour, Avahi, etc.).
A couple other things to note, regarding wired to wireless multicast transmissions:
  1. Most LAN multicasts, such as mDNS, are done using special multicast address ranges that are not meant to be routed across routers. Since Wi-Fi-capable home gateways with NAT enabled count as routers, mDNS is not meant to cross from WAN to [W]LAN. But it SHOULD work from LAN to WLAN.
  2. Because multicasts on Wi-Fi have to be sent at a low data rate, they take up a lot of airtime. So they're "expensive", and you don't want to have too many of them. That's the opposite of how things work on wired Ethernet, where multicasts are "less expensive" than sending separate unicasts to each machine "tuning into a multicast video stream" for example. Because of this, many Wi-Fi APs will do "IGMP Snooping" to watch which machines are sending Internet Group Management Protocol (IGMP) requests, expressing their desire to tune into a given multicast stream. Wi-Fi APs that do IGMP Snooping won't automatically forward some classes of multicasts onto the wireless network unless they see a wireless client try to subscribe to that stream via IGMP. The documents that describe how to do IGMP Snooping properly make it clear that certain classes of low-bandwidth multicasts (mDNS fits in this category) are supposed to always be forwarded even if no one has explicitly asked for them via IGMP. However, I wouldn't be surprised if there are broken IGMP Snooping implementations out there that absolutely never forward any kind of multicast until it sees an IGMP request for it.
tl;dr: Bugs. Lots of opportunities for bugs. And occasional poorly-designed features and configuration errors. Your best defense is to buy high-quality APs from companies that care about making sure multicasts work. Since Apple loves Bonjour (mDNS) so much, Apple's APs are probably the most consistently excellent at passing multicasts reliably, and Apple's Wi-Fi client devices are probably the most consistently excellent at receiving multicasts reliably.

August 13, 2015

how to start/stop and disable/enable a service in Ubuntu

http://askubuntu.com/questions/19320/how-to-enable-or-disable-services

Look at the 2nd answer.

Backup:

Currently there are actually 2 different ways for software to be started as a service in Ubuntu. A service is defined here as a program run by the system in the background, as opposed to one started and run directly by the user.
The traditional way to start services in Linux was to place a script in /etc/init.d, and then use the update-rc.d command (or in RedHat based distros, chkconfig) to enable/disable it. This command, btw, uses some mildly complicated logic to create symlinks in /etc/rc#.d, that control the order of starting services. If you run ls /etc/rc2.d you can see the order that services will be killed (K##xxxx) and started (S##xxxx).
The issue with that was that when booting the system, everything had to be done in serial, one thing after another, making system boot times really slow. Attempts were made to parallelize this, but they were haphazard and hard to take full advantage of. This was the main reason that Upstart was created.
Upstart uses job definition files in /etc/init to define on what events a service should be started. So, while the system is booting, upstart processes various events, and then can start multiple services in parallel. This allows them to fully utilize the resources of the system, for instance, by starting a disk-bound service up while another CPU-bound service runs, or while the network is waiting for a dynamic IP address to be assigned.
You can see all of the upstart job files by running ls /etc/init/*.conf
Let me just stop here and say that if you don't know what a service is, or what it does, DO NOT disable it!
Not all services have been converted to upstart. While working on the server team at Canonical for the past few months, I've worked on a number of converted job files, and the nicest part is that it allows one to get rid of all the script "magic" and just put in a few commands here and there to define exactly how to start the service, and nothing more. But for now, only a handful of traditional network services, like squid and samba, have been converted.
In order to figure out if a service is upstart based, you can run the status command:
status servicename
If its an upstart job, it will show this:
$ status statd
statd start/running, process 942
But if its not, you'll see something more like this:
$ status apache2
status: Unknown job: apache2
In this case, apache2 has not been converted to upstart. So, to disable apache2 you just run
sudo update-rc.d apache2 disable
sudo service apache2 stop
Upstart job definitions do not have an update-rc.d command. To disable the job, you need to edit the job file directly to disable it. There are two ways to do this.
If you want to still be able to manually start it, then you need to comment out the 'start on' condition. Say you want to install samba, but not have it start automatically.. here is its job file (in natty):
description "SMB/CIFS File Server"
author      "Steve Langasek <steve.langasek@ubuntu.com>"

start on local-filesystems
stop on runlevel [!2345]

respawn

pre-start script
    RUN_MODE="daemons"

    [ -r /etc/default/samba ] && . /etc/default/samba

    [ "$RUN_MODE" = inetd ] && { stop; exit 0; }

    install -o root -g root -m 755 -d /var/run/samba
end script

exec smbd -F
To disable it, you can just put a # in front of the 'start on local-filesystems'. Note that while it won't start back up on boot, you still need to stop it this time with
sudo service smbd stop
If, however, you never want it to start, I'd suggest actually removing the package. If, however, you want it installed, but not startable, you can also do:
mv /etc/init/smbd.conf /etc/init/smbd.conf.disabled
Starting with the version of upstart that will be in 11.04, there is a new keyword that disables the 'start on' and 'stop on' stanzas, it is 'manual'. So another way to disable the service as of 11.04 is to do:
command using sudo
echo 'manual' | sudo tee /etc/init/mysql.override

command from root shell
echo manual >> /etc/init/mysql.override
And, hopefully real soon, you will be able to create an "override" file to disable a service without editing the job definition at all, by just putting the 'manual' keyword in it.

July 16, 2015

gvim display GB2312 chinese

set guifont=NSimSun:h12:cGB2312

July 11, 2015

Simple Golang Example of os.exec on Windows

package main

import (
"fmt"
"log"
"os/exec"
"strings"
)

func doCmd(cmd string) string {
parts := strings.Fields(cmd)
head := parts[0]
parts = parts[1:len(parts)]
out, err := exec.Command(head, parts...).Output()
if err != nil {
log.Fatal(err)
}
return string(out)
}

func main() {
cmd := "netsh wlan show networks mode=bssid"
out := doCmd(cmd)
fmt.Printf("%s\n", out)
}

Windows 7 WiFi scripting

To display all wireless interfaces:
netsh wlan show interfaces
To show the wireless drivers installed run this command. This is particularly interesting as exploits in drivers do exist and most admins do not pay as close attention to driver versions as other types of software:
netsh wlan show drivers
To list available wireless networks (similar to Linux’s iwlist scan option)
netsh wlan show networks
or 
netsh wlan show networks mode=bssid (this shows more BSSID and signal strength)
To view profiles of networks saved on this machine:
netsh wlan show profiles
To make Windows connect to the specified profile (usually named after the SSID of the network):
netsh wlan connect name="ProfileName"
To export the profile details to an XML file (which includes an encrypted version of the PSK if applicable):
netsh wlan export profile name="ProfileName"

To delete a profile
netsh wlan delete profile name="ProfileName"

To Add a profile
netsh wlan add profile filename=c:\temp\myprofile.xml

XML for a WPA2-PSK Wifi networks looks like this


<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
<name>YOUR_NETWORK_NAME</name>
<SSIDConfig>
<SSID>
<hex>HEX-of-your-network-name, for example, "abc" would be "616263"</hex>
<name>YOUR_NETWORK_NAME</name>
</SSID>
</SSIDConfig>
<connectionType>ESS</connectionType>
<connectionMode>auto</connectionMode>
<MSM>
<security>
<authEncryption>
<authentication>WPA2PSK</authentication>
<encryption>AES</encryption>
<useOneX>false</useOneX>
</authEncryption>
<sharedKey>
<keyType>passPhrase</keyType>
<protected>false</protected>
<keyMaterial>YOUR-NETWORK-PASSOWRD</keyMaterial>
</sharedKey>
</security>
</MSM>
</WLANProfile>



Now crucially, here are the commands to turn the Windows 7 (or Server 2008 R2) into an Access Point sharing its existing wireless connection out to others:
netsh wlan set hostednetwork mode=allow ssid=SomeSSID key=passphrase
The hosted network is now created but it is not yet started. To start it, issue the command:
netsh wlan start hostednetwork
Your Windows box is now advertising a network “SomeSSID” (in this case) which other machines can connect to. No notification is given on the Windows box that this has happened and no further notification happens when someone connects.

Vivek stated Microsoft’s response was it wasn’t being exploited “in the wild” therefore nothing would be done about it. Happy WiFi backdooring. :-)

A simple C++ logger class

log.h

#ifndef __LOG1_H__

#define __LOG1_H__

#include <sstream>
#include <string>
#include <stdio.h>

class Log
{
public:
    Log(){};
    ~Log();
    std::ostringstream& Get();
protected:
    std::ostringstream os;
};

inline Log::~Log()
{
    os << std::endl;
    fprintf(stderr, "%s", os.str().c_str());
    fflush(stderr);
}

inline std::ostringstream& Log::Get()
{
    os << " " <<  ": ";
    return os;
}

#define log() Log().Get()

#endif //__LOG_H__


test.cpp:
log() << "A loop with " << count << " iterations";

June 23, 2015

linux process with a lot of open sockets

etc/sysctl.conf file:


# General gigabit tuning:
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_syncookies = 1
# this gives the kernel more memory for tcp
# which you need with many (100k+) open socket connections
net.ipv4.tcp_mem = 50576   64768   98152
net.core.netdev_max_backlog = 2500

then run as root: sysctl -p

Then:

sudo bash
ulimit -n 999999
./your-program

June 18, 2015

May 27, 2015

ath9k create multiple interfaces

  • To create/delete virtual interfaces:
    iw dev wlan0 interface add [virtual-sta-name] type station
    iw dev wlan0 interface add [virtual-ap-name] type __ap
    ip link set [interface-name] address [unique-mac-addr]
  • To delete virtual interface:
    iw dev [dev-name] del
  • use iw to check wlan device status

    $ cat /proc/net/wireless 
    Inter-| sta-|   Quality        |   Discarded packets               | Missed | WE
     face | tus | link level noise |  nwid  crypt   frag  retry   misc | beacon | 22
     wlan0: 0000   56.  -54.  -256        0      0      0      0      0        0
    
    
    
    
    OR
    
    
    $ iw dev wlan0 link
    Connected to 00:10:7A:93:AE:BF (on wlan0)
        SSID: SECRETSSID
        freq: 2462
        RX: 89045514 bytes (194863 packets)
        TX: 34783321 bytes (164504 packets)
        signal: -54 dBm
        tx bitrate: 48.0 MBit/s

    May 26, 2015

    openwrt unbind led gpios

    # Turn off the LEDs
    root@OpenWrt:/# echo "leds-gpio" > /sys/bus/platform/drivers/leds-gpio/unbind
    root@OpenWrt:/# cat /sys/kernel/debug/gpio
    GPIOs 0-21, ar2315-gpio:
    gpio-0   (sysfs               ) in  lo
    gpio-5   (reset               ) in  hi
    gpio-6   (sysfs               ) in  lo

    Now you can use gpio export (i.e echo 11 > /sys/class/gpio/export), and change 'direction' and 'value'  of the gpio to directly control it.