自google退出大陆市场,google.cn转入google.com/hk后,今日又有了新动作,那就是大陆访客都默认启用了google的“安全 搜索功能”,在你搜索性-感,成-人,尤-物,等词语时,会提示“”性-感” 已被 Google 筛选掉,因为启用了 Google 的安全搜索功能”。

此前由于受主管部门的要求,谷歌中国一直屏蔽了不少敏感关键词,难道是因启用了安全搜索把网页都给过滤掉了?

原来Google有个过滤功能,能把一些敏感的信息都过滤掉,这是因为启用了 Google 的安全搜索功能。这个功能在谷歌中是默认开启的,特别是中文google,你根本找不到关闭的选项。

3/22/2010 12:03:00 PM

On January 12, we announced on this blog that Google and more than twenty other U.S. companies had been the victims of a sophisticated cyber attack originating from China, and that during our investigation into these attacks we had uncovered evidence to suggest that the Gmail accounts of dozens of human rights activists connected with China were being routinely accessed by third parties, most likely via phishing scams or malware placed on their computers. We also made clear that these attacks and the surveillance they uncovered—combined with attempts over the last year to further limit free speech on the web in China including the persistent blocking of websites such as Facebook, Twitter, YouTube, Google Docs and Blogger—had led us to conclude that we could no longer continue censoring our results on Google.cn.

So earlier today we stopped censoring our search services—Google Search, Google News, and Google Images—on Google.cn. Users visiting Google.cn are now being redirected to Google.com.hk, where we are offering uncensored search in simplified Chinese, specifically designed for users in mainland China and delivered via our servers in Hong Kong. Users in Hong Kong will continue to receive their existing uncensored, traditional Chinese service, also from Google.com.hk. Due to the increased load on our Hong Kong servers and the complicated nature of these changes, users may see some slowdown in service or find some products temporarily inaccessible as we switch everything over.

Figuring out how to make good on our promise to stop censoring search on Google.cn has been hard. We want as many people in the world as possible to have access to our services, including users in mainland China, yet the Chinese government has been crystal clear throughout our discussions that self-censorship is a non-negotiable legal requirement. We believe this new approach of providing uncensored search in simplified Chinese from Google.com.hk is a sensible solution to the challenges we’ve faced—it’s entirely legal and will meaningfully increase access to information for people in China. We very much hope that the Chinese government respects our decision, though we are well aware that it could at any time block access to our services. We will therefore be carefully monitoring access issues, and have created this new web page, which we will update regularly each day, so that everyone can see which Google services are available in China.

In terms of Google’s wider business operations, we intend to continue R&D work in China and also to maintain a sales presence there, though the size of the sales team will obviously be partially dependent on the ability of mainland Chinese users to access Google.com.hk. Finally, we would like to make clear that all these decisions have been driven and implemented by our executives in the United States, and that none of our employees in China can, or should, be held responsible for them. Despite all the uncertainty and difficulties they have faced since we made our announcement in January, they have continued to focus on serving our Chinese users and customers. We are immensely proud of them.

以下为 Google 官方博客的全文翻译:

今年1月12日,我们在本博客上宣布,Google及另外二十余家美国公司受到了来自中国的、复杂的网络攻击,在对这些攻击进 行深入调查的过程中,通过我们所收集到的证据表明,几十个与中国有关的人权人士的Gmail帐号定期受到第三方的侵入,而这大部分侵入是通过安装在他们电 脑上的钓鱼软件或恶意软件进行的。这些攻击以及它们所暴露的网络审查问题,加上去年以来中国进一步限制网络言论自由,包括 对FaceBook、Twitter、YouTube、Google Docs 和 Blogger 等网站的持续屏蔽,使我们做出结论:我们不能继续在Google.cn搜索结果上进行自我审查。

从今天早上开始,我们已停止了在Google.cn搜索服务上的自我审查,包括 Google Search (网页搜索)、Google News(资讯搜索)和Google Images (图片搜索)。 访问 Google.cn 的用 户从现在开始将被指向Google.com.hk,在这个域名上,我们将提供未经审查的简体中文搜索结果,这些为中国大陆用户设计的服务将通过我们在香港 的服务器实现。香港地区的用户还将继续通过Google.com.hk获得跟现在一样的、未经审查的繁体中文搜索服务。在我们进行迁移的过程中,由于香港 服务器负荷的增加以及这些变化的复杂程度,用户可能会发现搜索速度变慢,或发现某些产品暂时不能访问。

