Basic Stamp tokenizing and flashing from the Linux CLI

For those of you who aren’t familiar with the Basic Stamp, its a small microcontroller available from Parallax. It runs at a whopping 20Mhz and has a full 2K of storage on board for instructions. Although it may not sound like much, its more than enough to program the stamp to do some interesting things. The Basic Stamp is programmed in PBASIC, parallax’s version of BASIC the stamp interprets.

Rather than waste your time explaining the coolness of the Basic Stamp, (if you’ve found this page you’re probably already interested) I want to focus on how it can be utilized under linux. Currently, Parallax has a very nice PBASIC IDE but its available for windows only. It can however be run under Wine, and with the proper font settings it doesn’t look too horrible and is quite functional. But what is the fun in that?

If you’re more of a ‘vim’ guy like me, there’s a better alternative. A quick visit over to sourceforge and you’ll find a set of command line tools available for download that make it easy to tokenize code and send it to the stamp without needing to reboot into windows.

After downloading the tarball, unpack it and move into the directory. All you need to do now is build it, and it shouldn’t require much more than a simple ‘make’.

unpack…

tdavis@tdavis-64:~$ tar xvzf bstamp-2006.05.31.tar.gz
bstamp/
bstamp/bstamp_run.cpp
bstamp/tokenizer.h
bstamp/PBASIC_Tokenizer_Software_Distribution_License.txt
bstamp/pbasic_examples/
bstamp/pbasic_examples/hello.bs2
bstamp/pbasic_examples/Makefile
bstamp/pbasic_examples/touch.bs2
bstamp/Makefile
bstamp/PBASIC_Tokenizer_Software_Distribution_License.pdf
bstamp/TODO.txt
bstamp/GPL.txt
bstamp/README.txt
bstamp/CHANGES.txt
bstamp/COPYING.txt
bstamp/bstamp_tokenize.cpp
bstamp/tokenizer.so
bstamp/error_handling.cpp
tdavis@tdavis-64:~$

move into the directory where you unpacked the tarball…

tdavis@tdavis-64:~$ cd bstamp/
tdavis@tdavis-64:~/bstamp$

and build it…

tdavis@tdavis-64:~/bstamp$ make
tdavis@tdavis-64:~/bstamp$ make install

The last step is to make a symbolic link to whatever serial port your stamp is hooked up to. In my case it was /dev/ttyUSB0 because I’m using a serial to usb converter, but for a regular serial connection its likely to be /dev/ttyS0 or /dev/ttyS1. The symlink needs to point the serial device to a new location, /dev/bstamp. If at any point you encounter any problems, don’t forget to check out the README.txt that comes with the program.

tdavis@tdavis-64:~/bstamp$ sudo ln -s /dev/ttyUSB0 /dev/bstamp

Now you just need some code to tokenize, as an example here’s a simple program I wrote that does nothing more than monitor the light levels off a photo resistor and produce output accordingly. (It beeps and blinks!) Its certainly not the most beautiful code, but it does the trick.

'for Basic Stamp 2
'basic light meter that shows on 7 segment display
'and controls LEDs related to the amount
'of light detected; can also produce
'audio output through a piezo electric
'speaker based on the amount of light detected
'@ Tyler Davis 2007
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "program running!"
index VAR Nib
time VAR Word
dark CON 25
OUTH = %00000000
DIRH = %11111111
'FREQOUT 2, 2000, 4500 'test spk on p2


DO
GOSUB Get_RC 'grab light level info
GOSUB Delay 'delay between refreshes
GOSUB Update_Display
GOSUB sound 'play sound that changes as light
'measurments do
LOOP

Get_RC:
HIGH 0 '0 pin
PAUSE 3
RCTIME 0, 1, time
DEBUG HOME, "time = ", DEC5 time
IF (time > 200) THEN HIGH 6 'for green and
IF (time < 200) THEN LOW 6 'red lights
IF (time < 200) THEN HIGH 4
IF (time > 200) THEN LOW 4
IF (time < 35) THEN HIGH 5
IF (time > 35) THEN LOW 5
IF (time > 400) THEN HIGH 3
IF (time < 400) THEN LOW 3
RETURN
Delay:
PAUSE time
RETURN

