## Hardware hack and supply chain security

In its recent issue, it featured a cover story: “The Big Hack: How China Used a Tiny Chip to Infiltrate U.S. Companies“, detailed how Chinese military planted malicious chips into motherboards manufactured by Taiwanese supplier, supplying motherboards to a major server provider whose servers were used by almost 30 US companies, including Amazon and Apple. And by  doing this, Chinese military gains potential access to these companies and even US military.

Here’s the malicious chip on the motherboard:

Here’s a illustration of that process directly from their website:

You don’t have to be a genius to know how big this is, in many ways.

Technically, this is very advanced. Just compare to what the NSA was found doing back in 2014:

I mean, what the NSA has done can be accomplished by just any organization with a team of security experts. What the Bloomberg described, needs collaboration of experts from different areas spanning different industries. It needs state level sponsorship. The only thing that comes comparable, is the Stuxnet.

But even Stuxnet pales in comparison in terms of long term impact. When Stuxnet was made public in 2010, it has already done its tasks. There were a lot of speculation before US and Israeli officials “half confirmed” its origin, but ultimately, its impact is largely limited to technology world.

The hack described in Bloomberg’s article on the other hand, will help re-shape the global supply chain in the years to come. It would be naive to think this is pure coincident that this hack was published almost the same time when Mike Pence spoke at the Hudson Institution.

Despite its length, the article actually doesn’t describe the hacking in detail. The chip in its cover photo (shown below) was dismissed by experts as a very cheap passive component, “not possible to do anything remotely related to hacking”.

The way the chip supposed to work is also vague. From the article, it seems the chip may hijack the code verification process to bypass some security check. But it is unclear whether it was the IPMI code verification or the UEFI code verification or both.

As of this writing, both Apple and Amazon have denied the claim the hack claim. However, the shell has been dropped and the storm is still brewing. Because despite all these refusals, it successfully made people believe that this kind of hack is possible, and it is only possible in mainland China. The seed has been planted. How lucky we are, to witness such historical drama unfolding before our eyes.

《形而上学》亚里士多德

# 2.1 埃及乘法

$$1a=a$$                                   （2.1）

$$(n+1)a=na+a$$                 （2.2）

int multiply0(int n, int a){
if (n==1) return a;
return multiply0(n-1, a)+a;
}

Ahmes所描述的算法，这个算法在古希腊被称为“埃及乘法”，而很多现代作者则称之为“俄罗斯农民算法”，建立在如下洞察上：

$$4a=((a+a)+a)+a=(a+a)+(a+a)$$

$$a+(b+c)=(a+b)+c$$

1        ♦         59
2                  118
4                  236
8        ♦        472
16                944
32      ♦      1888

$$41*59=(1*59)+(8*59)+(32*59)$$

$$n=n/2+n/2$$    说明n是偶数
$$n=(n-1)/2+(n-1)/2+1$$  说明n是奇数

odd(n) 意味着 half(n)=half(n-1)

int multiply1(int n, int a){
if (n==1) return a;
int result=multiply1(half(n), a+a);
if (odd(n)) result=result +a;
return result;
}

odd(x)很容易实现，测试x的最低位就可以了，half(x)则可以通过对x进行一次右移实现：

bool odd(int n) { return n&0x1;}
int half(int n) { return n>>1; }

multiply1需要进行多少次加法运算？每次调用这个函数，我们需要在a+a的地方进行一次加法，因为我们是不断对n取半，我们需要调用这个函数logn次。在某些时候，我们还需要在result+a的地方进行加法运算，因此加法运算的总次数为：

$$\#(n)=logn + v(n)-1$$

$$\#(15)=3+4-1=6$$

int multiply_by_15(int a){
int b=(a+a)+a;    //b == 3*a
int c+b+b;        //c == 6*a
return (c+c)+b;   //12*a + 3*a
}

# 2.2 改进算法

r+na

int mult_acc0{int r, int n, int a) {
if (n==1) return r+a;
if (odd(n)){
return mult_acc0(r+a, half(n), a+a);
}else{
return mult_acc0(r, half(n), a+a);
}
}

int mult_acc1(int r, int n, int a) {
if (n==1) return r+a;
if (odd(n)) r=r+a;
return mult_acc1(r, half(n), a+a);
}

• n=1的情况很少发生；
• 如果n是偶数，则完全没有必要判断它还是不是1.

int mult_acc2(int r, int n, int a) {
if (odd(n)) {
r=r+a;
if (n==1) return r;
}
return mult_acc2(r, half(n), a+a);
}

int mult_acc3 (int r, int n, int a) {
if (odd(n)) {
r=r+a;
if (n==1) return r;
}
n=half(n);
a=a+a;
return mult_acc3(r,n,a);
}

int mult_acc4(int r, int n, int a) {
while (true) {
if (odd(n)) {
r=r+a;
if (n==1) return r;
}
n=half(n);
a=a+a;
}
}

int multiply2(int n, int a) {
if (n==1) return a;
return mult_acc4(a, n-1, a);
}

int multiply3(int n, int a) {
while (!odd(n)) {
a=a+a;
n=half(n);
}
if (n==1) return a;
return mult_acc4(a, n-1, a);
}

int multiply4(int n, int a) {
while (!odd(n)) {
a=a+a;
n=half(n);
}
if (n==1) return n;
return mult_acc4(a, half(n-1), a+a);
}

# 2.3 本章的思考

## SSH Logon takes long time?

I’ve been suffering from this on CentOS 7 for quite some time now but haven’t really have time to dig into it.

Just today, I noticed the line after a successful logon:

Last login: Fri March 27 16:03:23 2016 from gateway.

Aha, now I know where the time has been spent. The SSHd must have taken a long time to figure out the host name of my login IP.

