Thursday, January 14, 2016

Cookie

Set cookie

 Cookie cookie = new Cookie("name","abc"); //new Cookie(String,String)
 cookie.setMaxAge(1000); 
 //in seconds, default if not set: cleaned when browser closed
 response.addCookie(cookie); //remember to call addCookie

Get cookie

//Have to get all cookies as Array, then loop through each of them

Cookie cookies[] = request.getCookies(); //return null if none cookies
for(int i=0;i&ltcookies.length;i++){
 Cookie cookie = cookies[i];
 cookie.getName();
 cookie.getValue();
 cookie.getMaxAge();
}



  • Multiple browsers can share the same cookie 
  • If set the same key word, the old value will be replaced 
  • Every cookie can have their own life length 
  • All cookie from the same webapp will store in one single file on client hard drive, with timestamp
  • 20 most cookies a webapp can save to browser, each cookie have 4k max space
  • a browser can save 300 most cookies
Cookie life cycle
  • Cookie by default, will be destroyed when browser closed
  • setMaxAge(1000) can set length of life time for cookie
  • setMaxAge(0) destroy cookie
  • setMaxAge(-1000) cookie will destroy when browser closed
Cookie with Different Character set
  Saving
String val = java.net.URLEncoder.encode("中国字","utf-8");
Cookie cookie = new Cookie("name",val);
Getting
  String val = java.net.URLDecoder.decode(cookie.getValue(),"utf-8");

Thursday, January 7, 2016

Wireshark Filters


Search string from tcp

tcp contains facebook

To search HTTP contents from TLSv1.2 protocol
https://jimshaver.net/2015/02/11/decrypting-tls-browser-traffic-with-wireshark-the-easy-way/


Logical expression

!(ip.src == 192.168.68.18)
!(ip.src == 192.168.68.18) and  (ip.dst == 192.168.69.7)
(ip.src == 192.168.68.18) or  (ip.dst == 192.168.69.7)

IP
ip.addr == 192.168.0.1
ip.src == 192.168.0.1
ip.dst == 192.168.0.1

Input Protocol names directly

tcp
dns
http
udp
tcp or udp

tcp.port == 80

!(arp or dns or icmp)


Packet lost or re-transmission tracking

tcp.analysis.flags


Track one TCP connection
Right click on the record, "Follow TCP Stream"


HTTP Request
http.request
http.response.code == 200

Security usages
If server being attack

tcp.flags.syn == 1
tcp.flags.reset == 1

Signal RTP
sip && rtp

TCP/IP Protocols

TCP Port Numbers

21 FTP
23 Telnet
25 SMTP
80 HTTP
110 POP3
443 HTTPS

UDP Port Numbers
69 TFTP
520 RIP

TCP/UDP Common Ports
53 DNS
161 SNMP
531 AOL Instant Messenger IRC

OSI Model Layers
  1. Application Layer
  2. Presentation Layer
  3. Session Layer
  4. Transport Layer
  5. Network Layer
  6. Data Link Layer
  7. Physical Layer


Reduced Layers
  1. Application Layer
  2. Transmission Layer
  3. Internet Layer
  4. Data Link Layer
  5. Physical Layer

TCP: Three-way handshake

=> SYN Seq=0
<= SYN, ACK Seq=0 Ack=1
=> ACK Seq=1 ACK=1

(response ACK = received Seq+1)




Monday, January 4, 2016

Servlet Utils

request.getContextPath()  // return "/webapp"

Tomcat default main directory:

getServletContext().getRealPath("/"); /var/lib/tomcat7/webapps/webapp1/

System.getProperty("catalina.base"); /var/lib/tomcat7


Load config file
Properties pp = new Properties();
InputStream fis = getServletContext().getResourceAsStream("WEB-INF/settings.properties");
pp.load(fis);


Read File


 String filePath = this.getServletContext().getRealPath("abc.text");
 FileReader fileReader = new FileReader(filePath);
 BufferedReader bufferedReader = new BufferedReader(fileReader);
 String line = bufferedReader.readLine();
 //do with line
 bufferedReader.close();
 fileReader.close();

Sunday, January 3, 2016

HttpServletResponse