Update_Display: 'to adjust 7 segment display
IF index = 6 THEN index = 0
LOOKUP index, [ %01000000,
%10000000,
%00000100,
%00000010,
%00000001,
%00100000 ], OUT
index = index + 1
RETURN

sound: 'to create audible sounds related to
'detected illumination levels
FREQOUT 1, 50, time + 4000
RETURN

Now I’m gonna assume you’re using your own code since my code is kind of worthless without a corresponding schematic, but I guess it could be figured out. Since I don’t feel like drawing up one I’ll just post a picture and if someone wants to try and figure it out they’re welcome to. (Sorry but they’re terrible pictures, I’ll try and get better ones up as soon as I get a chance).



Otherwise the process of tokenizing the code and writing it to the stamp is quite straightforward.

tdavis@tdavis-64:~/bstamp$ ./bstamp_tokenize lightmeter.bs2 lightmeter.tokenized
: Success
PBASIC Tokenizer Library version 1.23

tdavis@tdavis-64:~/bstamp$

tdavis@tdavis-64:~/bstamp$ cat lightmeter.tokenized | bstamp_run

If you run into any problems, be sure to verify you’re working with the correct serial device. Try a ‘dmesg | grep ttyS’ and see what it brings up. Or replace ‘ttyS’ with ‘ttyU’ if you have a USB connection.

Mining on an R9 290 in Linux

Picked up a couple of the newer R9 290s to add to my mining rig recently and encountered a number of problems with the latest AMD/ATI drivers as well as cgminer. It seems to be fairly common for the latest drivers to cause issues on both Linux and Windows .

The fact that cgminer no longer supports GPU’s compounds the issue, but it is a fixable problem. The latest beta drivers (13.12)need to be patched to keep them from crashing and failing entirely. More information on such can be found here. Someone was also nice enough to provide binaries for Ubuntu.

If you can get that far, you’ll quickly find that cgminer will crash upon exit (use version 3.7 and earlier if you want GPU support as its been dropped), and lock the GPU resource (fans will continue running for example) until reboot.

With their Hawaii architecture, AMD updated some things for the R9 290/290X. Specifically, they’ve updated their Overdrive engine from version 5 to 6

This means cgminer isn’t capable of handling the latest series of graphics cards. The solution involves compiling cgminer from scratch and fixing the adl calls or just disabling anything related to temperature and fan control — a bad idea considering how hard mining is on video cards. Its critical to monitor and control temps or risk destroying very expensive GPUs. So based on information attained here I went ahead and recompiled cgminer with the latest and greatest SDK and AMD drivers — it works perfectly now. No more crashes on exit.

In the adl.c file around line 472, just before the ADL_Overdrive5_FanSpeed_Get call, add a line above: ga->DefFanSpeedValue.iSpeedType=ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;

However I had some issues when I actually installed the SDK and compiled cgminer, it adds a number of things to /usr/include and /usr/lib and they ended up causing hardware recognition issues for me on Ubuntu 13.10. In the latest versions of AMD/ATI drivers, the SDK is ‘included’, however in practice cgminer won’t find it without specifically pointing to it. The solution was to fix adl.c in cgminer, and point to local user space copies of the latest SDK for ADL and openCL recognition/compatibility, without actually installing them. This provided access to all 5 GPUs from userspace, without even needing sudo. I highly recommend NOT installing the 6.0 ADL if you are in a similar situation, I encountered endless issues with such, since the drivers technically already include the SDK.

Depending on where you unpack the 2.9 SDK you can use the following line to successfully build cgminer with proper temp and fan control support, without having to actually install (and risk ruining your ability for cgminer to find your GPUs) the SDK:

CFLAGS=”-O2 -Wall -march=native -I/location/to/SDK/AMD-APP-SDK-v2.9-RC-lnx64/include/” LDFLAGS=”-L/location/to/SDK/AMD-APP-SDK-v2.9-RC-lnx64/lib/x86_64″ ./configure –enable-opencl –enable-scrypt

Last but not least here are some good settings for a PowerColor AXR9 290 4GBD5-MDH/OC Radeon R9 290. I’m getting about 875 Kh/s out of these particular cards with this setup. This seems to be near the top of the list for similar setups.