实施我们做出的在Google.cn上停止审查搜索结果的承诺是一个十分艰难的过程。我们希望全球尽可能多的用户都能访问到我们的 服务,包括在中国大陆的用户。中国政府在与我们讨论的过程中已经十分明确地表示,自我审查是一个不可谈判的法律要求。为此,我们相信,一个解决我们所面临 挑战的可行方案是在Google.com.hk上提供未经审查的简体中文搜索结果——它完全符合法律要求,同时也有助于提高中国大陆用户对信息的访问。我 们十分希望中国政府尊重我们的这一决定,尽管我们知道,用户对Google服务的访问有可能随时被阻止。为此,我们将密切监测网址访问问题,并制作了一个 新页面,用户可以实时地了解到在中国哪些Google服务是可用的。

至于Google的广泛的业务运营,我们计划继续在中国的研发工作,并将保留销售团队,然而销售团队的规模显然部分取决于中国大陆 用户能否访问Google.com.hk 。最后,我们要清楚表明:所有这些决定都是由美国的管理团队做出和实施的,没有任何一位中国员工能够、或者应该为这些决定负责。自我们在1月份发布博客以 来,尽管面临着众多的不确定性和困难,他们仍然坚守在工作岗位,专注于服务我们的中国用户和客户。我们为拥有这样的员工感到深深的骄傲。

高级副总裁、公司发展和首席法务官 David Drummond 于 2010年3月22日 下午12:03:00 发布

3月23日消息,据国外媒体报道,谷歌周一宣布,已正式关闭中国内地网站Google.cn,同时将用户重新定向到谷歌香港网站。

谷歌称:“履行我们不再对Google.cn进行审查的承诺很难,我们希望全世界尽可能多的网民访问到我们的服务,其中包括中国内地用户。但是,在整个谈判过程中,中国政府的态度很明确,自我审查是一个不可商量的法律规定。”

谷歌表示:“我们相信,这种通过谷歌香港网站(Google.com.hk)提供未经审查的简体中文搜索服务的做法是可行的,而且完全合法,将在很大程度上提升中国网民所访问的信息量。”

谷歌还表示:“我们十分希望中国政府能够尊重我们的决定。”另外,关闭Google.cn网站后,谷歌在内地的研发和销售团队将予以保留。

受该决定影响,谷歌在内地的一部分员工可能将失业,但知情人士称,大部分员工仍将继续为香港网站Google.com.hk服务。

以下为对www.google.cn的http请求的检测:

#1 Server Response: http://www.google.cn
HTTP Status Code: HTTP/1.1 302 Found
Location: http://www.google.com.hk/url?sa=p&cki=PREF%3DID%3Dd1dcd756500a085c:U%3D4a925b03c0a1424b:FF%3D2:LD%3Dzh-CN:TM%3D1266820487:LM%3D1269305162:S%3Dxb-GTcD-JqtcAVVU&q=http://www.google.com.hk/&ust=1269305192233794&usg=AFQjCNGIZIXQ1-XQDPzE1qzktq3myXj9Rw
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Date: Tue, 23 Mar 2010 00:46:02 GMT
Server: gws
Content-Length: 459
X-XSS-Protection: 0
Redirect Target: http://www.google.com.hk/url?sa=p&cki=PREF%3DID%3Dd1dcd756500a085c:U%3D4a925b03c0a1424b:FF%3D2:LD%3Dzh-CN:TM%3D1266820487:LM%3D1269305162:S%3Dxb-GTcD-JqtcAVVU&q=http://www.google.com.hk/&ust=1269305192233794&usg=AFQjCNGIZIXQ1-XQDPzE1qzktq3myXj9Rw
#2 Server Response: http://www.google.com.hk/url?sa=p&cki=PREF%3DID%3Dd1dcd756500a085c:U%3D4a925b03c0a1424b:FF%3D2:LD%3Dzh-CN:TM%3D1266820487:LM%3D1269305162:S%3Dxb-GTcD-JqtcAVVU&q=http://www.google.com.hk/&ust=1269305192233794&usg=AFQjCNGIZIXQ1-XQDPzE1qzktq3myXj9Rw
HTTP Status Code: HTTP/1.0 302 Moved Temporarily
Location: http://www.google.com.hk/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
Date: Tue, 23 Mar 2010 00:46:02 GMT
Server: gws
Content-Length: 222
X-XSS-Protection: 0
X-Cache: MISS from .
Via: 1.0 .:8000 (squid)
Connection: close
Redirect Target: http://www.google.com.hk/
#3 Server Response: http://www.google.com.hk/
HTTP Status Code: HTTP/1.0 200 OK
Date: Tue, 23 Mar 2010 00:46:02 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=Big5
Server: gws
X-XSS-Protection: 0
X-Cache: MISS from .
Via: 1.0 .:8001 (squid)
Connection: close