I’ve suspected this before, but in my sshd_config file, the line “UseDNS” was commented out, so I thought it must be something else.

A simple “man sshd_config” revealed that, “UseDNS yes” is actually the default setting:

UseDNS  Specifies whether sshd(8) should look up the remote host name and check that the resolved host name for the remote IP address maps back to the very same IP address.  The default is “yes”.

So I just add “UseDNS no” in the configuration file and restarted sshd. Problem solved.

## A documentary on algorithms

I guess it’s not easy to get journalism and algorithm together. Finally here’s comes a documentary about algorithms, made by BBC – The Secret Rules of Modern Living: Algorithms.

It’s intended for general public, so it’s just a gentle introduction. From Euclid’s algorithm to find greatest common divisor to different sorting algorithms to traveling sales man problem to matching algorithm and eventually to machine learning. The algorithms are well illustrated and explained. It’s just enough to expose the audience to the world of algorithm without intimidating them. More importantly, from what I can see, there’s no misconceptions that are commonly seen in introductions of algorithms made by mass media (except maybe the comparison between bubble sort and merge sort is overly simplified and didn’t take memory consumption into consideration).

One interesting scene in the documentary: President Obama was in an interview and was presented a typical computer science question:

“What is the most efficient way to sort a million 32-bit integers?”

After some hesitation, he actually answered:

“I think Bubble Sort would be the wrong way to go.”

Not bad for a president, I must say. Some say it is staged. But it’s still funny to see how Mr. President tried to circumvent the real question with a safe correct answer. You can see the interview here on YouTube.

## setting up OTRS 4

I was asked to evaluate some ticket tracking tools and OTRS came back into my mind. The following steps outline the procedure to have OTRS correctly installed on CentOS 7:

2. Install and configure Postfix and Dovecot.
3. Make sure the following configuration for MySQL has been taken care of, especially for the 3rd line:
        max_allowed_packet=50M
query_cache_size=32M
innodb_log_file_size=1073741824
4. Install some additional perl modules from EPEL repository.
5. Install OTRS from command line:
yum install --nogpgcheck http://ftp.otrs.org/pub/otrs//RPMS/rhel/7/otrs-4.0.11-01.noarch.rpm
6. Once the installation is done, continue with the web installer from:
http://<your host ip>/otrs/installer.pl
7. Simply following the wizard and then you should have a working OTRS installation.

Up to here, you have a working installation. In order to work with OTRS, now you have to configure all the queues, agents, customers, groups, templetes, etc. Have fun!

## 通过截获gettext调用来修改WordPress的部分翻译

1. 创建一个child theme。
2. 在child theme的functions.php中加入如下代码：
function change_attribute_line( $translated_text,$text, $domain ) { switch ($text ) {
$translated_text = __( '低调地使用%s'); break; } return$translated_text;
}


## Replacing WordPress translation by hooking to gettext

In a typical WordPress website, you see this line in the page footer:

This line links to WordPress website. The official Chinese translation of this line is:

“自豪地采用WordPress”

I’m using WordPress in several of my websites and I found it’s annoying: It’s a word to word translation and it’s simply not what we Chinese would say when we want to attribute something to someone. After some thinking, I think the appropriate translation should be:

“采用”emphasis the choosing of WordPress, “使用”emphasis the fact we are using WordPress right now.

The message is clear and typical Chinese: I’m a humble webmaster. All the glories go to WordPress.

So I set out to change the translation. It turned out to be rather complicated, but here’s how I finally accomplished it:

1. Create a child theme of your current theme, because you’ll have to rewrite its logic and you don’t want your effort to be overwritten by an upgrade of that theme. The procedure of creating a child theme can be found here.
function change_attribute_line( $translated_text,$text, $domain ) { switch ($text ) {
$translated_text = __( '低调地采用%s'); break; } return$translated_text;
}


That’s it.

Notice that I search for $text instead of$translated_text. You can also search for $translated_text. That would be:  switch ($translated_text ) {
case '自豪地采用%s' :
$translated_text = __( '低调地使用%s'); break; } return$translated_text;


I search for \$text because I assume (I’m not sure) search for non-unicode string will be faster.

It’s worth noting that both strings have placeholders in it. Initially I was trying to search the whole string, “Proudly powered by WordPress” or “自豪地采用WordPress” and failed. The fact that the above code works tells us, gettext() and its filters are called before the placeholders get replaced. I spent a lot of time figuring this out. It’s not mentioned in the document, nor did anyone post this on the web. This is actually the key reason why I write this post. Someone from WordPress should update the document.

## Computer, another origin

When talking about computer origin, people tend to think about chips, CPU, world war II and ENIAC, and in some occasions, date back to slide rule and Suanpan. This is also how it is taught in schools and universities. The logic behind this story line is, computer originated from the need for fast computing, especially arithmetic computing.

However, there’s another origin that is at least as important, has a longer history, and at least to some people, more fascinating. That is how the architecture of modern computer came into being. Computer nowadays is so powerful that sometimes it seems inconceivable to link it to its ancient ancestors. On the other hand, you only have to look into some of the remarkable masterpieces of the past to know the linkage is simply undeniable.

A recent BBC program I watched, “Mechanical Marvels – Clockwork Dreams” showed one of such remarkable masterpieces: A mechanical machine boy that is able to write up to 40 letters of text, depending on the configuration, built back in 1770s.

What makes it remarkable is, the text is customizable. That means, the machine boy is programmable.

Of course, it’s still an Finite State Machine, but being programmable, that means adding scratch memory to it and then it will be a complete Turing Machine!

It’s actually from this origin where techniques were developed for modern computer scientists to deal with abstract topics like computational complexity, formal language.