./cgminer –scrypt -o yourpool.com:port -u user.worker -p password -I 20 -g 1 -w 256 –auto-fan –lookup-gap 2 –thread-concurrency 24550 –gpu-memclock 1500 –gpu-engine 1000 –gpu-powertune 20 -v 1

scrypt-r9-290-settings

But thats running one of the cards fairly hot. Personally I like them to run a little cooler so I had to adjust my cooling a bit to make up for the difference. Lowering the intensity down to 19 and the powertune down to 10 yielded me lower temps but still respectable hash rates (840khash).

If you are having issues compiling, I went ahead and compiled a verison of cgminer with the adl fixes and the latest and greatest sdk. It should work on most 64 bit ubuntu systems with the latest AMD/ATI drivers. You can download it here.

I am unfamiliar with this particular hosting site, but it was free so I posted it, please double check the md5sum before opening. If it hasn’t been tampered with it should be:

md5sum cgminer-3.7.0-amd-r9-fix.tar.gz
7caacb590eb419c380b7d223797dc959

Latest mining rig:

2013-12-22(2) - Copy

LTC: LTjmMFrxm4mnRWM4wvH5WQdJPnw3fUmYiz

Using a packet sniffer to track games/applications phoning home, and how to block them via the routing table or the hosts file

The other day I was trying to play an old game I bought some time ago on my LAN. I was just trying to enjoy a little cooperative bot killing fun with my buddy, but for some reason, the game kept telling me my ‘master key’ or something along those lines didn’t check out. Needless to say I wasn’t happy with it… I bought this game… and maybe I wasn’t supposed to play it on two machines at once… but at the very least I certainly didn’t want it phoning home for a simple LAN game, a game mode where no master server or internet should be required.

So I decided to see what my ‘game’ was up to. A quick download of Wireshark (formerly Ethereal) and an even quicker packet sniff and sure enough I could see where my ‘game’ was initiating connections and across what port.

After booting up wireshark, simply click on the “Capture” menu and select “Interfaces…” and from there it will show you a list of available ones to capture from. Select your active connection (the one with an IP thats NOT your localhost (127.0.0.1)), and click start. You should see something similar to this screenshot as Wireshark attempts to capture all the packets entering and leaving your machine.

If you’ve closed all your other connections (browsers, streaming music, etc.) you should now try to recreate the traffic you wish to sniff. In my case this involves launching the game I want to play and trying to connect to a LAN game, with which I am promptly kicked back for failing to ‘check out’ with the master server correctly. Now exit the game and pull up Wireshark to see what it caught.

In the interest of not angering anyone, I’ve obscured the specific IP and domain name that my machine is connecting to. But for these purposes, it shouldn’t matter. Notice there are numerous outbound and inbound packets originating from the IP 216.27.xx.xxx, and in the right hand column you can [almost] see that an actual domain name has also been found.

This is where the windows ‘hosts’ file comes into action. The hosts file is a pretty simple file to deal with, usually located at c:/windows/system32/drivers/etc/hosts on xp machines (On most linux distributions a similar file is located at /etc/hosts). Here is where you can redirect certain domains to other IPs for a variety of reasons. In our case we want to redirect the server xxxxx.server1.xxxxx.com to the localhost, to prevent the game from phoning home.

In the screenshot above you’ll notice I’ve added a line to my hosts file to redirect the offending domain. Now all outbound connections to xxxxx.server1.xxxxx.com will be redirected to the localhost, preventing any real connection from occurring between the two. But what if there’s no domain name listed? Then the hosts file will be fairly worthless to you, but there are a couple of possible alternatives.

First, try doing an ‘nslookup’ on the IP that looks suspect, and see if you get a domain name to return, in most cases you probably won’t. The next possible solution is to do a google search for the statistics, analytics or authentication server your program is trying to connect to. In many cases google will know the answer already for you. But if both of these methods fail, its time edit the routing table on your machine.

Bring up a [lame] windows command line, if you don’t know how simply click the ‘start’ menu, select ‘run…’ and type in ‘cmd.’ From here you can view and alter the routing table.