完整的代码如下:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* JAVA操作SSL协议,通过Socket访问Https的程序代码例子。
*
*
*/
public class ReadHttpsURL {
// 默认的HTTPS 端口      visit
static final int HTTPS_PORT = 443;
public static void main(String argv[]) throws Exception {
// 受访主机
String host = “www.google.com”;
// 受访的页面
String url = “/adsense/?sourceid=aso&subid=ZH_CN-ET-AS-ADSBY6&medium=link&hl=zh_CN”;
// 自定义的管理器
X509TrustManager xtm = new Java2000TrustManager();
TrustManager mytm[] = { xtm };
// 得到上下文
SSLContext ctx = SSLContext.getInstance(“SSL”);
// 初始化
ctx.init(null, mytm, null);
// 获得工厂
SSLSocketFactory factory = ctx.getSocketFactory();
// 从工厂获得Socket连接
Socket socket = factory.createSocket(host, HTTPS_PORT);
// 剩下的就和普通的Socket操作一样了
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.write(“GET ” + url + ” HTTP/1.0

“);
out.flush();
System.out.println(“start   work!”);
String line;
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null) {
sb.append(line + ”
“);
}
out.close();
in.close();
System.out.println(sb.toString());
}
}
/**
* 自定义的认证管理类。
*
*
*/
class Java2000TrustManager implements X509TrustManager {
Java2000TrustManager() {
// 这里可以进行证书的初始化操作
}
// 检查客户端的可信任状态
public void checkClientTrusted(X509Certificate chain[], String authType) throws CertificateException {
System.out.println(“检查客户端的可信任状态…”);
}
// 检查服务器的可信任状态
public void checkServerTrusted(X509Certificate chain[], String authType) throws CertificateException {
System.out.println(“检查服务器的可信任状态”);
}
// 返回接受的发行商数组
public X509Certificate[] getAcceptedIssuers() {
System.out.println(“获取接受的发行商数组…”);
return null;
}
}

在web应用交互过程中,有很多场景需要保证通信数据的安全;在前面也有好多篇文章介绍了在Web Service调用过程中用WS-Security来保证接口交互过程的安全性,值得注意的是,该种方式基于的传输协议仍然是Http,采用这种方式可扩 展性和数据交互效率比较高;另外一种实现方式就是用Https,他是在协议层对Http的再次封装,加入了SSL/TLS,采用该协议进行通信的数据全部 都会被加密,由于目前Web开发编程中对此都有了一定程度的封装,所以采用Https对外提供服务,除了证书以外,对编程能力的要求并不高,相对于前者门 槛较低,但是由于对双方通信的所有数据都进行加密,而且交互过程中还有多次握手等,所以效率较低;以下就介绍下在Java中访问Https链接时会出现的 一些问题;

在Java中要访问Https链接时,会用到一个关键类HttpsURLConnection;参见如下实现代码:

// 创建URL对象
URL myURL = new URL(“https://www.sun.com”);

// 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
HttpsURLConnection httpsConn = (HttpsURLConnection) myURL
.openConnection();

// 取得该连接的输入流,以读取响应内容
InputStreamReader insr = new InputStreamReader(httpsConn
.getInputStream());

// 读取服务器的响应内容并显示
int respInt = insr.read();
while (respInt != -1) {
System.out.print((char) respInt);
respInt = insr.read();
}

在取得connection的时候和正常浏览器访问一样,仍然会验证服务端的证书是否被信任(权威机构发行或者被权威机构签名);如果服务端证书不被信任,则默认的实现就会有问题,一般来说,用SunJSSE会抛如下异常信息:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

上面提到SunJSSE,JSSE(Java Secure Socket Extension)是实现Internet安全通信的一系列包的集合。它是一个SSL和TLS的纯Java实现,可以透明地提供数据加密、服务器认证、 信息完整性等功能,可以使我们像使用普通的套接字一样使用JSSE建立的安全套接字。JSSE是一个开放的标准,不只是Sun公司才能实现一个 SunJSSE,事实上其他公司有自己实现的JSSE,然后通过JCA就可以在JVM中使用。
关于JSSE的详细信息参考官网Reference:http://java.sun.com/j2se/1.5.0/docs/guide/security/jsse/JSSERefGuide.html
以及Java Security Guide:http://java.sun.com/j2se/1.5.0/docs/guide/security/

在深入了解JSSE之前,需要了解一个有关Java安全的概念:客户端的TrustStore文件。客户端的TrustStore文件中保存着被客 户端所信任的服务器的证书信息。客户端在进行SSL连接时,JSSE将根据这个文件中的证书决定是否信任服务器端的证书。在SunJSSE中,有一个信任 管理器类负责决定是否信任远端的证书,这个类有如下的处理规则:
1、若系统属性javax.net.sll.trustStore指定了TrustStore文件,那么信任管理器就去jre安装路径下的lib/security/目录中寻找并使用这个文件来检查证书。
2、若该系统属性没有指定TrustStore文件,它就会去jre安装路径下寻找默认的TrustStore文件,这个文件的相对路径为:lib/security/jssecacerts
3、若jssecacerts不存在,但是cacerts存在(它随J2SDK一起发行,含有数量有限的可信任的基本证书),那么这个默认的TrustStore文件就是lib/security/cacerts

那遇到这种情况,怎么处理呢?有以下两种方案:
1、按照以上信任管理器的规则,将服务端的公钥导入到jssecacerts,或者是在系统属性中设置要加载的trustStore文件的路径;证书导入可以用如下命令:keytool -import -file src_cer_file –keystore dest_cer_store;至于证书可以通过浏览器导出获得;
2、实现自己的证书信任管理器类,比如MyX509TrustManager,该类必须实现X509TrustManager接口中的三个method;然后在HttpsURLConnection中加载自定义的类,可以参见如下两个代码片段,其一为自定义证书信任管理器,其二为connect时的代码:

package test;

import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

public class MyX509TrustManager implements X509TrustManager {

/*
* The default X509TrustManager returned by SunX509.  We’ll delegate
* decisions to it, and fall back to the logic in this class if the
* default X509TrustManager doesn’t trust it.
*/
X509TrustManager sunJSSEX509TrustManager;

MyX509TrustManager() throws Exception {
// create a “default” JSSE X509TrustManager.

KeyStore ks = KeyStore.getInstance(“JKS”);
ks.load(new FileInputStream(“trustedCerts”),
“passphrase”.toCharArray());

TrustManagerFactory tmf =
TrustManagerFactory.getInstance(“SunX509″, “SunJSSE”);

tmf.init(ks);

TrustManager tms [] = tmf.getTrustManagers();

/*
* Iterate over the returned trustmanagers, look
* for an instance of X509TrustManager.  If found,
* use that as our “default” trust manager.
*/
for (int i = 0; i < tms.length; i++) {
if (tms[i] instanceof X509TrustManager) {
sunJSSEX509TrustManager = (X509TrustManager) tms[i];
return;
}
}

/*
* Find some other way to initialize, or else we have to fail the
* constructor.
*/
throw new Exception(“Couldn’t initialize”);
}

/*
* Delegate to the default trust manager.
*/
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
try {
sunJSSEX509TrustManager.checkClientTrusted(chain, authType);
} catch (CertificateException excep) {
// do any special handling here, or rethrow exception.
}
}

/*
* Delegate to the default trust manager.
*/
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
try {
sunJSSEX509TrustManager.checkServerTrusted(chain, authType);
} catch (CertificateException excep) {
/*
* Possibly pop up a dialog box asking whether to trust the
* cert chain.
*/
}
}

/*
* Merely pass this through.
*/
public X509Certificate[] getAcceptedIssuers() {
return sunJSSEX509TrustManager.getAcceptedIssuers();
}
}

// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance(“SSL”, “SunJSSE”);

sslContext.init(null, tm, new java.security.SecureRandom());

// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();

// 创建URL对象
URL myURL = new URL(“https://ebanks.gdb.com.cn/sperbank/perbankLogin.jsp”);

// 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
httpsConn.setSSLSocketFactory(ssf);

// 取得该连接的输入流,以读取响应内容
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());

// 读取服务器的响应内容并显示
int respInt = insr.read();
while (respInt != -1) {
System.out.print((char) respInt);
respInt = insr.read();
}

对于以上两种实现方式,各有各的优点,第一种方式不会破坏JSSE的安全性,但是要手工导入证书,如果服务器很多,那每台服务器的JRE都必须做相同的操作;第二种方式灵活性更高,但是要小心实现,否则可能会留下安全隐患;

java模拟网站登录