Output:
  • getWriter() //characters only
  • getOutputStream() //bytes and characters.getByte()

We cannot use both method for one response, because an output will be auto closed, so the other output will not go through.
  • sendRedirect()
    response.sendRedirect("/webapp/anotherPage")// use at least "webapp" go back to client, client browser then send request to the new url
    //request.getContextPath() will return "/webapp"
    //the url can be anywhere in the world
    //the url from browser will change to the new url
    //after the sendRedirect, all code continues to run, so make sure to return and finish the service
    //To prevent user refreshing page, normally we use sendRedirect to direct to another url, so when user refresh, it keeps in the new url
  • flush()
    when response.flush() called, print writer committed all buffer to client, it can still push more content to client, but no forward contents will be affected.
    So if we call flush() first, request.getRequestDispatcher(url).forward(req,res) will not affect anything on the client side
    And before flush(), if we call request.getRequestDispatcher(url).forward(req,res) the printed buffer will not be affect, client will only see the forwarded contents.
  • Refresh: 1;url=http://www.baidu.com     //redirect to url after 1 second
    //response.setHeader("Refresh", "5;url=http://www.sohu.com");
  • cache: set http header, disable/enable cache
    response.setDateHeader("Expires",-1); //or value to System.currentTimeMillis()+1*1000;
    response.setHeader("Cache-Control","no-cache");
    response.setHeader("Pragma","no-cache");=
  • download file
    response.setHeader("Content-Disposition", "attachment; filename=winter.jpg");
    String path = getServletContext().getRealPath("/images/Winter.jpg");
    FileInputStream fis = new FileInputStream(path);
    byte buff[] = new byte[1024];
    int length=0;
    while((length=fis.read(buff))>0){
    response.getOutputStream().write(buff, 0, length);
    }
    response.getOutputStream().close();
    fis.close();
                    • Image type
                      response.setHeader("Content-Type", "image/jped");
                    • Encode URL, with SESSIONID in url
                      url = response.encodeURL("/webapp/servlet1");
                      // return /webapp/servlet1?SESSIONID=ABCDEFGHIJKLMN
                    • Captcha


                    Summary
                    1. Control response header, so you can manage the actions from browser
                    2. Use HttpServletResponse provided functions
                    3. Output stream will be auto closed when service is finished

                    Saturday, January 2, 2016

                    Character Encoding Issue, all solutions

                    Hibernate
                      <property name="hibernate.connection.CharSet">utf8</property>
                      <property name="hibernate.connection.characterEncoding">utf8</property>
                      <property name="hibernate.connection.useUnicode">true</property>



                    MySQL
                    Here is an doc for MySQL and Java io: http://blog.csdn.net/liuxueyongyu/article/details/2026788

                    Server encoding: iso-8859-1

                    Set page encoding to show Chinese character
                    response.setContentType("text/html;charset=utf-8");


                    Data request

                    1. form post
                    2. form get
                    3. url from html
                    4. sendRedirect

                    1. form post

                    Browser: utf-8

                    Web Server: iso-8859-1

                    Solution
                    request.setCharacterEncoding("utf-8")

                    2. form get / 3. url from html
                    Browser: utf-8

                    Web Server: iso-8859-1

                    Solution 1

                    @WebFilter("/CharacterEncoding")
                    public class CharacterEncoding implements Filter {
                     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                      request.setCharacterEncoding("utf-8"); //set request character encoding
                      chain.doFilter(request, response);
                     }
                     @Override
                     public void init(FilterConfig filterConfig) throws ServletException {
                     }
                     @Override
                     public void destroy() {
                     }
                    }
                    


                    In web.xml
                    Write a filter by yourself

                    <filter>
                     <filter-name>CharacterEncoding</filter-name>
                     <filter-class>com.gvace.notebook.web.filter.CharacterEncoding</filter-class>
                    </filter>
                    <filter-mapping>
                     <filter-name>CharacterEncoding</filter-name>
                     <url-pattern>/*</url-pattern>
                    </filter-mapping>
                    

                    or Spring Web Filter(do not need to write a class then)
                     <!-- Encoding with spring filter -->
                     <filter>
                      <filter-name>encoding</filter-name>
                      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
                      <init-param>
                       <param-name>encoding</param-name>
                       <param-value>UTF-8</param-value>
                      </init-param>
                     </filter>
                     <filter-mapping>
                      <filter-name>encoding</filter-name>
                      <url-pattern>/*</url-pattern>
                     </filter-mapping>
                    


                    Solution 2
                    request.setCharacterEncoding("utf-8")

                    Solution 3
                    new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");




                    4. sendRedirect or All strings in URL

                    String info= java.net.URLEncoder.encode(name,"utf-8");
                    response.sendRedirect("EncodingPro/Welcome?username="+info);


                    Data Response

                    response.setContentType("text/html;charset=utf-8");


                    Cookie with Different Character set
                      Saving
                    String val = java.net.URLEncoder.encode("中国字","utf-8");
                    Cookie cookie = new Cookie("name",val);
                    
                    Getting
                      String val = java.net.URLDecoder.decode(cookie.getValue(),"utf-8");
                    
                    
                    Properties file
                    For character encoding in properties file
                    Use native2ascii from jdk/bin/, copy the String into native2ascii, it will translate to ascii




                    HTTP Protocol

                    1. http is built based on tcp/ip
                    2. Hyper Text Transfer Protocol
                    3. Http 1.0, and 1.1, now normally using 1.1
                        http1.0: short time connection, disconnect immediately
                        http1.1: longer time connection, 30 seconds

                    Request Header
                    GET /test/hello.html HTTP/1.1
                    Accept: */*
                    Referer: http://localhost:8080/test/abc.html
                    Accept-Language: zh-cn
                    User-Agent: Mozilla/4.0
                    Accept-Encoding: gzip, deflate
                    Host: localhost:8080
                    Connection: Keep-Alive
                    [empty line]
                    dataname1=data1
                    dataname2=data2

                    1. Accept: text/html, image/*
                    2. Accept-Charset: ISO-8859-1
                    3. Accept-Encoding: gzip, compress
                    4. Accept-Language: en-us,zh-cn
                    5. Host: www.sohu.com:80
                    6. If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT    //cache time, refresh if there is newer
                    7. Referer: http://www.sohu.com/index.jsp //tell host where this request come from, prevent spam
                    8. User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
                    9. Cookie
                    10. Connection: close    // or Keep-Alive
                    11. Date: Tue, 11 Jul 2000 18:23:51 GMT


                    Request Methods:

                    POST GET HEAD OPTIONS DELETE TRACE PUT


                    Response Header


                    1. HTTP/1.1 200 OK
                    2. Location: http://www.baidu.com //tell browser to redirect new url
                    3. Server: apache tomcat
                    4. Content-Encoding: gzip
                    5. Content-Length: 80
                    6. Content-Language: zh-cn
                    7. Content-Type: text/html; charset=GB2312 //response.setContentType("text/html;charset=GB2312");
                    8. Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT
                    9. Refresh: 1;url=http://www.baidu.com     //redirect to url after 1 second
                      //response.setHeader("Refresh", "5;url=http://www.sohu.com");
                    10. Content-Disposition: attachment; filename=aaa.zip
                      //tell browser that there is file to download
                      //response.setHeader("Content-Disposition","attachment; filename=aaa.zip");
                    11. Transfer-Encoding: chunked     //checksum verified
                    12. Set-Cookie:SS=Q0=5Lb_nQ; path=/search   //to be introduced
                    13. Expires: -1                               //tell browser(IE) how to cache data
                    14. Cache-Control: no-cache         //tell browser(FireFox) how to cache data
                    15. Pragma: no-cache                    //tell browser(some browser) how to cache data
                      //response.setDateHeader("Expires",-1); //or value to System.currentTimeMillis()+1*1000;
                      //response.setHeader("Cache-Control","no-cache");
                      //response.setHeader("Pragma","no-cache");
                    16. Connection: close/Keep-Alive
                    17. Date: Tue, 11 Jul 2000 18:23:51 GMT


                    State Code

                    • 200 normal
                    • 302 server request browser redirect to another resource
                      response.sendRedirect("anotherPage")
                      // or response.setStatus(302); response.setHeader("Location","anotherPage");
                    • 404 not found
                    • 500 server error
                      throw new RuntimeException();