Type ‘route print’ at the command line and it should print out the routing table for you to like the screenshot above. If at any point you wish to learn more about route just type in ‘route’ by itself and it will print out detailed help. What we want to do now is create a route for the offensive IP to be sent to the localhost. In some cases this can be done by simply pointing the offending IP to 127.0.0.1, however in my case I had to point it to the actual local IP address of my machine (192.168.2.5) since there was already a route in place to direct traffic heading to 192.168.2.5 to the localhost at 127.0.0.1.

C:\Documents and Settings\Administrator>route print

IPv4 Route Table
===========================================================================
Interface List
0×1 ……………………… MS TCP Loopback interface
0×2 …00 01 29 d2 2c 2b …… NVIDIA nForce Networking Controller – Packet Sch
eduler Miniport
0×3 …00 01 29 d2 2c 2a …… Marvell Yukon 88E8001/8003/8010 PCI Gigabit Ethe
rnet Controller – Packet Scheduler Miniport


===========================================================================
===========================================================================
Active Routes:
Network Destination Netmask Gateway Interface Metric
0.0.0.0 0.0.0.0 192.168.2.1 192.168.2.5 20
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 1
192.168.2.0 255.255.255.0 192.168.2.5 192.168.2.5 20
192.168.2.5 255.255.255.255 127.0.0.1 127.0.0.1 20
192.168.2.255 255.255.255.255 192.168.2.5 192.168.2.5 20
224.0.0.0 240.0.0.0 192.168.2.5 192.168.2.5 20
255.255.255.255 255.255.255.255 192.168.2.5 192.168.2.5 1
255.255.255.255 255.255.255.255 192.168.2.5 2 1
Default Gateway: 192.168.2.1
===========================================================================
Persistent Routes:

C:\Documents and Settings\Administrator>

Use the ‘add’ parameter to add a new route to your table like this (change destination and source IP accordingly)

C:\Documents and Settings\Administrator>route add 216.27.xx.xxx 192.168.2.5
C:\Documents and Settings\Administrator>

You can also use the route command to change the route for entire subnets, but for my purposes its unnecessary, all I needed to do is redirect a specific IP.

Now launch a new session of Wireshark and begin capturing. Open up the offending game or application and test that it works (no longer phones home for any reason). In my case I can see that a bunch of traffic formerly headed to that rouge IP is now heading to 192.168.2.5, which then heads to 127.0.0.1, and effectively gets nowhere.

Congratulations! You’ve now stopped your game or application from ‘checking home.’ It should be noted that on many high end firewalls/routers, its possible to do similar things from within the router itself, but I’ve found that with most consumer level firewalls this still isn’t an option. For instance, on my cheap Belkin I can restrict specific port ranges by internal IP, which would actually work fine for this particular problem, but is a less than perfect solution since it would block ALL traffic outbound on that specific port (a less than optimal solution if the application is using a standard port).

In this day and age you can really never be too careful about privacy, more and more seemingly everyday games and applications religiously phone home your personal information about everything from your private browsing habits to your choice of music. In my case, I’d like to keep them from finding that type of information out.

This entry was posted in Freedom. Bookmark the permalink.

2 Responses to Using a packet sniffer to track games/applications phoning home, and how to block them via the routing table or the hosts file

  1. Tanman YoungerMan says:

    Tyler,
    Thanks for the great write up, I wanted to add a few notes that might help out your readers who are using other systems.

    On Vista and Win 7, in order to edit your c:/windows/system32/drivers/etc/hosts you need to right click the file and deselect the “read only” check box. If you are using UAC you will be prompted to enter an administrator password. After this you can edit the file and save your changes, otherwise you will be unable to save you changes to the c:/windows/system32/drivers/etc/hosts file. For BSD and variants thereof such as Mac OS 10.x a simple “sudo vi /etc/hosts” will do the trick. If you don’t want to leave the comfort of your terminal a simple “sudo tcpdump -i en1″ (replacing en1 for whatever you Ethernet device happens to be). en1 is the default wireless interface on a mac, en0 is the default wired port, for other *nix variants you might have eth(x) or wlan(x), ra(x), vr(x), and so on and so forth.