Posted by 冰河 at 13:18 No Responses » 25,568 Views
202010

web登陆无非就是网页获取,cookie 的管理,post和get方式的模拟。

1.网页内容获取 java.io.InputStream   in;
java.net.URL url = new java.net.URL(www.xyz.com/content.html);
java.net.HttpURLConnection connection = (java.net.HttpURLConnection)
url.openConnection();
connection = (java.net.HttpURLConnection) url.openConnection();
//模拟成IE
connection.setRequestProperty(“User-Agent”,”Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)”);
connection.connect();
in = connection.getInputStream();
java.io.BufferedReader breader =
new BufferedReader(new InputStreamReader(in , “GBK”));
String str=breader.readLine());
while(st != null){
System.out.println(str); str=breader.readLine());
}
2.cookie管理
1.直接的方式
取得cookie:
HttpURLConnection huc= (HttpURLConnection) url.openConnection();
InputStream is = huc.getInputStream();
// 取得sessionID.
String cookieVal = hc.getHeaderField(“Set-Cookie”);
String sessionId;
if(cookieVal != null)
{
sessionId = cookieVal.substring(0, cookieVal.indexOf(“;”));
}
发送设置cookie:
HttpURLConnection huc= (HttpURLConnection) url.openConnection();
if(sessionId != null)
{
huc.setRequestProperty(“Cookie”, sessionId);
}
InputStream is = huc.getInputStream();

2.利用的jcookie包(http://jcookie.sourceforge.net/ )
获取cookie:
URL url = new URL(“http://www.site.com/”);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.connect();
InputStream is = huc.getInputStream();
Client client = new Client();
CookieJar cj = client.getCookies(huc);

新的请求,利用上面获取的cookie:
url = new URL(“http://www.site.com/”);
huc = (HttpURLConnection) url.openConnection();
client.setCookies(huc, cj);

3.post方式的模拟
URL url = new URL(“www.xyz.com”);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
//设置允许output
huc.setDoOutput(true);
//设置为post方式
huc.setRequestMethod(“POST”);
huc.setRequestProperty(“User-Agent”,”Mozilla/4.7 [en] (Win98; I)”);
StringBuffer sb = new StringBuffer();
sb.append(“userName=”+userNme);
sb.append(“&password=”+password);

//post信息
OutputStream os = huc.getOutputStream();
os.write(sb.toString().getBytes(“GBK”));
os.close();

BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()))
huc.connect();
String line = br.readLine();
while(line != null){
System.out.printli(line);
line = br.readLine();
}

202010

package com.john.j2se.util;
/**
* MD5加密算法
*/
import java.security.MessageDigest;

public class MD5Util {
public final static String MD5(String s) {
char hexDigits[] = { ’0′, ’1′, ’2′, ’3′, ’4′,
’5′, ’6′, ’7′, ’8′, ’9′,
‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’};
try {
byte[] btInput = s.getBytes();
MessageDigest mdInst = MessageDigest.getInstance(“MD5″);
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

MD5加密算法是一种单向加密算法。

import java.io.UnsupportedEncodingException;

public class Base64 {
private static char[] base64EncodeChars = new char[] {
‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’,
‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’,
‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’,
‘Y’, ‘Z’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’,
‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’,
‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’,
‘w’, ‘x’, ‘y’, ‘z’, ’0′, ’1′, ’2′, ’3′,
’4′, ’5′, ’6′, ’7′, ’8′, ’9′, ‘+’, ‘/’ };

private static byte[] base64DecodeChars = new byte[] {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 };
//编码

public static String encode(byte[] data) {
StringBuffer sb = new StringBuffer();
int len = data.length;
int i = 0;
int b1, b2, b3;
while (i < len) {
b1 = data[i++] & 0xff;
if (i == len)
{
sb.append(base64EncodeChars[b1 >>> 2]);
sb.append(base64EncodeChars[(b1 & 0x3) << 4]);
sb.append(“==”);
break;
}
b2 = data[i++] & 0xff;
if (i == len)
{
sb.append(base64EncodeChars[b1 >>> 2]);
sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
sb.append(base64EncodeChars[(b2 & 0x0f) << 2]);
sb.append(“=”);
break;
}
b3 = data[i++] & 0xff;
sb.append(base64EncodeChars[b1 >>> 2]);
sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]);
sb.append(base64EncodeChars[b3 & 0x3f]);
}
return sb.toString();
}

//解码
public static byte[] decode(String str) throws UnsupportedEncodingException {
StringBuffer sb = new StringBuffer();
byte[] data = str.getBytes(“US-ASCII”);
int len = data.length;
int i = 0;
int b1, b2, b3, b4;
while (i < len) {
/* b1 */
do {
b1 = base64DecodeChars[data[i++]];
} while (i < len && b1 == -1);
if (b1 == -1) break;
/* b2 */
do {
b2 = base64DecodeChars
[data[i++]];                 } while (i < len && b2 == -1);
if (b2 == -1) break;
sb.append((char)((b1 << 2) | ((b2 & 0×30) >>> 4)));
/* b3 */
do {
b3 = data[i++];
if (b3 == 61) return sb.toString().getBytes(“iso8859-1″);
b3 = base64DecodeChars[b3];
} while (i < len && b3 == -1);
if (b3 == -1) break;
sb.append((char)(((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));
/* b4 */
do {
b4 = data[i++];
if (b4 == 61) return sb.toString().getBytes(“iso8859-1″);
b4 = base64DecodeChars[b4];
} while (i < len && b4 == -1);
if (b4 == -1) break;
sb.append((char)(((b3 & 0×03) << 6) | b4));
}
return sb.toString().getBytes(“iso8859-1″);
}
}

Base64 是一种双向加密算法

sql注入

Posted by 冰河 at 18:58 No Responses » 114,586 Views
192010

sql注入

  所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如先前的很多影视网站泄露VIP会员密码大多就是通过WEB表单递交查询字符暴出的,这类表单特别容易受到SQL注入式攻击.

  当应用程序使用输入内容来构造动态sql语句以访问数据库时,会发生sql注入攻击。如果代码 使用存储过程,而这些存储过程作为包含未筛选的用户输入的字符串来传递,也会发生sql注入。sql注入可能导致攻击者使用应用程序登陆在数据库中执行命 令。如果应用程序使用特权过高的帐户连接到数据库,这种问题会变得很严重。在某些表单中,用户输入的内容直接用来构造(或者影响)动态sql命令,或者作 为存储过程的输入参数,这些表单特别容易受到sql注入的攻击。而许多网站程序在编写时,没有对用户输入的合法性进行判断或者程序中本身的变量处理不当, 使应用程序存在安全隐患。这样,用户就可以提交一段数据库查询的代码,根据程序返回的结果,获得一些敏感的信息或者控制整个服务器,于是sql注入就发生 了。

注入大致方法:

  
  先猜表名
  And (Select count(*) from 表名)<>0
  猜列名
  And (Select count(列名) from 表名)<>0
  或者也可以这样
  and exists (select * from 表名)
  and exists (select 列名 from 表名)
  返回正确的,那么写的表名或列名就是正确
  这里要注意的是,exists这个不能应用于猜内容上,例如and exists (select len(user) from admin)>3 这样是不信的
  现在很多人都是喜欢查询里面的内容,一旦iis没有关闭错误提示的,那么就可以利用报错方法轻松获得库里面的内容
  获得数据库连接用户名:;and user>0
  这个是小竹提出来的,我这里引用《SQL注入天书》里面的一段话来讲解:
  ----------------------------------------------------------------------------------------------------------
  "重点在and user>0,我们知道,user是SQLServer的一个内置变量,它的值是当前连接的用户名,类型为nvarchar。拿一个 nvarchar的值跟int的数0比较,系统会先试图将nvarchar的值转成int型,当然,转的过程中肯定会出错,SQLServer
  的出错提示是:将nvarch"
  -----------------------------------------------------------------------------------------------------------
  看到这里大家明白了吧,报错的原理就是利用SQLserver内置的系统表进行转换查询,转换 过程会出错,然后就会显示出在网页上,另外还有类似的and 1=(selet top 1 user from admin),这种语句也是可以爆出来的。;and db_name()>0 则
  是暴数据库名。
  一旦关闭了IIS报错,那么还可以用union(联合查询)来查内容,主要语句就是
  Order by 10
  And 1=2 union select 1,2,3,4,5,6,7,8,9,10 from admin
  And 1=2 union select 1,2,3,user,5,passwd,7,8,9,10 from admin
  上面的order by 10主要就是查字段数目,admin就是表名,可以自己猜,user,passwd是列名
  反正就是返回正确即对,返回异常即错
  另外还有十分常用的ascll码拆半法
  先要知道指定列名,例如user里的内容的长度
  and (select len(user) from admin)=2 就是查询长度为不为2位,返回错误的增加或减少数字,一般这个数字不会太大,太大的就要放弃了,猜也多余
  后面的逻辑符号可以根据不同要求更改的,
  >大于 <小于 =就是等于咯,更新语句的话,=也可以表示传递符号 <>就是不等
  知道了长度后就可以开始猜解了
  And (Select top 1 asc(mid(user,n,1)) from admin)>100
  n就是猜解的表名的第几位,最后的长度数字就是刚才猜解出来的列名长度了,And (Select top 1 asc(mid(user,1,1)) from admin)>100 就是猜解user里内容的第一位的ASCLL字符是不是大于100
  正确的话,那么表示USER第一个字符的ASCLL码大于100,那么就猜>120,返回错误就是介于100-120之间,然后再一步一步的缩少,最终得到正确字符XXX,然后用ASCLL转换器吧这个转换成普通字符就可以了
  然后就是第二位 And (Select top 1 asc(mid(user,2,1)) from admin)>100 一直猜下去
  加在url后面,列名表名还是先猜解,返回正确的代表帐号的ascll码大于100,那么就再向前猜,指导报错,把猜出来的ascll码拿去ascll转换器转换就可以了,中文是负数,加上asb取绝对值
  And (Select top 1 asb(asc(mid(user,n,1))) from admin)>15320
  得到之后就记得在数字前加-号,不然ASCLL转换器转换不来的,中文在ASCLL码里是-23423这样的,所以猜起来挺麻烦
  这个猜解速度比较慢,但是效果最好,最具有广泛性
  2.2.后台身份验证绕过漏洞
  验证绕过漏洞就是'or'='or'后台绕过漏洞,利用的就是AND和OR的运算规则,从而造成后台脚本逻辑性错误
  例如管理员的账号密码都是admin,那么再比如后台的数据库查询语句是
  user=request("user")
  passwd=request("passwd")
  sql='select admin from adminbate where user='&'''&user&'''&' and passwd='&'''&passwd&'''
  那么我使用'or 'a'='a来做用户名密码的话,那么查询就变成了
  select admin from adminbate where user=''or 'a'='a' and passwd=''or 'a'='a'
  这样的话,根据运算规则,这里一共有4个查询语句,那么查询结果就是 假or真and假or真,先算and 再算or,最终结果为真,这样就可以进到后台了
  这种漏洞存在必须要有2个条件,第一个:在后台验证代码上,账号密码的查询是要同一条查询语句,也就是类似
  sql="select * from admin where username='"&username&'&"passwd='"&passwd&'
  如果一旦账号密码是分开查询的,先查帐号,再查密码,这样的话就没有办法了。
  第二就是要看密码加不加密,一旦被MD5加密或者其他加密方式加密的,那就要看第一种条件有没有可以,没有达到第一种条件的话,那就没有戏了
  3 防御方法
  对于怎么防御SQL注入呢,这个网上很多,我这里讲几个
  如果自己编写防注代码,一般是先定义一个函数,再在里面写入要过滤的关键词,如select ; “”;form;等,这些关键词都是查询语句最常用的词语,一旦过滤了,那么用户自己构造提交的数据就不会完整地参与数据库的操作。
  当然如果你的网站提交的数据全部都是数字的,可以使用小竹提供的方法
  Function SafeRequest(ParaName,ParaType)
  '— 传入参数 —
  'ParaName:参数名称-字符型
  'ParaType:参数类型-数字型(1表示以上参数是数字,0表示以上参数为字符)
  Dim ParaValue
  ParaValue=Request(ParaName)
  If ParaType=1 then
  If not isNumeric(ParaValue) then
  Response.write "参数" & ParaName & "必须为数字型!"
  Response.end
  End if
  Else
  ParaValue=replace(ParaValue,"'","''")
  End if
  SafeRequest=ParaValue
  End function
  然后就用SafeRequest()来过滤参数 ,检查参数是否为数字,不是数字的就不能通过

Firefox是最适合搭配AutoProxy使用的浏览器,而且最初AutoProxy也只是个Firefox插件。

1.安装AutoProxy插件

首先到https://addons.mozilla.org/zh-CN/firefox/addon/11009安装AutoProxy插件,打开页面后选中“让我安装这个实验性附加组件“,完后点击“添加到Firefox”按钮(图1),接着在弹出的窗口中点“立即安装” (图2),安装完毕后点击“重新启动Firefox”重启浏览器(图3)。

图1

2010010611.jpg

图2

2010010612.jpg

图3

2010010613.jpg

2.配置AutoProxy插件

重启浏览器后Firefox会弹出AutoProxy的订阅列表,目前还只有一个列表(图4),选中“gfwList”并点击“默认代理“,在这里你可以选择你使用的翻墙软件(图5),如果你使用的翻墙软件没有列在这里,还需点击“自定义“按钮进行设置。接着在弹出的窗口中输入你所使用的代理服务器名称和地址(这里以SSH,HTTP代理为例,地址为127.0.0.1:8116),你可以选择点击“增加代理“,增加一个代理,或者随便修改列表中的一个代理,将其内容替换成你所使用的代理地址和类型即可(图6)。注意区分http、socks4和socks5代理。如何搭建这些代理可以参考:

MyEnTunnel使用说明
Tunnelier使用说明

图4

2010010614.jpg

图5

2010010615.jpg

图6

2010010616.jpg

接着点击两次确定,回到Firefox的主界面。在Firefox界面的右下方的AutoProxy图标上点击右键(图7),选择“首选项“,即可看到gfwList的详细内容了,如果某个网站已经被墙,但是不在gfwList中,你可以点击“增加规则“按钮,然后再弹出的文本框中输入被墙的网址,如abctest.com(图8 ),然后点击“确定“即可,如果你要一次添加多个网址,添加第一个网址后点击代理规则下方的任意位置(如“订阅组“这一行),接着这个网址即会被添加到“自定义代理组“列表中,而且这个窗口也不会自动关闭(图9),如果这个网站有二级域名,则需输入.abctest.com(注意:最前面有个点)。接下来AutoProxy就会自动调用代理访问这个网站了。

图7

2010010617.jpg

图8

2010010618.jpg

图9

2010010619.jpg

你也可以尝试更新一下gfwList,说不定这个网站已经提交到gfwList中了,只是还没有更新到本地,在AutoProxy的图标上点右键,选择“首选项“-”订阅规则“-”更新所有订阅” (图10),待状态显示为“成功“并且更新时间显示为当前时间(图11),即说明gfwList已经更新为最新版的了。

图10

20100106110.jpg

图11

201001061111.jpg

你还可以到http://tr.im/gfwList将这个网址提交到gfwList 中,提交时最好能留下联系的Email,方便维护gfwList的工作人员联系你,因为某些网站各地的封锁状况不一,可能在一个城市不能访问,在另一个城市就能访问,甚至在同一城市换一个ISP就又能访问了。

如果你不想再访问某个网站时使用代理,只要右键点击AutoProxy的图标,选择“在xxxxx禁用代理“即可。使用“全局模式“可以让Firefox的所有连接(包括插件)都通过gfwList中的规则访问网络,选择“禁用AutoProxy”则可暂时禁用AutoProxy(图12)。

图12

2010010617.jpg

如果你想更换代理服务器,右键点击AutoProxy图标,在弹出的窗口中选择“代理服务器“-”选择代理服务器“,在这里你就可以方便的在多个代理服务器中切换(图13),选择“代理服务器“-”增加代理服务器“即可添加新的代理服务器。

图13

201001061112.jpg

你还可以在gfwList中选中某个网址,点击右键,选择“删除“或“禁用“来删除或者禁止AutoPorxy在访问某个网址自动调用代理(图14)。

图14

201001061114.jpg

原文链接:http://twitbrowser.net/blog/608

如果你还不知道AutoProxy/AutoProxy2PAC是什么,请先阅读 《AutoProxy使用详解一:什么是AutoProxy,它可以做什么,如何使用?》。

OperaChrome/Chromiume使用PAC的方法和IE差不多,这里就配几张图简单的说一下设置的方法。

1.Opera

点击工具“-”首选项“-”高级“-”网络“-”代理服务器,在这里填入PAC文件的位置就可以利用PAC中设置的规则上网了。PAC的路径格式和IE一样,具体可参照AutoProxy使用详解一:什么是AutoProxy,它可以做什么,如何使用?》中的设置IE浏览器的部分。

20100103101.jpg

20100103102.jpg

20100103103.jpg

2.Chrome/Chromium

Chrome/Chromium本身就是直接调用IE的代理设置,你可以直接在IE里设置,或者点击浏览器右上角的扳手图标,选择选项“-”高级选项“-”更改代理设置,接下来Chrome会自动打开IE的代理设置窗口,设置方法可参照《AutoProxy使用详解一:什么是AutoProxy,它可以做什么,如何使用?》中的设置IE浏览器的部分。

20100103104.jpg

20100103105.jpg

原文链接:http://twitbrowser.net/blog/506

© 2009 - 2024 冰河的博客